Página 1 de 1

[FASM] API Hooking

Publicado: 04 May 2011, 21:30
por mDrinky
Bueno para aprender Api hooking he creado este código con la ayuda de [Zero]...
Para los que no sepan que es el api Hooking lean esto:

[Enlace externo eliminado para invitados] ... 962.0.html

Sin mas que decir ahí os dejo el código:

Código: Seleccionar todo

;##########################################################
;##  Ejemplo API Hooking by Drinky94                      ##
;##  Agradecimientos a:                                  ##
;##      [Zero] por todo en lo que me ayuda              ##
;##       YST por su funcion ASCIITOUNICODE              ##
;##########################################################

format PE GUI 4.0 DLL
entry DllEntryPoint
 
include 'win32ax.inc'
 
section '.code' code readable executable
 
proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
        locals
            proteccion dd ?
        endl

        stdcall ASCIITOUNICODE,mensajito,buffer;pasamos la cadena que se mostrara en MessageBoxW a Unicode

        invoke LoadLibrary,'user32.dll' ;Cargamos User32
        invoke GetProcAddress,eax,"MessageBoxA" ;obtenemos la direccion de la api
        mov ebx,eax; ebx  = direccion MessageBoxA

        mov eax,hook  ;Calculamos la distancia entre el jmp y la funcion donde saltaremos
        sub eax,ebx
        sub eax,4

        mov ecx,eax

        push ebx
        push ecx

        invoke VirtualProtect,ebx,5,PAGE_EXECUTE_READWRITE,addr proteccion     ;le damos a 5 bytes permiso de escritura

        pop ecx
        pop ebx

        mov byte[ebx],0xE9  ;escribimos un jmp
        inc ebx
        mov dword[ebx],ecx ;escriimos la longitud del salto
        add ebx,4
        ret
endp
 
proc hook,uno,dos,tres,cuatro  ;funcion que remplaza a MesasgeBoxA
    invoke MessageBox,0,buffer,0,0 ;Si se llama a MessageBoxA, mostramos nuestro mensagito :PP
    mov eax,0  ;devolvemos cero
    jmp ebx  ;saltamos donde nos quedamos para continuar la ejecucion.
endp

proc ASCIITOUNICODE,Cadena,Buffer
;Funcion By YST

push ecx ebx
mov  eax,[Cadena]
mov ebx,[Buffer]
dec eax
sub ebx,2
.bucle:
inc eax
cmp byte[eax],0
je .salir
add ebx,2
mov cl,byte[eax]
mov byte[ebx],cl
mov byte[ebx+1],0
jmp .bucle
.salir:
pop ebx ecx
ret
endp

proc ShowErrorMessage hWnd,dwError
  local lpBuffer:DWORD
        lea     eax,[lpBuffer]
        invoke  FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM,0,[dwError],LANG_NEUTRAL,eax,0,0
        invoke  MessageBox,[hWnd],[lpBuffer],NULL,MB_ICONERROR+MB_OK
        invoke  LocalFree,[lpBuffer]
        ret
endp
 
; VOID ShowLastError(HWND hWnd);
 
proc ShowLastError hWnd
        invoke  GetLastError
        stdcall ShowErrorMessage,[hWnd],eax
        ret
endp
section '.data' data readable writeable
        mensajito db 'Msgbox Hookeado',0
        buffer db ?
 
 
section '.idata' import data readable writeable
 
  library kernel,'KERNEL32.DLL',\
          user,'USER32.DLL'
 
  import kernel,\
         GetLastError,'GetLastError',\
         SetLastError,'SetLastError',\
         FormatMessage,'FormatMessageA',\
         LocalFree,'LocalFree',\
         LoadLibrary,'LoadLibraryA',\
         GetProcAddress,'GetProcAddress',\
         VirtualProtect,'VirtualProtect'
 
  import user,\
         MessageBox,'MessageBoxW'
 
section '.edata' export data readable
 
  export 'ERRORMSG.DLL',\
         ShowErrorMessage,'ShowErrorMessage',\
         ShowLastError,'ShowLastError'
 
section '.reloc' fixups data discardable  



                                    
salu2! y espero que os ayudeeee