#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main(int argc,char* argv[])
{
    STARTUPINFO structInfo = {0};
    PROCESS_INFORMATION structInfoProc = {0};
    DEBUG_EVENT debugEvent = {0};
    CONTEXT context = {0};
    int retour;

    retour = CreateProcess(argv[1],NULL,NULL,NULL,FALSE,DEBUG_PROCESS|DEBUG_ONLY_THIS_PROCESS,NULL,NULL,&structInfo,&structInfoProc);
    if(retour == 0)return 0;
    printf("Rewrite EIP par 0vercl0k.\n\n");
    printf("[+]Creation du processus reussis.\n");
    while(1)
    {
        WaitForDebugEvent(&debugEvent,INFINITE);
        if(debugEvent.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
        {
            printf("\t-Création d'un processus.\n");
            context.ContextFlags = CONTEXT_CONTROL;
            GetThreadContext(structInfoProc.hThread,&context);
            context.Eip = 0x0401290;
            SetThreadContext(structInfoProc.hThread,&context);
        }
        else if(debugEvent.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
        {
            printf("\t- Fin du process.\n");
            break;
        }
        else if(debugEvent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
        {
            if(debugEvent.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
            {

                ContinueDebugEvent(debugEvent.dwProcessId,debugEvent.dwThreadId,DBG_CONTINUE);
                continue;
            }
        }
        else;
        ContinueDebugEvent(debugEvent.dwProcessId,debugEvent.dwThreadId,DBG_EXCEPTION_NOT_HANDLED);

    }


    CloseHandle(&structInfoProc.dwProcessId);
    CloseHandle(&structInfoProc.dwThreadId);
    return 0;
}