-
Notifications
You must be signed in to change notification settings - Fork 7
/
afot.py
275 lines (216 loc) · 8.67 KB
/
afot.py
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
#!/usr/bin/python
import pip
try:
__import__('termcolor')
except ImportError:
pip.main(['install', 'termcolor'])
try:
__import__('simplejson')
except ImportError:
pip.main(['install', 'simplejson'])
from termcolor import colored
from os import system
from platform import python_version
import codecs
import fileinput
import hashlib
import urllib2
import zipfile
import io
import os
import stat
import re
import subprocess
import platform
import sys
import time
import csv
import ntpath
from argparse import ArgumentParser
from datetime import datetime
from string import whitespace
from time import sleep
from traceback import format_exc
reload(sys)
sys.setdefaultencoding('utf8')
### Below are global internal variables. Do not edit these. #############
__VERSION__ = '0.0.1' #
__AUTHOR__ = 'Charalampos Raftopoulos (@harris_rafto)' #
__EMAIL__ = '[email protected]' #
__ENDOFLINE__ = '\n----- End of Line -----\n\n' #
#########################################################################
__VIRUSTOTALAPIKEY__ = ''; # Add your virus-total api key here
__VIRUSTOTALSEARCHURL__ = 'http://didierstevens.com/files/software/virustotal-search_V0_1_2.zip'
__NSRLURL__ = 'http://didierstevens.com/files/software/nsrl_V0_0_2.zip'
__NISTDATABASE__ = 'http://www.nsrl.nist.gov/RDS/rds_2.52/rds_252m.zip'
__ANALYZEPESIGURL__ = 'http://didierstevens.com/files/software/AnalyzePESig_V0_0_0_2.zip'
def checkPythonVersion():
"""
Check python version the user is using for compatibility reasons
"""
version = python_version()
if "2.7" not in version:
print colored('\n------------------------------\nYour python version might not be sufficient for our script. Please use version 2.7 ! Keep going though...\n------------------------------', 'red')
time.sleep(1)
def downloadFile(fileUrl):
"""
Download needed script
"""
url = fileUrl
file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (
file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8) * (len(status) + 1)
print status,
f.close()
def unzipFile(fileName):
"""
Extract a zipped file
"""
zip_ref = zipfile.ZipFile(fileName, 'r')
zip_ref.extractall()
zip_ref.close()
def deleteFile(fileName):
"""
Delete a file
"""
os.remove(fileName)
def printInfo():
"""
Print all user's info
"""
print colored('\n\n------------------------------\nAutomated FOrensics Script: afos.py\n------------------------------', 'yellow')
print colored('Version: %s' % __VERSION__, 'green')
print colored('Author: %s' % __AUTHOR__, 'green')
print colored('Email: %s' % __EMAIL__, 'green')
print colored('%s' % __ENDOFLINE__, 'yellow')
def main():
"""
Main routine
"""
checkPythonVersion()
printInfo()
time.sleep(1)
path = raw_input('Enter folder path (eg. c:/): ')
formattedPath = path.replace('/', '\\')
# if path not provided, set default path to "c:\"
if not path:
formattedPath = 'c:\\'
if not os.path.isdir('AnalyzePESig'):
print colored('\n------------------------------\nYou do not have AnalyzePESig! Downloading now...!\n------------------------------', 'red')
time.sleep(1)
# Download the script
downloadFile(__ANALYZEPESIGURL__)
# Unzip it
unzipFile('AnalyzePESig_V0_0_0_2.zip')
# Delete the downloaded zip file
deleteFile('AnalyzePESig_V0_0_0_2.zip')
if platform.system() == "Windows":
# Check if AnalyzePESig exists, if not download it
print colored('\n------------------------------\nGo grab a cup of coffee...this is gonna take a while!\n------------------------------', 'yellow')
print colored('\n------------------------------\nChecking all files for signatures!\n------------------------------', 'yellow')
time.sleep(1)
subprocess.call(['AnalyzePESig\\Release\\AnalyzePESig.exe',
"-e", "-v", "-s", "-o", "windows.csv", formattedPath])
# Get all hashes that are not signed
hashes = []
with open('windows.csv', 'rb') as f:
reader = csv.reader(f, delimiter=';')
for row in reader:
if row[11] == "0":
hashes.append(row[1])
with open('hashes.txt', 'w+') as hashfile:
for item in hashes[0:len(hashes)]:
hashfile.write("%s\n" % item)
print colored('\n------------------------------\nCross Checking with the NIST database...please be patient!\n------------------------------', 'yellow')
time.sleep(1)
# Check if nsrl script exists, if not download it
if not os.path.isfile('nsrl.py'):
print colored('\n------------------------------\nYou do not have the nsrl.py script! Downloading now...!\n------------------------------', 'red')
time.sleep(1)
# Download the script
downloadFile(__NSRLURL__)
# Unzip it
unzipFile('nsrl_V0_0_2.zip')
# Delete the downloaded zip file
deleteFile('nsrl_V0_0_2.zip')
# Check if NIST database 'Reduced Sets' exists, if not download it
if not os.path.isfile('rds_252m.zip'):
print colored('\n------------------------------\nYou do not have the Reduced Set of NIST\'s database! Downloading now...!\n------------------------------', 'red')
time.sleep(1)
# Download the file
downloadFile(__NISTDATABASE__)
# Cross-check with NIST's database
subs = system('python nsrl.py -f -q -o nist.csv hashes.txt rds_252m.zip')
hashes = []
with open('nist.csv', 'rb') as f:
reader = csv.reader(f, delimiter=';')
for row in reader:
hashes.append(row[0])
if hashes:
time.sleep(1)
print colored('\n------------------------------\nFound something! Please wait...\n------------------------------', 'yellow')
time.sleep(1)
# get all hashes that were find in NIST's database and write them into
# a text file
with open('infected.txt', 'w+') as thefile:
for item in hashes[1:len(hashes)]:
thefile.write("%s\n" % item)
print colored('\ninfected.txt', 'green') + colored(' file has been created containing all found hashes!', 'yellow')
time.sleep(1)
# Check if virustotal-search script exists, if not download it
if not os.path.isfile('virustotal-search.py'):
print colored('\n------------------------------\nYou do not have the virustotal-search.py script! Downloading now...!\n------------------------------', 'red')
time.sleep(1)
# Download the script
downloadFile(__VIRUSTOTALSEARCHURL__)
# Unzip it
unzipFile('virustotal-search_V0_1_2.zip')
# Delete the downloaded zip file
deleteFile('virustotal-search_V0_1_2.zip')
print colored('\nBegin querying VirusTotal...', 'yellow')
time.sleep(1)
# Search VirusTotal
virustotalsearch = system(
'python virustotal-search.py -k '+ __VIRUSTOTALAPIKEY__ +' -o report.csv infected.txt')
possibleMd5 = []
detections = []
# Read the report file
with open('report.csv', 'rb') as f:
reader = csv.reader(f, delimiter=';')
for row in reader:
possibleMd5.append(row[0])
detections.append(row[4])
print colored('\n------------------------------\nReport\n------------------------------', 'yellow')
# Format the array accordingly
fmt = '{:<8}{:<35}{}'
for i, (c1, c2) in enumerate(zip(possibleMd5, detections)):
print(fmt.format(i, c1, c2))
time.sleep(2)
print colored('Cleaning up unnecessary files...', 'yellow')
# Delete unnecessary files
deleteFile('windows.csv')
deleteFile('hashes.txt')
deleteFile('nist.csv')
deleteFile('infected.txt')
time.sleep(2)
print colored('\nreport.csv', 'green') + colored(' file has been created containing a full report!', 'yellow')
sys.exit(1)
else:
print colored('\nNothing found!\n------------------------------', 'green')
sys.exit(1)
if __name__ == '__main__':
main()