[C] Libreria Hook IAT

Iniciado por mr.blood, Mayo 14, 2013, 02:37:59 AM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Mayo 14, 2013, 02:37:59 AM Ultima modificación: Diciembre 08, 2014, 02:51:47 PM por Expermicid
Por si a alguien le ayuda. Es mejorable, no tiene ningun control de errores era solo para mostrar la idea ;).

No pierdo mas tiempo, el code.

hookiat.c
Código: c
#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: c
#include <string.h>
#include <windows.h>

void *ORIGINAL_FUNCTION;

void HookIAT(char *tohooklibrary, char *tohookfunc, void *newfunc);


Espero que a alguien le sirva. No hagais muchas maldades con el :P.

Sa1uDoS

Orale se ve que esta bueno el code, pero no le entiendo que es lo que hace, can you explain me please...

Saludos..
No tienes permitido ver los links. Registrarse o Entrar a mi cuenta
In my mind where before there was order, today there is only chaØs!

Claro, parto de que sabes lo que es Hookear una API.

Los .exe utilizan el formato PE. Este formato tiene una Tabla de importaciones (Import Table) y una Tabla de direcciones de importaciones (Import Address Table). En la Import Table estan los nombres de las API que llama tu codigo y la libreria a la que pertenecen, y cuando tu ejecutas un .exe el WinLoader rellena la Import Address Table con las direcciones de esas API (esto es lo que permite que en distintas maquinas funcione el mismo .exe). Este codigo cambia una direccion por la direccion de una funcion tuya (API Hooking).

Es dificil de explicar, dime si algo no te quedo claro y te pongo un codigo para que lo veas mas claro (aunque para entenderlo deberias leer sobre el formato PE).

Sa1uDoS

No entiendo, probaste si funciona?

Concretamente no entiendo la forma en la que localizas la API en la IAT:

Código: c
if(((*IMPORTED_FUNCTIONS)!=0) && (!strcmp((char *)IMPORTED_FUNCTION_NAME, tohookfunc)))


Comparas el nombre. Hace mucho que no veo nada de la IAT, pero hace tiempo hice un código que hacía esto en ASM y, a riesgo de colarme, diría que los nombres se reemplazan en runtime por las direcciones, por lo que la 'única' forma de localizar la API es por su dirección y no por su nombre.

Saludos
   
No tienes permitido ver los links. Registrarse o Entrar a mi cuenta

Una cosa es la IT y otra la IAT, una tiene los nombres y la otra las direcciones de estas :) Obviamente la he probado, si no no la hubiera subido.

Sa1uDoS