#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("Control your own pr0c3ss 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");
        }
        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)
            {
                context.ContextFlags = CONTEXT_CONTROL;
                GetThreadContext(structInfoProc.hThread,&context);
                context.EFlags = context.EFlags|0x100;
                SetThreadContext(structInfoProc.hThread,&context);
                ContinueDebugEvent(debugEvent.dwProcessId,debugEvent.dwThreadId,DBG_CONTINUE);
                continue;
            }
            else if(debugEvent.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_SINGLE_STEP)
            {
                context.ContextFlags = CONTEXT_CONTROL;
                GetThreadContext(structInfoProc.hThread,&context);
                context.EFlags = context.EFlags|0x100;
                SetThreadContext(structInfoProc.hThread,&context);
                ContinueDebugEvent(debugEvent.dwProcessId,debugEvent.dwThreadId,DBG_CONTINUE);
                //printf("EIP : %ld    EAX : %ld\n",context.Eip,context.Eax);
                //getchar();
                continue;
            }
        }
        else;
        ContinueDebugEvent(debugEvent.dwProcessId,debugEvent.dwThreadId,DBG_EXCEPTION_NOT_HANDLED);

    }


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