-
Notifications
You must be signed in to change notification settings - Fork 2
/
demystify-lite.py
189 lines (164 loc) · 6.51 KB
/
demystify-lite.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
"""Demystify-lite Pyscript front-end.
The form for Demystify is cleared and a user can then provide the
page with a File handle to a file-format analysis report on their own
file-system. The file is processed and the results returned to the page.
"""
import binascii
import json
import tempfile
from js import document, console, window, alert
from pyodide.ffi import create_proxy
from demystify.demystify import analysis_from_csv_lite
from demystify.libs.outputhandlers.htmloutputclass import FormatAnalysisHTMLOutput
from pyscript import when, display
def clear_data():
"""Clear the metadata fields associated with the file input and
output.
"""
document.getElementById("filename").innerHTML = ""
document.getElementById("filesize").innerHTML = ""
document.getElementById("filetype").innerHTML = ""
document.getElementById("filedate").innerHTML = ""
document.getElementById("results").innerHTML = ""
async def deny_list(event):
"""Handle file select and follow-on actions from HTML/Pyscript."""
use_deny_list = document.getElementById("use_deny_list").checked
if not use_deny_list:
document.getElementById("denylistTextBox").style.display = "none"
return
document.getElementById("denylistTextBox").style.display = ""
content = document.getElementById("denylist").value
if content.strip() == "":
with open("default_denylist.cfg", encoding="utf-8") as default_deny_list:
content = default_deny_list.read()
deny_list_json = json.dumps(json.loads(content), indent=2)
document.getElementById("denylist").value = deny_list_json
@when("click", "#analysis_button")
async def click_handler(event):
"""
Event handlers get an event object representing the activity that raised
them.
https://github.com/exponential-decay
"""
await file_select()
async def report_select(event):
"""Run analysis from an existing report."""
console.log("using existing report...")
clear_data()
event.stopPropagation()
event.preventDefault()
deny_list = "{}"
use_deny_list = document.getElementById("use_deny_list").checked
if use_deny_list:
deny_list = document.getElementById("denylist").value
try:
deny_list = json.loads(deny_list)
except json.decoder.JSONDecodeError as err:
console.log(f"denylist error: {err}")
document.getElementById("results").innerHTML = (
"<br/>"
"<h1>Processing Error</h1>"
"deny list JSON is invalid, please check, e.g. via "
"<a href='https://jsonlint.com/' target='_blank', rel='noopener'>JSONLint.com</a>"
"<br/>"
)
return
files = event.target.files
for file in files:
document.getElementById("filename").innerHTML = f"<b>File Name:</b> {file.name}"
document.getElementById("filesize").innerHTML = f"<b>File Size:</b> {file.size}"
if file.type:
document.getElementById(
"filetype"
).innerHTML = f"<b>File Type:</b> {file.type}"
document.getElementById(
"filedate"
).innerHTML = f"<b>File date:</b> {file.lastModified}"
content = await file.text()
with tempfile.NamedTemporaryFile("w", encoding="UTF8") as temp_file:
temp_file.write(content)
analysis = analysis_from_csv_lite(
temp_file.name,
label=file.name,
denylist=deny_list,
)
out = ""
try:
out = FormatAnalysisHTMLOutput(
analysis.analysis_results
).printHTMLResults()
except AttributeError:
out = (
f"<b>{analysis}</b>"
"Error processing content. Press F12 on your keyboard to open"
"developer tools, then select the console tab to view"
"additional debug output."
)
document.getElementById("results").innerHTML = out
async def file_select():
"""Handle file select and follow-on actions from HTML/Pyscript."""
console.log("using sf wasm...")
clear_data()
deny_list = "{}"
use_deny_list = document.getElementById("use_deny_list").checked
if use_deny_list:
deny_list = document.getElementById("denylist").value
try:
deny_list = json.loads(deny_list)
except json.decoder.JSONDecodeError as err:
console.log(f"denylist error: {err}")
document.getElementById("results").innerHTML = (
"<br/>"
"<h1>Processing Error</h1>"
"deny list JSON is invalid, please check, e.g. via "
"<a href='https://jsonlint.com/' target='_blank', rel='noopener'>JSONLint.com</a>"
"<br/>"
)
return
results = document.getElementById("sf-results")
content = results.value
content = content.strip()
bytes = content[:25]
bytes = binascii.hexlify(bytes.encode())
expected_bytes = b"2d2d2d0a7369656766726965642020203a"
console.log(f"{expected_bytes in bytes}")
console.log(f"received {bytes}")
console.log(f"expected {expected_bytes}")
if not content:
console.log("you need to select a file or directory for analysis...")
return
with tempfile.NamedTemporaryFile("w", encoding="UTF8", delete=False) as temp_file:
temp_file.write(content)
with open(temp_file.name, "r", encoding="utf8") as test:
console.log(test.read())
analysis = analysis_from_csv_lite(
temp_file.name,
label="Siegfried Browser-Based Analysis (WASM)",
denylist=deny_list,
)
out = ""
try:
out = FormatAnalysisHTMLOutput(analysis.analysis_results).printHTMLResults()
except AttributeError:
out = (
f"<b>{analysis}</b>"
"Error processing content. Press F12 on your keyboard to open"
"developer tools, then select the console tab to view"
"additional debug output."
)
document.getElementById("results").innerHTML = out
def setup_button():
"""Create a Python proxy for the callback function."""
file_select_proxy = create_proxy(report_select)
document.querySelector("#report_select input[type='file']").addEventListener(
"change",
file_select_proxy,
False,
)
deny_list_proxy = create_proxy(deny_list)
document.querySelector("#deny_list input[type='checkbox']").addEventListener(
"change",
deny_list_proxy,
)
if __name__ == "__main__":
setup_button()