Source Code C++ Debugger and Breakpoints

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

_kappa

Newbie
Full Member
Jun 24, 2016
31
478
4
C++:
#include <windows.h>
#include <stdio.h>

#define RF 16

typedef unsigned long dword;

CONTEXT context = { CONTEXT_ALL };

dword breakpoint = 0x77138E70;
dword thread_id = 0;

void debug_privilege()
{
	LUID identifier;

	HANDLE token;

	TOKEN_PRIVILEGES token_new;
	TOKEN_PRIVILEGES token_old;

	dword length;

	if (LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &identifier))
	{
		if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
		{
			token_new.PrivilegeCount = 1;
			token_new.Privileges->Attributes = SE_PRIVILEGE_ENABLED;
			token_new.Privileges->Luid.HighPart = identifier.HighPart;
			token_new.Privileges->Luid.LowPart = identifier.LowPart;

			AdjustTokenPrivileges(token, FALSE, &token_new, sizeof(TOKEN_PRIVILEGES), &token_old, &length);

			CloseHandle(token);
		}
	}
}

void hardware_breakpoint(void *thread, dword address)
{
	SuspendThread(thread);
	GetThreadContext(thread, &context);

	context.Dr0 = address;
	context.Dr7 ^= 1;

	SetThreadContext(thread, &context);
	ResumeThread(thread);
}

void exception_context(void* thread)
{
	GetThreadContext(thread, &context);

	printf("eip %08x\tflg %08x\n", context.Eip, context.EFlags);
	printf("eax %08x\tesi %08x\nebx %08x\tedi %08x\n", context.Eax, context.Esi, context.Ebx, context.Edi);
	printf("ecx %08x\tebp %08x\nedx %08x\tesp %08x\n", context.Ecx, context.Ebp, context.Edx, context.Esp);

	context.EFlags |= 1 << RF;

	SetThreadContext(thread, &context);
}

void windows_debugger(void *thread)
{
	DEBUG_EVENT debug_event;

	dword debug_status;

	debug_privilege();

	if (DebugActiveProcess(GetProcessIdOfThread(thread)))
	{
		hardware_breakpoint(thread, breakpoint);

		for (; ;)
		{
			WaitForDebugEvent(&debug_event, INFINITE);

			debug_status = DBG_CONTINUE;

			switch (debug_event.dwDebugEventCode)
			{
			case EXCEPTION_DEBUG_EVENT:
				switch (debug_event.u.Exception.ExceptionRecord.ExceptionCode)
				{
				case EXCEPTION_BREAKPOINT:
					break;

				case EXCEPTION_SINGLE_STEP:
					exception_context(thread);

					getchar();

					break;

				default:
					debug_status = DBG_EXCEPTION_NOT_HANDLED;

					break;
				}

				break;
			}

			ContinueDebugEvent(debug_event.dwProcessId, debug_event.dwThreadId, debug_status);
		}
	}
}

int main()
{
	windows_debugger(OpenThread(THREAD_ALL_ACCESS, FALSE, thread_id));

	getchar();
}
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods