HyperDbg/hyperdbg/libhyperdbg/code/debugger/commands/debugging-commands/wrmsr.cpp

181 lines
5 KiB
C++
Raw Permalink Normal View History

/**
* @file wrmsr.cpp
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
* @brief wrmsr command
* @details
* @version 0.1
* @date 2020-05-27
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
//
// Global Variables
//
extern BOOLEAN g_IsKdModuleLoaded;
2020-08-28 04:03:12 -07:00
/**
2023-07-13 16:05:42 +09:00
* @brief help of the wrmsr command
2021-02-10 15:19:01 -08:00
*
* @return VOID
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
VOID
CommandWrmsrHelp()
{
2022-04-10 22:24:56 +04:30
ShowMessages("wrmsr : writes on a model-specific register (MSR).\n\n");
ShowMessages("syntax : \twrmsr [Msr (hex)] [Value (hex)] [core CoreNumber (hex)]\n");
ShowMessages("\n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : wrmsr c0000082 fffff8077356f010\n");
ShowMessages("\t\te.g : wrmsr c0000082 @rcx\n");
ShowMessages("\t\te.g : wrmsr c0000082 @rcx+@rdx+12\n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : wrmsr c0000082 fffff8077356f010 core 2\n");
}
2020-08-28 04:03:12 -07:00
/**
* @brief wrmsr command handler
2021-02-10 15:19:01 -08:00
*
2024-07-30 14:17:06 +09:00
* @param CommandTokens
* @param Command
2023-07-26 16:53:27 +09:00
*
2021-02-10 15:19:01 -08:00
* @return VOID
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
VOID
CommandWrmsr(vector<CommandToken> CommandTokens, string Command)
2021-03-22 18:19:39 +04:30
{
BOOL Status;
UINT64 Msr;
ULONG ReturnedLength;
2023-07-26 15:15:16 +09:00
DEBUGGER_READ_AND_WRITE_ON_MSR MsrWriteRequest = {0};
BOOL IsNextCoreId = FALSE;
BOOL SetMsr = FALSE;
BOOL SetValue = FALSE;
UINT64 Value = 0;
UINT32 CoreNumer = DEBUGGER_READ_AND_WRITE_ON_MSR_APPLY_ALL_CORES;
BOOLEAN IsFirstCommand = TRUE;
2021-03-22 18:19:39 +04:30
2024-07-30 14:17:06 +09:00
if (CommandTokens.size() >= 6)
2021-03-22 18:19:39 +04:30
{
ShowMessages("incorrect use of the '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
CommandWrmsrHelp();
return;
}
2024-07-30 14:17:06 +09:00
for (auto Section : CommandTokens)
2021-03-22 18:19:39 +04:30
{
2023-07-26 15:15:16 +09:00
if (IsFirstCommand == TRUE)
2021-03-22 18:19:39 +04:30
{
2023-07-26 15:15:16 +09:00
IsFirstCommand = FALSE;
2021-03-22 18:19:39 +04:30
continue;
}
if (IsNextCoreId)
{
2024-07-30 14:17:06 +09:00
if (!ConvertTokenToUInt32(Section, &CoreNumer))
2021-03-22 18:19:39 +04:30
{
ShowMessages("please specify a correct hex value for core id\n\n");
CommandWrmsrHelp();
return;
}
2023-07-26 16:53:27 +09:00
2021-03-22 18:19:39 +04:30
IsNextCoreId = FALSE;
continue;
}
2024-07-30 14:17:06 +09:00
if (CompareLowerCaseStrings(Section, "core"))
2021-03-22 18:19:39 +04:30
{
IsNextCoreId = TRUE;
continue;
}
if (!SetMsr)
{
2024-07-30 14:17:06 +09:00
if (!ConvertTokenToUInt64(Section, &Msr))
2021-03-22 18:19:39 +04:30
{
ShowMessages("please specify a correct hex value to be read\n\n");
CommandWrmsrHelp();
return;
}
else
{
//
// Means that the MSR is set, next we should read value
//
SetMsr = TRUE;
continue;
}
}
if (SetMsr)
{
2024-07-30 14:17:06 +09:00
if (!SymbolConvertNameOrExprToAddress(GetCaseSensitiveStringFromCommandToken(Section), &Value))
2021-03-22 18:19:39 +04:30
{
ShowMessages(
"please specify a correct hex value or an expression to put on the msr\n\n");
2021-03-22 18:19:39 +04:30
CommandWrmsrHelp();
return;
}
else
{
SetValue = TRUE;
continue;
}
}
}
2021-03-22 18:19:39 +04:30
//
// Check if msr is set or not
//
if (!SetMsr)
{
ShowMessages("please specify a correct hex value to write\n\n");
CommandWrmsrHelp();
return;
}
2023-07-26 16:53:27 +09:00
2021-03-22 18:19:39 +04:30
if (!SetValue)
{
ShowMessages("please specify a correct hex value to put on msr\n\n");
CommandWrmsrHelp();
return;
}
2023-07-26 16:53:27 +09:00
2021-03-22 18:19:39 +04:30
if (IsNextCoreId)
{
ShowMessages("please specify a correct hex value for core\n\n");
CommandWrmsrHelp();
return;
}
AssertShowMessageReturnStmt(g_IsKdModuleLoaded, g_DeviceHandle, ASSERT_MESSAGE_KD_NOT_LOADED, ASSERT_MESSAGE_DRIVER_NOT_LOADED, AssertReturn);
2021-03-22 18:19:39 +04:30
MsrWriteRequest.ActionType = DEBUGGER_MSR_WRITE;
MsrWriteRequest.Msr = Msr;
MsrWriteRequest.CoreNumber = CoreNumer;
MsrWriteRequest.Value = Value;
Status = PlatformDeviceIoControl(
2021-03-22 18:19:39 +04:30
g_DeviceHandle, // Handle to device
IOCTL_DEBUGGER_READ_OR_WRITE_MSR, // IO Control Code (IOCTL)
2021-03-22 18:19:39 +04:30
&MsrWriteRequest, // Input Buffer to driver.
SIZEOF_DEBUGGER_READ_AND_WRITE_ON_MSR, // Input buffer length
&MsrWriteRequest, // Output Buffer from driver.
SIZEOF_DEBUGGER_READ_AND_WRITE_ON_MSR, // Length of output buffer in bytes.
&ReturnedLength, // Bytes placed in buffer.
2021-03-22 18:19:39 +04:30
NULL // synchronous call
);
if (!Status)
{
ShowMessages("ioctl failed with code (%x), either msr index or core id is invalid\n",
PlatformGetLastError());
2021-03-22 18:19:39 +04:30
return;
}
2021-03-22 18:19:39 +04:30
ShowMessages("\n");
}