HyperDbg/hyperdbg/libhyperdbg/code/debugger/commands/extension-commands/crwrite.cpp

206 lines
6.5 KiB
C++
Raw Permalink Normal View History

/**
* @file crwrite.cpp
* @author angel_killah
* @brief !crwrite command
* @details
* @version 0.1
* @date 2022-07-05
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
/**
2023-07-13 16:05:42 +09:00
* @brief help of the !crwrite command
*
* @return VOID
*/
VOID
CommandCrwriteHelp()
{
ShowMessages("!crwrite : monitors modification of control registers (CR0 / CR4).\n\n");
ShowMessages("syntax : \t!crwrite [Cr (hex)] [mask Mask (hex)] [pid ProcessId (hex)] "
2023-10-11 15:03:51 +09:00
"[core CoreId (hex)] [imm IsImmediate (yesno)] [sc EnableShortCircuiting (onoff)] "
"[stage CallingStage (prepostall)] [buffer PreAllocatedBuffer (hex)] [script { Script (string) }] "
2024-07-19 17:03:43 +09:00
"[asm condition { Condition (assembly/hex) }] [asm code { Code (assembly/hex) }] [output {OutputName (string)}]\n");
ShowMessages("\n");
ShowMessages("\t\te.g : !crwrite 0\n");
ShowMessages("\t\te.g : !crwrite 0 0x10000\n");
ShowMessages("\t\te.g : !crwrite 4 pid 400\n");
ShowMessages("\t\te.g : !crwrite 4 core 2 pid 400\n");
2024-07-20 22:05:39 +09:00
ShowMessages("\t\te.g : !crwrite 4 script { printf(\"4th control register is modified at: %%llx\\n\", @rip); }\n");
ShowMessages("\t\te.g : !crwrite 4 asm code { nop; nop; nop }\n");
}
/**
* @brief !crwrite command handler
*
2024-07-30 18:44:15 +09:00
* @param CommandTokens
* @param Command
2024-07-30 18:44:15 +09:00
*
* @return VOID
*/
VOID
CommandCrwrite(vector<CommandToken> CommandTokens, string Command)
{
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;
UINT64 TargetRegister = 1;
UINT64 MaskRegister = 0xFFFFFFFFFFFFFFFF;
BOOLEAN GetRegister = FALSE;
BOOLEAN GetMask = FALSE;
DEBUGGER_EVENT_PARSING_ERROR_CAUSE EventParsingErrorCause;
2024-07-30 18:44:15 +09:00
if (CommandTokens.size() < 2)
{
ShowMessages("incorrect use of the '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
CommandCrwriteHelp();
return;
}
//
// Interpret and fill the general event and action fields
//
//
if (!InterpretGeneralEventAndActionsFields(
2024-07-30 18:44:15 +09:00
&CommandTokens,
CONTROL_REGISTER_MODIFIED,
&Event,
&EventLength,
&ActionBreakToDebugger,
&ActionBreakToDebuggerLength,
&ActionCustomCode,
&ActionCustomCodeLength,
&ActionScript,
&ActionScriptLength,
&EventParsingErrorCause))
{
return;
}
//
// Interpret command specific details (if any)
//
2024-07-30 18:44:15 +09:00
for (auto Section : CommandTokens)
{
2024-07-30 18:44:15 +09:00
if (CompareLowerCaseStrings(Section, "!crwrite"))
{
continue;
}
else if (!GetRegister)
{
//
// It's probably a CR
//
2024-07-30 18:44:15 +09:00
if (!ConvertTokenToUInt64(Section, &TargetRegister))
{
//
2024-03-17 19:00:14 +09:00
// Unknown parameter
//
2024-07-30 18:44:15 +09:00
ShowMessages("unknown parameter '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(Section).c_str());
CommandCrwriteHelp();
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
return;
}
else
{
GetRegister = TRUE;
}
}
else if (!GetMask)
{
2024-07-30 18:44:15 +09:00
if (!ConvertTokenToUInt64(Section, &MaskRegister))
{
//
2024-03-17 19:00:14 +09:00
// Unknown parameter
//
2024-07-30 18:44:15 +09:00
ShowMessages("unknown parameter '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(Section).c_str());
CommandCrwriteHelp();
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
2023-01-18 20:23:40 +09:00
return;
}
else
{
GetMask = TRUE;
}
}
else
{
//
2024-03-17 19:00:14 +09:00
// Unknown parameter
//
2024-07-30 18:44:15 +09:00
ShowMessages("unknown parameter '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(Section).c_str());
CommandCrwriteHelp();
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
return;
}
}
if (TargetRegister != VMX_EXIT_QUALIFICATION_REGISTER_CR0 && TargetRegister != VMX_EXIT_QUALIFICATION_REGISTER_CR4)
{
ShowMessages("please choose either 0 for cr0 or 4 for cr4\n");
CommandCrwriteHelp();
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
2023-01-18 20:23:40 +09:00
return;
}
//
// Set the target control register
//
2023-10-14 21:28:08 +09:00
Event->Options.OptionalParam1 = TargetRegister;
2023-01-18 20:23:40 +09:00
//
// Set the mask to filter control register bits
//
2023-10-14 21:28:08 +09:00
Event->Options.OptionalParam2 = MaskRegister;
//
// Send the ioctl to the kernel for event registration
//
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
//
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
return;
}
//
// Add the event to the kernel
//
if (!RegisterActionToEvent(Event,
ActionBreakToDebugger,
ActionBreakToDebuggerLength,
ActionCustomCode,
ActionCustomCodeLength,
ActionScript,
ActionScriptLength))
{
//
// There was an error
//
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
return;
}
2023-03-22 17:45:52 +09:00
}