-
Notifications
You must be signed in to change notification settings - Fork 24
/
malwarelu.py
259 lines (215 loc) · 8.26 KB
/
malwarelu.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
#!/usr/bin/env python
# malwarelu.py
# _ _
# _ __ ___ __ _| |_ ____ _ _ __ ___ | |_ _
# | '_ ` _ \ / _` | \ \ /\ / / _` | '__/ _ \ | | | | |
# | | | | | | (_| | |\ V V / (_| | | | __/_| | |_| |
# |_| |_| |_|\__,_|_| \_/\_/ \__,_|_| \___(_)_|\__,_|
#
# Script to make request on the malware.lu API
#
# It use multipartposthandler class available on last ubuntu
# $ sudo apt-get install python-multipartposthandler
# if you don't have it you can grab it with easy_install
# $ sudo easy_install MultipartPostHandler
# $ easy_install --user MultipartPostHandler
# last solution take it online and put it on the same
# folder as the script
#
# Warning no SSL certificat verification
# due to urllib2, so don't use it on hostile
# connection, if you patch the code to support this feature
# contact us
#
# any subjection, tips, improvement are welcome
import re
import hashlib
import urllib, urllib2
import argparse, sys
try:
import simplejson
except ImportError:
raise ImportError, 'The simplejson module is required to run this program : http://pypi.python.org/pypi/simplejson/'
try:
import MultipartPostHandler
except ImportError:
raise ImportError, 'The MultipartPostHandler module is required to run this program : http://pypi.python.org/pypi/MultipartPostHandler/0.1.0'
apikey = ""
version = "0.8"
class MalwareLuAlreadyHere(Exception):
def __init__(self, md5):
self.md5 = md5
def __str__(self):
return "Hash %s already in database" % self.md5
class MalwareLu():
def __init__(self, apikey):
base = "https://www.malware.lu/api/"
self.apikey = apikey
self.url_download = base + "download"
self.url_upload = base + "upload"
self.url_check = base + "check"
self.url_stats = base + "stats"
self.url_search = base + "search"
self.url_submiturl = base + "submit"
def filetohash(self, filename):
file_content = open(filename, "rb").read()
md5 = hashlib.md5(file_content).hexdigest()
return md5
def get_content_type(self, filename):
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
def download(self, md5):
param = { 'hash': md5,
'apikey': self.apikey }
data = urllib.urlencode(param)
req = urllib2.Request(self.url_download, data)
f = urllib2.urlopen(req)
headers = f.info()
if headers['content-type'] == 'application/octet-stream':
filename = re.findall('filename=(\S+)',
headers['Content-Disposition'])[0]
fp = open(filename, 'w')
fp.write(f.read())
fp.close()
return True
return simplejson.loads(f.read())
def check(self, md5):
param = { 'hash': md5,
'apikey': self.apikey }
data = urllib.urlencode(param)
f = urllib2.urlopen(self.url_check, data)
data = f.read()
return simplejson.loads(data)
def search(self, name):
param = { 'name': name,
'apikey': self.apikey }
data = urllib.urlencode(param)
f = urllib2.urlopen(self.url_search, data)
data = f.read()
return simplejson.loads(data)
def stats(self):
param = { 'apikey': self.apikey }
data = urllib.urlencode(param)
f = urllib2.urlopen(self.url_stats, data)
data = f.read()
return simplejson.loads(data)
def submit_url(self, url):
param = { 'url': url,
'apikey': self.apikey }
data = urllib.urlencode(param)
f = urllib2.urlopen(self.url_submiturl, data)
data = f.read()
return simplejson.loads(data)
def upload(self, filename):
if self.check(self.filetohash(filename))['status'] == True:
raise MalwareLuAlreadyHere(self.filetohash(filename))
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
params = {'file': open(filename, 'rb'), 'apikey': self.apikey}
f = opener.open(self.url_upload, params)
data = f.read()
return simplejson.loads(data)
class bcolors:
GREEN = '\033[92m'
RED = '\033[91m'
ENDC = '\033[0m'
def disable(self):
self.GREEN = ''
self.RED = ''
self.ENDC = ''
def show_sample_info(s):
print "-"*72
print "SHA256: %s" % s['sha256']
print "MD5: %s" % s['md5']
print ("Detection ratio: %s%s/%s%s") % \
(bcolors.RED, s['positives'], s['total'], bcolors.ENDC)
print ("Insert date: %s%s%s") % (bcolors.GREEN, s['insert_date_ts'],
bcolors.ENDC)
print ("Scan date: %s%s%s") % (bcolors.GREEN, s['scan_date_ts'],
bcolors.ENDC)
print ("First seen: %s%s%s") % (bcolors.GREEN, s['first_seen_ts'],
bcolors.ENDC)
print ("Last seen: %s%s%s") % (bcolors.GREEN, s['last_seen_ts'],
bcolors.ENDC)
print "Size: %s" % s['file_size']
av = ['nod32', 'avast', 'microsoft', 'f_prot', 'kaspersky',
'bitdefender', 'sophos', 'f_secure',
'mcafee_gw_edition', 'symantec', 'gdata']
for a in av:
if s[a] != None:
print ("%s: %s%s%s") % (a, bcolors.RED, s[a], bcolors.ENDC)
tools = ['peid', 'f_prot_unpacker' ]
for t in tools:
if s[t] != None:
print ("%s: %s%s%s") % (t, bcolors.RED, s[t], bcolors.ENDC)
def main():
parser = argparse.ArgumentParser(description = "Malware.lu API tools")
parser.add_argument('--version', action='version',
version="%(prog)s version " + version)
parser.add_argument('-k', '--apikey', action="store", default=apikey)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-c', '--check', nargs="+", dest="check", metavar="hash",
help="Check a hash on malware.lu")
group.add_argument('--stats', dest="action",
action='store_const', const="stats",
help="Show your stats on malware.lu")
group.add_argument('-s', '--search',
help="Search by AV name on malware.lu")
group.add_argument('--submit-url', nargs="+",
help="Submit a malware by URL on malware.lu")
group.add_argument('-u', '--upload', nargs="+", dest="upload", metavar="file",
help="Upload a file to malware.lu")
group.add_argument('-d', '--download', nargs="+", dest="download", metavar="hash",
help="Download a hash from malware.lu")
r = parser.parse_args()
m = MalwareLu(r.apikey)
try:
# Upload files loop
if r.upload != None:
for arg in r.upload:
print("Upload of %s..." % arg)
try:
ret = m.upload(arg)
if ret['status'] == True :
print "Upload %s successfully" % arg
else:
print ret['error']
except MalwareLuAlreadyHere as e:
print e
# Download files loop
if r.download != None:
for arg in r.download:
print "Download of %s..." % arg
ret = m.download(arg)
if ret == True :
print "Download %s successfully" % arg
else:
print ret['error']
# Check files loop
if r.check != None:
for arg in r.check:
ret = m.check(arg)
if ret['status'] == True:
print "%s is in database" % arg
else:
print "%s not in database" % arg
if r.submit_url != None:
for arg in r.submit_url:
ret = m.submit_url(arg)
if ret['status'] == True:
print "URL %s correctly submit with md5 %s" % (arg, ret['md5'])
else:
print ret['error']
if r.search != None:
ret = m.search(r.search)
for s in ret['samples']:
show_sample_info(s)
# stats action
if r.action == "stats":
ret = m.stats()
print "Downloads stats %s" % ret['stats']
except urllib2.HTTPError, e:
if e.code == 401:
print "Wrong api key"
else:
print e
if __name__ == '__main__':
main()