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

wrmsr command More...

#include "pch.h"

Functions

VOID CommandWrmsrHelp ()
 help of the wrmsr command
VOID CommandWrmsr (vector< CommandToken > CommandTokens, string Command)
 wrmsr command handler

Variables

BOOLEAN g_IsKdModuleLoaded
 shows whether the kernel debugger (KD) module is loaded or not

Detailed Description

wrmsr command

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

Function Documentation

◆ CommandWrmsr()

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

wrmsr command handler

Parameters
CommandTokens
Command
Returns
VOID
48{
49 BOOL Status;
50 UINT64 Msr;
51 ULONG ReturnedLength;
52 DEBUGGER_READ_AND_WRITE_ON_MSR MsrWriteRequest = {0};
53 BOOL IsNextCoreId = FALSE;
54 BOOL SetMsr = FALSE;
56 UINT64 Value = 0;
58 BOOLEAN IsFirstCommand = TRUE;
59
60 if (CommandTokens.size() >= 6)
61 {
62 ShowMessages("incorrect use of the '%s'\n\n",
63 GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
65 return;
66 }
67
68 for (auto Section : CommandTokens)
69 {
70 if (IsFirstCommand == TRUE)
71 {
72 IsFirstCommand = FALSE;
73 continue;
74 }
75
76 if (IsNextCoreId)
77 {
78 if (!ConvertTokenToUInt32(Section, &CoreNumer))
79 {
80 ShowMessages("please specify a correct hex value for core id\n\n");
82 return;
83 }
84
85 IsNextCoreId = FALSE;
86 continue;
87 }
88
89 if (CompareLowerCaseStrings(Section, "core"))
90 {
91 IsNextCoreId = TRUE;
92 continue;
93 }
94
95 if (!SetMsr)
96 {
97 if (!ConvertTokenToUInt64(Section, &Msr))
98 {
99 ShowMessages("please specify a correct hex value to be read\n\n");
101 return;
102 }
103 else
104 {
105 //
106 // Means that the MSR is set, next we should read value
107 //
108 SetMsr = TRUE;
109 continue;
110 }
111 }
112
113 if (SetMsr)
114 {
116 {
117 ShowMessages(
118 "please specify a correct hex value or an expression to put on the msr\n\n");
120 return;
121 }
122 else
123 {
124 SetValue = TRUE;
125 continue;
126 }
127 }
128 }
129
130 //
131 // Check if msr is set or not
132 //
133 if (!SetMsr)
134 {
135 ShowMessages("please specify a correct hex value to write\n\n");
137 return;
138 }
139
140 if (!SetValue)
141 {
142 ShowMessages("please specify a correct hex value to put on msr\n\n");
144 return;
145 }
146
147 if (IsNextCoreId)
148 {
149 ShowMessages("please specify a correct hex value for core\n\n");
151 return;
152 }
153
155
156 MsrWriteRequest.ActionType = DEBUGGER_MSR_WRITE;
157 MsrWriteRequest.Msr = Msr;
158 MsrWriteRequest.CoreNumber = CoreNumer;
159 MsrWriteRequest.Value = Value;
160
161 Status = DeviceIoControl(
162 g_DeviceHandle, // Handle to device
163 IOCTL_DEBUGGER_READ_OR_WRITE_MSR, // IO Control Code (IOCTL)
164 &MsrWriteRequest, // Input Buffer to driver.
165 SIZEOF_DEBUGGER_READ_AND_WRITE_ON_MSR, // Input buffer length
166 &MsrWriteRequest, // Output Buffer from driver.
167 SIZEOF_DEBUGGER_READ_AND_WRITE_ON_MSR, // Length of output buffer in bytes.
168 &ReturnedLength, // Bytes placed in buffer.
169 NULL // synchronous call
170 );
171
172 if (!Status)
173 {
174 ShowMessages("ioctl failed with code (%x), either msr index or core id is invalid\n",
175 GetLastError());
176 return;
177 }
178
179 ShowMessages("\n");
180}
VOID SetValue(PGUEST_REGS GuestRegs, SCRIPT_ENGINE_GENERAL_REGISTERS *ScriptGeneralRegisters, PSYMBOL Symbol, UINT64 Value)
Set the value.
Definition ScriptEngineEval.c:185
int BOOL
Definition BasicTypes.h:25
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
unsigned long ULONG
Definition BasicTypes.h:31
#define DEBUGGER_READ_AND_WRITE_ON_MSR_APPLY_ALL_CORES
Read and write MSRs to all cores.
Definition Constants.h:727
#define IOCTL_DEBUGGER_READ_OR_WRITE_MSR
ioctl, request to read or write on a special MSR
Definition Ioctls.h:150
struct _DEBUGGER_READ_AND_WRITE_ON_MSR DEBUGGER_READ_AND_WRITE_ON_MSR
request to read or write on MSRs
#define SIZEOF_DEBUGGER_READ_AND_WRITE_ON_MSR
Definition RequestStructures.h:441
@ DEBUGGER_MSR_WRITE
Definition RequestStructures.h:451
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
BOOLEAN ConvertTokenToUInt32(CommandToken TargetToken, PUINT32 Result)
check and convert command token to a 32 bit unsigned integer
Definition common.cpp:546
RequestedActionOfThePacket Value(0x1) 00000000
#define ASSERT_MESSAGE_KD_NOT_LOADED
Definition common.h:29
#define AssertShowMessageReturnStmt(expr1, expr2, message1, message2, rc)
Definition common.h:59
#define AssertReturn
Definition common.h:19
#define ASSERT_MESSAGE_DRIVER_NOT_LOADED
Definition common.h:27
HANDLE g_DeviceHandle
Holds the global handle of device which is used to send the request to the kernel by IOCTL,...
Definition globals.h:481
BOOLEAN g_IsKdModuleLoaded
shows whether the kernel debugger (KD) module is loaded or not
Definition globals.h:22
UINT32 CoreNumber
Definition RequestStructures.h:461
DEBUGGER_MSR_ACTION_TYPE ActionType
Definition RequestStructures.h:464
UINT64 Msr
Definition RequestStructures.h:460
UINT64 Value
Definition RequestStructures.h:465
BOOLEAN SymbolConvertNameOrExprToAddress(const string &TextToConvert, PUINT64 Result)
check and convert string to a 64 bit unsigned integer and also check for symbol object names and eval...
Definition symbol.cpp:359
VOID CommandWrmsrHelp()
help of the wrmsr command
Definition wrmsr.cpp:25

◆ CommandWrmsrHelp()

VOID CommandWrmsrHelp ( )

help of the wrmsr command

Returns
VOID
26{
27 ShowMessages("wrmsr : writes on a model-specific register (MSR).\n\n");
28
29 ShowMessages("syntax : \twrmsr [Msr (hex)] [Value (hex)] [core CoreNumber (hex)]\n");
30
31 ShowMessages("\n");
32 ShowMessages("\t\te.g : wrmsr c0000082 fffff8077356f010\n");
33 ShowMessages("\t\te.g : wrmsr c0000082 @rcx\n");
34 ShowMessages("\t\te.g : wrmsr c0000082 @rcx+@rdx+12\n");
35 ShowMessages("\t\te.g : wrmsr c0000082 fffff8077356f010 core 2\n");
36}

Variable Documentation

◆ g_IsKdModuleLoaded

BOOLEAN g_IsKdModuleLoaded
extern

shows whether the kernel debugger (KD) module is loaded or not