-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecure-File-Inspector.java
More file actions
279 lines (214 loc) · 8.17 KB
/
Secure-File-Inspector.java
File metadata and controls
279 lines (214 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Secure_File_Inspector
{
static ArrayList<FileRecord> filelist=new ArrayList<>();
static ArrayList<SecurityPattern> patterns=new ArrayList<>();
static Scanner input=new Scanner(System.in);
static class SecurityPattern
{
String name;
String regex;
String riskLevel;
public SecurityPattern(String n,String r,String l)
{
name=n;
regex=r;
riskLevel=l;
}
}
static class FileRecord
{
File file;
String name;
long size;
String ext;
String category;
String status;
public FileRecord(File f,String n,long s,String e,String c,String st)
{
file=f;
name=n;
size=s;
ext=e;
category=c;
status=st;
}
}
public static void main(String[] args)
{
patterns.add(new SecurityPattern("Email Address","[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}","PII_HIGH"));
patterns.add(new SecurityPattern("IPv4 Address","\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b","PII_MEDIUM"));
patterns.add(new SecurityPattern("Phone Number","\\b\\d{10}","PII_MEDIUM"));
patterns.add(new SecurityPattern("Java Runtime","Runtime\\.getRuntime\\(\\)\\.exec","MALICIOUS_HIGH"));
patterns.add(new SecurityPattern("ProcessBuilder","ProcessBuilder","MALICIOUS_HIGH"));
patterns.add(new SecurityPattern("CMD Exec","cmd\\.exe","MALICIOUS_HIGH"));
patterns.add(new SecurityPattern("PowerShell","powershell","MALICIOUS_HIGH"));
patterns.add(new SecurityPattern("Eval Func","eval\\(","MALICIOUS_MEDIUM"));
patterns.add(new SecurityPattern("Base64 Decode","base64_decode","MALICIOUS_MEDIUM"));
while(true)
{
System.out.println("\n=== Secure File Inspector ===");
System.out.println("1.Scan Directory");
System.out.println("2.Delete Infected File");
System.out.println("3.Export Log");
System.out.println("4.Exit");
System.out.print("Choice: ");
String choice=input.nextLine();
if(choice.equals("1"))
{
scanfolder();
}
else if(choice.equals("2"))
{
deletefile();
}
else if(choice.equals("3"))
{
exportfile();
}
else if(choice.equals("4"))
{
break;
}
}
}
public static void scanfolder()
{
System.out.print("Enter path : ");
String path=input.nextLine();
File folder=new File(path);
if(folder.exists()==false)
{
System.out.println("Invalid folder.");
return;
}
filelist.clear();
File[] files=folder.listFiles();
System.out.println("\n----------------------------------------------------------------------------------------------------");
System.out.printf("%-20s | %-10s | %-6s | %-20s | %-20s%n","NAME","SIZE","TYPE","CATEGORY","STATUS");
System.out.println("----------------------------------------------------------------------------------------------------");
if(files!=null)
{
for(File file:files)
{
if(file.isFile())
{
checkfile(file);
}
}
}
System.out.println("----------------------------------------------------------------------------------------------------");
}
public static void checkfile(File file)
{
String name=file.getName();
String ext=getext(name);
long size=file.length();
String category="General";
String status="Safe";
try{
BufferedReader reader=new BufferedReader(new FileReader(file));
String line;
while((line=reader.readLine())!=null)
{
for(SecurityPattern p:patterns)
{
Pattern regex=Pattern.compile(p.regex);
Matcher m=regex.matcher(line);
if(m.find())
{
if(p.riskLevel.startsWith("PII"))
{
category="SENSITIVE";
}
if(p.riskLevel.startsWith("MALICIOUS"))
{
status="INFECTED: "+p.name;
}
}
}
if(status.equals("Safe")==false && category.equals("General")==false)
{
break;
}
}
reader.close();
}
catch(Exception e)
{
status="Read Error";
}
filelist.add(new FileRecord(file,name,size,ext,category,status));
System.out.printf("%-20s | %-10d | %-6s | %-20s | %-20s%n",shorten(name),size,ext,category,status);
}
public static void deletefile()
{
System.out.print("Enter name of infected file to delete: ");
String target=input.nextLine();
for(int i=0;i<filelist.size();i++)
{
FileRecord rec=filelist.get(i);
if(rec.name.equals(target))
{
if(rec.status.startsWith("INFECTED"))
{
if(rec.file.delete())
{
System.out.println("Deleted!");
filelist.remove(i);
}
else
{
System.out.println("Permission denied.");
}
}
else
{
System.out.println("File is not infected");
}
return;
}
}
System.out.println("File not found in list.");
}
public static void exportfile()
{
try {
FileWriter report=new FileWriter("Scan_Report.csv");
report.write("Name,Size,Type,Category,Status\n");
for(FileRecord rec:filelist)
{
report.write(rec.name+","+rec.size+","+rec.ext+","+rec.category+","+rec.status+"\n");
}
report.close();
System.out.println("Saved to Scan_Report.csv");
}
catch(Exception e)
{
System.out.println("Error saving file.");
}
}
public static String getext(String str)
{
int i=str.lastIndexOf('.');
if(i > 0)
{
return str.substring(i + 1);
}
else
{
return "file";
}
}
public static String shorten(String s)
{
if(s.length()>18)
{
return s.substring(0,15)+"...";
}
return s;
}
}