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

171 lines
4.8 KiB
C++
Raw 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;
extern BOOLEAN g_IsDebuggerModulesLoaded;
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
}
else if (!g_IsDebuggerModulesLoaded && !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
2022-01-15 01:51:40 +03:30
Sleep(300);
//
// It is because we didn't query the target debuggee auto-unpause variable
//
if (!g_IsConnectedToRemoteDebuggee)
{
if (g_AutoUnpause)
{
ShowMessages(
"\npausing...\nauto-unpause mode is enabled, "
"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");
}
else
{
ShowMessages(
"\npausing...\nauto-unpause mode is disabled, you "
"should run 'g' when you want to continue, otherwise run "
"'settings "
"autounpause on'\n\n");
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
if (g_ActiveProcessDebuggingState.IsActive)
{
UdPauseProcess(g_ActiveProcessDebuggingState.ProcessDebuggingToken);
2022-01-15 01:51:40 +03:30
}
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
}