Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <iostream>
- using namespace std;
- //---------------------Defines-------------------\\
- #define HWBP_LOCAL 1
- #define HWBP_GLOBAL 2
- #define HWBP_EXECUTE 0
- #define HWBP_WRITE 1
- #define HWBP_READWRITE 3
- #define HWBP_BYTE 0
- #define HWBP_WORD 1
- #define HWBP_DWORD 3
- typedef struct {
- ULONG HWBP0_MODE : 2;
- ULONG HWBP1_MODE : 2;
- ULONG HWBP2_MODE : 2;
- ULONG HWBP3_MODE : 2;
- ULONG LE : 1;
- ULONG GE : 1;
- ULONG __unused : 6;
- ULONG HWBP0_ACCESS : 2;
- ULONG HWBP0_LENGTH : 2;
- ULONG HWBP1_ACCESS : 2;
- ULONG HWBP1_LENGTH : 2;
- ULONG HWBP2_ACCESS : 2;
- ULONG HWBP2_LENGTH : 2;
- ULONG HWBP3_ACCESS : 2;
- ULONG HWBP3_LENGTH : 2;
- }DR7;
- typedef struct {
- ULONG CF : 1;
- ULONG __unused0 : 1;
- ULONG PF : 1;
- ULONG __unused1 : 1;
- ULONG AF : 1;
- ULONG __unused2 : 1;
- ULONG ZF : 1;
- ULONG SF : 1;
- ULONG TF : 1;
- ULONG IF : 1;
- ULONG DF : 1;
- ULONG OF : 1;
- ULONG IOPL : 2;
- ULONG __unused3 : 1;
- ULONG RF : 1;
- ULONG VM : 1;
- ULONG AC : 1;
- ULONG VIF : 1;
- ULONG VIP : 1;
- ULONG ID : 1;
- ULONG __unused4 : 10;
- }EFLAGS;
- //----------------------------------------------\\
- //-------------------Main Functions--------------\\
- bool SetHWBP(HANDLE hThread, unsigned int linearAddress, int type, int length, int count)
- {
- CONTEXT context = { CONTEXT_ALL | CONTEXT_DEBUG_REGISTERS };
- DR7 dr7;
- if (GetThreadContext(hThread, &context))
- {
- dr7 = *(DR7*)&context.Dr7;
- switch (count)
- {
- case 0:
- {
- context.Dr0 = linearAddress;
- dr7.HWBP0_MODE = HWBP_LOCAL;
- dr7.HWBP0_LENGTH = length;
- dr7.HWBP0_ACCESS = type;
- break;
- };
- case 1:
- {
- context.Dr1 = linearAddress;
- dr7.HWBP1_MODE = HWBP_LOCAL;
- dr7.HWBP1_LENGTH = length;
- dr7.HWBP1_ACCESS = type;
- break;
- };
- case 2:
- {
- context.Dr2 = linearAddress;
- dr7.HWBP2_MODE = HWBP_LOCAL;
- dr7.HWBP2_LENGTH = length;
- dr7.HWBP2_ACCESS = type;
- break;
- };
- case 3:
- {
- context.Dr3 = linearAddress;
- dr7.HWBP3_MODE = HWBP_LOCAL;
- dr7.HWBP3_LENGTH = length;
- dr7.HWBP3_ACCESS = type;
- break;
- };
- default:
- return false;
- };
- context.Dr7 = *(PDWORD)&dr7;
- return SetThreadContext(hThread, &context);
- };
- return false;
- };
- bool RemoveHWBP(HANDLE hThread, int count)
- {
- CONTEXT context = { CONTEXT_ALL | CONTEXT_DEBUG_REGISTERS };
- DR7 dr7;
- if (GetThreadContext(hThread, &context))
- {
- dr7 = *(DR7*)&context.Dr7;
- switch (count)
- {
- case 0:
- {
- context.Dr0 = 0;
- dr7.HWBP0_MODE = 0;
- break;
- };
- case 1:
- {
- context.Dr1 = 0;
- dr7.HWBP1_MODE = 0;
- break;
- };
- case 2:
- {
- context.Dr2 = 0;
- dr7.HWBP2_MODE = 0;
- break;
- };
- case 3:
- {
- context.Dr3 = 0;
- dr7.HWBP3_MODE = 0;
- break;
- };
- default:
- return false;
- };
- context.Dr7 = *(PDWORD)&dr7;
- return SetThreadContext(hThread, &context);
- };
- return false;
- };
- void GetRegister(CONTEXT *context)
- {
- DWORD Register;
- if (context->Eip == 0x014BD4D1) //EIP Register always pointing At The Address Number (0x014BD4D1 is The Address)
- {
- cout << hex << context->Ebp << endl;
- cout << hex << context->Esp << endl;
- };
- };
- //--------------------------------------------------\\
- int main()
- {
- DEBUG_EVENT DebugEvent;
- DWORD EventCode;
- DWORD ExceptionCode;
- HWND Window = FindWindow(NULL, L"GameWindow");
- if (Window == NULL)
- {
- cout << "Couldn't Find The Window" << endl;
- }
- DWORD ProcessID;
- GetWindowThreadProcessId(Window, &ProcessID);
- HANDLE handel = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
- if (ProcessID == NULL)
- {
- cout << "Couldn't Open The Process" << endl;
- }
- bool Debug_Active_Process = DebugActiveProcess(ProcessID);
- if (Debug_Active_Process)
- {
- while (Debug_Active_Process)
- {
- WaitForDebugEvent(&DebugEvent, INFINITE);
- EventCode = DebugEvent.dwDebugEventCode;
- ExceptionCode = DebugEvent.u.Exception.ExceptionRecord.ExceptionCode;
- switch (ExceptionCode)
- {
- case EXCEPTION_DEBUG_EVENT:
- {
- if (EXCEPTION_SINGLE_STEP == ExceptionCode)
- {
- HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, false, DebugEvent.dwThreadId);
- if (hThread)
- {
- CONTEXT context = { CONTEXT_ALL | CONTEXT_DEBUG_REGISTERS | CONTEXT_CONTROL };
- if (GetThreadContext(hThread, &context))
- {
- if ((context.Dr6 & (1 << 0)))
- {
- GetRegister(&context);
- RemoveHWBP(hThread, 0);
- }
- };
- CloseHandle(hThread);
- };
- };
- };
- ContinueDebugEvent(ProcessID, DebugEvent.dwThreadId, DBG_CONTINUE);
- };
- };
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement