2020-07-24 14:38:53 -07:00
|
|
|
/**
|
2024-07-10 20:13:31 +09: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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2022-05-04 16:38:21 -07:00
|
|
|
#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-12-21 14:50:11 -08:00
|
|
|
|
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
|
|
|
//
|
2021-09-01 01:08:18 +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
|
|
|
//
|
2021-11-29 22:09:10 +03:30
|
|
|
// check if we should ignore the break requests or not
|
2021-04-28 17:42:44 +04:30
|
|
|
//
|
2021-11-29 22:09:10 +03:30
|
|
|
if (g_IgnorePauseRequests)
|
2021-04-28 17:42:44 +04:30
|
|
|
{
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-01 16:01:45 +04:30
|
|
|
//
|
|
|
|
|
// 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)
|
|
|
|
|
{
|
2021-11-18 18:27:33 +03:30
|
|
|
if (g_IsInstrumentingInstructions)
|
|
|
|
|
{
|
|
|
|
|
g_IsInstrumentingInstructions = FALSE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-07-28 16:57:28 +09:00
|
|
|
KdBreakControlCheckAndPauseDebugger(TRUE);
|
2021-11-18 18:27:33 +03:30
|
|
|
}
|
2021-03-22 18:19:39 +04:30
|
|
|
}
|
2026-05-29 00:10:44 +02:00
|
|
|
else if (!g_IsKdModuleLoaded && !g_IsConnectedToRemoteDebuggee)
|
2021-09-01 01:08:18 +04:30
|
|
|
{
|
|
|
|
|
//
|
2021-09-01 16:01:45 +04:30
|
|
|
// vmm module is not loaded here
|
2021-09-01 01:08:18 +04:30
|
|
|
//
|
|
|
|
|
}
|
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
|
|
|
|
2026-07-17 17:54:05 +02:00
|
|
|
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(
|
2025-07-09 01:55:36 +02:00
|
|
|
"\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 "
|
2025-07-09 01:55:36 +02:00
|
|
|
"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(
|
2025-07-09 01:55:36 +02:00
|
|
|
"\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 "
|
2025-07-09 01:55:36 +02:00
|
|
|
"'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-12-21 14:50:11 -08:00
|
|
|
}
|
2020-08-28 06:07:44 -07:00
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
//
|
2021-09-01 01:08:18 +04:30
|
|
|
// 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
|
|
|
}
|