-
Notifications
You must be signed in to change notification settings - Fork 69
/
loadlibrary.js
200 lines (182 loc) · 11.5 KB
/
loadlibrary.js
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
//--------------------------//
// Calling Native Functions //
//--------------------------//
// Native function pointer
var pGetModuleFileNameW = Module.findExportByName("Kernel32.dll","GetModuleFileNameW");
// Function prototype
var fGetModuleFileNameW = new NativeFunction(
pGetModuleFileNameW,
"uint32",
[
"pointer",
"pointer",
"uint32"
]
);
// Array to store LoadLibrary calls
var aModules = new Array();
//-------------------------//
// Load library call chain //
//-------------------------//
// Native function pointers
var pLoadLibraryA = Module.findExportByName('Kernel32.dll', 'LoadLibraryA')
var pLoadLibraryExA = Module.findExportByName('Kernel32.dll', 'LoadLibraryExA')
var pLoadLibraryW = Module.findExportByName('Kernel32.dll', 'LoadLibraryW')
var pLoadLibraryExW = Module.findExportByName('Kernel32.dll', 'LoadLibraryExW')
var pGetProcAddress = Module.findExportByName('Kernel32.dll', 'GetProcAddress')
// Enums
var Dll_dwFlags = {
NONE: 0x0,
DONT_RESOLVE_DLL_REFERENCES: 0x1,
LOAD_IGNORE_CODE_AUTHZ_LEVEL: 0x10,
LOAD_LIBRARY_AS_DATAFILE: 0x2,
LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE: 0x40,
LOAD_LIBRARY_AS_IMAGE_RESOURCE: 0x20,
LOAD_LIBRARY_SEARCH_APPLICATION_DIR: 0x200,
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS: 0x1000,
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR: 0x100,
LOAD_LIBRARY_SEARCH_SYSTEM32: 0x800,
LOAD_LIBRARY_SEARCH_USER_DIRS: 0x400,
LOAD_WITH_ALTERED_SEARCH_PATH: 0x8
}
// Helpers
function ParseDllFlags(FlagVal) {
var BitMask = [];
if (FlagVal == Dll_dwFlags.NONE) {
BitMask.push("NONE");
} else {
if ((FlagVal & Dll_dwFlags.DONT_RESOLVE_DLL_REFERENCES) == Dll_dwFlags.DONT_RESOLVE_DLL_REFERENCES) BitMask.push("DONT_RESOLVE_DLL_REFERENCES");
if ((FlagVal & Dll_dwFlags.LOAD_IGNORE_CODE_AUTHZ_LEVEL) == Dll_dwFlags.LOAD_IGNORE_CODE_AUTHZ_LEVEL) BitMask.push("LOAD_IGNORE_CODE_AUTHZ_LEVEL");
if ((FlagVal & Dll_dwFlags.LOAD_LIBRARY_AS_DATAFILE) == Dll_dwFlags.LOAD_LIBRARY_AS_DATAFILE) BitMask.push("LOAD_LIBRARY_AS_DATAFILE");
if ((FlagVal & Dll_dwFlags.LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE) == Dll_dwFlags.LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE) BitMask.push("LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE");
if ((FlagVal & Dll_dwFlags.LOAD_LIBRARY_AS_IMAGE_RESOURCE) == Dll_dwFlags.LOAD_LIBRARY_AS_IMAGE_RESOURCE) BitMask.push("LOAD_LIBRARY_AS_IMAGE_RESOURCE");
if ((FlagVal & Dll_dwFlags.LOAD_LIBRARY_SEARCH_APPLICATION_DIR) == Dll_dwFlags.LOAD_LIBRARY_SEARCH_APPLICATION_DIR) BitMask.push("LOAD_LIBRARY_SEARCH_APPLICATION_DIR");
if ((FlagVal & Dll_dwFlags.LOAD_LIBRARY_SEARCH_DEFAULT_DIRS) == Dll_dwFlags.LOAD_LIBRARY_SEARCH_DEFAULT_DIRS) BitMask.push("LOAD_LIBRARY_SEARCH_DEFAULT_DIRS");
if ((FlagVal & Dll_dwFlags.LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR) == Dll_dwFlags.LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR) BitMask.push("LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR");
if ((FlagVal & Dll_dwFlags.LOAD_LIBRARY_SEARCH_SYSTEM32) == Dll_dwFlags.LOAD_LIBRARY_SEARCH_SYSTEM32) BitMask.push("LOAD_LIBRARY_SEARCH_SYSTEM32");
if ((FlagVal & Dll_dwFlags.LOAD_LIBRARY_SEARCH_USER_DIRS) == Dll_dwFlags.LOAD_LIBRARY_SEARCH_USER_DIRS) BitMask.push("LOAD_LIBRARY_SEARCH_USER_DIRS");
if ((FlagVal & Dll_dwFlags.LOAD_WITH_ALTERED_SEARCH_PATH) == Dll_dwFlags.LOAD_WITH_ALTERED_SEARCH_PATH) BitMask.push("LOAD_WITH_ALTERED_SEARCH_PATH");
}
if (BitMask.length == 0) {
BitMask.push(FlagVal);
}
return BitMask.join("|");
}
Interceptor.attach(pLoadLibraryA, {
onEnter: function (args) {
// Make sure var is reset
this.sideLoad = false;
var sPath = args[0].readAnsiString();
// send("LoadLibraryW,LPCWSTR: " + args[0].readUtf16String())
if (!sPath.toLowerCase().includes("\\windows") && sPath.toLowerCase().includes("dll") && !sPath.toLowerCase().includes("kernel") && !sPath.toLowerCase().includes("ms-win") && !sPath.toLowerCase().includes("advapi") && !sPath.toLowerCase().includes("ntdll") && !sPath.toLowerCase().includes("user32") && !sPath.toLowerCase().includes("gdiplus") && !sPath.toLowerCase().includes("imm32") && !sPath.toLowerCase().includes("gdi32") && !sPath.toLowerCase().includes("ole32") && !sPath.toLowerCase().includes("shell32") && !sPath.toLowerCase().includes("wiatrace") && !sPath.toLowerCase().includes("mscoree") && !sPath.toLowerCase().includes("comctl32") && !sPath.toLowerCase().includes("version") && !sPath.toLowerCase().includes("oleaut32") && !sPath.toLowerCase().includes("wintrust") && !sPath.toLowerCase().includes("crypt32") && !sPath.toLowerCase().includes("sxs") && !sPath.toLowerCase().includes("d3d10warp")) {
send("LoadLibraryA,LPCSTR: " + args[0].readAnsiString())
// Store the path to the dll
this.sPath = args[0].readAnsiString();
// Set sideload to true to store it in the array of modules
this.sideLoad = true;
}
},
onLeave: function (retval) {
if (this.sideLoad){
// if sideload was true, store it in the array of modules
var sVal = retval.toString() + " - " + this.sPath;
if (aModules.indexOf(sVal) == -1) {
aModules.push(sVal);
}
}
}
});
Interceptor.attach(pLoadLibraryExA, {
onEnter: function (args) {
// Make sure var is reset
this.sideLoad = false;
var sPath = args[0].readAnsiString();
// send("LoadLibraryW,LPCWSTR: " + args[0].readUtf16String())
if (!sPath.toLowerCase().includes("\\windows") && sPath.toLowerCase().includes("dll") && !sPath.toLowerCase().includes("kernel") && !sPath.toLowerCase().includes("ms-win") && !sPath.toLowerCase().includes("advapi") && !sPath.toLowerCase().includes("ntdll") && !sPath.toLowerCase().includes("user32") && !sPath.toLowerCase().includes("gdiplus") && !sPath.toLowerCase().includes("imm32") && !sPath.toLowerCase().includes("gdi32") && !sPath.toLowerCase().includes("ole32") && !sPath.toLowerCase().includes("shell32") && !sPath.toLowerCase().includes("wiatrace") && !sPath.toLowerCase().includes("mscoree") && !sPath.toLowerCase().includes("comctl32") && !sPath.toLowerCase().includes("version") && !sPath.toLowerCase().includes("oleaut32") && !sPath.toLowerCase().includes("wintrust") && !sPath.toLowerCase().includes("crypt32") && !sPath.toLowerCase().includes("sxs") && !sPath.toLowerCase().includes("d3d10warp")) {
var FlagVals = ParseDllFlags(args[2])
if (!FlagVals.includes("SYSTEM32") && !FlagVals.includes("DATAFILE")){
send("LoadLibraryExA,LPCSTR: " + args[0].readAnsiString() + ", dwFlags : " + FlagVals)
// Store the path to the dll
this.sPath = args[0].readAnsiString();
// Set sideload to true to store it in the array of modules
this.sideLoad = true;
}
}
},
onLeave: function (retval) {
if (this.sideLoad){
// if sideload was true, store it in the array of modules
var sVal = retval.toString() + " - " + this.sPath;
if (aModules.indexOf(sVal) == -1) {
aModules.push(sVal);
}
}
}
});
Interceptor.attach(pLoadLibraryW, {
onEnter: function (args) {
// Make sure var is reset
this.sideLoad = false;
var sPath = args[0].readUtf16String();
// send("LoadLibraryW,LPCWSTR: " + args[0].readUtf16String())
if (!sPath.toLowerCase().includes("\\windows") && sPath.toLowerCase().includes("dll") && !sPath.toLowerCase().includes("kernel") && !sPath.toLowerCase().includes("ms-win") && !sPath.toLowerCase().includes("advapi") && !sPath.toLowerCase().includes("ntdll") && !sPath.toLowerCase().includes("user32") && !sPath.toLowerCase().includes("gdiplus") && !sPath.toLowerCase().includes("imm32") && !sPath.toLowerCase().includes("gdi32") && !sPath.toLowerCase().includes("ole32") && !sPath.toLowerCase().includes("shell32") && !sPath.toLowerCase().includes("wiatrace") && !sPath.toLowerCase().includes("mscoree") && !sPath.toLowerCase().includes("comctl32") && !sPath.toLowerCase().includes("version") && !sPath.toLowerCase().includes("oleaut32") && !sPath.toLowerCase().includes("wintrust") && !sPath.toLowerCase().includes("crypt32") && !sPath.toLowerCase().includes("sxs") && !sPath.toLowerCase().includes("d3d10warp")) {
send("LoadLibraryW,LPCWSTR: " + args[0].readUtf16String())
// Store the path to the dll
this.sPath = args[0].readUtf16String();
// Set sideload to true to store it in the array of modules
this.sideLoad = true;
}
},
onLeave: function (retval) {
if (this.sideLoad){
// if sideload was true, store it in the array of modules
var sVal = retval.toString() + " - " + this.sPath;
if (aModules.indexOf(sVal) == -1) {
aModules.push(sVal);
}
}
}
});
Interceptor.attach(pLoadLibraryExW, {
onEnter: function (args) {
// Make sure var is reset
this.sideLoad = false;
var sPath = args[0].readUtf16String();
// send("1 - LoadLibraryExW,LPCWSTR : " + args[0].readUtf16String())
if (!sPath.toLowerCase().includes("\\windows") && sPath.toLowerCase().includes("dll") && !sPath.toLowerCase().includes("kernel") && !sPath.toLowerCase().includes("ms-win") && !sPath.toLowerCase().includes("advapi") && !sPath.toLowerCase().includes("ntdll") && !sPath.toLowerCase().includes("user32") && !sPath.toLowerCase().includes("gdiplus") && !sPath.toLowerCase().includes("imm32") && !sPath.toLowerCase().includes("gdi32") && !sPath.toLowerCase().includes("ole32") && !sPath.toLowerCase().includes("shell32") && !sPath.toLowerCase().includes("wiatrace") && !sPath.toLowerCase().includes("mscoree") && !sPath.toLowerCase().includes("comctl32") && !sPath.toLowerCase().includes("version") && !sPath.toLowerCase().includes("oleaut32") && !sPath.toLowerCase().includes("wintrust") && !sPath.toLowerCase().includes("crypt32") && !sPath.toLowerCase().includes("sxs") && !sPath.toLowerCase().includes("d3d10warp")) {
var FlagVals = ParseDllFlags(args[2])
// send(FlagVals)
if (!FlagVals.includes("SYSTEM32") && !FlagVals.includes("DATAFILE")){
send("LoadLibraryExW,LPCWSTR : " + args[0].readUtf16String() + ", dwFlags : " + FlagVals)
// Store the path to the dll
this.sPath = args[0].readUtf16String();
// Set sideload to true to store it in the array of modules
this.sideLoad = true;
}
}
},
onLeave: function (retval) {
if (this.sideLoad){
// if sideload was true, store it in the array of modules
var sVal = retval.toString() + " - " + this.sPath;
if (aModules.indexOf(sVal) == -1) {
aModules.push(sVal);
}
}
}
});
Interceptor.attach(pGetProcAddress, {
onEnter: function (args) {
// Call function
for (var i = 0; i < aModules.length; i++) {
// send(aModules[i].toString() + " = " + args[0].toString())
if ((aModules[i].toString()).startsWith(args[0].toString())) {
// send(aModules[i].toString() + " " + args[0].toString())
var lpText = Memory.alloc(0x1000)
var CallResult = fGetModuleFileNameW(args[0], lpText, 0x1000);
// Print function return value
send("GetProcAddress,hModule : " + lpText.readUtf16String() + ", LPCSTR: " + args[1].readAnsiString())
}
}
}
});