-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathqbreport.py
executable file
·258 lines (217 loc) · 9.21 KB
/
qbreport.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
'''
__G__ = "(G)bd249ce4"
backend -> report
'''
from os import path
from json import loads, dumps, JSONEncoder
from pickle import load as pload
from base64 import b64encode
from datetime import datetime
from tinydb import TinyDB, Query
from binascii import unhexlify
from jinja2 import Template, Environment, FileSystemLoader
from shared.logger import log_string, ignore_excpetion
from shared.settings import defaultdb
from shared.mongodbconn import add_item_fs, find_item
class ComplexEncoder(JSONEncoder):
'''
this will be used to encode objects
'''
def default(self, obj):
'''
override default
'''
if not isinstance(obj, str):
return str(obj)
return JSONEncoder.default(self, obj)
def pretty_json(value):
'''
object to json
'''
return dumps(value, indent=4)
def make_json_table(env, data, header) -> str:
'''
render json html table
'''
parsed_header = header.replace(' ', '_')
temp = """
<div class="tablewrapper">
<table>
<thead>
<tr>
<th colspan="1" onclick=toggle_class(".table-{{ parsed_header }}")>{{ header }}</th>
</tr>
</thead>
<tbody class="table-{{ parsed_header }}" style="display:none";>
{%- for row in data -%}
<tr>
<td><pre>{{ row | pretty_json }}</pre></td>
</tr>
{%- endfor -%}
</tbody>
</table>
</div>"""
result = env.from_string(temp).render(header=header, parsed_header=parsed_header, data=data)
return result
def make_json_table_no_loop(env, data, header) -> str:
'''
render json html table
'''
parsed_header = header.replace(' ', '_')
temp = """
<div class="tablewrapper">
<table>
<thead>
<tr>
<th colspan="1" onclick=toggle_class(".table-{{ parsed_header }}")>{{ header }}</th>
</tr>
</thead>
<tbody class="table-{{ parsed_header }}" style="display:none";>
<tr>
<td><pre>{{ data | pretty_json }}</pre></td>
</tr>
</tbody>
</table>
</div>"""
result = env.from_string(temp).render(header=header, parsed_header=parsed_header, data=data)
return result
def make_text_table(env, data, header) -> str:
'''
render text html table
'''
parsed_header = header.replace(' ', '_')
temp = """
<div class="tablewrapper">
<table>
<thead>
<tr>
<th colspan="1" onclick=toggle_class(".table-{{ parsed_header }}")>{{ header }}</th>
</tr>
</thead>
<tbody class="table-{{ parsed_header }}" style="display:none";>
{%- for row in data -%}
<tr>
<td>{{ row }}</td>
</tr>
{%- endfor -%}
</tbody>
</table>
</div>"""
result = env.from_string(temp).render(header=header, parsed_header=parsed_header, data=data)
return result
def make_image_table_base64(env, data, header) -> str:
'''
render image inside html table
'''
parsed_header = header.replace(' ', '_')
temp = """
<div class="tablewrapper">
<table>
<thead>
<tr>
<th colspan="1" onclick=toggle_class(".table-{{ parsed_header }}")>{{ header }}</th>
</tr>
</thead>
<tbody class="table-{{ parsed_header }}" style="display:none";>
<tr>
<td><img class="fullsize" src="{{ data }}" /></td>
</tr>
</tbody>
</table>
</div>"""
result = env.from_string(temp).render(header=header, parsed_header=parsed_header, data=data)
return result
ENV_JINJA2 = Environment(autoescape=True, loader=FileSystemLoader('/tmp'), trim_blocks=True, lstrip_blocks=True)
ENV_JINJA2.filters['pretty_json'] = pretty_json
def make_report(parsed):
'''
make the html table
'''
table = ""
full_table = ""
analyzer_db = None
sniffer_db = None
analyzer_path = "{}{}{}".format(parsed['locations']['box_output'], parsed['task'], parsed['locations']['analyzer_logs'])
sniffer_path = "{}{}{}".format(parsed['locations']['box_output'], parsed['task'], parsed['locations']['sniffer_logs'])
analyzer_db = TinyDB(analyzer_path)
sniffer_db = TinyDB(sniffer_path)
with open(analyzer_path) as file:
temp_id = add_item_fs(defaultdb["dbname"], defaultdb["reportscoll"], file.read(), parsed['task'], None, parsed['task'], "application/json", datetime.now())
with ignore_excpetion():
screenshot_table = analyzer_db.table('screenshot_table')
item = screenshot_table.search(lambda x: x if 'normal_image' in x else 0)
if item:
bimage = b64encode(unhexlify(item[0]['normal_image'].encode('utf-8')))
img_base64 = "data:image/jpeg;base64, {}".format(bimage.decode("utf-8", errors="ignore"))
table += make_image_table_base64(ENV_JINJA2, img_base64, "Screenshot")
log_string("Parsed normal screenshot", task=parsed['task'])
with ignore_excpetion():
screenshot_table = analyzer_db.table('screenshot_table')
item = screenshot_table.search(lambda x: x if 'full_image' in x else 0)
if item:
bimage = b64encode(unhexlify(item[0]['full_image'].encode('utf-8')))
img_base64 = "data:image/jpeg;base64, {}".format(bimage.decode("utf-8", errors="ignore"))
table += make_image_table_base64(ENV_JINJA2, img_base64, "Full Screenshot")
log_string("Parsed full screenshot", task=parsed['task'])
with ignore_excpetion():
network_table = analyzer_db.table('network_table')
item = network_table.search(lambda x: x if 'circular_layout' in x else 0)
if item:
bimage = b64encode(unhexlify(item[0]['circular_layout'].encode('utf-8')))
img_base64 = "data:image/jpeg;base64, {}".format(bimage.decode("utf-8", errors="ignore"))
table += make_image_table_base64(ENV_JINJA2, img_base64, "Network Graph")
log_string("Parsed Network Graph", task=parsed['task'])
with ignore_excpetion():
words_table = analyzer_db.table('extracted_table')
item = words_table.search(lambda x: x if 'dns_records' in x else 0)
if item:
table += make_json_table_no_loop(ENV_JINJA2, item[0]["dns_records"], "DNS Records")
with ignore_excpetion():
words_table = analyzer_db.table('extracted_table')
item = words_table.search(lambda x: x if 'Request_Headers' in x else 0)
if item:
table += make_json_table_no_loop(ENV_JINJA2, item[0]["Request_Headers"], "Request Headers")
with ignore_excpetion():
words_table = analyzer_db.table('extracted_table')
item = words_table.search(lambda x: x if 'Response_Headers' in x else 0)
if item:
table += make_json_table_no_loop(ENV_JINJA2, item[0]["Response_Headers"], "Response Headers")
with ignore_excpetion():
words_table = analyzer_db.table('extracted_table')
item = words_table.search(lambda x: x if 'Certificate' in x else 0)
if item:
table += make_json_table_no_loop(ENV_JINJA2, item[0]["Certificate"], "Certificate")
with ignore_excpetion():
words_table = analyzer_db.table('words_table')
item = words_table.search(lambda x: x if 'all_words' in x else 0)
if item:
table += make_json_table_no_loop(ENV_JINJA2, item, "OCR Words")
with ignore_excpetion():
extracted_table = analyzer_db.table('extracted_table')
item = extracted_table.search(lambda x: x if 'extracted_links' in x else 0)
if item:
table += make_json_table_no_loop(ENV_JINJA2, item[0]["extracted_links"], "Extracted links")
with ignore_excpetion():
extracted_table = analyzer_db.table('extracted_table')
item = extracted_table.search(lambda x: x if 'extracted_scripts' in x else 0)
if item:
table += make_json_table_no_loop(ENV_JINJA2, item[0]["extracted_scripts"], "Extracted scripts")
with ignore_excpetion():
analyzer_table = analyzer_db.table('analyzer_table')
if len(analyzer_table.all()) > 0:
table += make_json_table(ENV_JINJA2, analyzer_table.all(), "Browser")
with ignore_excpetion():
sniffer_table = sniffer_db.table('sniffer_table')
if len(sniffer_table.all()) > 0:
table += make_json_table_no_loop(ENV_JINJA2, sniffer_table.all(), "Sniffer")
all_logs = find_item(defaultdb["dbname"], defaultdb["taskdblogscoll"], {'task': parsed['task']})
if all_logs:
full_table = make_text_table(ENV_JINJA2, all_logs['logs'], "Logs")
log_string("Adding logs", task=parsed['task'])
full_table += table
if len(full_table) == 0:
full_table = "Error"
with open("template.html") as file:
rendered = Template(file.read()).render(title=parsed['task'], content=full_table)
temp_id = add_item_fs(defaultdb["dbname"], defaultdb["reportscoll"], rendered, parsed['task'], None, parsed['task'], "text/html", datetime.now())
temp_id = add_item_fs(defaultdb["dbname"], defaultdb["taskfileslogscoll"], "\n".join(all_logs['logs']), "log", None, parsed['task'], "text/plain", datetime.now())