elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Estamos en la red social de Mastodon


+  Foro de elhacker.net
|-+  Seguridad Informática
| |-+  Análisis y Diseño de Malware (Moderador: fary)
| | |-+  Ayuda api hoking :P
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Ayuda api hoking :P  (Leído 4,450 veces)
fary
Moderador
***
Desconectado Desconectado

Mensajes: 1.062



Ver Perfil WWW
Ayuda api hoking :P
« en: 3 Mayo 2011, 20:41 pm »

Bueno estoi intentando hookear una API desde una DLL creada en Fasm pero no lo consigo y nose que estoi haciendo mal, este es el código de la dll:

Código
  1. format PE GUI 4.0 DLL
  2. entry DllEntryPoint
  3.  
  4. include 'win32ax.inc'
  5.  
  6. section '.code' code readable executable
  7.  
  8. proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
  9.        locals
  10.            proteccion dd ?
  11.        endl
  12.  
  13.        invoke LoadLibrary,'user32.dll'
  14.        invoke GetProcAddress,eax,"MessageBoxA"
  15.        mov ebx,eax; ebx  = direccion MessageBoxA
  16.  
  17.        invoke VirtualProtect,-1,ebx,5,PAGE_EXECUTE_READWRITE,edx
  18.  
  19.        mov ebx,0xE9
  20.        inc ebx
  21.        mov dword[ebx],hook
  22.        add ebx,4
  23.        ret
  24. endp
  25.  
  26. proc hook,uno,dos,tres,cuatro
  27.    invoke MessageBox,0,0,0,0
  28.    mov eax,0
  29.    ret
  30. endp
  31. ; VOID ShowErrorMessage(HWND hWnd,DWORD dwError);
  32.  
  33. proc ShowErrorMessage hWnd,dwError
  34.  local lpBuffer:DWORD
  35.        lea     eax,[lpBuffer]
  36.        invoke  FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM,0,[dwError],LANG_NEUTRAL,eax,0,0
  37.        invoke  MessageBox,[hWnd],[lpBuffer],NULL,MB_ICONERROR+MB_OK
  38.        invoke  LocalFree,[lpBuffer]
  39.        ret
  40. endp
  41.  
  42. ; VOID ShowLastError(HWND hWnd);
  43.  
  44. proc ShowLastError hWnd
  45.        invoke  GetLastError
  46.        stdcall ShowErrorMessage,[hWnd],eax
  47.        ret
  48. endp
  49. section '.data' data readable writeable
  50.        mensajito db ?
  51.  
  52.  
  53. section '.idata' import data readable writeable
  54.  
  55.  library kernel,'KERNEL32.DLL',\
  56.          user,'USER32.DLL'
  57.  
  58.  import kernel,\
  59.         GetLastError,'GetLastError',\
  60.         SetLastError,'SetLastError',\
  61.         FormatMessage,'FormatMessageA',\
  62.         LocalFree,'LocalFree',\
  63.         LoadLibrary,'LoadLibraryA',\
  64.         GetProcAddress,'GetProcAddress',\
  65.         VirtualProtect,'VirtualProtectEx'
  66.  
  67.  import user,\
  68.         MessageBox,'MessageBoxA'
  69.  
  70. section '.edata' export data readable
  71.  
  72.  export 'ERRORMSG.DLL',\
  73.         ShowErrorMessage,'ShowErrorMessage',\
  74.         ShowLastError,'ShowLastError'
  75.  
  76. section '.reloc' fixups data discardable  


Esperando repuesta...

salu2!
En línea

Un byte a la izquierda.
[Zero]
Wiki

Desconectado Desconectado

Mensajes: 1.082


CALL DWORD PTR DS:[0]


Ver Perfil WWW
Re: Ayuda api hoking :P
« Respuesta #1 en: 4 Mayo 2011, 16:24 pm »

Mira la declaración de VirtualProtectEx:

Código:
http://msdn.microsoft.com/en-us/library/aa366899(v=vs.85).aspx

Citar
hProcess [in]
A handle to the process whose memory protection is to be changed. The handle must have the PROCESS_VM_OPERATION access right. For more information, see Process Security and Access Rights.

