-
Notifications
You must be signed in to change notification settings - Fork 1
/
pastebin-collector.py
213 lines (199 loc) · 10.2 KB
/
pastebin-collector.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
# plan:
# walk over entire tree
# for each folder:
# check if corresponding file in path exists in malware path, create if ot
# check if zipfile exists in folder. open if so, create if not
# for each file in folder:
# check if file already in archive, add if not
# check file type, if in set of things to add to set:
# add to malware archive zip
# for each file in folder again:
# check if file in archive: if so, remove
# if any changes made to malware zip file, submit path to analyzer.
import datetime
import json
import logging
import os
import typing
from zipfile import ZipFile
import requests
from PastebinDecoder import PastebinDecoder
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
class Collector:
def __init__(self,
path: str,
malware_path: str,
archive_prefix: str,
archive_url: str,
archive_token: str,
archive_password: str,
malware_file_types: typing.Optional[typing.List] = None,
skip_file_types: typing.Optional[typing.List] = None):
self.path = path
self.malware_path = malware_path
self.archive_prefix = archive_prefix
self.archive_url = archive_url
self.archive_token = archive_token
self.archive_password = archive_password
self.decoder = PastebinDecoder.PasteDecoder()
if malware_file_types is None:
self.malware_file_types = ["application", "image"]
else:
self.malware_file_types = malware_file_types
if skip_file_types is None:
self.skip_file_types = ["application/json",
"application/octet-stream",
"application/x-wine-extension-ini"]
else:
self.skip_file_types = skip_file_types
def send_zip_to_archiver(self, zip_path: str):
headers = {"Authorization": f"Token {self.archive_token}"}
path_to_archiver = zip_path.replace(self.malware_path, self.archive_prefix)
body = {"path": path_to_archiver,
"password": self.archive_password,
"source": "pastebin"}
logger.info(f"sending zipfile to analyzer {body}")
response = requests.post(self.archive_url, headers=headers, json=body)
logger.info(f"response from analyzer {response.content}")
if response.status_code not in (200, 201):
logger.error(f"error submitting to archiver {response.content}")
def find_malware_path(self, original_path: str):
# assumes a path naming pattern like "/paste/2021/3/21/", meaning March 21, 2021
#
removed_base = original_path.replace(self.path, "")
if removed_base.startswith(os.path.sep):
removed_base = removed_base.replace(os.path.sep, "", 1)
path_parts = removed_base.split(os.path.sep)
if len(path_parts) != 4:
raise Exception(f"error with path: {removed_base} ")
yeardirname, monthdirname, daydirname, filename = path_parts
contained_dir = os.path.join(self.malware_path, yeardirname, monthdirname, daydirname)
if not os.path.isdir(contained_dir):
os.makedirs(contained_dir)
new_path = os.path.join(contained_dir, filename)
return new_path
@staticmethod
def archive_files_into_zip(target, dir_path):
with ZipFile(target, mode="a") as outputFile:
existing_files = outputFile.namelist()
for entry in os.listdir(dir_path):
file_path = os.path.join(dir_path, entry)
if entry.endswith('.zip'):
# don't add the zipfile itself, or any other existing zip files.
continue
if os.path.isdir(file_path):
continue
if entry.startswith("."):
continue
if entry not in existing_files:
try:
outputFile.write(file_path, arcname=entry)
except FileNotFoundError:
# concerning: this means we have a file in the listing that isn't openable.
logger.error("error opening file {}".format(file_path))
except Exception:
logger.exception("error writing entry: {}".format(entry))
@staticmethod
def remove_archived_files(target, dir_path):
with ZipFile(target, "r") as saved_file:
successfully_saved_files = {entry.filename for entry in saved_file.filelist}
for entry in os.listdir(dir_path):
file_path = os.path.join(dir_path, entry)
if entry.endswith('.zip'):
continue
if os.path.isdir(file_path):
continue
if entry in successfully_saved_files:
try:
os.remove(file_path)
except Exception:
logger.exception("error deleting entry: {}".format(entry))
def extract_interesting_files(self, target, malware_archive_path):
# re-open one last time to sync with extracted malware zip.
changed_saved_file = False
with ZipFile(target, "r") as archive, \
ZipFile(malware_archive_path, "a") as outputMalwareFile:
existing_malware_files = outputMalwareFile.namelist()
for entry in archive.namelist():
if entry not in existing_malware_files:
filehandle = archive.open(entry, "r")
data = filehandle.read()
decoded = json.loads(data)
file_type, file_data, _ = self.decoder.handle(decoded['body'].encode("utf-8"))
keep_file = False
for prefix in self.malware_file_types:
if file_type.startswith(prefix) and file_type not in self.skip_file_types:
keep_file = True
if keep_file:
logger.info(f"keeping file {entry}")
try:
outputMalwareFile.writestr(data=file_data[0], zinfo_or_arcname=entry)
changed_saved_file = True
except FileNotFoundError:
# concerning: this means we have a file in the listing that isn't openable.
logger.error("error opening file {}".format(entry))
except Exception:
logger.exception("error writing entry: {}".format(entry))
return changed_saved_file
def zip_dir(self, dir_path: typing.AnyStr, file_name: typing.AnyStr) -> None:
# make a zip file of the contents of the directory...remove the contents once you succeed
# to make sure we don't delete a file until we're sure it's archived properly, put all
# the files into the zip file, and close it. Then re-open it, get it's contents, and
# then walk the list of files in the directory...only delete the ones in the directory
# that exist in the zip file (and make sure you don't delete the zip file while you're at it).
# first, make the zip file
target = os.path.join(dir_path, file_name + ".zip")
logger.info("archiving files")
self.archive_files_into_zip(target, dir_path)
# next, re-open it and read it's file listing, remove all files successfully archived to
# the zip
logger.info("removing saved files")
self.remove_archived_files(target, dir_path)
try:
logger.info(f"processing malware in {dir_path}")
malware_archive_path = self.find_malware_path(target)
logger.info(f"malware archive path is {malware_archive_path}")
changed_archive = self.extract_interesting_files(target, malware_archive_path)
logger.info(f"changed archive {changed_archive}")
if changed_archive:
logger.info("sending updated malware zip to archiver")
self.send_zip_to_archiver(malware_archive_path)
except Exception:
logger.exception(f"error processing {dir_path}, {file_name}, skipping malware archive")
return
def run(self):
# recurse through all the directories, analyze all of them, except yesterday and today,
# since those are likely to still be getting files.
# identify yesterday's directory, analyze just that one.
today = datetime.datetime.now(datetime.timezone.utc)
yesterday = today - datetime.timedelta(days=1)
yesterday_dirpath = os.path.join(self.path, str(yesterday.year), str(yesterday.month), str(yesterday.day))
today_dirpath = os.path.join(self.path, str(today.year), str(today.month), str(today.day))
for (dirpath, dirnames, filenames) in os.walk(self.path):
if dirpath in (today_dirpath, yesterday_dirpath):
# skip today and yesterday, since they might still be getting written.
continue
elif filenames:
# this is the bottom of the tree, run the decode here:
logger.info(f"working on {dirpath}")
last_name = os.path.split(dirpath)[-1]
self.zip_dir(dirpath, last_name)
logger.info(f"finished working on {dirpath}")
else:
# don't do the decode elsewhere
continue
if __name__ == "__main__":
pastebin_path = os.environ.get("PASTEBIN_PATH", "/paste")
malware_path = os.environ.get("MALWARE_PATH", "/malware")
archive_prefix = os.environ.get("ANALYZER_PATH", "pastebin-extractions")
archive_url = os.environ.get("ANALYZER_URL", "http://malware-analysis-site:8000/api/job")
archive_token = os.environ.get("ANALYZER_TOKEN")
archive_password = os.environ.get("ARCHIVE_PASSWORD")
collector = Collector(path=pastebin_path,
malware_path=malware_path,
archive_prefix=archive_prefix,
archive_url=archive_url,
archive_token=archive_token,
archive_password=archive_password)
collector.run()