-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.py
More file actions
executable file
·233 lines (185 loc) · 7.68 KB
/
strings.py
File metadata and controls
executable file
·233 lines (185 loc) · 7.68 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
#!/usr/bin/python3
import sys, getopt, re, pprint, json, hashlib
import vt
def help():
print("This tool is designed to gather useful information from compiled binary files."
"\n\nThe default usage is:\n\tstrings.py [options] path_to_file\n\n"
"Flags can be set to specify certain types of data.\n\nOptions:"
"\n\t-o filename -- this option specifies a file to write the output to, instead of stdout. Unlike stdout, this option does not add tabs before strings."
"\n\t-l length -- sets viable string length"
"\n\t-u -- parse URLs"
"\n\t-d -- parse DLLs"
"\n\t-i -- parse IP Addresses"
"\n\t-p -- parse File Paths"
"\n\t-k -- parse Registry Keys"
"\n\t-f -- parse Files"
"\n\t-f -- parse timestamps and dates"
"\n\t-h -- computes hash values"
"\n\t-v -- checks the SHA1sum value against VirusTotal's database and prints the report. If -o is selected this information is saved in a second file VirusTotalReport-filename. Automatically sets the -h option."
)
sys.exit()
def stdout(hashes, DLLs, PATHs, IPs, URLs, Keys, Files, DATES, UNCAT, opts):
if hashes:
print(f'\nMD5 Hash:\n\t{hashes[0]}\nSHA1 Hash:\n\t{hashes[1]}\nSHA256 Hash:\n\t{hashes[2]}\n')
if DLLs:
print("DLLs Found:")
[print("\t",d) for d in DLLs]
if PATHs:
print("\nPaths Found:")
[print("\t",d) for d in PATHs]
if IPs:
print("\nIPs Found:")
[print("\t", i) for i in IPs]
if URLs:
print("\nURLs Found:")
[print("\t", u) for u in URLs]
if Keys:
print("\nRegistry Keys Found:")
[print("\t", k) for k in Keys]
if Files:
print("\nFiles Found:")
[print("\t",f) for f in Files]
if DATES:
print("\nDates Found:")
[print("\t",d) for d in DATES]
yn = input("Would you like to see uncategorized strings? (y/N) ")
if 'Y' in yn.upper():
[print("\t",u) for u in UNCAT]
if '-v' in opts:
print("\nVirusTotal report:\n")
pprint.pprint(vt.VTreport(hashes[1]))
def writeFile(hashes, DLLs, PATHs, IPs, URLs, Keys, Files, DATES, UNCAT, opts):
yn = input("Would you like to write uncategorized strings? (y/N) ")
try:
with open(opts['-o'], 'w') as f:
if hashes:
f.write('\nMD5 Hash:')
f.write("\n" + hashes[0] +'\n')
f.write('\nSHA1 Hash:')
f.write("\n" + hashes[1] +'\n')
f.write('\nSHA256 Hash:')
f.write("\n" + hashes[2] +'\n')
if DLLs:
f.write("\n\nDLLs Found:")
[f.write("\n" + d) for d in DLLs]
if PATHs:
f.write("\n\nPaths Found:")
[f.write("\n" + d) for d in PATHs]
if IPs:
f.write("\n\nIPs Found:")
[f.write("\n" + i) for i in IPs]
if URLs:
f.write("\n\nURLs Found:")
[f.write("\n" + u) for u in URLs]
if Keys:
f.write("\n\nRegistry Keys Found:")
[f.write("\n" + k) for k in Keys]
if Files:
f.write("\n\nFiles Found:")
[f.write("\n" + fi) for fi in Files]
if DATES:
f.write("\n\nDates Found:")
[f.write("\n" + d) for d in DATES]
if 'Y' in yn.upper():
f.write("\n\nUncategorized Strings:")
[f.write("\n" + u) for u in UNCAT]
if '-v' in opts:
print(f"Writing VirusTotalReport-{opts['-o']}...")
with open(f"VirusTotalReport-{opts['-o']}", 'w') as v:
json.dump(vt.VTreport(hashes[1]), v)
except Exception as e:
print("Error writing to file.")
print(e)
sys.exit()
def getOpts():
opts, args = getopt.getopt(sys.argv[1:], "udipkfhtvo:l:",['help'])
opts = dict(opts)
optionsSelected = []
for o in opts:
optionsSelected.append(o[1])
if '--help' in opts:
help()
selectedCount = 0
for o in optionsSelected:
if o in "udiptkfh":
selectedCount += 1
if selectedCount == 0:
opts['-u'] = ''
opts['-d'] = ''
opts['-i'] = ''
opts['-p'] = ''
opts['-k'] = ''
opts['-f'] = ''
opts['-h'] = ''
opts['-t'] = ''
if '-l' not in opts:
opts['-l'] = 3
if '-v' in opts:
opts['-h'] = ''
return (opts, args)
def main():
opts, args = getOpts()
print(f"Analyzing File: {args[0]}".center(80, '-'))
sha1 = hashlib.sha1()
sha256 = hashlib.sha256()
md5 = hashlib.md5()
hashes = None
try:
strings = []
with open(args[0], 'rb') as f:
for line in f:
sha1.update(line)
sha256.update(line)
md5.update(line)
string = []
for char in line:
if char > 31 and char < 127:
string +=chr(char)
elif string:
if len(string) > int(opts['-l']):
strings.append(''.join(string))
string = []
except Exception as e:
print(e)
else:
if '-h' in opts:
hashes = [md5.hexdigest(), sha1.hexdigest(), sha256.hexdigest()]
DLL = re.compile(".*\.DLL.*", re.I)
URL = re.compile("((.*http|.*https|http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@y\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*")
IP = re.compile(".*(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).*")
REG = re.compile('.*(HKEY_LOCAL_MACHINE|HKLM|hkey_local_machine)\\\\([a-zA-Z0-9\s_@\-\^!#.\:\/\$%&+={}\[\]\\\\*])+$')
FILENAMES = re.compile(".*\.(EXE|TXT|JPG|JPEG|GIF|DOC|DOCX|XLS|XLSX|CSV|PPT|PPTX|LNK|PDF|RTF|MP3|MPG|MPEG|MOV|MP4|CPP|PY).*", re.I)
PATH = re.compile("[A-Z]:\\\\",re.I)
DATE = re.compile(".*\d\d(\d+)?[. /-]\d\d[./ -]\d\d(\d+)?")
IPs = []
URLs = []
KEYs = []
FILEs = []
DLLs = []
PATHs = []
DATES = []
UNCAT = []
for string in strings:
if re.match(DATE, string.strip()) and '-t' in opts:
DATES.append(string)
if re.match(PATH,string.strip()) and '-p' in opts:
PATHs.append(string)
elif re.match(DLL, string.strip()) and '-d' in opts:
DLLs.append(string)
elif re.match(FILENAMES, string.strip()) and '-f' in opts:
FILEs.append(string)
elif re.match(URL, string.strip()) and '-u' in opts:
URLs.append(string)
elif re.match(IP, string.strip()) and '-i' in opts:
IPs.append(string)
elif re.match(REG, string.strip()) and '-k' in opts:
KEYs.append(string)
else:
UNCAT.append(string)
if '-o' not in opts:
stdout(hashes, DLLs, PATHs, IPs, URLs, KEYs, FILEs, DATES, UNCAT, opts)
else:
print("Saving results to file...")
writeFile(hashes, DLLs, PATHs, IPs, URLs, KEYs, FILEs, DATES, UNCAT, opts)
if __name__ == "__main__":
main()