No dice que pueda ser NULL. Y además:

Citar
lpflOldProtect [out]
A pointer to a variable that receives the previous access protection value of the first page in the specified region of pages. If this parameter is NULL or does not point to a valid variable, the function fails.

Saludos

Edito: Además, en el jmp con el que pisas el comienzo de la api, no tienes que poner la dirección hacia la que quieres saltar, sinó la distancia de salto.

Edioto 2:

Además, con esta línea:
Código:
mov ebx,0xE9

Te estás cargando el puntero, tú quieres hacer mov byte[ebx],0xE9
« Última modificación: 4 Mayo 2011, 16:46 pm por [Zero] » En línea


“El Hombre, en su orgullo, creó a Dios a su imagen y semejanza.”
Nietzsche
fary
Moderador
***
Desconectado Desconectado

Mensajes: 1.062



Ver Perfil WWW
Re: Ayuda api hoking :P
« Respuesta #2 en: 4 Mayo 2011, 19:34 pm »

Este es el código que he reparado con los errores que me diiste Zero pero sigue sin funcionar:

Código
  1. format PE GUI 4.0 DLL
  2. entry DllEntryPoint
  3.  
  4. include 'win32ax.inc'
  5.  
  6. section '.code' code readable executable
  7.  
  8. proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
  9.        locals
  10.            proteccion dd ?
  11.        endl
  12.  
  13.        invoke LoadLibrary,'user32.dll'
  14.        invoke GetProcAddress,eax,"MessageBoxA"
  15.        mov ebx,eax; ebx  = direccion MessageBoxA
  16.  
  17.        invoke VirtualProtect,ebx,5,PAGE_EXECUTE_READWRITE,addr proteccion
  18.  
  19.        mov byte[ebx],0xE9
  20.        inc ebx
  21.        call distancia
  22.        distancia:
  23.        pop ecx
  24.        add ecx,4
  25.        sub ecx,hook
  26.        mov dword[ebx],ecx
  27.        add ebx,4
  28.        ret
  29. endp
  30.  
  31. proc hook,uno,dos,tres,cuatro
  32.    invoke MessageBox,0,'Juan te hookeo',0,0
  33.    mov eax,0
  34.    ret
  35. endp
  36. ; VOID ShowErrorMessage(HWND hWnd,DWORD dwError);
  37.  
  38. proc ShowErrorMessage hWnd,dwError
  39.  local lpBuffer:DWORD
  40.        lea     eax,[lpBuffer]
  41.        invoke  FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM,0,[dwError],LANG_NEUTRAL,eax,0,0
  42.        invoke  MessageBox,[hWnd],[lpBuffer],NULL,MB_ICONERROR+MB_OK
  43.        invoke  LocalFree,[lpBuffer]
  44.        ret
  45. endp
  46.  
  47. ; VOID ShowLastError(HWND hWnd);
  48.  
  49. proc ShowLastError hWnd
  50.        invoke  GetLastError
  51.        stdcall ShowErrorMessage,[hWnd],eax
  52.        ret
  53. endp
  54. section '.data' data readable writeable
  55.        mensajito db ?
  56.  
  57.  
  58. section '.idata' import data readable writeable
  59.  
  60.  library kernel,'KERNEL32.DLL',\
  61.          user,'USER32.DLL'
  62.  
  63.  import kernel,\
  64.         GetLastError,'GetLastError',\
  65.         SetLastError,'SetLastError',\
  66.         FormatMessage,'FormatMessageA',\
  67.         LocalFree,'LocalFree',\
  68.         LoadLibrary,'LoadLibraryA',\
  69.         GetProcAddress,'GetProcAddress',\
  70.         VirtualProtect,'VirtualProtect'
  71.  
  72.  import user,\
  73.         MessageBox,'MessageBoxA'
  74.  
  75. section '.edata' export data readable
  76.  
  77.  export 'ERRORMSG.DLL',\
  78.         ShowErrorMessage,'ShowErrorMessage',\
  79.         ShowLastError,'ShowLastError'
  80.  
  81. section '.reloc' fixups data discardable          

