-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathav_multiscan.py
More file actions
executable file
·195 lines (173 loc) · 6.11 KB
/
av_multiscan.py
File metadata and controls
executable file
·195 lines (173 loc) · 6.11 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
#!/usr/bin/env python
# encoding: utf-8
#
# This program is intended to run on a Linux machine, but you could
# easily use it on Windows (remove the Wine stuff and run exe's directly)
#
"""
av_multi_scanner.py
Created by Matthew Richard on 2010-01-1.
Copyright (c) 2010. All rights reserved.
"""
import sys
import os
import yara
from hashlib import md5
import subprocess
import socket
from time import localtime, strftime
import re
from optparse import OptionParser
# configuration information to use when processing the
# various AV products
# please update to reflect the paths on your system
yara_conf_file = "magic.yara"
clam_conf_file = "clam_shellcode.ndb"
yara_packer_file = "packer.yara"
path_to_ssdeep = "/usr/local/bin/ssdeep"
path_to_clamscan = "/usr/local/bin/clamscan"
path_to_fpscan = "/usr/local/bin/fpscan"
path_to_officemalscanner = "/data/tools/OfficeMalScanner/OfficeMalScanner.exe"
ssdeep_params = '-l'
# add new functions by invoking the scanner
# and returning a dictionary that contains
# the keys 'name' and 'result'
# where 'name' is the name of the scanner
# and 'result' contains a string representing the results
def md5sum(data):
m = md5()
m.update(data)
return ({'name': 'md5', 'result': m.hexdigest()})
def ssdeep(fname):
if os.path.isfile(path_to_ssdeep):
output = subprocess.Popen([path_to_ssdeep, ssdeep_params, fname], stdout=subprocess.PIPE).communicate()[0]
response = output.split()[1].split(',')[0]
else:
response = 'ERROR - SSDEEP NOT FOUND'
return ({'name': 'ssdeep', 'result': response})
def yarascan(data2):
if os.path.isfile(yara_conf_file):
rules = yara.compile(yara_conf_file)
result = rules.match(data=data2)
out = ''
for m in result:
out += "'%s' " % m
response = out
else:
response = "ERROR - YARA Config Missing"
return ({'name': 'yara', 'result': response })
def yara_packer(data2):
if os.path.isfile(yara_packer_file):
rules = yara.compile(yara_packer_file)
result = rules.match(data=data2)
out = ''
for m in result:
out += "'%s' " % m
response = out
else:
response = "ERROR - YARA Config Missing"
return ({'name': 'yara_packer', 'result': response })
def clam_custom(fname):
# check to see if the right path for the scanner and
# the custom configuration file exist
if os.path.isfile(path_to_clamscan) and os.path.isfile(clam_conf_file):
output = subprocess.Popen([path_to_clamscan, "-d",clam_conf_file, fname], stdout = subprocess.PIPE).communicate()[0]
result = output.split('\n')[0].split(': ')[1]
else:
result = 'ERROR - %s not found' % path_to_clamscan
return ({'name': 'clam_custom', 'result': result })
def clamscan(fname):
if os.path.isfile(path_to_clamscan):
output = subprocess.Popen([path_to_clamscan, fname], stdout = subprocess.PIPE).communicate()[0]
result = output.split('\n')[0].split(': ')[1]
else:
result = 'ERROR - %s not found' % path_to_clamscan
return ({'name':'clamav', 'result': result})
def fpscan(fname):
if os.path.isfile(path_to_fpscan):
output = subprocess.Popen([path_to_fpscan,"--report",fname], stdout = subprocess.PIPE, stderr = None).communicate()[0]
result = output.split('\n')[8].split('\t')[0]
else:
result = 'ERROR - %s not found' % path_to_fpscan
return ({'name': 'f-prot', 'result': result})
def officemalscanner(fname):
if os.path.isfile(path_to_officemalscanner):
env = os.environ.copy()
env['WINEDEBUG'] = '-all'
output = subprocess.Popen(["wine", path_to_officemalscanner,
fname, "scan", "brute"], stdout = subprocess.PIPE, stderr = None, env=env).communicate()[0]
if "Analysis finished" in output:
output = output.split('\r\n')
while "Analysis finished" not in output[0]:
output = output[1:]
result = output[3]
else:
result = "Not an MS Office file"
else:
result = 'ERROR - %s not found' % path_to_officemalscanner
return ({'name': 'officemalscanner', 'result': result})
def cymruscan(data):
# this scanner works by sending a request to hash.cymru.com
# over port 43 and sending a list of md5's to check
# if the md5 exists in the database they return the
# number of av vendors detecting the file and the
# date the file was last seen
md5 = md5sum(data)
md5 = md5['result']
request = '%s\r\n' % md5
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect(('hash.cymru.com', 43))
s.send('begin\r\n')
s.recv(1024)
s.send(request)
response = s.recv(1024)
s.send('end\r\n')
s.close()
if len(response) > 0:
resp_re = re.compile('\S+ (\d+) (\S+)')
match = resp_re.match(response)
response = "%s - %s" % (strftime("%a, %d %b %Y %H:%M:%S", localtime(int(match.group(1)))), match.group(2))
except socket.error:
response = "ERROR - NOT AVAILABLE"
return ({'name': 'cymru_hash_db', 'result': response})
def filesize(data):
return ({'name': 'filesize', 'result': str(len(data))})
def filename(filename):
return({'name': 'filename', 'result': filename})
def main():
parser = OptionParser()
parser.add_option("-f", "--file", action="store", dest="filename",
type="string", help="scanned FILENAME")
parser.add_option("-v", "--verbose", action="store_true", default=False,
dest="verbose", help="verbose")
(opts, args) = parser.parse_args()
if opts.filename == None:
parser.print_help()
parser.error("You must supply a filename!")
if not os.path.isfile(opts.filename):
parser.error("%s does not exist" % opts.filename)
data = open(opts.filename, 'rb').read()
results = []
results.append(filename(opts.filename))
results.append(filesize(data))
results.append(md5sum(data))
results.append(ssdeep(opts.filename))
results.append(clamscan(opts.filename))
results.append(clam_custom(opts.filename))
results.append(yarascan(data))
results.append(yara_packer(data))
results.append(officemalscanner(opts.filename))
results.append(fpscan(opts.filename))
results.append(cymruscan(data))
if opts.verbose:
print "[+] Using YARA signatures %s" % yara_conf_file
print "[+] Using ClamAV signatures %s" % clam_conf_file
print "\r\n"
for result in results:
if ("ERROR" in result['result']) and (opts.verbose == False):
continue
print "%20s\t%-s" % (result['name'],result['result'])
print "\r\n"
if __name__ == '__main__':
main()