2020-06-11 15:24:59 -07:00
|
|
|
/**
|
2020-07-13 14:27:04 -07:00
|
|
|
* @file dr.cpp
|
2022-01-18 22:38:56 +03:30
|
|
|
* @author Sina Karvandi (sina@hyperdbg.org)
|
2020-06-11 15:24:59 -07:00
|
|
|
* @brief !dr commands
|
|
|
|
|
* @details
|
|
|
|
|
* @version 0.1
|
|
|
|
|
* @date 2020-06-11
|
|
|
|
|
*
|
|
|
|
|
* @copyright This project is released under the GNU Public License v3.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2022-05-04 16:38:21 -07:00
|
|
|
#include "pch.h"
|
2020-06-11 15:24:59 -07:00
|
|
|
|
2020-08-28 04:03:12 -07:00
|
|
|
/**
|
2023-07-13 16:05:42 +09:00
|
|
|
* @brief help of the !dr command
|
2020-10-22 06:27:28 -07:00
|
|
|
*
|
|
|
|
|
* @return VOID
|
2020-08-28 04:03:12 -07:00
|
|
|
*/
|
2021-03-22 18:19:39 +04:30
|
|
|
VOID
|
|
|
|
|
CommandDrHelp()
|
|
|
|
|
{
|
2022-04-10 22:24:56 +04:30
|
|
|
ShowMessages("!dr : monitors any access to debug registers.\n\n");
|
2022-04-17 23:05:24 +04:30
|
|
|
|
2022-02-11 02:53:28 +03:30
|
|
|
ShowMessages("syntax : \t!dr [pid ProcessId (hex)] [core CoreId (hex)] "
|
2023-07-31 15:05:14 +09:00
|
|
|
"[imm IsImmediate (yesno)] [sc EnableShortCircuiting (onoff)] [stage CallingStage (prepostall)] "
|
2024-07-19 17:03:43 +09:00
|
|
|
"[buffer PreAllocatedBuffer (hex)] [script { Script (string) }] [asm condition { Condition (assembly/hex) }] "
|
|
|
|
|
"[asm code { Code (assembly/hex) }] [output {OutputName (string)}]\n");
|
2020-06-11 15:24:59 -07:00
|
|
|
|
2022-04-17 23:05:24 +04:30
|
|
|
ShowMessages("\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
ShowMessages("\t\te.g : !dr\n");
|
|
|
|
|
ShowMessages("\t\te.g : !dr pid 400\n");
|
|
|
|
|
ShowMessages("\t\te.g : !dr core 2 pid 400\n");
|
2024-07-20 22:05:39 +09:00
|
|
|
ShowMessages("\t\te.g : !dr script { printf(\"debug register modified!\\n\"); }\n");
|
|
|
|
|
ShowMessages("\t\te.g : !dr asm code { nop; nop; nop }\n");
|
2020-06-11 15:24:59 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-28 04:03:12 -07:00
|
|
|
/**
|
|
|
|
|
* @brief !dr command handler
|
2020-10-22 06:27:28 -07:00
|
|
|
*
|
2024-07-30 18:44:15 +09:00
|
|
|
* @param CommandTokens
|
2024-07-31 14:08:14 +09:00
|
|
|
* @param Command
|
2024-07-30 18:44:15 +09:00
|
|
|
*
|
2020-10-22 06:27:28 -07:00
|
|
|
* @return VOID
|
2020-08-28 04:03:12 -07:00
|
|
|
*/
|
2021-03-22 18:19:39 +04:30
|
|
|
VOID
|
2024-07-31 14:08:14 +09:00
|
|
|
CommandDr(vector<CommandToken> CommandTokens, string Command)
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
2021-08-21 20:48:17 +04:30
|
|
|
PDEBUGGER_GENERAL_EVENT_DETAIL Event = NULL;
|
|
|
|
|
PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger = NULL;
|
|
|
|
|
PDEBUGGER_GENERAL_ACTION ActionCustomCode = NULL;
|
|
|
|
|
PDEBUGGER_GENERAL_ACTION ActionScript = NULL;
|
|
|
|
|
UINT32 EventLength;
|
|
|
|
|
UINT32 ActionBreakToDebuggerLength = 0;
|
|
|
|
|
UINT32 ActionCustomCodeLength = 0;
|
|
|
|
|
UINT32 ActionScriptLength = 0;
|
|
|
|
|
DEBUGGER_EVENT_PARSING_ERROR_CAUSE EventParsingErrorCause;
|
2020-08-28 04:03:12 -07:00
|
|
|
|
|
|
|
|
//
|
2021-03-22 18:19:39 +04:30
|
|
|
// Interpret and fill the general event and action fields
|
|
|
|
|
//
|
2020-08-28 04:03:12 -07:00
|
|
|
//
|
2021-03-22 18:19:39 +04:30
|
|
|
if (!InterpretGeneralEventAndActionsFields(
|
2024-07-30 18:44:15 +09:00
|
|
|
&CommandTokens,
|
2021-03-22 18:19:39 +04:30
|
|
|
DEBUG_REGISTERS_ACCESSED,
|
|
|
|
|
&Event,
|
|
|
|
|
&EventLength,
|
|
|
|
|
&ActionBreakToDebugger,
|
|
|
|
|
&ActionBreakToDebuggerLength,
|
|
|
|
|
&ActionCustomCode,
|
|
|
|
|
&ActionCustomCodeLength,
|
|
|
|
|
&ActionScript,
|
2021-08-21 20:48:17 +04:30
|
|
|
&ActionScriptLength,
|
|
|
|
|
&EventParsingErrorCause))
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
|
|
|
|
return;
|
2020-10-22 06:27:28 -07:00
|
|
|
}
|
2021-03-22 18:19:39 +04:30
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Check for size
|
|
|
|
|
//
|
2024-07-30 18:44:15 +09:00
|
|
|
if (CommandTokens.size() > 1)
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
2024-08-10 23:11:59 +09:00
|
|
|
ShowMessages("incorrect use of the '%s'\n\n",
|
2024-07-30 18:44:15 +09:00
|
|
|
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
|
2021-03-22 18:19:39 +04:30
|
|
|
CommandDrHelp();
|
2022-05-09 01:34:07 +04:30
|
|
|
|
|
|
|
|
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
|
2021-03-22 18:19:39 +04:30
|
|
|
return;
|
2020-10-22 06:27:28 -07:00
|
|
|
}
|
2021-03-22 18:19:39 +04:30
|
|
|
|
|
|
|
|
//
|
2022-05-06 19:21:56 +04:30
|
|
|
// Send the ioctl to the kernel for event registration
|
2021-03-22 18:19:39 +04:30
|
|
|
//
|
|
|
|
|
if (!SendEventToKernel(Event, EventLength))
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// There was an error, probably the handle was not initialized
|
|
|
|
|
// we have to free the Action before exit, it is because, we
|
|
|
|
|
// already freed the Event and string buffers
|
|
|
|
|
//
|
2022-05-08 22:13:44 +04:30
|
|
|
|
2022-05-09 01:34:07 +04:30
|
|
|
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
|
2021-03-22 18:19:39 +04:30
|
|
|
return;
|
2020-10-22 06:27:28 -07:00
|
|
|
}
|
2020-06-11 15:24:59 -07:00
|
|
|
|
2020-08-28 04:03:12 -07:00
|
|
|
//
|
2021-03-22 18:19:39 +04:30
|
|
|
// Add the event to the kernel
|
2020-08-28 04:03:12 -07:00
|
|
|
//
|
2022-05-09 01:34:07 +04:30
|
|
|
if (!RegisterActionToEvent(Event,
|
|
|
|
|
ActionBreakToDebugger,
|
2022-03-13 01:33:21 +03:30
|
|
|
ActionBreakToDebuggerLength,
|
|
|
|
|
ActionCustomCode,
|
|
|
|
|
ActionCustomCodeLength,
|
|
|
|
|
ActionScript,
|
|
|
|
|
ActionScriptLength))
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// There was an error
|
|
|
|
|
//
|
2022-05-09 01:34:07 +04:30
|
|
|
|
|
|
|
|
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
|
2021-03-22 18:19:39 +04:30
|
|
|
return;
|
|
|
|
|
}
|
2020-06-11 15:24:59 -07:00
|
|
|
}
|