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

 

 


Tema destacado: Rompecabezas de Bitcoin, Medio millón USD en premios


+  Foro de elhacker.net
|-+  Seguridad Informática
| |-+  Análisis y Diseño de Malware (Moderador: fary)
| | |-+  API Hooking by IAT Patching
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: API Hooking by IAT Patching  (Leído 2,104 veces)
Binary_Death

Desconectado Desconectado

Mensajes: 214



Ver Perfil
API Hooking by IAT Patching
« en: 22 Abril 2012, 17:00 pm »

Gracias al tutorial de YST & Hacker_Zero entendí el API Hooking, pero por desgracia ese manual quedó a medias y jamás se explicó el método para hookear una API modificando la IAT.

Me puse y salió esto, cualquier error por favor decidmelo:

Código:
.386
.model flat, stdcall
option casemap:none

assume fs:nothing

include windows.inc
include kernel32.inc
include user32.inc
includelib kernel32.lib
includelib user32.lib


.data
Process db 'ejecutable.exe',0
Kernel db 'kernel32.dll',0
GetProc db 'GetProcAddress',0
.data?
Function dd ?
FunctionLen dd ?
pInfo PROCESS_INFORMATION <>
sInfo STARTUPINFO <>
function_start PROTO
.code
start:
invoke CreateProcess,0,addr Process,0,0,0,0,0,0,addr sInfo,addr pInfo

mov eax,offset function_end
sub eax,offset function_start
mov [FunctionLen],eax

invoke VirtualAllocEx,[pInfo.hProcess],0,[FunctionLen],MEM_COMMIT+MEM_RESERVE,PAGE_EXECUTE_READWRITE
mov [Function],eax

invoke GetModuleHandle,offset Kernel
mov [KernelBase],eax
push offset GetProcAddressName

invoke GetProcAddress,eax,offset GetProc
mov [pGetProcAddress],eax

invoke WriteProcessMemory,[pInfo.hProcess],[Function],offset function_start,[FunctionLen],0
invoke CreateRemoteThread,[pInfo.hProcess],0,0,[Function],0,0,0
invoke ExitProcess,0

;Injecting Function
function_start PROC
    call delta
delta:
pop ebp
sub ebp, offset delta
mov ebx, fs:[030h]
;Getting host process IAT address and its size
mov eax,[ebx+08h]
mov ebx,[eax+03Ch]
add ebx,eax
mov edx,[ebx+0D8h]
add edx,eax
mov [ebp+pIAT],edx
mov edx,[ebx+0DCh]
mov [ebp+IATsize],edx

;Getting the needed API's
;GetModuleHandle
mov ebx,offset GetModuleHandleName
add ebx,ebp
push ebx
push dword ptr [ebp+KernelBase]
call [ebp+pGetProcAddress]
mov [ebp+pGetModuleHandle],eax
;VirtualProtect
mov ebx,offset VirtualProtectName
add ebx,ebp
push ebx
push dword ptr [ebp+KernelBase]
call [ebp+pGetProcAddress]
mov [ebp+pVirtualProtect],eax
;Getting address of MessageBox
mov ebx,offset user32name
add ebx,ebp
push ebx
call [ebp+pGetModuleHandle]
mov ebx,offset MessageBoxName
add ebx,ebp
push ebx
push eax
call [ebp+pGetProcAddress]
mov [ebp+pMessageBox],eax
;Showing a MessageBox
push MB_OK + MB_ICONINFORMATION
push 0
mov ebx,offset MsgInjected
add ebx,ebp
push ebx
push 0
call [ebp+pMessageBox]
;Finding pointing address
xor edx,edx
mov eax,[ebp+IATsize]
mov ecx,04h
div ecx
mov ecx,eax
mov eax,[ebp+pMessageBox]
mov edi,[ebp+pIAT]
repnz scasd
sub edi,04h
;Patching the IAT
mov ebx,offset OldProtect
add ebx,ebp
push ebx
push PAGE_EXECUTE_READWRITE
push 04h
push edi
call [ebp+pVirtualProtect]
mov ebx,offset Hook
add ebx,ebp
mov [edi],ebx
ret

pIAT dd ?
IATsize dd ?
BaseImage dd ?
KernelBase dd ?
pGetProcAddress dd ?
GetProcAddressName db "GetProcAddress",0
user32name db "user32.dll",0
MessageBoxName db "MessageBoxA",0
pMessageBox dd ?
GetModuleHandleName db "GetModuleHandleA",0
pGetModuleHandle dd ?
VirtualProtectName db "VirtualProtect",0
pVirtualProtect dd ?
OldProtect dd ?
MsgInjected db "The function has been injected!",0

Hook:
;Delta offset
call deltahook
deltahook:
pop ebp
sub ebp,offset deltahook
pop ebx ;Saving to return after
mov edi,offset msghook
add edi,ebp
mov [esp+04h],edi ;Changing the stack parameters
call [ebp+pMessageBox] ;Calling the original address of MessageBox
push ebx ;Setting the returning address
ret
msghook db "MessageBox Hooked!", 0
function_start endp
function_end:
end start


El ejecutable que usé lo que hace es lanzar dos MessageBox, uno detrás de otro (podéis programarlo para ver cómo funciona).
No abre un proceso existente, sino que lo crea (arreglarlo es cuestión de un par de API's).
El proceso no lo crea suspendido, así pues al principio lanza el primer MessageBox junto con otro MessageBox que anuncia que la función ha sido inyectada. Después, al cerrar el mensaje de alerta y posteriormente el primer mensaje, lanza otro MessageBox que en teoría debería de ser el segundo del programa, pero como hookea la API hará que a partir de ahí todos los MessageBox muestren "MessageBox Hooked!"


Saludos!
« Última modificación: 22 Abril 2012, 17:12 pm por Binary_Death » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Api Hooking en VB « 1 2 »
Programación Visual Basic
Freeze. 15 7,064 Último mensaje 18 Febrero 2008, 15:56 pm
por Freeze.
Api hooking with dll injection
Programación Visual Basic
ntaryl 5 2,354 Último mensaje 10 Julio 2008, 22:01 pm
por cobein
Simple Hooking Example
Programación Visual Basic
ntaryl 0 1,047 Último mensaje 10 Julio 2008, 23:50 pm
por ntaryl
Inline Patching NotePad -Asm & OllyDBG-
Ingeniería Inversa
Иōҳ 4 5,769 Último mensaje 13 Octubre 2009, 19:24 pm
por MCKSys Argentina
API Hooking (C++) « 1 2 »
Programación C/C++
HaX991 11 8,435 Último mensaje 12 Julio 2010, 18:20 pm
por Eternal Idol
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines