HyperDbg/hyperdbg/libhyperdbg/code/debugger/core/break-control.cpp

175 lines
5.1 KiB
C++
Raw Permalink Normal View History

2020-07-24 14:38:53 -07:00
/**
* @file break-control.cpp
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
2020-07-24 14:38:53 -07:00
* @brief break control is the handler for CTRL+C and CTRL+BREAK Signals
* @details
* @version 0.1
* @date 2020-07-24
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
2020-07-24 14:38:53 -07:00
2020-08-28 04:03:12 -07:00
//
// Global Variables
//
2022-01-17 14:54:41 +03:30
extern BOOLEAN g_BreakPrintingOutput;
2026-05-29 00:10:44 +02:00
extern BOOLEAN g_IsKdModuleLoaded;
2022-01-17 14:54:41 +03:30
extern BOOLEAN g_AutoUnpause;
extern BOOLEAN g_IsConnectedToRemoteDebuggee;
extern BOOLEAN g_IsSerialConnectedToRemoteDebuggee;
extern BOOLEAN g_IsExecutingSymbolLoadingRoutines;
extern BOOLEAN g_IsInstrumentingInstructions;
extern BOOLEAN g_IgnorePauseRequests;
extern ACTIVE_DEBUGGING_PROCESS g_ActiveProcessDebuggingState;
2020-08-28 04:03:12 -07:00
/**
* @brief handle CTRL+C and CTRL+Break events
2020-08-28 06:07:44 -07:00
*
* @param CtrlType
* @return BOOL
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
BOOL
BreakController(DWORD CtrlType)
{
switch (CtrlType)
{
//
2021-11-30 17:05:43 +03:30
// Handle the CTRL + C & CTRL + Break signal
2021-03-22 18:19:39 +04:30
//
case CTRL_BREAK_EVENT:
2021-03-22 18:19:39 +04:30
case CTRL_C_EVENT:
2021-04-28 17:42:44 +04:30
//
// check if we should ignore the break requests or not
2021-04-28 17:42:44 +04:30
//
if (g_IgnorePauseRequests)
2021-04-28 17:42:44 +04:30
{
return TRUE;
}
//
// Check for aborting symbol loading routines
//
if (g_IsExecutingSymbolLoadingRoutines)
{
//
// Avoid to calling it again
//
g_IsExecutingSymbolLoadingRoutines = FALSE;
//
// Abort the execution
//
ScriptEngineSymbolAbortLoadingWrapper();
}
2021-03-22 18:19:39 +04:30
//
// Check if the debuggee is running because of pausing or not
//
if (g_IsSerialConnectedToRemoteDebuggee)
{
if (g_IsInstrumentingInstructions)
{
g_IsInstrumentingInstructions = FALSE;
}
else
{
KdBreakControlCheckAndPauseDebugger(TRUE);
}
2021-03-22 18:19:39 +04:30
}
2026-05-29 00:10:44 +02:00
else if (!g_IsKdModuleLoaded && !g_IsConnectedToRemoteDebuggee)
{
//
// vmm module is not loaded here
//
}
2021-03-22 18:19:39 +04:30
else
{
2022-01-15 01:51:40 +03:30
if (g_IsInstrumentingInstructions)
2021-03-22 18:19:39 +04:30
{
2022-01-15 01:51:40 +03:30
g_IsInstrumentingInstructions = FALSE;
2021-03-22 18:19:39 +04:30
}
2022-01-15 01:51:40 +03:30
else
2021-03-22 18:19:39 +04:30
{
2022-01-15 01:51:40 +03:30
//
// Sleep because the other thread that shows must be stopped
//
g_BreakPrintingOutput = TRUE;
//
// Check if its a remote debuggee then we should send the 'pause' command
//
if (g_IsConnectedToRemoteDebuggee)
2021-03-22 18:19:39 +04:30
{
2024-03-14 14:02:18 +09:00
RemoteConnectionSendCommand("pause", (UINT32)strlen("pause") + 1);
2021-03-22 18:19:39 +04:30
}
2021-04-28 02:16:33 +04:30
PlatformSleep(300);
2022-01-15 01:51:40 +03:30
//
// It is because we didn't query the target debuggee auto-unpause variable
//
if (!g_IsConnectedToRemoteDebuggee)
{
if (g_AutoUnpause)
{
ShowMessages(
"\npausing...%s\nauto-unpause mode is enabled, "
2022-01-15 01:51:40 +03:30
"debugger will automatically continue when you run a new "
"event command, if you want to change this behaviour then "
"run run 'settings autounpause off'\n\n",
g_ActiveProcessDebuggingState.IsActive ? PAUSE_MESSAGE_IN_USER_DEBUGGER : "");
2022-01-15 01:51:40 +03:30
}
else
{
ShowMessages(
"\npausing...%s\nauto-unpause mode is disabled, you "
2022-01-15 01:51:40 +03:30
"should run 'g' when you want to continue, otherwise run "
"'settings autounpause on'\n\n",
g_ActiveProcessDebuggingState.IsActive ? PAUSE_MESSAGE_IN_USER_DEBUGGER : "");
2022-01-17 14:54:41 +03:30
}
2022-01-15 01:51:40 +03:30
2022-01-17 14:54:41 +03:30
//
// Show the signature of HyperDbg
//
2022-07-15 14:52:07 -07:00
HyperDbgShowSignature();
2022-01-17 14:54:41 +03:30
2025-06-29 20:22:51 +02:00
//
// Not pause the process if CTRL+C is pressed (Commented)
//
// if (g_ActiveProcessDebuggingState.IsActive)
// {
// UdPauseProcess(g_ActiveProcessDebuggingState.ProcessDebuggingToken);
// }
2021-03-22 18:19:39 +04:30
}
}
}
2020-08-28 06:07:44 -07:00
2021-03-22 18:19:39 +04:30
return TRUE;
//
// CTRL+CLOSE: confirm that the user wants to exit.
2021-03-22 18:19:39 +04:30
//
case CTRL_CLOSE_EVENT:
return TRUE;
case CTRL_LOGOFF_EVENT:
return FALSE;
2020-07-24 14:38:53 -07:00
2021-03-22 18:19:39 +04:30
case CTRL_SHUTDOWN_EVENT:
return FALSE;
2020-07-24 14:38:53 -07:00
2021-03-22 18:19:39 +04:30
default:
2020-08-28 04:03:12 -07:00
2021-03-22 18:19:39 +04:30
//
// Return TRUE if handled this message, further handler functions won't be
// called.
// Return FALSE to pass this message to further handlers until default
// handler calls ExitProcess().
//
return FALSE;
}
2020-07-24 14:38:53 -07:00
}