salu2! y gracias por la ayudaaa
En línea

Un byte a la izquierda.
[Zero]
Wiki

Desconectado Desconectado

Mensajes: 1.082


CALL DWORD PTR DS:[0]


Ver Perfil WWW
Re: Ayuda api hoking :P
« Respuesta #3 en: 4 Mayo 2011, 19:48 pm »

Estás calculando mal la distancia, es la distancia desde la api hasta la dirección donde está hook una vez carga la DLL.

Saludos
En línea


“El Hombre, en su orgullo, creó a Dios a su imagen y semejanza.”
Nietzsche
fary
Moderador
***
Desconectado Desconectado

Mensajes: 1.062



Ver Perfil WWW
Re: Ayuda api hoking :P
« Respuesta #4 en: 4 Mayo 2011, 20:55 pm »

Muchas gracias una vez más  :-[  :-* jajaja


Este es el código funcional, lo comente para que mas gente lo comprenda tambien  :)

Código
  1. ;##########################################################
  2. ;##  Ejemplo API Hoking by Drinky94                      ##
  3. ;##  Agradecimientos a:                                  ##
  4. ;##      [Zero] por todo en lo que me ayuda              ##
  5. ;##       YST por su funcion ASCIITOUNICODE              ##
  6. ;##########################################################
  7.  
  8. format PE GUI 4.0 DLL
  9. entry DllEntryPoint
  10.  
  11. include 'win32ax.inc'
  12.  
  13. section '.code' code readable executable
  14.  
  15. proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
  16.        locals
  17.            proteccion dd ?
  18.        endl
  19.  
  20.        stdcall ASCIITOUNICODE,mensajito,buffer;pasamos la cadena que se mostrara en MessageBoxW a Unicode
  21.  
  22.        invoke LoadLibrary,'user32.dll' ;Cargamos User32
  23.        invoke GetProcAddress,eax,"MessageBoxA" ;obtenemos la direccion de la api
  24.        mov ebx,eax; ebx  = direccion MessageBoxA
  25.  
  26.        mov eax,hook  ;Calculamos la distancia entre el jmp y la funcion donde saltaremos
  27.        sub eax,ebx
  28.        sub eax,4
  29.  
  30.        mov ecx,eax
  31.  
  32.        push ebx
  33.        push ecx
  34.  
  35.        invoke VirtualProtect,ebx,5,PAGE_EXECUTE_READWRITE,addr proteccion     ;le damos a 5 bytes permiso de escritura
  36.  
  37.        pop ecx
  38.        pop ebx
  39.  
  40.        mov byte[ebx],0xE9  ;escribimos un jmp
  41.        inc ebx
  42.        mov dword[ebx],ecx ;escriimos la longitud del salto
  43.        add ebx,4
  44.        ret
  45. endp
  46.  
  47. proc hook,uno,dos,tres,cuatro  ;funcion que remplaza a MesasgeBoxA
  48.    invoke MessageBox,0,buffer,0,0 ;Si se llama a MessageBoxA, mostramos nuestro mensagito :PP
  49.    mov eax,0  ;devolvemos cero
  50.    jmp ebx  ;saltamos donde nos quedamos para continuar la ejecucion.
  51. endp
  52.  
  53. proc ASCIITOUNICODE,Cadena,Buffer
  54. ;Funcion By YST
  55.  
  56. push ecx ebx
  57. mov  eax,[Cadena]
  58. mov ebx,[Buffer]
  59. dec eax
  60. sub ebx,2
  61. .bucle:
  62. inc eax
  63. cmp byte[eax],0
  64. je .salir
  65. add ebx,2
  66. mov cl,byte[eax]
  67. mov byte[ebx],cl
  68. mov byte[ebx+1],0
  69. jmp .bucle
  70. .salir:
  71. pop ebx ecx
  72. ret
  73. endp
  74.  
  75. proc ShowErrorMessage hWnd,dwError
  76.  local lpBuffer:DWORD
  77.        lea     eax,[lpBuffer]
  78.        invoke  FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM,0,[dwError],LANG_NEUTRAL,eax,0,0
  79.        invoke  MessageBox,[hWnd],[lpBuffer],NULL,MB_ICONERROR+MB_OK
  80.        invoke  LocalFree,[lpBuffer]
  81.        ret
  82. endp
  83.  
  84. ; VOID ShowLastError(HWND hWnd);
  85.  
  86. proc ShowLastError hWnd
  87.        invoke  GetLastError
  88.        stdcall ShowErrorMessage,[hWnd],eax
  89.        ret
  90. endp
  91. section '.data' data readable writeable
  92.        mensajito db 'Msgbox Hookeado',0
  93.        buffer db ?
  94.  
  95.  
  96. section '.idata' import data readable writeable
  97.  
  98.  library kernel,'KERNEL32.DLL',\
  99.          user,'USER32.DLL'
  100.  
  101.  import kernel,\
  102.         GetLastError,'GetLastError',\
  103.         SetLastError,'SetLastError',\
  104.         FormatMessage,'FormatMessageA',\
  105.         LocalFree,'LocalFree',\
  106.         LoadLibrary,'LoadLibraryA',\
  107.         GetProcAddress,'GetProcAddress',\
  108.         VirtualProtect,'VirtualProtect'
  109.  
  110.  import user,\
  111.         MessageBox,'MessageBoxW'
  112.  
  113. section '.edata' export data readable
  114.  
  115.  export 'ERRORMSG.DLL',\
  116.         ShowErrorMessage,'ShowErrorMessage',\
  117.         ShowLastError,'ShowLastError'
  118.  
  119. section '.reloc' fixups data discardable  
  120.  
  121.  
  122.  
  123.  
  124.  

salu2!

« Última modificación: 4 Mayo 2011, 21:25 pm por Drinky94 » En línea

Un byte a la izquierda.
[Zero]
Wiki

Desconectado Desconectado

Mensajes: 1.082


CALL DWORD PTR DS:[0]


Ver Perfil WWW
Re: Ayuda api hoking :P
« Respuesta #5 en: 5 Mayo 2011, 00:35 am »

Tienes un error, ese jmp ebx que haces en la función que recibe el hook no va a ir a donde tu quieres, ahí ebx puede valer cualquier cosa, tienes que obtener la dirección de la API y saltar a dirAPI+5 si quieres volver a la API o sacar los parámetros y la dirección de retorno de la pila para volver al código que llamó a MessageBoxA.

Saludos
En línea


“El Hombre, en su orgullo, creó a Dios a su imagen y semejanza.”
Nietzsche
fary
Moderador
***
Desconectado Desconectado

Mensajes: 1.062



Ver Perfil WWW
Re: Ayuda api hoking :P
« Respuesta #6 en: 5 Mayo 2011, 15:33 pm »

A mi me funciona perfectamente Zero, sigue perfectamente la ejecucion del programa...
En línea

Un byte a la izquierda.
[Zero]
Wiki

Desconectado Desconectado

Mensajes: 1.082


CALL DWORD PTR DS:[0]


Ver Perfil WWW
Re: Ayuda api hoking :P
« Respuesta #7 en: 5 Mayo 2011, 18:36 pm »

A mi me funciona perfectamente Zero, sigue perfectamente la ejecucion del programa...

Seguro? Con el jmp ebx ahí?

Saludos
En línea


“El Hombre, en su orgullo, creó a Dios a su imagen y semejanza.”
Nietzsche
fary
Moderador
***
Desconectado Desconectado

Mensajes: 1.062



Ver Perfil WWW
Re: Ayuda api hoking :P
« Respuesta #8 en: 5 Mayo 2011, 21:18 pm »

si  :)

salu2!
En línea

Un byte a la izquierda.
Иōҳ


Desconectado Desconectado

Mensajes: 563


Ver Perfil
Re: Ayuda api hoking :P
« Respuesta #9 en: 6 Mayo 2011, 07:24 am »

parece ser que no se altera el registro ebx hasta llegar a ese procedimiento.

Nox.
En línea

Eres adicto a la Ing. Inversa? -> www.noxsoft.net
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines