-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathdump_injected_pe_rwemem.wdbg
More file actions
393 lines (334 loc) · 18.7 KB
/
dump_injected_pe_rwemem.wdbg
File metadata and controls
393 lines (334 loc) · 18.7 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
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
$$
$$ Author: Javier Vicente Vallejo
$$ Twitter: @vallejocc
$$ Web: http://www.vallejo.cc
$$
$$ $$>a<dump_injected_pe_rwemem.wdbg <destination directory>
$$
$$ This windbg script will walk the results of !address command for each process in the debuggee machine,
$$ searching for RWE memory containing PE files (based on the analysis of PE header).
$$
$$ When a PE file in RWE memory is found, the script will dump it. In addition to dump it, it will fix
$$ some fields of PE header: imagebase will be set to the address where the PE is loaded, and
$$ section[i].PointerToRawData = section[i].VirtualAddress (because we are dumping a mapped PE to disk and,
$$ if we want to analyze the dumped PE with a disassembler for example, we need to fix the sections).
$$
$$.sympath SRV*c:\symcache*http://msdl.microsoft.com/download/symbols
$$.reload
.logopen ${$arg1}\dump_injected_pe_rwemem.start
.printf "start"
.logclose
aS stage @$t19
aS temp @$t18
aS temp2 @$t17
aS temp3 @$t16
aS isPossiblePE @$t15
aS isDosMessageBased @$t14
aS pe @$t13
aS fileheader @$t12
aS optionalheader @$t11
aS sections @$t10
aS nsections @$t9
aS lastsect @$t8
aS prev1 @$t7
aS prev2 @$t6
aS baseaddr @$t5
aS baseaddrlen @$t4
.block
{
.sympath "SRV*c:\symcache*http://msdl.microsoft.com/download/symbols";
.reload
}
.block
{
r stage = 2
.printf "xxxx"
.foreach (processes_tok { !process 0 0 })
{
.if($scmp("${processes_tok}","PROCESS")==0)
{
.if(${stage}==2)
{
$$stage==2 is used to skip the first apparition of PROCESS string in the results of !process 0 0
r stage = 0
}
.else
{
r stage = 1
}
}
.elsif(${stage}==1)
{
.printf /D "<b>Analyzing process ${processes_tok}</b>\n"
r stage = 0
.process /i ${processes_tok}
g
.block
{
.reload /user
}
$$search for memory blocks with ReadWriteExecute protection
$$careful:
$$ when the baseaddress is over 0x10000000 findstr tokens will be:
$$ 93:7640:20010000
$$ 2002c000
$$ 1c000
$$ UserRange
$$ ...
$$ however if the addess is under 0x10000000:
$$ 25:1364:
$$ 60000
$$ 61000
$$ 1000
$$ UserRange
$$ The reason for this its windbg puts spaces before the base address when it hasnt 8 characters to complete 8 characters, but if the address is
$$ ???????? then it doesnt put spaces. We need to have in mind both case, and this is the reason of the stages of the next code
$$
.foreach (tok { .shell -ci "!address" findstr /N /O /R /C:"UserRange.*ExecuteReadWrite" /C:"UserRange.*ReadWriteExecute" })
{
r isPossiblePE = 0
r isDosMessageBased = 0
.printf "${tok}\n"
.if($spat("${tok}","*:*:*")!=0)
{
r stage = 1
}
.elsif(${stage}==1)
{
r prev1 = ${tok}
r stage = 2
}
.elsif(${stage}==2)
{
r prev2 = ${tok}
r stage = 3
}
.elsif(${stage}==3)
{
.if($spat("${tok}","*UserRange*")!=0)
{
r baseaddr = prev1 - prev2
r baseaddrlen = prev2
r stage = 5
}
.else
{
r baseaddr = prev1
r baseaddrlen = ${tok}
r stage = 4
}
}
.elsif(${stage}==4)
{
r stage = 5
}
.elsif(${stage}==5)
{
$$for each block with ReadWriteExecute protection, check MZ / PE
r @$t0 = baseaddr
.if(@$t0!=0)
{
.printf "base %x\n", @$t0
.pagein /p ${processes_tok} @$t0
g
$$!address @$t0
.if($vvalid(@$t0, 2)==1)
{
.printf "valid base address\n"
.printf "PE_header %x\n", @$t0+@@c++(((nt!_IMAGE_DOS_HEADER * )@$t0)->e_lfanew)
.if($vvalid(@$t0+@@c++(((nt!_IMAGE_DOS_HEADER * )@$t0)->e_lfanew), 2)==1)
{
.printf "valid PE_header address\n"
.if(wo(@$t0)==0x5a4d & dwo(@$t0+@@c++(((nt!_IMAGE_DOS_HEADER * )@$t0)->e_lfanew))==0x454E)
{
$$We can find MZ / NE images, we ignore them
}
.elsif(wo(@$t0)==0x5a4d & dwo(@$t0+@@c++(((nt!_IMAGE_DOS_HEADER * )@$t0)->e_lfanew))==0x4550)
{
$$if MZ and PE signatures, valid pe header
.printf "valid PE_header\n"
r isPossiblePE = 1
}
.else
{
$$if not MZ or not PE signature, but msdos message is found, its a possible pe header
r temp = 0
.foreach (tok2 { s -a @$t0 L 0x80 "This program cannot " })
{
r temp = 1
}
.if(${temp}==1)
{
.printf "possible pe header\n"
r isPossiblePE = 1
r isDosMessageBased = 1
}
}
}
.else
{
.printf "not valid PE_header address\n"
}
}
.else
{
.printf "not valid base address\n"
}
$$if we have found a possible PE in a memory zone with ReadWriteExecute protection, we will check if the base address is in the list of loaded module
.if(${isPossiblePE}==1)
{
.printf "is possible module %x\n", @$t0
$$ r temp = 0
$$ $$search for valid from "is not valid address"
$$ .foreach (tok3 { .shell -ci "!lmi @$t0" findstr /N /O "valid.address" })
$$ {
$$ r temp = ${temp} + 1
$$ }
$$ .printf "%x\n", ${temp}
$$
$$ We are going to disable !lmi check. I have found malware that is unmapping
$$ the main module of a executable when it does process hollowing, and it loads
$$ its injected PE in that address. In this cases, !lmi is not answering not valid
$$ address for that address. However !address command is containing the path
$$ of the module (for example Section [\Windows\explorer.exe]) when the module is
$$ a valid loaded module, and it wont contain the path when it is an injected module
$$ in RWE mem. So, we will skip !lmi check, and we will check !address results
$$ vvvvvvvv DISABLE !LMI CHECK vvvvvvvvv
r temp = 4
$$ ^^^^^^^^ DISABLE !LMI CHECK ^^^^^^^^^
$$ "is not valid address" was found
.if(${temp} > 3)
{
$$ there are some modules that !lmi command is answering: is not valid address, however they seems to be valid loaded modules (not interesing for us).
$$ However if we consults information about the address with !address we find things as:
$$ Memory Usage: Section [\WINDOWS\System32\blablabla.mui] (it happens usually with .mui files, but not only with them
$$ We will discard results of !address with .dll], .mui] and .exe]
r temp = 0
.foreach (tok4 { .shell -ci "!address @$t0" findstr /N /O /R /I "\.mui\]" })
{
r temp = ${temp} + 1
}
r temp2 = 0
.foreach (tok5 { .shell -ci "!address @$t0" findstr /N /O /R /I "\.dll\]" })
{
r temp2 = ${temp2} + 1
}
r temp3 = 0
.foreach (tok6 { .shell -ci "!address @$t0" findstr /N /O /R /I "\.exe\]" })
{
r temp3 = ${temp3} + 1
}
.printf "search !address .mui %x\n", ${temp}
.printf "search !address .dll %x\n", ${temp2}
.printf "search !address .exe %x\n", ${temp3}
.if(${temp} < 4 and ${temp2} < 4 and ${temp3} < 4)
{
.if(${isDosMessageBased}==0)
{
.printf /D "<b>---------------------------------------------------------------------------</b>\n"
.printf /D "<b>Process: ${processes_tok} base: %x -> Possible injected or unpacked PE</b>\n", @$t0
.printf /D "<b>---------------------------------------------------------------------------</b>\n"
}
.else
{
.printf /D "-------------------------------------------------------------------------------------------------------------------------------------\n"
.printf /D "Process: ${processes_tok} base: %x -> Possible injected or unpacked PE, based on the dos header message: This program cannot...\n", @$t0
.printf /D "--------------------------------------------------------------------------------------------------------------------------------------\n"
}
.printf "xxxx1\n"
r pe = @$t0 + poi(@$t0 + @@(#FIELD_OFFSET(nt!_IMAGE_DOS_HEADER, e_lfanew)))
.printf "xxxx2 %x\n", ${pe}
r fileheader = ${pe} + @@(#FIELD_OFFSET(nt!_IMAGE_NT_HEADERS, FileHeader))
.printf "xxxx3 %x\n", ${fileheader}
r optionalheader = ${pe} + @@(#FIELD_OFFSET(nt!_IMAGE_NT_HEADERS, OptionalHeader))
.printf "xxxx4 %x\n", ${optionalheader}
r nsections = poi(${fileheader} + @@(#FIELD_OFFSET(nt!_IMAGE_FILE_HEADER, NumberOfSections )))&0xffff
.printf "xxxx5 %x\n", ${nsections}
r sections = ${pe} + @@c++(sizeof(nt!_IMAGE_NT_HEADERS))
.printf "xxxx6 %x\n", ${sections}
$$ reach the end of the PE file (last section rva + last section vsize)
r lastsect = ${sections} + ( ${nsections} - 1 ) * @@c++(sizeof(nt!_IMAGE_SECTION_HEADER))
r @$t1 = @$t0 + @@c++( ( ( nt!_IMAGE_SECTION_HEADER * ) ${lastsect} )->VirtualAddress ) + @@c++( ( ( nt!_IMAGE_SECTION_HEADER * ) ${lastsect} )->Misc.VirtualSize )
r @$t2 = @$t0 + @@c++( ( ( nt!_IMAGE_SECTION_HEADER * ) ${lastsect} )->VirtualAddress ) + @@c++( ( ( nt!_IMAGE_SECTION_HEADER * ) ${lastsect} )->SizeOfRawData )
r @$t3 = @$t0
.if (@$t2 > @$t1) { r @$t1 = @$t2 }
$$limit to dump = 0x100000 bytes
.if (@$t1-@$t0 > 0x100000)
{
r @$t1 = @$t0+0x100000
}
.printf "xxxx7"
$$ before dumping the PE, we are going to modify sections' raw offset = virtual address for coherency in the dumped PE when we analyze it with IDA or any other tool
r $t2 = ${sections}
r lastsect = ${sections} + ( ${nsections} ) * @@c++(sizeof(nt!_IMAGE_SECTION_HEADER))
.while (@$t2 < ${lastsect})
{
ed (@$t2+@@( #FIELD_OFFSET(nt!_IMAGE_SECTION_HEADER, PointerToRawData))) @@c++((( nt!_IMAGE_SECTION_HEADER *)@$t2)->VirtualAddress)
.if( @@c++((( nt!_IMAGE_SECTION_HEADER *)@$t2)->SizeOfRawData) < @@c++((( nt!_IMAGE_SECTION_HEADER *)@$t2)->Misc.VirtualSize))
{
ed (@$t2+@@( #FIELD_OFFSET(nt!_IMAGE_SECTION_HEADER, SizeOfRawData))) @@c++((( nt!_IMAGE_SECTION_HEADER *)@$t2)->Misc.VirtualSize)
}
.else
{
ed (@$t2+@@( #FIELD_OFFSET(nt!_IMAGE_SECTION_HEADER, Misc))) @@c++((( nt!_IMAGE_SECTION_HEADER *)@$t2)->SizeOfRawData)
}
r $t2 = @$t2 + @@c++(sizeof(nt!_IMAGE_SECTION_HEADER))
}
$$update sizeofimage
.block
{
ed (${optionalheader} + @@(#FIELD_OFFSET(nt!_IMAGE_OPTIONAL_HEADER, SizeOfImage))) (@$t1-@$t0)&0x7fffffff
}
.printf "xxxx8"
$$update imagebase
.block
{
ed (${optionalheader} + @@(#FIELD_OFFSET(nt!_IMAGE_OPTIONAL_HEADER, ImageBase))) ${baseaddr}
}
.printf "xxxx9"
.printf "dumping file address start %x end %x", @$t0, @$t1
.while (@$t0 < @$t1)
{
.printf "paging in: %x\n", @$t0
.pagein /p ${processes_tok} @$t0
g
r @$t0 = @$t0 + 0x1000;
};
.foreach /pS 4 (baseaddr_tok { ? baseaddr })
{
.foreach /pS 4 (baseaddrlen_tok { ? baseaddrlen })
{
.printf "${processes_tok}_${baseaddr_tok}_${baseaddrlen_tok}.pedmp"
.writemem ${$arg1}\${processes_tok}_${baseaddr_tok}_${baseaddrlen_tok}.pedmp @$t3 L (@$t1 - @$t3)
}
}
}
}
}
r stage = 0
}
}
}
r stage = 0
}
}
.logopen ${$arg1}\dump_injected_pe_rwemem.end
.printf "end"
.logclose
ad stage
ad temp
ad temp2
ad temp3
ad isPossiblePE
ad isDosMessageBased
ad pe
ad fileheader
ad optionalheader
ad sections
ad nsections
ad lastsect
ad prev1
ad prev2
ad baseaddr
ad baseaddrlen
}