HyperDbg/hyperdbg/libhyperdbg/code/debugger/commands/debugging-commands/bp.cpp

347 lines
9.4 KiB
C++
Raw Permalink Normal View History

2021-03-10 18:52:49 +03:30
/**
* @file bp.cpp
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
2021-03-10 18:52:49 +03:30
* @brief bp command
* @details
* @version 0.1
* @date 2021-10-03
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
2021-03-10 18:52:49 +03:30
//
// Global Variables
//
extern BOOLEAN g_IsSerialConnectedToRemoteDebuggee;
extern BOOLEAN g_IsVmmModuleLoaded;
extern ACTIVE_DEBUGGING_PROCESS g_ActiveProcessDebuggingState;
2021-03-10 18:52:49 +03:30
/**
2023-07-13 16:05:42 +09:00
* @brief help of the bp command
2021-03-10 18:52:49 +03:30
*
* @return VOID
*/
2021-03-22 18:19:39 +04:30
VOID
CommandBpHelp()
{
2022-04-10 22:24:56 +04:30
ShowMessages("bp : puts a breakpoint (0xcc).\n");
2021-03-22 18:19:39 +04:30
ShowMessages(
"Note : 'bp' is not an event, if you want to use an event version "
"of breakpoints use !epthook or !epthook2 instead. See "
2024-03-17 01:01:22 +09:00
"documentation for more information.\n\n");
ShowMessages("syntax : \tbp [Address (hex)] [pid ProcessId (hex)] [tid ThreadId (hex)] [core CoreId (hex)]\n");
ShowMessages("\n");
ShowMessages("\t\te.g : bp nt!ExAllocatePoolWithTag\n");
ShowMessages("\t\te.g : bp nt!ExAllocatePoolWithTag+5\n");
ShowMessages("\t\te.g : bp nt!ExAllocatePoolWithTag+@rcx+rbx\n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : bp fffff8077356f010\n");
ShowMessages("\t\te.g : bp fffff8077356f010 pid 0x4\n");
ShowMessages("\t\te.g : bp fffff8077356f010 tid 0x1000\n");
ShowMessages("\t\te.g : bp fffff8077356f010 pid 0x4 core 2\n");
2021-03-10 18:52:49 +03:30
}
/**
* @brief Apply breakpoint on the user debugger
* @param BpPacket
*
* @return BOOLEAN
*/
BOOLEAN
CommandBpPerformApplyingBreakpointOnUserDebugger(DEBUGGEE_BP_PACKET * BpPacket)
{
BOOL Status;
ULONG ReturnedLength;
if (g_IsSerialConnectedToRemoteDebuggee)
{
//
// Check if the debugger is connected to a remote debuggee, this request
// is not for the kernel debugger
//
return FALSE;
}
else
{
AssertShowMessageReturnStmt(g_IsVmmModuleLoaded, g_DeviceHandle, ASSERT_MESSAGE_VMM_NOT_LOADED, ASSERT_MESSAGE_DRIVER_NOT_LOADED, AssertReturnFalse);
//
// Send IOCTL
//
2026-07-20 12:12:21 +02:00
Status = PlatformDeviceIoControl(
g_DeviceHandle, // Handle to device
IOCTL_SET_BREAKPOINT_USER_DEBUGGER, // IO Control Code (IOCTL)
BpPacket, // Input Buffer to driver.
SIZEOF_DEBUGGEE_BP_PACKET, // Input buffer length (not used in this case)
BpPacket, // Output Buffer from driver.
SIZEOF_DEBUGGEE_BP_PACKET, // Length of output buffer in bytes.
&ReturnedLength, // Bytes placed in buffer.
NULL // synchronous call
);
if (!Status)
{
2026-07-20 12:12:21 +02:00
ShowMessages("ioctl failed with code 0x%x\n", PlatformGetLastError());
return FALSE;
}
if (BpPacket->Result == DEBUGGER_OPERATION_WAS_SUCCESSFUL)
{
return TRUE;
}
else
{
//
// An err occurred, no results
//
ShowErrorMessage(BpPacket->Result);
return FALSE;
}
}
}
/**
* @brief request breakpoint
*
* @param Address Address
* @param Pid Process Id
* @param Tid Thread Id
* @param CoreNumer Core Number
*
* @return BOOLEAN
*/
BOOLEAN
CommandBpRequest(UINT64 Address, UINT32 Pid, UINT32 Tid, UINT32 CoreNumer)
{
DEBUGGEE_BP_PACKET BpPacket = {0};
//
// Check if the debugger is connected to a remote debuggee or a user-debugger is active
//
if (!g_IsSerialConnectedToRemoteDebuggee && !g_ActiveProcessDebuggingState.IsActive)
{
return FALSE;
}
//
// Set the details for the remote packet
//
BpPacket.Address = Address;
BpPacket.Core = CoreNumer;
BpPacket.Pid = Pid;
BpPacket.Tid = Tid;
//
// Send the bp packet either to the user debugger or the kernel debugger
//
if (g_ActiveProcessDebuggingState.IsActive)
{
return CommandBpPerformApplyingBreakpointOnUserDebugger(&BpPacket);
}
else if (g_IsSerialConnectedToRemoteDebuggee)
{
return KdSendBpPacketToDebuggee(&BpPacket);
}
else
{
//
// couldn't set breakpoint, no active process or remote debuggee
//
return FALSE;
}
}
2021-03-10 18:52:49 +03:30
/**
* @brief bp command handler
*
2024-07-29 18:46:12 +09:00
* @param CommandTokens
* @param Command
2024-07-29 18:46:12 +09:00
*
2021-03-10 18:52:49 +03:30
* @return VOID
*/
2021-03-22 18:19:39 +04:30
VOID
CommandBp(vector<CommandToken> CommandTokens, string Command)
2021-03-22 18:19:39 +04:30
{
BOOL IsNextCoreId = FALSE;
BOOL IsNextPid = FALSE;
BOOL IsNextTid = FALSE;
BOOLEAN SetCoreId = FALSE;
BOOLEAN SetPid = FALSE;
BOOLEAN SetTid = FALSE;
BOOLEAN SetAddress = FALSE;
2024-07-29 18:46:12 +09:00
UINT32 Tid = DEBUGGEE_BP_APPLY_TO_ALL_THREADS;
UINT32 Pid = DEBUGGEE_BP_APPLY_TO_ALL_PROCESSES;
UINT32 CoreNumer = DEBUGGEE_BP_APPLY_TO_ALL_CORES;
UINT64 Address = NULL;
BOOLEAN IsFirstCommand = TRUE;
2021-03-22 18:19:39 +04:30
2024-07-29 18:46:12 +09:00
if (CommandTokens.size() >= 9)
2021-03-22 18:19:39 +04:30
{
ShowMessages("incorrect use of the '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
2021-03-22 18:19:39 +04:30
CommandBpHelp();
return;
}
2021-03-10 18:52:49 +03:30
//
// Disable user-mode debugger in this version
//
#if ActivateUserModeDebugger == FALSE
if (!g_IsSerialConnectedToRemoteDebuggee)
{
ShowMessages("the user-mode debugger in VMI Mode is still in the beta version and not stable. "
"we decided to exclude it from this release and release it in future versions. "
"if you want to test the user-mode debugger in VMI Mode, you should build "
"HyperDbg with special instructions. But starting processes is fully supported "
"in the Debugger Mode.\n"
"(it's not recommended to use it in VMI Mode yet!)\n");
return;
}
#endif // !ActivateUserModeDebugger
//
// If the user is debugging a process, use its pid
//
if (g_ActiveProcessDebuggingState.IsActive)
{
Pid = g_ActiveProcessDebuggingState.ProcessId;
}
2024-07-29 18:46:12 +09:00
for (auto Section : CommandTokens)
2021-03-22 18:19:39 +04:30
{
//
// Ignore the first argument as it's the command string itself (bp)
//
2023-07-26 15:15:16 +09:00
if (IsFirstCommand == TRUE)
2021-03-22 18:19:39 +04:30
{
2023-07-26 15:15:16 +09:00
IsFirstCommand = FALSE;
2021-03-22 18:19:39 +04:30
continue;
}
if (IsNextCoreId)
{
2024-07-29 18:46:12 +09:00
if (!ConvertTokenToUInt32(Section, &CoreNumer))
2021-03-22 18:19:39 +04:30
{
ShowMessages("please specify a correct hex value for core id\n\n");
CommandBpHelp();
return;
}
IsNextCoreId = FALSE;
continue;
}
if (IsNextPid)
{
2024-07-29 18:46:12 +09:00
if (!ConvertTokenToUInt32(Section, &Pid))
2021-03-22 18:19:39 +04:30
{
ShowMessages("please specify a correct hex value for process id\n\n");
CommandBpHelp();
return;
}
IsNextPid = FALSE;
continue;
}
if (IsNextTid)
{
2024-07-29 18:46:12 +09:00
if (!ConvertTokenToUInt32(Section, &Tid))
2021-03-22 18:19:39 +04:30
{
ShowMessages("please specify a correct hex value for thread id\n\n");
CommandBpHelp();
return;
}
IsNextTid = FALSE;
continue;
}
2024-07-29 18:46:12 +09:00
if (CompareLowerCaseStrings(Section, "pid"))
2021-03-22 18:19:39 +04:30
{
IsNextPid = TRUE;
continue;
}
2024-07-29 18:46:12 +09:00
if (CompareLowerCaseStrings(Section, "tid"))
2021-03-22 18:19:39 +04:30
{
IsNextTid = TRUE;
continue;
}
2024-07-29 18:46:12 +09:00
if (CompareLowerCaseStrings(Section, "core"))
2021-03-22 18:19:39 +04:30
{
IsNextCoreId = TRUE;
continue;
}
if (!SetAddress)
{
2024-07-29 18:46:12 +09:00
if (!SymbolConvertNameOrExprToAddress(GetCaseSensitiveStringFromCommandToken(Section), &Address))
2021-03-22 18:19:39 +04:30
{
//
2024-03-17 01:01:22 +09:00
// Couldn't resolve or unknown parameter
//
ShowMessages("err, couldn't resolve error at '%s'\n\n",
2024-07-29 18:46:12 +09:00
GetCaseSensitiveStringFromCommandToken(Section).c_str());
2021-03-22 18:19:39 +04:30
CommandBpHelp();
return;
}
else
{
//
// Means that address is received
//
SetAddress = TRUE;
continue;
}
}
}
2021-03-10 18:52:49 +03:30
//
2021-03-22 18:19:39 +04:30
// Check if address is set or not
2021-03-10 18:52:49 +03:30
//
2021-03-22 18:19:39 +04:30
if (!SetAddress)
{
ShowMessages("please specify a correct hex value as the breakpoint address\n\n");
2021-03-10 18:52:49 +03:30
CommandBpHelp();
return;
}
2021-03-22 18:19:39 +04:30
if (IsNextPid)
{
2021-03-10 18:52:49 +03:30
ShowMessages("please specify a correct hex value for process id\n\n");
CommandBpHelp();
return;
}
2021-03-22 18:19:39 +04:30
if (IsNextCoreId)
{
ShowMessages("please specify a correct hex value for core\n\n");
2021-03-10 18:52:49 +03:30
CommandBpHelp();
return;
}
2021-03-22 18:19:39 +04:30
if (IsNextTid)
{
ShowMessages("please specify a correct hex value for thread id\n\n");
2021-03-10 18:52:49 +03:30
CommandBpHelp();
return;
2021-03-22 18:19:39 +04:30
}
2021-03-10 18:52:49 +03:30
if (!g_IsSerialConnectedToRemoteDebuggee && !g_ActiveProcessDebuggingState.IsActive)
2021-03-22 18:19:39 +04:30
{
ShowMessages("setting breakpoints is not possible when you're not connected "
"to a target debuggee (kernel debugger or user debugger)\n");
2021-03-22 18:19:39 +04:30
return;
2021-03-10 18:52:49 +03:30
}
2021-03-22 18:19:39 +04:30
//
// Request breakpoint the bp packet
2021-03-22 18:19:39 +04:30
//
if (!CommandBpRequest(Address, Pid, Tid, CoreNumer))
{
ShowMessages("err, couldn't set breakpoint\n");
}
2021-03-10 18:52:49 +03:30
}