Foro de elhacker.net

Programación => ASM => Mensaje iniciado por: harry_the_blogger en 16 Agosto 2014, 22:27 pm



Título: Ayuda con metodo para encontrar la direccion del Kernel y llamar APIs en memoria
Publicado por: harry_the_blogger en 16 Agosto 2014, 22:27 pm
Hola, ¿Como están? Mi problema es el siguiente: Estoy desarrollando un virus en ASM y necesito llamar las APIs de Windows sin usar import tables en mi ejecutable, para mejorar la portabilidad del virus. Conseguí un código que busca la direccion del kernel32 y llama a unas APIs en esta pagina, pero no entiendo partes del codigo:  http://www.rohitab.com/discuss/topic/38717-quick-tutorial-finding-kernel32-base-and-walking-its-export-table/ (http://www.rohitab.com/discuss/topic/38717-quick-tutorial-finding-kernel32-base-and-walking-its-export-table/)

Me he encargado de comentar las lineas que entiendo en ingles, pero aún así hay partes que no comprendo. Si pudieran ayudarme indicandome que hacen las partes sin comentar estaría muy agradecido. Tengo algunos conocimientos sobre las estructuras internas de Windows, así que no piensen que estoy haciendo preguntas de novatos o fuera de lugar, espero no haberme equivocado en postear esta pregunta. Gracias por sus respuestas de antemano.

Aquí está el codigo fuente:

Código
  1. ;This is a fragment of code that could be used to make malware that use API dynamicly without
  2. ;tells to the assembler which API we want to use. This is very useful to generate malware that
  3. ;no uses import table in the executable, provided a better way to perform actions without
  4. ;modify the host import table, like a virus, trojan. The original code was written by
  5. ;SIGSEV, but I added comments to the code to clarify its actions. I can't understand all, I need help
  6.  
  7. format pe console 4.0
  8.  
  9. pushad
  10. call CodeStart
  11. CodeStart:
  12. pop ebp
  13. sub ebp,CodeStart ; delta offset shit
  14.  
  15. finding_kernel32:
  16. mov ebx, [FS : 0x30]        ;FS:0x30 points to the address of Process Environment Block (PEB)
  17.                                ;Now the Base Pointer (BX) points to the address of PEB in memory
  18.  
  19. mov ebx, [ebx + 0x0C]       ;Now we move 0x0C (12 bytes) from the address of PEB
  20.                                ;We get the value of ebx+0x0c, in other words, the address that has the PEB->Ldr pointer
  21.  
  22.                                ;Now we are in Ldr structure. We move the ebx pointer following the address in the
  23.                                ;PEB->Ldr pointer.
  24.  
  25. mov ebx, [ebx + 0x14]       ; get PEB->Ldr.InMemoryOrderModuleList.Flink (1st entry)
  26. mov ebx, [ebx]            ;2nd Entry
  27. mov ebx, [ebx]            ;3rd Entry
  28. mov ebx, [ebx + 0x10]   ; Get Kernel32 Base
  29. mov [ebp+dwKernelBase] , ebx
  30. add ebx, [ebx+0x3C] ; Start of PE header
  31. mov ebx, [ebx+0x78] ; RVA of export dir
  32. add ebx, [ebp+dwKernelBase]  ; VA of export dir
  33. mov [ebp+dwExportDirectory] , ebx
  34.  
  35. lea edx,[ebp+api_GetProcAddress]    ;Load in ebp the address of the API function name.
  36. mov ecx,[ebp+len_GetProcAddress]    ;Load in ecx the size of the API function name
  37. call GetFunctionAddress             ;Call GetFunctionAddress
  38.  
  39. mov [ebp+AGetProcAddressA] , eax    ;The API function address in memory was stored by the
  40.                                        ;GetFunctionAddress function in eax, now we save the address
  41.                                        ;of the GetProcAddress in a variable for later use.
  42.  
  43. lea edx,[ebp+api_LoadLibrary]       ;Load in edx the API function name of LoadLibrary
  44. push edx                            ;Save edx in the stack
  45. push dword [ebp+dwKernelBase]
  46. call eax
  47. mov [ebp+ALoadLibraryA] , eax
  48. lea edx , [ebp+szUser32]
  49. push edx
  50. call eax
  51. lea edx , [ebp+api_MessageBoxA]
  52. push edx
  53. push eax
  54. mov ebx,[ebp+AGetProcAddressA]
  55. call ebx
  56. mov [ebp+AMessageBoxAA] , eax
  57.  
  58. push 0
  59. lea edx,[ebp+szTitle]
  60. push edx
  61. lea edx,[ebp+szMsg]
  62. push edx
  63. push 0
  64. call eax
  65. popad
  66.  
  67. push 0xBBBBBBBB   ;OEP
  68. retn
  69.  
  70. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  71. ;  <<<<< GetFunctionAddress >>>>>> ;
  72. ; Extracts Function Address From Export Directory and returns it in eax   ;
  73. ; Parameters :  Function name in edx , Length in ecx  ;
  74. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  75.  
  76. GetFunctionAddress:
  77. push ebx    ;Save status of registers before execute the subroutine
  78. push esi    ;to preserve its content. It could make some errors to the
  79. push edi    ;caller function if modify the values that are common of the whole
  80.                ;program
  81.  
  82. mov esi, [ebp+dwExportDirectory]
  83. mov esi, [esi+0x20] ;RVA of ENT
  84. add esi, [ebp+dwKernelBase]  ;VA of ENT
  85. xor ebx,ebx
  86. cld
  87.  
  88. looper:
  89.  inc ebx
  90.  lodsd     ;Load a byte from the string pointed by SI into al register
  91.  add eax , [ebp+dwKernelBase]   ;eax now points to the string of a function
  92.  push esi ;preserve it for the outer loop
  93.  mov esi,eax
  94.  mov edi,edx
  95.  cld
  96.  push ecx
  97.  repe cmpsb
  98.  pop ecx
  99.  pop esi
  100.  jne looper
  101.  
  102.  dec ebx
  103.  mov eax,[ebp+dwExportDirectory]
  104.  mov eax,[eax+0x24]   ;RVA of EOT
  105.  add eax,[ebp+dwKernelBase] ;VA of EOT
  106.  movzx eax , word [ebx*2+eax] ;eax now holds the ordinal of our function
  107.  mov ebx,[ebp+dwExportDirectory]
  108.  mov ebx,[ebx+0x1C]   ;RVA of EAT
  109.  add ebx,[ebp+dwKernelBase] ;VA of EAT
  110.  mov ebx,[eax*4+ebx]
  111.  add ebx,[ebp+dwKernelBase]
  112.  mov eax,ebx
  113.  
  114. pop edi     ;Restore the registers to avoid generate a problem
  115. pop esi     ;in the caller function.
  116. pop ebx
  117. ret
  118.  
  119. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  120. ; Data Shit ;
  121. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  122.  
  123. szTitle:  db  "Yo !",0
  124. szMsg: db "GreeTz From SIGSEGV",0
  125. szUser32 db  "User32.dll",0
  126. AGetProcAddressA:   dd  0
  127. api_GetProcAddress:   db  "GetProcAddress"
  128. len_GetProcAddress:   dd  $-api_GetProcAddress
  129. ALoadLibraryA: dd  0
  130. api_LoadLibrary:   db  "LoadLibraryA",0
  131. AMessageBoxAA: dd  0
  132. api_MessageBoxA:   db  "MessageBoxA",0
  133. dwKernelBase: dd  0
  134. dwExportDirectory:   dd  0
  135.  


