forked from hugsy/windbg_js_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateGraph.js
More file actions
174 lines (135 loc) · 4.74 KB
/
Copy pathCreateGraph.js
File metadata and controls
174 lines (135 loc) · 4.74 KB
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
/**
*
* Generate a callgraph from function name visible with MermaidJS
*
* Note for people without a sense of humor:
* This script was made as a troll following this (https://twitter.com/aionescu/status/1052385986045345794)
* and never as a replacement to IDA
*
* Usage
* windbg> .scriptload \\ph0ny\code\windbg_js_scripts\CreateGraph.js
*
* Example
* windbg> !CreateGraph "ntdll!NtCreateFile"
*/
"use strict";
const log = x => host.diagnostics.debugLog(x + "\n");
const system = x => host.namespace.Debugger.Utility.Control.ExecuteCommand(x);
const u8 = x => host.memory.readMemoryValues(x, 1, 1)[0];
const u16 = x => host.memory.readMemoryValues(x, 1, 2)[0];
const u32 = x => host.memory.readMemoryValues(x, 1, 4)[0];
const u64 = x => host.memory.readMemoryValues(x, 1, 8)[0];
function IsX64(){return host.namespace.Debugger.State.PseudoRegisters.General.ptrsize == 8;}
function IsKd() { return host.namespace.Debugger.Sessions.First().Attributes.Target.IsKernelTarget === true; }
function $(r){ if(!IsKd()) return host.currentThread.Registers.User[r]; else return host.namespace.Debugger.State.DebuggerVariables.curprocess.Threads.First().Registers.User[r] || host.namespace.Debugger.State.DebuggerVariables.curprocess.Threads.First().Registers.Kernel[r]; }
function GetSymbolFromAddress(x){ return system('.printf "%y", ' + x.toString(16)).First(); }
var g_OutfileName ;
/**
*
*/
function GetAddressFromSymbol(sym)
{
if (sym.indexOf("!") == -1)
{
let default_modules = ["nt", "ntdll", "kernel32", "kernelbase"];
for (let mod of default_modules)
{
var res = host.getModuleSymbolAddress(mod, sym);
if (res != undefined)
{
return res;
}
}
}
var parts = sym.split("!");
return host.getModuleSymbolAddress(parts[0], parts[1]);
}
/**
*
*/
function GetBasicBlockIdByAddress(BasicBlocks, Address)
{
var i = 0;
for( let BasicBlock of BasicBlocks )
{
let s1 = Address.toString(16);
let s2 = BasicBlock.StartAddress.toString(16);
let s3 = BasicBlock.EndAddress.toString(16);
//log(`${i} ${s1} in [${s2}, ${s3}[`);
if(BasicBlock.StartAddress.compareTo(Address) <= 0 && BasicBlock.EndAddress.compareTo(Address) > 0)
{
return i;
}
i = i + 1;
}
return null;
}
/**
*
*/
function CreateGraph(location)
{
let target = GetAddressFromSymbol(location);
let address = target.toString(16);
// log("[+] Generating CG for '" + location + "' -> 0x" + address + "...");
let dis = host.namespace.Debugger.Utility.Code.CreateDisassembler();
let bbs = dis.DisassembleFunction(target).BasicBlocks.ToArray();
let nb_bbs = bbs.Count();
// log("[+] Found " + nb_bbs.toString() + " basic blocks");
if (nb_bbs == 0)
{
return;
}
//
// create the basic blocks
//
var OutputStr = "";
OutputStr += `<html><head><title>${location}</title><script src='https://cdnjs.cloudflare.com/ajax/libs/mermaid/8.0.0/mermaid.min.js'/></head>`;
OutputStr += "<body></script><script>mermaid.initialize({startOnLoad:true});</script>";
OutputStr += "<div class='mermaid'>\n";
OutputStr += "graph TD\n\n";
// log("[+] Create the nodes...");
var i = 0;
for( let bb of bbs )
{
//log("[" + i.toString() + "] " + bb.toString());
OutputStr += `${i.toString()}("\n`;
for( let ins of bb.Instructions)
{
OutputStr += `[0x${ins.Address.toString(16)}] ${ins.toString() } <br/>\n`;
}
OutputStr += '")\n';
i++;
}
// log("[+] Link the nodes...");
for( let bb of bbs )
{
let LastInsn = bb.Instructions.Last();
let CurrentBasicBlockId = GetBasicBlockIdByAddress(bbs, LastInsn.Address);
for (let obb of bb.OutboundControlFlows)
{
let NextBasicBlockId = GetBasicBlockIdByAddress(bbs, obb.LinkedBlock.StartAddress);
OutputStr += `${CurrentBasicBlockId} --> ${NextBasicBlockId}\n` ;
}
}
OutputStr += "</div></body></html>";
//
// now write to file
//
g_OutfileName = host.namespace.Debugger.Utility.FileSystem.TempDirectory + "\\WinDbgCreateGraph.html" ;
let hFile = host.namespace.Debugger.Utility.FileSystem.CreateFile(g_OutfileName, "CreateAlways");
let TextWriter = host.namespace.Debugger.Utility.FileSystem.CreateTextWriter(hFile);
TextWriter.WriteLine(OutputStr);
hFile.Close();
g_OutfileName = host.namespace.Debugger.Utility.FileSystem.TempDirectory + "\\WinDbgCreateGraph.html" ;
log(`[+] Graph stored in '${g_OutfileName}'`);
}
/**
*
*/
function initializeScript()
{
return [
new host.functionAlias(CreateGraph, "CreateGraph"),
];
}