2020-06-02 07:20:06 -07:00
|
|
|
/**
|
|
|
|
|
* @file msrwrite.cpp
|
2022-01-18 22:38:56 +03:30
|
|
|
* @author Sina Karvandi (sina@hyperdbg.org)
|
2020-06-02 07:20:06 -07:00
|
|
|
* @brief !msrwrite command
|
|
|
|
|
* @details
|
|
|
|
|
* @version 0.1
|
|
|
|
|
* @date 2020-06-02
|
|
|
|
|
*
|
|
|
|
|
* @copyright This project is released under the GNU Public License v3.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2022-05-04 16:38:21 -07:00
|
|
|
#include "pch.h"
|
2020-06-02 07:20:06 -07:00
|
|
|
|
2020-08-28 04:03:12 -07:00
|
|
|
/**
|
2023-07-13 16:05:42 +09:00
|
|
|
* @brief help of the !msrwrite 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
|
|
|
|
|
CommandMsrwriteHelp()
|
|
|
|
|
{
|
2022-04-10 22:24:56 +04:30
|
|
|
ShowMessages("!msrwrite : detects the execution of wrmsr instructions.\n\n");
|
2022-04-17 23:05:24 +04:30
|
|
|
|
2023-10-11 15:03:51 +09:00
|
|
|
ShowMessages("syntax : \t!msrwrite [Msr (hex)] [pid ProcessId (hex)] [core CoreId (hex)] "
|
|
|
|
|
"[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-02 07:20:06 -07:00
|
|
|
|
2022-04-17 23:05:24 +04:30
|
|
|
ShowMessages("\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
ShowMessages("\t\te.g : !msrwrite\n");
|
|
|
|
|
ShowMessages("\t\te.g : !msrwrite 0xc0000082\n");
|
|
|
|
|
ShowMessages("\t\te.g : !msrwrite pid 400\n");
|
|
|
|
|
ShowMessages("\t\te.g : !msrwrite core 2 pid 400\n");
|
2024-07-20 22:05:39 +09:00
|
|
|
ShowMessages("\t\te.g : !msrwrite script { printf(\"msr write with the 'ecx' register equal to: %%llx\\n\", $context); }\n");
|
|
|
|
|
ShowMessages("\t\te.g : !msrwrite asm code { nop; nop; nop }\n");
|
2020-06-02 07:20:06 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-28 04:03:12 -07:00
|
|
|
/**
|
|
|
|
|
* @brief !msrwrite 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
|
|
|
CommandMsrwrite(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;
|
|
|
|
|
UINT64 SpecialTarget = DEBUGGER_EVENT_MSR_READ_OR_WRITE_ALL_MSRS;
|
|
|
|
|
BOOLEAN GetAddress = FALSE;
|
|
|
|
|
DEBUGGER_EVENT_PARSING_ERROR_CAUSE EventParsingErrorCause;
|
2020-06-02 07:20:06 -07:00
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
//
|
|
|
|
|
// Interpret and fill the general event and action fields
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
if (!InterpretGeneralEventAndActionsFields(
|
2024-07-30 18:44:15 +09:00
|
|
|
&CommandTokens,
|
2021-03-22 18:19:39 +04:30
|
|
|
WRMSR_INSTRUCTION_EXECUTION,
|
|
|
|
|
&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
|
|
|
{
|
2020-06-02 07:20:06 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
//
|
|
|
|
|
// Interpret command specific details (if any), it is because we can use
|
|
|
|
|
// special msr bitmap here
|
|
|
|
|
//
|
2024-07-30 18:44:15 +09:00
|
|
|
for (auto Section : CommandTokens)
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
2024-07-30 18:44:15 +09:00
|
|
|
if (CompareLowerCaseStrings(Section, "!msrwrite"))
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if (!GetAddress)
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// It's probably an msr
|
|
|
|
|
//
|
2024-07-30 18:44:15 +09:00
|
|
|
if (!ConvertTokenToUInt64(Section, &SpecialTarget))
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
|
|
|
|
//
|
2024-03-17 19:00:14 +09:00
|
|
|
// Unknown parameter
|
2021-03-22 18:19:39 +04:30
|
|
|
//
|
2024-07-30 18:44:15 +09:00
|
|
|
ShowMessages("unknown parameter '%s'\n\n",
|
|
|
|
|
GetCaseSensitiveStringFromCommandToken(Section).c_str());
|
2021-03-22 18:19:39 +04:30
|
|
|
CommandMsrwriteHelp();
|
2022-05-09 01:34:07 +04:30
|
|
|
|
|
|
|
|
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
|
2021-03-22 18:19:39 +04:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GetAddress = TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//
|
2024-03-17 19:00:14 +09:00
|
|
|
// Unknown parameter
|
2021-03-22 18:19:39 +04:30
|
|
|
//
|
2024-07-30 18:44:15 +09:00
|
|
|
ShowMessages("unknown parameter '%s'\n\n",
|
|
|
|
|
GetCaseSensitiveStringFromCommandToken(Section).c_str());
|
2021-03-22 18:19:39 +04:30
|
|
|
CommandMsrwriteHelp();
|
2022-05-09 01:34:07 +04:30
|
|
|
|
|
|
|
|
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
|
2021-03-22 18:19:39 +04:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-02 07:20:06 -07:00
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
//
|
|
|
|
|
// Set the target msr (if not specific then it means all msrs)
|
|
|
|
|
//
|
2023-10-14 21:28:08 +09:00
|
|
|
Event->Options.OptionalParam1 = SpecialTarget;
|
2020-08-28 04:03:12 -07:00
|
|
|
|
|
|
|
|
//
|
2022-05-08 22:13:44 +04:30
|
|
|
// Send the ioctl to the kernel for event registration
|
2020-08-28 04:03:12 -07:00
|
|
|
//
|
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-02 07:20:06 -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-02 07:20:06 -07:00
|
|
|
}
|