Source file html.ml
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
open Format
module Cfg = Instr_cfg
module VAList = Map.Make(Virtual_address)
module Html = struct
let = {header|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/htmx.org@1.7.0/dist/htmx.min.js"></script>
<script src="https://unpkg.com/hyperscript.org@0.9.5/dist/_hyperscript_web.min.js"></script>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://unpkg.com/@hpcc-js/wasm@0.3.11/dist/index.min.js"></script>
<script src="https://unpkg.com/d3-graphviz@3.0.5/build/d3-graphviz.js"></script>
<script>
results = {};
|header};;
let = {header|
var dotSrc = `
|header};;
let = {header|
`;
var ranges = [];
var repeat;
function linkGraph() {
var graphviz = d3.select("#graph").graphviz();
graphviz.renderDot(dotSrc).on("end", function () {
var nodes = document.getElementsByClassName("node");
for (let i = 0; i < nodes.length; i++) {
repeat = undefined;
ranges.forEach(range => {
if (range[0] == nodes[i].children[2].innerHTML && range[1] == nodes[i].children[3].innerHTML) {
nodes[i].children[1].classList.add(range[2]);
repeat = range[2];
}
});
if (repeat == undefined) {
ranges.push([nodes[i].children[2].innerHTML, nodes[i].children[3].innerHTML, nodes[i].id]);
repeat = nodes[i].id;
}
nodes[i].setAttribute("group", repeat);
nodes[i].addEventListener('mouseover', () => {
if (selected == undefined || getNodeClass(selected) != nodes[i].getAttribute("group")) {
highlightGroup(nodes[i].getAttribute("group"), '#FEF08F');
}
});
nodes[i].addEventListener('mouseout', () => {
if (getNodeClass(selected) != nodes[i].getAttribute("group")) {
highlightGroup(nodes[i].getAttribute("group"), 'white');
}
});
nodes[i].addEventListener('click', () => {
if(document.getElementsByClassName(nodes[i].getAttribute("group")).length != 0) {
document.getElementsByClassName(nodes[i].getAttribute("group"))[0].scrollIntoView({ behavior: 'smooth', block: 'center' })
}
});
}
addTagsToLines();
});
}
function addTagsToLines() {
const lines = document.querySelectorAll('.line');
lines.forEach(line => {
range = searchRange(line.children[0].innerHTML);
if (range != undefined) {
line.classList.add(range);
line.style.color = "#FFF8EB";
}
});
}
function searchRange(element) {
var res = undefined;
element = element.trim();
ranges.forEach(function (range, i) {
if (element >= range[0] && element <= range[1]) {
res = range[2];
}
})
return res;
};
function paintNode(node, color) {
if (node != undefined) {
document.getElementById(node).children[1].style.fill = color;
}
}
function highlightGroup(name, color) {
var elements = document.getElementsByClassName(name);
Array.from(elements).forEach(element => {
if (element.tagName == "polygon") {
element.style.fill = color;
} else {
element.style.color = color;
}
});
paintNode(name, color);
}
function getNodeClass(node) {
if (node != undefined) {
return node.classList[2]
}
}
function loadDhunk(instructions) {
let res = "";
JSON.parse(instructions).forEach((instr, i) => {
res += i + ": " + dhunk[instr] + "\n";
})
return res;
}
let selected = undefined;
function loadExplanations() {
const lines = document.querySelectorAll('.line');
const explanationBox = document.querySelector('.middle-column .explanation');
const resultsBox = document.querySelector('.middle-column .results');
let maintain = false;
lines.forEach(line => {
line.addEventListener('mouseover', () => {
if (getNodeClass(line) != getNodeClass(selected)) {
highlightGroup(getNodeClass(line), '#FEF08F');
}
if (!maintain){
explanationBox.textContent = loadDhunk(line.getAttribute('data-explanation'));
explanationBox.style.display = 'block';
var res = results[line.children[0].innerHTML.trim()];
if (res != undefined){
resultsBox.textContent = results[line.children[0].innerHTML.trim()].join("");
resultsBox.style.display = 'block';
}
}
});
line.addEventListener('mouseout', () => {
if (!maintain){
explanationBox.style.display = 'none';
resultsBox.style.display = 'none';
}
if (getNodeClass(line) != getNodeClass(selected)) {
highlightGroup(getNodeClass(line), 'white');
}
});
line.addEventListener('click', () => {
if (!maintain) {
highlightGroup(getNodeClass(line), '#FB7170');
selected = line;
line.style.color = '#bf7947';
explanationBox.textContent = loadDhunk(line.getAttribute('data-explanation'));
explanationBox.style.display = 'block';
var res = results[line.children[0].innerHTML.trim()];
if (res != undefined){
resultsBox.textContent = results[line.children[0].innerHTML.trim()].join("");
resultsBox.style.display = 'block';
}
maintain = true;
} else {
if (selected == line) {
if (getNodeClass(line) != undefined) {
highlightGroup(getNodeClass(line), '#FEF08F');
} else {
line.style.removeProperty('color');
}
maintain = false;
selected = undefined;
} else {
if (getNodeClass(selected) != getNodeClass(line)) {
if (getNodeClass(selected) == undefined) {
selected.style.removeProperty('color');
highlightGroup(getNodeClass(line), '#FB7170');
} else {
highlightGroup(getNodeClass(selected), 'white');
highlightGroup(getNodeClass(line), '#FB7170');
selected.style.color = 'white';
}
} else {
if (getNodeClass(selected) == undefined) {
selected.style.removeProperty('color');
} else {
selected.style.color = '#FB7170'
}
}
selected = line;
line.style.color = '#bf7947';
explanationBox.textContent = loadDhunk(line.getAttribute('data-explanation'));
explanationBox.style.display = 'block';
var res = results[line.children[0].innerHTML.trim()];
if (res != undefined){
resultsBox.textContent = results[line.children[0].innerHTML.trim()].join("");
resultsBox.style.display = 'block';
} else {
resultsBox.textContent = "";
resultsBox.style.display = 'none';
}
}
}
});
});
}
const dhunk = {
|header};;
let = {header|
}
</script>
<style>
body {
font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace;
background-color: #001B2E;
color: #FFF8EB;
font-size: 12px;
}
tr {
color: gray;
}
* {
box-sizing: border-box;
}
/* Create two equal columns that floats next to each other */
.column {
float: left;
width: 33%;
padding: 10px;
box-shadow: 0 3px 5px rgba(0, 0, 0, 0.2);
}
.scrollbar::-webkit-scrollbar-track
{
border: 1px solid black;
background-color: #FFF8EB;
}
.scrollbar::-webkit-scrollbar
{
width: 10px;
}
.scrollbar::-webkit-scrollbar-thumb
{
background-color: #ADB6C4;
}
.hoverable:hover {
color: #FEF08F;
}
.row:after {
content: "";
display: table;
clear: both;
}
th {
padding-right: 20px;
text-align: left;
}
.explanation, .results {
font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace;
display: none;
padding: 10px;
border-radius: 5px;
background-color: #294C60;
color: #FFF8EB;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-top: 10px;
overflow: auto;
line-height: 1.5em;
}
.alarms {
font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace;
padding: 10px;
border-radius: 5px;
background-color: #294C60;
color: #FFF8EB;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-top: 10px;
overflow: auto;
}
.left-column:hover + .middle-column .explanation {
display: block;
}
.left-column:hover + .middle-column .results {
display: block;
}
svg {
width: 100%;
height: 80%;
}
.right-column {
background-color: #294C60;
max-height: 90vh;
overflow: hidden;
}
</style>
<title>Binsec/Codex Analysis</title>
</head>
<body onload="loadExplanations(); linkGraph();">
<h2>Binsec/Codex Analysis</h2>
<div class="row">
<div class="column scrollbar" style="background-color:#294C60; overflow-y: scroll; height: 90vh;">
<h2>Disassembly Results</h2>
<table id="disasmTable">
|header};;
let = {footer|
</table>
</div>
<div class="column middle-column" style="background-color:#001B2E;">
<div style="height: 30vh;">
<h2>DBA blocks</h2>
<pre style="overflow: auto; max-height: 80%;" class="explanation scrollbar">Explanation will appear here.</pre>
</div>
<div style="height: 30vh;">
<h2>Analysis Result</h2>
<pre style="overflow: auto; max-height: 80%;" class="results scrollbar"></pre>
</div>
<div style="height: 21vh;">
<h2>Alarms</h2>
<div class="alarms scrollbar" style="max-height: 21vh; overflow: auto;">
|footer};;
let = {footer|
</div>
</div>
</div>
<div id="graph" class="column right-column" style="text-align: center;">
</div>
</div>
</body>
</html>
|footer};;
let logger_hashtable fmt hashtable =
Hashtbl.iter (fun str id -> fprintf fmt "\"%d\" : \"%s\",\n" id
(Str.global_replace (Str.regexp "\n") "\\n"
(Str.global_replace (Str.regexp "\\undef") "\\undef" str))
) hashtable
;;
let add_unique_to_table hashtable instructionlist =
List.iter (fun (addr, instr) ->
let instrstring = Format.asprintf "%a" (Dba_printer.Ascii.pp_instruction_maybe_goto ~current_id:addr) instr in
if not (Hashtbl.mem hashtable instrstring)
then Hashtbl.add hashtable instrstring (Hashtbl.length hashtable)) instructionlist
;;
let rec load_addresses_from instr nextva valist hashtbl =
let dhunk = instr.Instruction.dba_block in
let dhunklist = Dhunk.flatten dhunk in
add_unique_to_table hashtbl dhunklist;
let binstr = Instruction.to_generic_instruction instr in
let opcode_str = asprintf "%a" Instruction.Generic.pp_opcode binstr |> String.trim in
let valist = VAList.add instr.Instruction.address (dhunk, opcode_str, binstr) valist in
try match nextva with
| Some validNext -> let ni, nnva = Disasm_core.decode validNext in load_addresses_from ni nnva valist hashtbl;
| _ -> valist
with e -> valist
;;
let rec print_dhunk hashtable fmt = function
[] -> ()
| [(addr, instr)] ->
let instrstring = Format.asprintf "%a" (Dba_printer.Ascii.pp_instruction_maybe_goto ~current_id:addr) instr in
Format.fprintf fmt "%d" (Hashtbl.find hashtable instrstring);
| (addr, instr)::xs ->
let instrstring = Format.asprintf "%a" (Dba_printer.Ascii.pp_instruction_maybe_goto ~current_id:addr) instr in
Format.fprintf fmt "%d," (Hashtbl.find hashtable instrstring);
print_dhunk hashtable fmt xs
;;
let print_html fmt valist hashtable =
VAList.iter (fun va (dhunk, opcode_str, binstr) ->
let dhunklist = Dhunk.flatten dhunk in
fprintf fmt "<tr class=\"line hoverable\" data-explanation=\"[%a]\"> <th> %a </th> <th> %s </th> <th> %a </th> </tr>\n"
(print_dhunk hashtable) dhunklist
Virtual_address.pp va
opcode_str
Instruction.Generic.pp_mnemonic binstr
) valist
;;
end