forked from killeven/ObjToShellCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVxShellCode.inc
More file actions
839 lines (695 loc) · 14.1 KB
/
VxShellCode.inc
File metadata and controls
839 lines (695 loc) · 14.1 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
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
{$ifndef VX_SHELLCODE}
{$define VX_SHELLCODE}
// ============================================================================
// Memory Function's
// ============================================================================
// String Function's
function _StrlenA(lpszStr: PAnsiChar): LongWord;
asm
{$ifdef CPUX64}
push rdi
mov rdi, rcx
xor rcx, rcx // 6 bytes
not rcx
//or rcx, -1 // 7 bytes
xor rax, rax
repne scasb
not rcx
dec rcx
pop rdi
{$else}
push edi
mov edi, eax
or ecx, -1
xor eax, eax
repne scasb
not ecx
dec ecx
mov eax, ecx
pop edi
{$endif}
end;
function _StrlenW(lpszStr: PWideChar): LongWord;
asm
{$ifdef CPUX64}
push rdi
mov rdi, rcx
xor rcx, rcx // 6 bytes
not rcx
//or rcx, -1 // 7 bytes
xor rax, rax
repne scasw
not rcx
dec rcx
pop rdi
{$else}
push edi
mov edi, eax
or ecx, -1
xor eax, eax
repne scasw
not ecx
dec ecx
pop edi
{$endif}
end;
// ============================================================================
// Hash Function's
function _HashStringFromRol3Xor(lpszStr: PAnsiChar): LongWord;
asm
{$ifdef CPUX64}
{$else}
push esi
cdq
test eax, eax
jz @@finished
mov esi, eax
xor eax, eax
@@calc_hash:
lodsb
test al, al
jz @@finished
rol edx, 3
xor dl, al
jmp @@calc_hash
@@finished:
mov eax, edx
pop esi
{$endif}
end;
//function _HashStringFromRol3Xor
function _HashStringFromRor13Add(lpszStr: PAnsiChar): LongWord;
asm
{$ifdef CPUX64}
{$else}
push esi
cdq
test eax, eax
jz @@finished
mov esi, eax
xor eax, eax
@@calc_hash:
lodsb
test al, al
jz @@finished
ror edx, 13
add edx, eax
jmp @@calc_hash
@@finished:
mov eax, edx
pop esi
{$endif}
end;
// ============================================================================
// ShellCode Function's
function _GetDelta(): UIntPtr;
asm
{$ifdef CPUX64}
xor rax, rax
{$else}
call @Delta
@Delta:
pop eax
sub eax, offset @Delta
{$endif}
end;
function _GetFixAddress(p: Pointer): Pointer;
asm
{$ifdef CPUX64}
mov rax, rcx
{$else}
call @Delta
@Delta:
pop ecx
sub ecx, offset @Delta
add eax, ecx
{$endif}
end;
function _GetFixString(p: PTChar): PTChar;
asm
{$ifdef CPUX64}
mov rax, rcx
{$else}
call @Delta
@Delta:
pop ecx
sub ecx, offset @Delta
add eax, ecx
{$endif}
end;
function _GetDelta_Pas(lpParam: Pointer): UIntPtr; stdcall;
{$ifdef WIN32}
const
CALL_CODE_LENGTH = 5;
var
lpCallAddr, lpGetDeltaAddr: Pointer;
begin
if (lpParam = nil) then
begin
Result := _GetDelta_Pas(@_GetDelta_Pas);
end else
begin
lpCallAddr := Pointer(PLongWord(LongWord(@lpParam) - SizeOf(LongWord))^ - CALL_CODE_LENGTH);
lpGetDeltaAddr := Pointer(LongWord(lpCallAddr) + PLongword(LongWord(lpCallAddr) + SizeOf(Byte))^);
Result := (LongWord(lpGetDeltaAddr) + CALL_CODE_LENGTH) - UIntPtr(lpParam);
end;
end;
{$else}
begin
Result := 0;
end;
{$endif}
function _GetKernelBase_PEB(): HMODULE;
asm
{$ifdef WIN64}
// A:(9 byte)
//mov rax, [gs:60h]
// B:(8 bytes)
//push rsi
//push 60h
//pop rsi
//lods [gs:60h]
//pop rsi
// C:(8 bytes)
xor rax, rax
mov rax, [gs:rax + 60h]
// A:(18 bytes)
mov rax, [rax + 18h]
mov rax, [rax + 30h] // InInitializationOrderModuleList : _LIST_ENTRY
mov rax, [rax]
mov rax, [rax]
mov rax, [rax + 10h]
// B:(21 bytes)win7+ (理论上来说无法保证准确性[比A好一点]-所以只适合kernel32)
{mov rdx, [rax + 18h]
mov rdx, [rdx + 30h] // InInitializationOrderModuleList : _LIST_ENTRY
@@next_module:
mov rax, [rdx + 10h]
cmp byte [rdx + 38h], 18h // (kernel32.dll-宽字符长24字节)
mov rdx, [rdx]
jnz @@next_module}
{$else}
// A:(7 bytes)
//mov eax, [fs:30h]
// B:(7 bytes)
//push esi
//push 30h
//pop esi
//lods [fs:esi]
//pop esi
// C:(5 bytes)
//cdq
//mov eax, [fs:edx + 30h]
xor eax, eax
mov eax, [fs:eax + 30h]
{
test eax, eax
jns @@os_Nt
@@os_9x:
mov eax, [eax + 34h]
//lea eax, [eax + 7ch]
//mov eax, [eax + 3ch]
mov eax, [eax + 0b8h]
ret
}
@@os_Nt:
// A:(13 bytes)win7+ (理论上来说无法保证准确性-所以只适合kernel32)
mov eax, [eax + 0ch]
mov eax, [eax + 14h] // InMemoryOrderModuleList : _LIST_ENTRY
mov eax, [eax]
mov eax, [eax]
mov eax, [eax + 10h]
// B:(17 bytes)win7+ (理论上来说无法保证准确性[比A,B好一点]-所以只适合kernel32)
{mov edx, [eax + 0ch]
mov edx, [edx + 1ch] // InInitializationOrderModuleList : _LIST_ENTRY
@@next_module:
mov eax, [edx + 8h]
cmp byte [edx + 1ch], 18h // (kernel32.dll-宽字符长24字节)
mov edx, [edx]
jnz @@next_module}
// C:(14 bytes)win7+ (理论上来说无法保证准确性-所以只适合kernel32)
{push esi
mov eax, [eax + 0ch]
mov esi, [eax + 0ch] // InLoadOrderModuleList : _LIST_ENTRY
lodsd
xchg esi, eax
lodsd
mov eax, [eax + 18h] // +0x018 DllBase
pop esi}
{$endif}
end;
// 不区分大小写的宽字符串Hash(Ror13 & add)
function _String_Ror13HashIW(lpszStr: PWideChar): LongWord;
asm
{$ifdef CPUX64}
push rsi
push rdi
mov rsi, rcx
{$else}
push esi
push edi
mov esi, eax
xor eax, eax
@@next_char:
lodsb
or al, al
jz @@finished
mov edx, eax
add dl, 9fh
sub dl, 1ah
jnb @@not_lowercase
sub al, 20h
@@not_lowercase:
ror edi, 13
add edi, eax
jmp @@next_char
@@finished:
mov eax, edi
pop edi
pop esi
{$endif}
end;
function _GetModuleHandleW_FromHash(nNameHash: LongWord): HMODULE;
asm
{$ifdef WIN64}
push rsi
push rdi
mov ebp, ecx
xor rax, rax
mov rax, [gs:rax + 60h]
mov rdx, [rax + 18h]
mov r9, [rdx + 30h] // InInitializationOrderModuleList : _LIST_ENTRY
@@next_mod:
movzx ecx, word ptr[r9 + 38h] // .FullDllName.Length
jecxz @@error // 判断模块名称是否为NULL
mov rsi, [r9 + 40h] // .FullDllName.Buffer
xor edi, edi // dwhash = 0
@@loop_modname:
xor rax, rax
lodsb
mov rdx, rax
add dl, 9fh
sub dl, 1ah
jnb @@not_lowercase
sub al, 20h
@@not_lowercase:
ror edi, 13
add edi, eax
loop @@loop_modname
cmp edi, ebp // "kernel32.dll"(WideChar) = 06A4ABC5Bh
mov rax, [r9 + 10h]
mov r9, [r9]
jne @@next_mod
jmp @@finished
@@error:
xor rax, rax
@@finished:
pop rdi
pop rsi
{$else}
pushad
mov ebp, eax
cdq
mov edx, [fs:edx + 30h]
mov eax, [edx + 0ch]
mov ebx, [eax + 14h]
@@next_mod:
movzx ecx, word ptr[ebx + 24h] // .FullDllName.Length
jecxz @@error // 判断模块名称是否为NULL
mov esi, [ebx + 28h] // .FullDllName.Buffer
xor edi, edi // dwhash = 0
@@loop_modname:
xor eax, eax
lodsb
mov edx, eax
add dl, 9fh
sub dl, 1ah
jnb @@not_lowercase
sub al, 20h
@@not_lowercase:
ror edi, 13
add edi, eax
loop @@loop_modname
cmp edi, ebp // "kernel32.dll"(WideChar) = 06A4ABC5Bh
mov eax, [ebx + 10h]
mov ebx, [ebx]
jne @@next_mod
jmp @@finished
@@error:
xor eax, eax
@@finished:
mov [esp+(4*7)], eax
popad
{$endif}
end;
function _GetProcAddress_FromHash(nFuncHash: LongWord; fnGetProcAddres: Pointer; hModuleBase: HMODULE): Pointer;
{$ifdef WIN64}
asm
push rcx
push rdx
push rbx
push rdi
push rsi
push rbp
push r10
push r11
mov rbp, r8 // hModuleBase save to rbp
mov r10, rdx // fnGetProcAddress save to r11
mov r11, rcx // nFuncHash save to rbx
mov eax, [rbp+3ch] // PE
mov eax, [rbp+rax+88h] // Export Table RVA
lea rsi, [rbp+rax+18h] // Export Table VA+18h
lodsd
xchg rax, rcx // NumberOfNames
lodsd // AddressOfFunctions
push rax
lodsd // AddressOfNames
xchg rax, rdx
lodsd // AddressOfNameOrdinals
add rax, rbp
xchg rax, rbx
lea rsi, [rbp+rdx]
@@next_func:
lodsd
add rax, rbp
xor edi, edi
xchg rax, rdi
// Calc function name hash
xor edx, edx
@@calc_hash:
rol edx, 3
xor dl, byte ptr[rdi]
inc rdi
cmp byte ptr[rdi], 0
jnz @@calc_hash
// comp hash
cmp r11d, edx
jnz @@loop_func
// Needed function found
or r10, r10
jz @@get_ord
mov eax, [rsi-4]
add rax, rbp
sub rsp, (8*4)
mov rdx, rax
mov rcx, rbp
call r10 // GetProcAddress
add rsp, (8*4)
jmp @@jmp_end
@@get_ord:
movzx eax, word ptr[rbx] // Name ordinal
shl eax, 2 // Multiply by 4
add eax, [rsp]
mov eax, [rax+rbp]
add rax, rbp
@@jmp_end:
jmp @@finished
//
@@loop_func:
inc rbx
inc rbx
loop @@next_func
@@finished:
pop rcx
pop r11
pop r10
pop rbp
pop rsi
pop rdi
pop rbx
pop rdx
pop rcx
end;
{$else}
const
pushad_eax = 7 * 4;
pushad_ecx = 6 * 4;
pushad_edx = 5 * 4;
asm
pushad
mov ebp, [esp+pushad_ecx]
mov eax, [ebp+3ch] // PE
mov eax, [ebp+eax+78h] // Export Table RVA
lea esi, [ebp+eax+18h] // Export Table VA+18h
lodsd
xchg eax, ecx // NumberOfNames
lodsd // AddressOfFunctions
push eax
lodsd // AddressOfNames
xchg eax, edx
lodsd // AddressOfNameOrdinals
add eax, ebp
xchg eax, ebx
lea esi, [edx+ebp]
@@next_func:
lodsd
add eax, ebp
xor edi, edi
xchg eax, edi
// Calc function name hash
xor edx, edx
@@calc_hash:
rol edx, 3
xor dl, byte ptr[edi]
inc edi
cmp byte ptr[edi], 0
jnz @@calc_hash
//
cmp [esp+(4+pushad_eax)], edx
jnz @@loop_func
// Needed function found
mov edx, [esp+(4+pushad_edx)]
or edx, edx
jz @@get_ord
mov eax, [esi-4]
add eax, ebp
push eax
push ebp
call edx // GetProcAddress
jmp @@jmp_end
@@get_ord:
movzx eax, word ptr[ebx] // Name ordinal
shl eax, 2 // Multiply by 4
add eax, [esp]
mov eax, [eax+ebp]
add eax, ebp
@@jmp_end:
jmp @@finished
//
@@loop_func:
inc ebx
inc ebx
loop @@next_func
@@finished:
pop ecx
mov [esp+pushad_eax], eax
popad
end;
{$endif}
// ============================================================================
// aPlib 1.1.1
// aP_depack_asm(const void *source, void *destination)
function aP_depack_asm(const source: Pointer; destination: Pointer): LongWord; stdcall;
{$ifdef WIN64}
asm
push rbx
push rsi
push rdi
push rdx
//mov rsi, rcx
//mov rdi, rdx
mov rsi, [source]
mov rdi, [destination]
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, r8d
@domatch_lastpos:
mov eax, r8d
mov bl, 1
@domatch:
push rsi
mov rsi, rdi
sub rsi, rax
rep movsb
pop rsi
jmp @nexttag
@getbit:
add dl, dl
jnz @@stillbitsleft
mov dl, [rsi]
inc rsi
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:
pop rdx
sub rdi, rdx
xchg eax, edi
pop rdi
pop rsi
pop rbx
end;
{$else}
const
_ret = 7*4;
_src = 8*4 + 4;
_dst = 8*4 + 8;
asm
pushad
mov esi, [source]
mov edi, [destination]
//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
sub edi, [destination]
mov [Result], edi
popad
end;
{$endif}
{$endif}