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

rdmsr command More...

#include "pch.h"

Typedefs

typedef BOOL(WINAPI * glpie_t) (LOGICAL_PROCESSOR_RELATIONSHIP, PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD)
 defines the GetLogicalProcessorInformationEx function
 

Functions

VOID CommandRdmsrHelp ()
 help of the rdmsr command
 
VOID CommandRdmsr (vector< string > SplitCommand, string Command)
 rdmsr command handler
 

Detailed Description

rdmsr command

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

Typedef Documentation

◆ glpie_t

typedef BOOL(WINAPI * glpie_t) (LOGICAL_PROCESSOR_RELATIONSHIP, PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD)

defines the GetLogicalProcessorInformationEx function

Function Documentation

◆ CommandRdmsr()

VOID CommandRdmsr ( vector< string > SplitCommand,
string Command )

rdmsr command handler

Parameters
SplitCommand
Command
Returns
VOID
115{
116 BOOL Status;
117 SIZE_T NumCPU;
118 DEBUGGER_READ_AND_WRITE_ON_MSR MsrReadRequest;
119 ULONG ReturnedLength;
120 UINT64 Msr;
121 BOOL IsNextCoreId = FALSE;
122 BOOL SetMsr = FALSE;
124 BOOLEAN IsFirstCommand = TRUE;
125
126 if (SplitCommand.size() >= 5)
127 {
128 ShowMessages("incorrect use of the 'rdmsr'\n\n");
130 return;
131 }
132
133 for (auto Section : SplitCommand)
134 {
135 if (IsFirstCommand == TRUE)
136 {
137 IsFirstCommand = FALSE;
138 continue;
139 }
140
141 if (IsNextCoreId)
142 {
143 if (!ConvertStringToUInt32(Section, &CoreNumer))
144 {
145 ShowMessages("please specify a correct hex value for core id\n\n");
147 return;
148 }
149 IsNextCoreId = FALSE;
150 continue;
151 }
152
153 if (!Section.compare("core"))
154 {
155 IsNextCoreId = TRUE;
156 continue;
157 }
158
159 if (SetMsr || !ConvertStringToUInt64(Section, &Msr))
160 {
161 ShowMessages("please specify a correct hex value to be read\n\n");
163 return;
164 }
165 SetMsr = TRUE;
166 }
167
168 //
169 // Check if msr is set or not
170 //
171 if (!SetMsr)
172 {
173 ShowMessages("please specify a correct hex value to be read\n\n");
175 return;
176 }
177 if (IsNextCoreId)
178 {
179 ShowMessages("please specify a correct hex value for core\n\n");
181 return;
182 }
183
185
186 MsrReadRequest.ActionType = DEBUGGER_MSR_READ;
187 MsrReadRequest.Msr = Msr;
188 MsrReadRequest.CoreNumber = CoreNumer;
189
190 //
191 // Find logical cores count
192 //
193 SIZE_T NumCores = GetWindowsNumaNumberOfCores();
194 NumCPU = NumCores > 0 ? NumCores : GetWindowsCompatibleNumberOfCores();
195
196 //
197 // allocate buffer for transferring messages
198 //
199 UINT64 * OutputBuffer = (UINT64 *)malloc(sizeof(UINT64) * NumCPU);
200
201 ZeroMemory(OutputBuffer, sizeof(UINT64) * NumCPU);
202
203 Status = DeviceIoControl(
204 g_DeviceHandle, // Handle to device
205 IOCTL_DEBUGGER_READ_OR_WRITE_MSR, // IO Control Code (IOCTL)
206 &MsrReadRequest, // Input Buffer to driver.
207 SIZEOF_DEBUGGER_READ_AND_WRITE_ON_MSR, // Input buffer length
208 OutputBuffer, // Output Buffer from driver.
209 (DWORD)(sizeof(UINT64) * NumCPU), // Length of output buffer in bytes.
210 &ReturnedLength, // Bytes placed in buffer.
211 NULL // synchronous call
212 );
213
214 if (!Status)
215 {
216 ShowMessages("ioctl failed with code (%x), either msr index or core id is invalid\n",
217 GetLastError());
218 free(OutputBuffer);
219 return;
220 }
221
222 //
223 // btw, %x is enough, no need to %llx
224 //
226 {
227 //
228 // Show all cores
229 //
230 for (SIZE_T i = 0; i < NumCPU; i++)
231 {
232 ShowMessages("core : 0x%x - msr[%llx] = %s\n", i, Msr, SeparateTo64BitValue((OutputBuffer[i])).c_str());
233 }
234 }
235 else
236 {
237 //
238 // Show for a single-core
239 //
240 ShowMessages("core : 0x%x - msr[%llx] = %s\n", CoreNumer, Msr, SeparateTo64BitValue((OutputBuffer[0])).c_str());
241 }
242
243 //
244 // Free the buffer
245 //
246 free(OutputBuffer);
247}
int BOOL
Definition BasicTypes.h:23
UCHAR BOOLEAN
Definition BasicTypes.h:39
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54
unsigned __int64 UINT64
Definition BasicTypes.h:21
unsigned long DWORD
Definition BasicTypes.h:22
unsigned int UINT32
Definition BasicTypes.h:48
unsigned long ULONG
Definition BasicTypes.h:37
#define DEBUGGER_READ_AND_WRITE_ON_MSR_APPLY_ALL_CORES
Read and write MSRs to all cores.
Definition Constants.h:599
#define IOCTL_DEBUGGER_READ_OR_WRITE_MSR
ioctl, request to read or write on a special MSR
Definition Ioctls.h:92
#define SIZEOF_DEBUGGER_READ_AND_WRITE_ON_MSR
Definition RequestStructures.h:422
@ DEBUGGER_MSR_READ
Definition RequestStructures.h:431
string SeparateTo64BitValue(UINT64 Value)
add ` between 64 bit values and convert them to string
Definition common.cpp:27
BOOLEAN ConvertStringToUInt64(string TextToConvert, PUINT64 Result)
check and convert string to a 64 bit unsigned integer and also check for special notations like 0x,...
Definition common.cpp:240
BOOLEAN ConvertStringToUInt32(string TextToConvert, PUINT32 Result)
check and convert string to a 32 bit unsigned it and also check for special notations like 0x etc.
Definition common.cpp:347
#define AssertShowMessageReturnStmt(expr, message, rc)
Definition common.h:51
#define AssertReturn
Definition common.h:19
#define ASSERT_MESSAGE_DRIVER_NOT_LOADED
Definition common.h:25
HANDLE g_DeviceHandle
Holds the global handle of device which is used to send the request to the kernel by IOCTL,...
Definition globals.h:471
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96
VOID CommandRdmsrHelp()
help of the rdmsr command
Definition rdmsr.cpp:20
request to read or write on MSRs
Definition RequestStructures.h:440
UINT32 CoreNumber
Definition RequestStructures.h:442
DEBUGGER_MSR_ACTION_TYPE ActionType
Definition RequestStructures.h:446
UINT64 Msr
Definition RequestStructures.h:441

◆ CommandRdmsrHelp()

VOID CommandRdmsrHelp ( )

help of the rdmsr command

Returns
VOID
21{
22 ShowMessages("rdmsr : reads a model-specific register (MSR).\n\n");
23
24 ShowMessages("syntax : \trdmsr [Msr (hex)] [core CoreNumber (hex)]\n");
25
26 ShowMessages("\n");
27 ShowMessages("\t\te.g : rdmsr c0000082\n");
28 ShowMessages("\t\te.g : rdmsr c0000082 core 2\n");
29}