Título: Re: Ayuda con metodo para encontrar la direccion del Kernel y llamar APIs en memoria
Publicado por: daryo en 16 Agosto 2014, 22:33 pm
http://foro.elhacker.net/bugs_y_exploits/creando_shellcodes_exportables_multiplataforma_universales-t130073.0.html


Título: Re: Ayuda con metodo para encontrar la direccion del Kernel y llamar APIs en memoria
Publicado por: harry_the_blogger en 16 Agosto 2014, 22:45 pm
Gracias daryo.


Título: Re: Ayuda con metodo para encontrar la direccion del Kernel y llamar APIs en memoria
Publicado por: Eternal Idol en 16 Agosto 2014, 23:20 pm
Kernel32.dll no es el Kernel, es una libreria de modo Usuario, el Kernel es NTOSKRNL.exe.


Título: Re: Ayuda con metodo para encontrar la direccion del Kernel y llamar APIs en memoria
Publicado por: harry_the_blogger en 17 Agosto 2014, 01:16 am
Ok, gracias por aclararme que ese no es el kernel, Eternal Idol. Yo solo me refería al kernel32 en donde estan las APIs. El link que me dio daryo es bueno, Gracias a todos por responder


Título: Re: Ayuda con metodo para encontrar la direccion del Kernel y llamar APIs en memoria
Publicado por: Eternal Idol en 17 Agosto 2014, 10:51 am
Ok, gracias por aclararme que ese no es el kernel, Eternal Idol. Yo solo me refería al kernel32 en donde estan las APIs. El link que me dio daryo es bueno, Gracias a todos por responder

De nada; notese que si no importas ninguna funcion tu programa no va a funcionar en versiones modernas de Windows.

PD. Las APIs de Windows tambien estan en otras librerias como User32, Advapi32, .etc.