[Libreria C] Hook a la IAT
mr.blood:
Por si a alguien le ayuda. Es mejorable, no tiene ningun control de errores era solo para mostrar la idea ;).
No se si esto va en esta sección o en la de Programacion en C. Creo que esta mas relacionado con esto.
hookiat.c
Código
#include "hookiat.h"
void HookIAT(char *tohooklibrary, char *tohookfunc, void *newfunc)
{
DWORD image_base=GetModuleHandleA(0);
PIMAGE_DOS_HEADER DOS;
PIMAGE_NT_HEADERS NT;
PIMAGE_IMPORT_DESCRIPTOR IT;
PIMAGE_IMPORT_BY_NAME *IMPORTED_FUNCTIONS;
PIMAGE_THUNK_DATA Funcion;
DWORD *IMPORTED_DLL_NAME;
DWORD *IMPORTED_FUNCTION_NAME;
unsigned int i=0;
DOS=(PIMAGE_DOS_HEADER)image_base;
NT=(PIMAGE_NT_HEADERS)(DOS->e_lfanew + image_base);
IT=(PIMAGE_IMPORT_DESCRIPTOR)(NT->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress + image_base);
IMPORTED_DLL_NAME=(DWORD *)(IT->Name + image_base);
while( (IT->Name) != 0 )
{
IMPORTED_DLL_NAME=(DWORD *)(IT->Name + image_base);
if(!strcmp((char *)IMPORTED_DLL_NAME, tohooklibrary))
{
break;
}
IT++;
}
IMPORTED_FUNCTIONS=(PIMAGE_IMPORT_BY_NAME *)(IT->Characteristics + image_base);
for(i=0;;i++)
{
IMPORTED_FUNCTION_NAME=(DWORD *)((*IMPORTED_FUNCTIONS)->Name + image_base);
if(((*IMPORTED_FUNCTIONS)!=0) && (!strcmp((char *)IMPORTED_FUNCTION_NAME, tohookfunc)))
{
break;
}
IMPORTED_FUNCTIONS++;
}
Funcion=(PIMAGE_THUNK_DATA)(IT->FirstThunk + image_base);
Funcion+=i;
ORIGINAL_FUNCTION=(void *)Funcion->u1.Function;
Funcion->u1.Function=(DWORD)newfunc;
}
hookiat.h
Código
#include <string.h>
#include <windows.h>
void *ORIGINAL_FUNCTION;
void HookIAT(char *tohooklibrary, char *tohookfunc, void *newfunc);
Sa1uDoS
Arkangel_0x7C5:
Buen codogo.
Como recomendación para su uso, seria recomendable hacer el hook al inicio de la aplicaccion y enganchar también GetProcAddress para que no se te escape cuando usen punteros a esas funciones
Saludos
mr.blood:
Si, es la desventaja de este tipo de Hooks, si usan GetProcAddress no los "engancha", hay que hookear GetProcAddress tambien.
Gracias por el comentario ;).
Pero a mi es el tipo de Hook que mas me gusta
Cita de: Arkangel_0x7C5 en 23 Enero 2013, 18:21 pm
Como recomendación para su uso, seria recomendable hacer el hook al inicio de la aplicaccion
¿A que te refieres con eso?
Sa1uDoS
Arkangel_0x7C5:
Cita de: mr.blood en 23 Enero 2013, 19:34 pm
¿A que te refieres con eso?
Sa1uDoS
A que pongan el gancho justo cuando la aplicaccion se ejecuta, para que no le de tiempo a llamar a GetProcAddress.
Tambien se puede usar para las importaciones de otras dll, por lo que un campo para el nombre del modulo estaría interesante
Saludos
mr.blood:
Ah bueno jaja. Pero esto es una funcion, que cada uno la use como mas conveniente crea.
Sa1uDoS
P.D.: Que gusto da este foro, la gente comenta.
Navegación
[#] Página Siguiente