-
Notifications
You must be signed in to change notification settings - Fork 4
/
shell.asm
563 lines (460 loc) · 11.5 KB
/
shell.asm
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
.586p
.model flat,stdcall
option casemap:none
include masm32\windows.inc
; ------------------------------------------------
; 结构体声明
; ------------------------------------------------
; 导入表
TOY_IMPORT_TYPE_DLLNAME equ 1
TOY_IMPORT_TYPE_FIRST_THUNK equ 2
TOY_IMPORT_TYPE_ORDINAL equ 3
TOY_IMPORT_TYPE_FUNC_NAME equ 4
TOY_TYPE_SECTION equ 1 ; 数据块表示一个区块
TOY_TYPE_IMPORT equ 2 ; 数据块表示导入表
TOY_TYPE_BASERELOC equ 3 ; 数据块表示基值重定位数据
TOY_IMPORT_THUNK struct
dwSize dd ?
dwType dd ?
TOY_IMPORT_THUNK ends
; 基值重定位
TOY_BASE_RELOC struct
dwVAddr dd ?
dwType dd ?
dwNumber dd ?
TOY_BASE_RELOC ends
; 区块信息
TOY_SECTION struct
dwEncrypt dd ?
dwOrignalAddr dd ?
dwPackedSize dd ?
TOY_SECTION ends
; 数据块信息
TOY_BLOCK_TYPE_SECTION equ 1
TOY_BLOCK_TYPE_IMPORT equ 2
TOY_BLOCK_TYPE_BASERELOC equ 3
TOY_BLOCK_HEADER struct
dwSize dd ?
dwType dd ?
TOY_BLOCK_HEADER ends
; 函数声明
ToyMemCpy proto dst:ptr byte, src:ptr byte, len:dword
ToyZeroMem proto src:ptr byte, len:dword
ToyDecode proto dst:ptr byte, src:ptr byte, len:dword
ToyCheckDebugger proto
ToyDoSection proto instance:dword, section:dword
ToyDoImport proto instance:dword, thunk:dword
ToyDoBaseReloc proto instance:dword, reloc:dword
ToyDoDataBlock proto instance:dword, blockBase:dword
public ToyShellBegin
public ToyShellEnd
public ToyShellImportBegin
public ToyShellImportEnd
public ToyShellArgs
assume fs:nothing
.code
ToyShellBegin label dword
nop ; 4字节对齐
nop
pushad
call next0
; -------------------------------------------------------------
; 外壳的输入表
; -------------------------------------------------------------
ToyShellImportBegin label dword
ToyImportTable dd AddressFirst - ToyImportTable ; OriginalFirstThunk
dd 0,0 ; TimeDataStamp, ForwardChain
dd DllName - ToyImportTable ; Name
dd AddressFirst - ToyImportTable ; FirstThunk
dd 0,0,0,0,0
AddressFirst dd FirstFunction - ToyImportTable ; 指向IMAGE_THUNK_DATA
AddressSecond dd SecondFunction - ToyImportTable ; 指向IMAGE_THUNK_DATA
AddressThird dd ThirdFunction - ToyImportTable ; 指向IMAGE_THUNK_DATA
AddressFourth dd FourthFunction - ToyImportTable ; 指向IMAGE_THUNK_DATA
AddressFifth dd FifthFunction - ToyImportTable ; 指向IMAGE_THUNK_DATA
dd 0
DllName db 'KERNEL32.dll'
dw 0
FirstFunction dw 0
db 'GetProcAddress', 0
SecondFunction dw 0
db 'GetModuleHandleA', 0
ThirdFunction dw 0
db 'LoadLibraryA', 0
FourthFunction dw 0
db 'VirtualAlloc', 0
FifthFunction dw 0
db 'VirtualFree', 0
ToyShellImportEnd label dword
; -------------------------------------------------------------
; 壳的变量
; -------------------------------------------------------------
ToyShellArgs label dword
toyPackVAddr dd next1 - ToyShellBegin
toyPackSize dd 0
toyBlockVAddr dd 0
toyBlockSize dd 0
origImageBase dd 0
origEntryPoint dd 0
; 内部使用的变量
isFirstRun dd 0
imageBase dd 0
ToyMemCpy proc dst:ptr byte, src:ptr byte, len:dword
mov edi, [dst]
mov esi, [src]
mov ecx, [len]
shr ecx, 2 ; 按DWORD复制
rep movsd
mov ecx, [len] ; 复制剩余的小于DWORD的部分
and ecx, 3
rep movsb
mov eax, [dst]
ret
ToyMemCpy endp
; ------------------------------------------------------
; aPLib
;-------------------------------------------------------
aP_depack_asm:
; aP_depack_asm(const void *source, void *destination)
_ret$ equ 7*4
_src$ equ 8*4 + 4
_dst$ equ 8*4 + 8
pushad
mov esi, [esp + _src$] ; C calling convention
mov edi, [esp + _dst$]
cld
mov dl, 80h
xor ebx,ebx
literal:
movsb
mov bl, 2
nexttag:
call getbit
jnc literal
xor ecx, ecx
call getbit
jnc codepair
xor eax, eax
call getbit
jnc shortmatch
mov bl, 2
inc ecx
mov al, 10h
getmorebits:
call getbit
adc al, al
jnc getmorebits
jnz domatch
stosb
jmp nexttag
codepair:
call getgamma_no_ecx
sub ecx, ebx
jnz normalcodepair
call getgamma
jmp domatch_lastpos
shortmatch:
lodsb
shr eax, 1
jz donedepacking
adc ecx, ecx
jmp domatch_with_2inc
normalcodepair:
xchg eax, ecx
dec eax
shl eax, 8
lodsb
call getgamma
cmp eax, 32000
jae domatch_with_2inc
cmp ah, 5
jae domatch_with_inc
cmp eax, 7fh
ja domatch_new_lastpos
domatch_with_2inc:
inc ecx
domatch_with_inc:
inc ecx
domatch_new_lastpos:
xchg eax, ebp
domatch_lastpos:
mov eax, ebp
mov bl, 1
domatch:
push esi
mov esi, edi
sub esi, eax
rep movsb
pop esi
jmp nexttag
getbit:
add dl, dl
jnz stillbitsleft
mov dl, [esi]
inc esi
adc dl, dl
stillbitsleft:
ret
getgamma:
xor ecx, ecx
getgamma_no_ecx:
inc ecx
getgammaloop:
call getbit
adc ecx, ecx
call getbit
jc getgammaloop
ret
donedepacking:
sub edi, [esp + _dst$]
mov [esp + _ret$], edi ; return unpacked length in eax
popad
ret 8h
; ------------------------------------------------------
; 壳的代码
; ------------------------------------------------------
next0:
pop ebp
sub ebp, (ToyImportTable - ToyShellBegin)
mov eax, [ebp + (isFirstRun - ToyShellBegin)]
.if eax != 0
jmp ToyReturnOEP
.endif
inc dword ptr [ebp + (isFirstRun - ToyShellBegin)]
;得到映像基值
push 0
call dword ptr [ebp + (AddressSecond - ToyShellBegin)] ; GetModuleHandle
mov [ebp + (imageBase - ToyShellBegin)], eax
; 解压
push PAGE_READWRITE
push MEM_COMMIT
push dword ptr [ebp + (toyPackSize - ToyShellBegin)]
push 0
call dword ptr [ebp + (AddressFourth - ToyShellBegin)] ; VirtualAddress
push eax
mov ecx, [ebp + (toyPackSize - ToyShellBegin)]
lea esi, [ebp + (next1 - ToyShellBegin)]
invoke ToyMemCpy, eax, esi, ecx
pop eax
lea esi, [ebp + (next1 - ToyShellBegin)]
push esi
push eax
call aP_depack_asm
jmp next1
; ------------------------------------------------------
; 下面部分运行时解压缩
; ------------------------------------------------------
next1:
; Anti-Debugger
invoke ToyCheckDebugger
; 处理数据块
mov eax, [ebp + (toyBlockVAddr - ToyShellBegin)]
add eax, [ebp + (imageBase - ToyShellBegin)]
invoke ToyDoDataBlock, ebp, eax
; 删除壳的输入表
mov edi, ebp
add edi, ToyShellImportBegin - ToyShellBegin
lea eax, ToyShellImportBegin
lea ecx, ToyShellImportEnd
sub ecx, eax
invoke ToyZeroMem, edi, ecx
; 返回
mov eax, [ebp + (origEntryPoint - ToyShellBegin)]
add eax, [ebp + (imageBase - ToyShellBegin)]
add dword ptr [ebp + (ToyReturnOEP - ToyShellBegin) + 1], eax ; SMC
popad
ToyReturnOEP:
push dword ptr[0]
ret
ToyDoDataBlock proc instance:dword, blockBase:dword
local @block:dword, @blockData:dword
mov edi, [blockBase]
mov [@block], edi
.while 1
mov edi, [@block]
mov eax, edi
add eax, TOY_BLOCK_HEADER.dwSize
mov eax, [eax]
.if eax == 0
.break
.endif
mov eax, edi
add eax, sizeof TOY_BLOCK_HEADER
mov [@blockData], eax
mov eax, edi
add eax, TOY_BLOCK_HEADER.dwType
mov eax, [eax]
.if eax == TOY_TYPE_SECTION
invoke ToyDoSection, [instance], [@blockData]
.elseif eax == TOY_TYPE_IMPORT
invoke ToyDoImport, [instance], [@blockData]
.elseif eax == TOY_TYPE_BASERELOC
invoke ToyDoBaseReloc, [instance], [@blockData]
.endif
mov edi, [@block]
mov ecx, [edi + TOY_BLOCK_HEADER.dwSize]
mov eax, edi
add eax, ecx
mov [@block], eax
; 清空当前数据块的内容
invoke ToyZeroMem, edi, ecx
.endw
ret
ToyDoDataBlock endp
ToyDoSection proc instance:dword, section:dword
local data:dword, encrypt:dword, origAddr:dword, packedSize:dword
mov edi, [section]
mov eax, edi
add eax, sizeof TOY_SECTION
mov data, eax
mov eax, [edi + TOY_SECTION.dwEncrypt]
mov [encrypt], eax
mov eax, [edi + TOY_SECTION.dwPackedSize]
mov [packedSize], eax
mov eax, edi
add eax, TOY_SECTION.dwOrignalAddr
mov eax, [eax]
mov ebx, [instance]
add eax, [ebx + (imageBase - ToyShellBegin)]
mov [origAddr], eax
.if encrypt == 0
invoke ToyMemCpy, [origAddr], [data], [packedSize]
.else
invoke ToyDecode, [origAddr], [data], [packedSize]
.endif
ret
ToyDoSection endp
ToyImportThunkNext macro t, n
push edi
mov edi, t
add edi, [edi + TOY_IMPORT_THUNK.dwSize]
mov n, edi
pop edi
endm
ToyImportThunkData macro t, d
push edi
mov edi, t
add edi, sizeof TOY_IMPORT_THUNK
mov d, edi
pop edi
endm
ToyDoImport proc instance:dword, thunk:dword
local @thunk:dword, @dll:dword, @firstThunk:dword, @thunkData:dword, @size:dword
; DllName
mov edi, [thunk]
mov eax, [edi + TOY_IMPORT_THUNK.dwType]
.if eax != TOY_IMPORT_TYPE_DLLNAME
ret
.endif
ToyImportThunkData [thunk], [@thunkData]
push [@thunkData]
mov ebx, [instance]
call dword ptr [ebx + (AddressSecond - ToyShellBegin)] ; GetModuleHandle
.if eax == 0
push [@thunkData]
mov ebx, [instance]
call dword ptr [ebx + (AddressThird - ToyShellBegin)] ; LoadLibrary
.endif
mov [@dll], eax
; FirstThunk
ToyImportThunkNext [thunk], [@thunk]
mov edi, [@thunk]
mov eax, [edi + TOY_IMPORT_THUNK.dwType]
.if eax != TOY_IMPORT_TYPE_FIRST_THUNK
ret
.endif
ToyImportThunkData [@thunk], [@thunkData]
mov edi, [@thunkData]
mov edi, [edi]
mov ebx, [instance]
add edi, [ebx + (imageBase - ToyShellBegin)]
mov [@firstThunk], edi
; Function
.while 1
ToyImportThunkNext [@thunk], [@thunk]
mov eax, [@thunk + TOY_IMPORT_THUNK.dwSize]
mov eax, [eax]
.if eax == 0
.break
.endif
ToyImportThunkData [@thunk], [@thunkData]
mov edi, [@thunk]
mov eax, [edi + TOY_IMPORT_THUNK.dwType]
.if eax == TOY_IMPORT_TYPE_ORDINAL
mov eax, [@thunkData]
push [eax]
.elseif eax == TOY_IMPORT_TYPE_FUNC_NAME
push [@thunkData]
.endif
push [@dll]
mov ebx, [instance]
call dword ptr [ebx + (AddressFirst - ToyShellBegin)] ; GetProcAddress
mov edx, [@firstThunk]
mov [edx], eax
mov eax, [@firstThunk]
add eax, sizeof IMAGE_THUNK_DATA32
mov [@firstThunk], eax
.endw
ret
ToyDoImport endp
ToyDoBaseReloc proc instance:dword, reloc:dword
local @addr:ptr dword, @diff:dword, @offset:ptr word, @VAddr:dword
mov ebx, [instance]
mov eax, [ebx + (imageBase - ToyShellBegin)]
sub eax, [ebx + (origImageBase - ToyShellBegin)]
mov [@diff], eax
mov edi, [reloc]
mov eax, [edi + TOY_BASE_RELOC.dwType]
.if eax != 3
ret
.endif
mov eax, [edi + TOY_BASE_RELOC.dwVAddr]
add eax, [ebx + (imageBase - ToyShellBegin)]
mov [@VAddr], eax
mov eax, edi
add eax, sizeof TOY_BASE_RELOC
mov [@offset], eax
mov ecx, [edi + TOY_BASE_RELOC.dwNumber]
xor esi, esi
.while esi < ecx
mov ebx, [@offset]
movzx eax, word ptr [ebx + sizeof WORD * esi] ; 16bit -> 32bit
add eax, [@VAddr]
mov edx, [@diff]
add [eax], edx
inc esi
.endw
ret
ToyDoBaseReloc endp
ToyZeroMem proc src:ptr byte, len:dword
mov edi, [src]
mov ecx, [len]
mov eax, 0
shr ecx, 2
rep stosd
mov ecx, [len]
and ecx, 3
rep stosb
xor eax, [src]
ret
ToyZeroMem endp
ToyCheckDebugger proc
ret
ToyCheckDebugger endp
ToyDecode proc dst:ptr byte, src:ptr byte, len:dword
mov edi, [dst]
mov esi, [src]
mov edx, [len]
xor ecx, ecx
.while ecx < edx
mov al, byte ptr [esi]
xor al, cl
mov byte ptr [edi], al
inc esi
inc edi
inc ecx
.endw
ret
ToyDecode endp
ToyShellEnd label dword
end