[C] Explorando la IT

Iniciado por mr.blood, Mayo 14, 2013, 02:40:21 AM

Tema anterior - Siguiente tema

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

Mayo 14, 2013, 02:40:21 AM Ultima modificación: Octubre 28, 2013, 09:19:11 AM por Expermicid
Pues traigo un programa que busca X funcion en X libreria en su propia Import Table. No hay mucha explicacion. Esta bastante chapucero solo queria que pudieran ver un codigo sencillo que haga esto.

Código: c
#include <string.h>
#include <stdio.h>
#include <windows.h>

int main()
{
        const char libreria[]="KERNEL32.dll";
        const char funcion[]="ExitProcess";
        DWORD image_base=(DWORD)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, libreria))
                {
                        printf("%s\n", (char *)IMPORTED_DLL_NAME);
                        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, funcion)))
                {
                        printf("\t%s\n", (char *)IMPORTED_FUNCTION_NAME);
                        break;
                }
                IMPORTED_FUNCTIONS++;
        }
       
        Funcion=(PIMAGE_THUNK_DATA)(IT->FirstThunk + image_base);
        Funcion+=i;
        printf("\t\tDireccion funcion: %p \t Addr en IT: %p\n", *Funcion, Funcion);
        getchar();
        return 0;
}


Sa1uDoS

Código: c
const char libreria[]="USER32.dll";
            const char funcion[]="MessageBoxA";


::) ::) ::) ::)

plof  :-\

Logico... el codigo no llama esa API por lo que no esta en la Import Table. Este codigo lee SU PROPIO IMPORT TABLE.

Utilizala en cualquier punto del codigo y deberia funcionar. ExitProcess la importan todos los programas bajo Windows ;).

Sa1uDoS

Código: c
    #include <string.h>
    #include <stdio.h>
    #include <windows.h>

    int main()
    {
            const char libreria[]="USER32.dll";
            const char funcion[]="MessageBoxA";
            DWORD image_base=(DWORD)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, libreria))
                    {
                            printf("%s\n", (char *)IMPORTED_DLL_NAME);
                            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, funcion)))
                    {
                            printf("\t%s\n", (char *)IMPORTED_FUNCTION_NAME);
                            break;
                    }
                    IMPORTED_FUNCTIONS++;
            }

            Funcion=(PIMAGE_THUNK_DATA)(IT->FirstThunk + image_base);
            Funcion+=i;
            printf("\t\tDireccion funcion: %p \t Addr en IT: %p\n", *Funcion, Funcion);
            getchar();
            return 0;

            MessageBoxA(0,0,0,0);
    }


Lo he probado correctamente y no funciona.

un saludo!

Pues no se mDrinky, ahora estoy sin ningun Windows, cuando pueda lo miro, aunque las pruebas que hice yo funcionó todo bien.

Por favor, que alguien mas lo pruebe y comente si no le funciona...

Sa1uDoS

@mDrinky: Revisa la IAT del ejecutable, sospecho que el compilador ha sido más listo que tú y ha quitado todo lo que hay después del return 0; xD
I code for $$$.

(PGP ID 0xCC050E77)
ASM, C, C++, VB6... skilled [malware] developer

No tienes permitido ver los links. Registrarse o Entrar a mi cuenta
@mDrinky: Revisa la IAT del ejecutable, sospecho que el compilador ha sido más listo que tú y ha quitado todo lo que hay después del return 0; xD


:o :o :o :o :o :o :o :o :-[ :-[ :-[ :-[ :-[ :-[


Efectivamente era eso... pero no veo el porque no lo compila, tendría que compilarlo desde mi punto de vista... Esto en ASM no pasa jajajaj

WTF?! No contabas con la optimizacion de codigo del compilador? :P Ahora ya te funciona?

Ni me fije que estaba despues del return, bien visto Karcrack!

Es que si en ASM te optimizan el codigo... cagate lorito! xD

Sa1uDoS

El compilador es terriblemente inteligente y es capaz de ver que dentro del scope (ámbito) actual ese código no será jamás ejecutado. Puedes desactivar casi todas las optimizaciones del compilador y obligarle a compilar a piñón.
Estuve estudiando el tema de los compiladores y es realmente interesante. Te recomiendo la lectura de: No tienes permitido ver los links. Registrarse o Entrar a mi cuenta de Alfred Aho, Ravi Sethi, Jeffrey Ullman.

En ASM no pasa porque se trata de un ensamblador. Algún HLA (High Level Assambly) hace estas optimizaciones y muchas otras.

Saludos :D
I code for $$$.

(PGP ID 0xCC050E77)
ASM, C, C++, VB6... skilled [malware] developer

La verdad que no sabia que el compilador de C hacía eso, no os voy a engañar  :)

De lo que me gustaría aprender es más de ensamblador a 16 bits, y como obtienen los datos del PC en 16 bits y eso.... estoy buscando por ahí algun libro gratis en pdf donde expliquen todo esto.



Tiene buena pinta la lectura Karcrack, le echaré un ojo ;).

En ASM no optimiza (o no debería) porque no se compila.



Si quieres trabajar mas con hardware, mira el código de Luxur o intenta hacer algo así desde 0.
mDrinky nunca pensaste en aprender a programar PICs? Creo que te gustaría... Y si quieres llevarlo a una placa de vidrio tampoco es caro el material ;).

Sa1uDoS

Es que el mayor problema que tengo a la hora de leer información es que esta en ingles... y no tengo ni idea de ingles  :-[ Este año que biene me apuntare a la academia haber si obtengo resultados jajajajajajjaa

un saludo.