Ver Mensaje Individual
  #8  
Antiguo 19-06-2014
Avatar de aguml
aguml aguml is offline
Miembro
 
Registrado: may 2013
Posts: 885
Reputación: 14
aguml Va por buen camino
amigo por el momento tengo otros problemas, te cuento.
He creado la dll la cual la inyecto y, si lo hago sobre el mismo programa funciona perfecto y parchea las apis que le indique con valores fijos para cada una (de momento solo parcheo y cuando haga correr bien la dll en cualquier funcion me lio con el tema de hooks).
El problema está en si lo intento hacer sobre otra aplicacion ajena a la que hace la inyeccion, la aplicacion inyectora pasa todos los condicionales correctamente hasta crear el hilo pero parece ser que el hilo no se crea porque no me muestra mensajes que tengo puestos ni nada, es como si no existiese la dll en el proceso.

El codigo lo he dejado asi:
El inyector:
Código:
void InjectDll( HANDLE hProcess, AnsiString DLL )
{
        ULONG bytesWritten, TID;
        HANDLE pThreadStartRoutine,hThread,Parameters;

        //Creamos espacio en el proceso
        Parameters = VirtualAllocEx(hProcess,NULL,DLL.Length()+1,MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
        if(Parameters != NULL)
        {
                WriteProcessMemory(hProcess,Parameters,DLL.c_str(),DLL.Length()+1, &bytesWritten);

                if(bytesWritten == (ULONG)DLL.Length()+1)
                {
                        pThreadStartRoutine = GetProcAddress(GetModuleHandle("kernel32.dll"),"LoadLibraryA");

                        if(pThreadStartRoutine != NULL)
                        {
                                hThread = CreateRemoteThread(hProcess,NULL,0,(LPTHREAD_START_ROUTINE)pThreadStartRoutine,Parameters,0,&TID);

                                if(hThread == NULL)
                                {
                                        MessageBox(GetCurrentProcess(),"No se pudo crear el hilo.","Error",MB_OK);
                                }else{
                                        WaitForSingleObject(hThread,INFINITE);
                                }
                        }else{
                                MessageBox(GetCurrentProcess(),"No se pudo obtener la direccion base de LoadLibraryA.","Error",MB_OK);
                        }
                }else{
                        MessageBox(GetCurrentProcess(),"No se pudo parchear la API.","Error",MB_OK);
                }
        }else{
                MessageBox(GetCurrentProcess(),"No se pudo obtener memoria para inyectar la dll.","Error",MB_OK);
        }
}
y la dll quedó asi:
Código:
#include <windows.h>
#include <UnitFunctions.h>
#pragma argsused

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
        BYTE injertoZwSetInformationThread[] = {0x33,0xC0,0xC2,0x10,0x00};
        BYTE injertoZwQueryInformationProcess[] = {0xC7,0x01,0x00,0x00,0x00,0x00,0x33,0xC0,0xC2,0x14,0x00};
        ULONG len;

        switch(reason)
        {
                case DLL_PROCESS_ATTACH:
                        if(lpReserved)
                                MessageBox(0,"Process has attached to DLL by static loading.","UnitEntryPoint.cpp",MB_OK);
                        else
                                //Parcheo ZwSetInformationThread
                                len = sizeof(injertoZwSetInformationThread);
                                PatchAPI(injertoZwSetInformationThread,len,"ntdll.dll","NtSetInformationThread");

                                //Parcheo ZwQueryInformationProcess
                                len = sizeof(injertoZwQueryInformationProcess);
                                PatchAPI(injertoZwQueryInformationProcess,len,"ntdll.dll","NtQueryInformationProcess");
                                MessageBox(0,"Process has attached to DLL by dinamic loading.","UnitEntryPoint.cpp",MB_OK);
                        break;

                case DLL_THREAD_ATTACH:
                        MessageBox(0,"Thread has attached to DLL.","UnitEntryPoint.cpp",MB_OK);
                        break;

                case DLL_THREAD_DETACH:
                        MessageBox(0,"Thread has detached from DLL.","UnitEntryPoint.cpp",MB_OK);
                        break;

                case DLL_PROCESS_DETACH:
                        MessageBox(0,"Process has detached from DLL.","UnitEntryPoint.cpp",MB_OK);
                        break;
        }
        
        return 1;
}
y aqui las funciones de la dll:
Código:
#pragma hdrstop

#include "UnitFunctions.h"
//---------------------------------------------------------------------------

void PatchAPI(BYTE *injerto,ULONG len,char *nameDLL,char *nameAPI)
{
        ULONG Dir;
        ULONG nWrite;
        ULONG oldProtect;

        Dir = GetAddressAPI(nameDLL,nameAPI);

        if(Dir == NULL)
        {
                MessageBox(NULL,"Error al obtener la direccion de NtSetInformationThread.","ERROR",MB_OK);
        }else{
                if(VirtualProtect((LPVOID)Dir,len,PAGE_WRITECOPY,&oldProtect) == 0)
                {
                        char mensaje[] = "VirtualProtect ha fallado.\nNo se pudo injertar en ";
                        strcat(mensaje,nameAPI);
                        MessageBox(NULL,mensaje,"ERROR",MB_OK);
                }else{
                        WriteProcessMemory(GetCurrentProcess(),(LPVOID)Dir,injerto,len,&nWrite);

                        if(VirtualProtect((LPVOID)Dir,len,oldProtect,&oldProtect) == 0)
                        {
                                char mensaje[] = "VirtualProtect ha fallado.\nNo se pudo restaurar los mermisos originales en ";
                                strcat(mensaje,nameAPI);
                                MessageBox(NULL,mensaje,"ERROR",MB_OK);
                        }
                }
        }
}
//---------------------------------------------------------------------------

ULONG GetAddressAPI(char* nameDLL,char* nameAPI)
{
        HMODULE hMod;

        hMod = LoadLibrary(nameDLL);
        return (ULONG)GetProcAddress(hMod,nameAPI);
}
//---------------------------------------------------------------------------

#pragma package(smart_init)
y el archivo de cabecera de las funciones:
Código:
#include <Windows.h>
#include <string.h>
#ifndef UnitFunctionsH
#define UnitFunctionsH
//---------------------------------------------------------------------------

/*
#ifdef __cplusplus
extern "C"
{
#endif
//Aqui se definen las funciones externas como en el siguiente ejemplo
__declspec(dllexport) const int GetAnswerOfLife();

#ifdef __cplusplus
}
#endif
*/

ULONG GetAddressAPI(char* nameDLL,char* nameAPI);
void PatchAPI(BYTE *injerto,ULONG len,char *nameDLL,char *nameAPI);

//---------------------------------------------------------------------------
#endif
Si el inyector no me da fallos ¿que estoy haciendo mal?

a la victima la creo con:
Código:
CreateProcess(PathFile.c_str(),NULL, NULL, NULL, FALSE, DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &si, &pi);
Ya que estaré depurandola y en el entrypoint del proceso hago esto:
Código:
AnsiString DLL = "DllHook.dll";
InjectDll(pi.hProcess, DLL);
Se me queda en la linea:
Código:
WaitForSingleObject(hThread,INFINITE);
Se queda esperando y nunca retorna. Cuando la victima es la misma funcion que crea la inyeccion si funciona correctamente.
No entiendo donde puede estar el error.
Responder Con Cita