forked from krmaxwell/maltrieve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaltrieve.py
executable file
·277 lines (241 loc) · 9.92 KB
/
maltrieve.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
276
277
# Copyright 2013 Kyle Maxwell
# Includes code from mwcrawler, (c) 2012 Ricardo Dias. Used under license.
# Maltrieve - retrieve malware from the source
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/
import urllib2
import logging
import argparse
import tempfile
import re
import hashlib
import os
import sys
import datetime
import xml.etree.ElementTree as ET
import itertools
import mimetools
import mimetypes
import urllib
import json
import pickle
import string
from MultiPartForm import *
from threading import Thread
from Queue import Queue
from lxml import etree
from bs4 import BeautifulSoup
from malutil import *
def get_malware(q, dumpdir):
while True:
url = q.get()
logging.info("Fetched URL %s from queue", url)
logging.info("%s items remaining in queue", q.qsize())
mal = get_URL(url)
if mal:
malfile = mal.read()
md5 = hashlib.md5(malfile).hexdigest()
# Is this a big race condition problem?
if md5 not in hashes:
logging.info("Found file %s at URL %s", md5, url)
logging.debug("Going to put file in directory %s", dumpdir)
# see http://stackoverflow.com/a/5032238
# may resolve issue #21
if not os.path.isdir(dumpdir):
try:
logging.info("Creating dumpdir %s", dumpdir)
os.makedirs(dumpdir)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
# store the file and log the data
with open(os.path.join(dumpdir, md5), 'wb') as f:
f.write(malfile)
logging.info("Stored %s in %s", md5, dumpdir)
if args.vxcage:
if os.path.exists(os.path.join(dumpdir, md5)):
f = open(os.path.join(dumpdir, md5), 'rb')
form = MultiPartForm()
form.add_file('file', md5, fileHandle=f)
form.add_field('tags', 'maltrieve')
request = urllib2.Request('http://localhost:8080/malware/add')
request.add_header('User-agent', 'Maltrieve')
body = str(form)
request.add_header('Content-type',
form.get_content_type())
request.add_header('Content-length', len(body))
request.add_data(body)
try:
response = urllib2.urlopen(request).read()
except:
logging.info("Exception caught from VxCage")
responsedata = json.loads(response)
logging.info("Submitted %s to VxCage, response was %s",
md5, responsedata["message"])
logging.info("Deleting file as it has been uploaded to VxCage")
try:
os.remove(os.path.join(dumpdir, md5))
except:
logging.info("Exception when attempting to delete file: %s",
os.path.join(dumpdir, md5))
if args.cuckoo:
f = open(os.path.join(dumpdir, md5), 'rb')
form = MultiPartForm()
form.add_file('file', md5, fileHandle=f)
request = urllib2.Request('http://localhost:8090/tasks/create/file')
request.add_header('User-agent', 'Maltrieve')
body = str(form)
request.add_header('Content-type', form.get_content_type())
request.add_header('Content-length', len(body))
request.add_data(body)
response = urllib2.urlopen(request).read()
responsedata = json.loads(response)
logging.info("Submitted %s to cuckoo, task ID %s", md5,
responsedata["task_id"])
hashes.add(md5)
q.task_done()
def get_XML_list(url, q):
malwareurls = []
descriptions = []
tree = get_XML(url)
if tree:
descriptions = tree.findall('channel/item/description')
for d in descriptions:
logging.info('Parsing description %s', d.text)
url = d.text.split(' ')[1].rstrip(',')
if url == '-':
url = d.text.split(' ')[4].rstrip(',')
url = re.sub('&', '&', url)
if not re.match('http', url):
url = 'http://'+url
malwareurls.append(url)
for url in malwareurls:
push_malware_URL(url, q)
def push_malware_URL(url, q):
url = url.strip()
if url not in pasturls:
logging.info('Adding new URL to queue: %s', url)
pasturls.add(url)
q.put(url)
else:
logging.info('Skipping previously processed URL: %s', url)
def main():
global hashes
hashes = set()
global pasturls
pasturls = set()
malq = Queue()
NUMTHREADS = 5
now = datetime.datetime.now()
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--proxy",
help="Define HTTP proxy as address:port")
parser.add_argument("-d", "--dumpdir",
help="Define dump directory for retrieved files")
parser.add_argument("-l", "--logfile",
help="Define file for logging progress")
parser.add_argument("-x", "--vxcage",
help="Dump the file to a VxCage instance running on the localhost",
action="store_true")
parser.add_argument("-c", "--cuckoo",
help="Enable cuckoo analysis", action="store_true")
global args
args = parser.parse_args()
if args.logfile:
logging.basicConfig(filename=args.logfile, level=logging.DEBUG,
format='%(asctime)s %(thread)d %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
else:
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(thread)d %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
# Enable thug support
# https://github.com/buffer/thug
# TODO: rewrite and test
'''
try:
if args.thug:
loadthug()
except Exception as e:
logging.warning('Could not enable thug (%s)', e)
'''
if args.proxy:
proxy = urllib2.ProxyHandler({'http': args.proxy})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
logging.info('Using proxy %s', args.proxy)
my_ip = urllib2.urlopen('http://whatthehellismyip.com/?ipraw').read()
logging.info('External sites see %s', my_ip)
# make sure we can open the directory for writing
if args.dumpdir:
try:
d = tempfile.mkdtemp(dir=args.dumpdir)
dumpdir = args.dumpdir
except Exception as e:
logging.error('Could not open %s for writing (%s), using default',
dumpdir, e)
dumpdir = '/tmp/malware'
else:
os.rmdir(d)
else:
dumpdir = '/tmp/malware'
logging.info('Using %s as dump directory', dumpdir)
if os.path.exists('hashes.obj'):
with open('hashes.obj', 'rb') as hashfile:
hashes = pickle.load(hashfile)
if os.path.exists('urls.obj'):
with open('urls.obj', 'rb') as urlfile:
pasturls = pickle.load(urlfile)
for i in range(NUMTHREADS):
worker = Thread(target=get_malware, args=(malq, dumpdir,))
worker.setDaemon(True)
worker.start()
get_XML_list('http://www.malwaredomainlist.com/hostslist/mdl.xml', malq)
get_XML_list('http://malc0de.com/rss', malq)
get_XML_list('http://www.malwareblacklist.com/mbl.xml', malq)
# TODO: wrap these in functions?
for url in get_URL('http://vxvault.siri-urz.net/URL_List.php'):
if re.match('http', url):
push_malware_URL(url, malq)
sacourtext = get_URL('http://www.sacour.cn/list/%d-%d/%d%d%d.htm' %
(now.year, now.month, now.year, now.month, now.day))
if sacourtext:
sacoursoup = BeautifulSoup(sacourtext)
for url in sacoursoup.stripped_strings:
if re.match("^http", url):
push_malware_URL(url, malq)
urlquerytext = get_URL('http://urlquery.net/')
if urlquerytext:
urlquerysoup = BeautifulSoup(urlquerytext)
for t in urlquerysoup.find_all("table", class_="test"):
for a in t.find_all("a"):
push_malware_URL(a['title'], malq)
cleanmxtext = get_URL('http://support.clean-mx.de/clean-mx/xmlviruses.php?')
if cleanmxtext:
cleanmxxml = etree.parse(cleanmxtext)
for line in cleanmxxml.xpath("//url"):
url = re.sub('&', '&', line.text)
push_malware_URL(url, malq)
malq.join()
if pasturls:
logging.info('Dumping past URLs to file')
with open('urls.obj', 'w') as urlfile:
pickle.dump(pasturls, urlfile)
if hashes:
with open('hashes.obj', 'w') as hashfile:
pickle.dump(hashes, hashfile)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit()