HyperDbg Debugger
Loading...
Searching...
No Matches
msrwrite.cpp File Reference

!msrwrite command More...

#include "pch.h"

Functions

VOID CommandMsrwriteHelp ()
 help of the !msrwrite command
VOID CommandMsrwrite (vector< CommandToken > CommandTokens, string Command)
 !msrwrite command handler

Detailed Description

!msrwrite command

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-06-02

Function Documentation

◆ CommandMsrwrite()

VOID CommandMsrwrite ( vector< CommandToken > CommandTokens,
string Command )

!msrwrite command handler

Parameters
CommandTokens
Command
Returns
VOID
48{
50 PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger = NULL;
51 PDEBUGGER_GENERAL_ACTION ActionCustomCode = NULL;
52 PDEBUGGER_GENERAL_ACTION ActionScript = NULL;
53 UINT32 EventLength;
54 UINT32 ActionBreakToDebuggerLength = 0;
55 UINT32 ActionCustomCodeLength = 0;
56 UINT32 ActionScriptLength = 0;
57 UINT64 SpecialTarget = DEBUGGER_EVENT_MSR_READ_OR_WRITE_ALL_MSRS;
58 BOOLEAN GetAddress = FALSE;
59 DEBUGGER_EVENT_PARSING_ERROR_CAUSE EventParsingErrorCause;
60
61 //
62 // Interpret and fill the general event and action fields
63 //
64 //
66 &CommandTokens,
68 &Event,
69 &EventLength,
70 &ActionBreakToDebugger,
71 &ActionBreakToDebuggerLength,
72 &ActionCustomCode,
73 &ActionCustomCodeLength,
74 &ActionScript,
75 &ActionScriptLength,
76 &EventParsingErrorCause))
77 {
78 return;
79 }
80
81 //
82 // Interpret command specific details (if any), it is because we can use
83 // special msr bitmap here
84 //
85 for (auto Section : CommandTokens)
86 {
87 if (CompareLowerCaseStrings(Section, "!msrwrite"))
88 {
89 continue;
90 }
91 else if (!GetAddress)
92 {
93 //
94 // It's probably an msr
95 //
96 if (!ConvertTokenToUInt64(Section, &SpecialTarget))
97 {
98 //
99 // Unknown parameter
100 //
101 ShowMessages("unknown parameter '%s'\n\n",
104
105 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
106 return;
107 }
108 else
109 {
110 GetAddress = TRUE;
111 }
112 }
113 else
114 {
115 //
116 // Unknown parameter
117 //
118 ShowMessages("unknown parameter '%s'\n\n",
121
122 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
123 return;
124 }
125 }
126
127 //
128 // Set the target msr (if not specific then it means all msrs)
129 //
130 Event->Options.OptionalParam1 = SpecialTarget;
131
132 //
133 // Send the ioctl to the kernel for event registration
134 //
135 if (!SendEventToKernel(Event, EventLength))
136 {
137 //
138 // There was an error, probably the handle was not initialized
139 // we have to free the Action before exit, it is because, we
140 // already freed the Event and string buffers
141 //
142
143 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
144 return;
145 }
146
147 //
148 // Add the event to the kernel
149 //
150 if (!RegisterActionToEvent(Event,
151 ActionBreakToDebugger,
152 ActionBreakToDebuggerLength,
153 ActionCustomCode,
154 ActionCustomCodeLength,
155 ActionScript,
156 ActionScriptLength))
157 {
158 //
159 // There was an error
160 //
161
162 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
163 return;
164 }
165}
UCHAR BOOLEAN
Definition BasicTypes.h:35
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
unsigned int UINT32
Definition BasicTypes.h:54
#define DEBUGGER_EVENT_MSR_READ_OR_WRITE_ALL_MSRS
Apply to all Model Specific Registers.
Definition Constants.h:751
@ WRMSR_INSTRUCTION_EXECUTION
Definition Events.h:129
struct _DEBUGGER_GENERAL_EVENT_DETAIL * PDEBUGGER_GENERAL_EVENT_DETAIL
struct _DEBUGGER_GENERAL_ACTION * PDEBUGGER_GENERAL_ACTION
std::string GetCaseSensitiveStringFromCommandToken(CommandToken TargetToken)
Get case sensitive string from command token.
Definition common.cpp:467
BOOLEAN CompareLowerCaseStrings(CommandToken TargetToken, const CHAR *StringToCompare)
Compare lower case strings.
Definition common.cpp:503
BOOLEAN ConvertTokenToUInt64(CommandToken TargetToken, PUINT64 Result)
add ` between 64 bit values and convert them to string
Definition common.cpp:447
VOID FreeEventsAndActionsMemory(PDEBUGGER_GENERAL_EVENT_DETAIL Event, PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger, PDEBUGGER_GENERAL_ACTION ActionCustomCode, PDEBUGGER_GENERAL_ACTION ActionScript)
Deallocate buffers relating to events and actions.
Definition debugger.cpp:1688
BOOLEAN InterpretGeneralEventAndActionsFields(vector< CommandToken > *CommandTokens, VMM_EVENT_TYPE_ENUM EventType, PDEBUGGER_GENERAL_EVENT_DETAIL *EventDetailsToFill, PUINT32 EventBufferLength, PDEBUGGER_GENERAL_ACTION *ActionDetailsToFillBreakToDebugger, PUINT32 ActionBufferLengthBreakToDebugger, PDEBUGGER_GENERAL_ACTION *ActionDetailsToFillCustomCode, PUINT32 ActionBufferLengthCustomCode, PDEBUGGER_GENERAL_ACTION *ActionDetailsToFillScript, PUINT32 ActionBufferLengthScript, PDEBUGGER_EVENT_PARSING_ERROR_CAUSE ReasonForErrorInParsing)
Interpret general event fields.
Definition debugger.cpp:1736
BOOLEAN SendEventToKernel(PDEBUGGER_GENERAL_EVENT_DETAIL Event, UINT32 EventBufferLength)
Register the event to the kernel.
Definition debugger.cpp:1367
BOOLEAN RegisterActionToEvent(PDEBUGGER_GENERAL_EVENT_DETAIL Event, PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger, UINT32 ActionBreakToDebuggerLength, PDEBUGGER_GENERAL_ACTION ActionCustomCode, UINT32 ActionCustomCodeLength, PDEBUGGER_GENERAL_ACTION ActionScript, UINT32 ActionScriptLength)
Register the action to the event.
Definition debugger.cpp:1483
enum _DEBUGGER_EVENT_PARSING_ERROR_CAUSE DEBUGGER_EVENT_PARSING_ERROR_CAUSE
Reason for error in parsing commands.
VOID CommandMsrwriteHelp()
help of the !msrwrite command
Definition msrwrite.cpp:20
NULL()
Definition test-case-generator.py:530
UINT64 OptionalParam1
Definition Events.h:277
DEBUGGER_EVENT_OPTIONS Options
Definition Events.h:396

◆ CommandMsrwriteHelp()

VOID CommandMsrwriteHelp ( )

help of the !msrwrite command

Returns
VOID
21{
22 ShowMessages("!msrwrite : detects the execution of wrmsr instructions.\n\n");
23
24 ShowMessages("syntax : \t!msrwrite [Msr (hex)] [pid ProcessId (hex)] [core CoreId (hex)] "
25 "[imm IsImmediate (yesno)] [sc EnableShortCircuiting (onoff)] [stage CallingStage (prepostall)] "
26 "[buffer PreAllocatedBuffer (hex)] [script { Script (string) }] [asm condition { Condition (assembly/hex) }] "
27 "[asm code { Code (assembly/hex) }] [output {OutputName (string)}]\n");
28
29 ShowMessages("\n");
30 ShowMessages("\t\te.g : !msrwrite\n");
31 ShowMessages("\t\te.g : !msrwrite 0xc0000082\n");
32 ShowMessages("\t\te.g : !msrwrite pid 400\n");
33 ShowMessages("\t\te.g : !msrwrite core 2 pid 400\n");
34 ShowMessages("\t\te.g : !msrwrite script { printf(\"msr write with the 'ecx' register equal to: %%llx\\n\", $context); }\n");
35 ShowMessages("\t\te.g : !msrwrite asm code { nop; nop; nop }\n");
36}