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

398 lines
12 KiB
C++
Raw Normal View History

2020-07-28 05:10:27 -07:00
/**
* @file e.cpp
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
2020-07-28 05:10:27 -07:00
* @brief e* command
* @details
* @version 0.1
* @date 2020-07-27
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
2020-07-28 05:10:27 -07:00
2021-05-07 02:56:34 +04:30
//
// Global Variables
//
extern BOOLEAN g_IsSerialConnectedToRemoteDebuggee;
extern ACTIVE_DEBUGGING_PROCESS g_ActiveProcessDebuggingState;
2021-05-07 02:56:34 +04:30
2020-08-28 04:03:12 -07:00
/**
* @brief help of !e* and e* commands
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
CommandEditMemoryHelp()
{
2022-04-10 22:24:56 +04:30
ShowMessages("eb !eb ed !ed eq !eq : edits the memory at specific address \n");
ShowMessages("eb Byte and ASCII characters\n");
ShowMessages("ed Double-word values (4 bytes)\n");
ShowMessages("eq Quad-word values (8 bytes). \n");
2021-03-22 18:19:39 +04:30
ShowMessages("\n If you want to edit physical (address) memory then add '!' "
"at the start of the command\n");
ShowMessages("syntax : \teb [Address (hex)] [Contents (hex)] [pid ProcessId (hex)]\n");
ShowMessages("syntax : \ted [Address (hex)] [Contents (hex)] [pid ProcessId (hex)]\n");
ShowMessages("syntax : \teq [Address (hex)] [Contents (hex)] [pid ProcessId (hex)]\n");
ShowMessages("\n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : eb fffff8077356f010 90 \n");
2021-05-27 18:31:56 +04:30
ShowMessages("\t\te.g : eb nt!Kd_DEFAULT_Mask ff ff ff ff \n");
ShowMessages("\t\te.g : eb nt!Kd_DEFAULT_Mask+10+@rcx ff ff ff ff \n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : eb fffff8077356f010 90 90 90 90 \n");
ShowMessages("\t\te.g : !eq 100000 9090909090909090\n");
ShowMessages("\t\te.g : !eq nt!ExAllocatePoolWithTag+55 9090909090909090\n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : !eq 100000 9090909090909090 9090909090909090 "
"9090909090909090 9090909090909090 9090909090909090\n");
2020-07-28 05:10:27 -07:00
}
2020-08-28 04:03:12 -07:00
/**
* @brief !e* and e* commands handler
2021-02-10 15:19:01 -08:00
*
2024-03-17 19:00:14 +09:00
* @param SplitCommand
2021-02-10 15:19:01 -08:00
* @param Command
* @return VOID
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
VOID
2024-03-17 19:00:14 +09:00
CommandEditMemory(vector<string> SplitCommand, string Command)
2021-03-22 18:19:39 +04:30
{
BOOL Status;
2021-03-24 14:19:01 +04:30
UINT64 Address;
UINT64 * FinalBuffer;
vector<UINT64> ValuesToEdit;
2021-03-22 18:19:39 +04:30
BOOL SetAddress = FALSE;
BOOL SetValue = FALSE;
BOOL SetProcId = FALSE;
BOOL NextIsProcId = FALSE;
DEBUGGER_EDIT_MEMORY EditMemoryRequest = {0};
2021-03-24 14:19:01 +04:30
UINT64 Value = 0;
2021-05-07 02:56:34 +04:30
UINT32 ProcId = 0;
2021-03-24 14:19:01 +04:30
UINT32 CountOfValues = 0;
UINT32 FinalSize = 0;
2024-03-17 19:00:14 +09:00
vector<string> SplitCommandCaseSensitive {Split(Command, ' ')};
UINT32 IndexInCommandCaseSensitive = 0;
2023-07-24 01:22:28 +09:00
BOOLEAN IsFirstCommand = TRUE;
2021-03-22 18:19:39 +04:30
//
// By default if the user-debugger is active, we use these commands
// on the memory layout of the debuggee process
//
if (g_ActiveProcessDebuggingState.IsActive)
{
ProcId = g_ActiveProcessDebuggingState.ProcessId;
}
2024-03-17 19:00:14 +09:00
if (SplitCommand.size() <= 2)
2021-03-22 18:19:39 +04:30
{
2023-07-13 16:05:42 +09:00
ShowMessages("incorrect use of the 'e*'\n\n");
2020-07-28 05:10:27 -07:00
CommandEditMemoryHelp();
return;
}
2024-03-17 19:00:14 +09:00
for (auto Section : SplitCommand)
2021-03-22 18:19:39 +04:30
{
IndexInCommandCaseSensitive++;
2023-07-24 01:22:28 +09:00
if (IsFirstCommand)
2021-03-22 18:19:39 +04:30
{
if (!Section.compare("!eb"))
{
EditMemoryRequest.MemoryType = EDIT_PHYSICAL_MEMORY;
EditMemoryRequest.ByteSize = EDIT_BYTE;
}
else if (!Section.compare("!ed"))
{
EditMemoryRequest.MemoryType = EDIT_PHYSICAL_MEMORY;
EditMemoryRequest.ByteSize = EDIT_DWORD;
}
else if (!Section.compare("!eq"))
{
EditMemoryRequest.MemoryType = EDIT_PHYSICAL_MEMORY;
EditMemoryRequest.ByteSize = EDIT_QWORD;
}
else if (!Section.compare("eb"))
{
EditMemoryRequest.MemoryType = EDIT_VIRTUAL_MEMORY;
EditMemoryRequest.ByteSize = EDIT_BYTE;
}
else if (!Section.compare("ed"))
{
EditMemoryRequest.MemoryType = EDIT_VIRTUAL_MEMORY;
EditMemoryRequest.ByteSize = EDIT_DWORD;
}
else if (!Section.compare("eq"))
{
EditMemoryRequest.MemoryType = EDIT_VIRTUAL_MEMORY;
EditMemoryRequest.ByteSize = EDIT_QWORD;
}
else
{
//
// What's this? :(
//
ShowMessages("unknown error happened !\n\n");
CommandEditMemoryHelp();
return;
}
2023-07-26 15:15:16 +09:00
IsFirstCommand = FALSE;
2021-03-22 18:19:39 +04:30
continue;
}
2022-10-06 17:45:07 +09:00
2021-03-22 18:19:39 +04:30
if (NextIsProcId)
{
//
// It's a process id
//
NextIsProcId = FALSE;
if (!ConvertStringToUInt32(Section, &ProcId))
{
2024-03-17 01:01:22 +09:00
ShowMessages("please specify a correct hex process id\n\n");
2021-03-22 18:19:39 +04:30
CommandEditMemoryHelp();
return;
}
else
{
//
// Means that the proc id is set, next we should read value
//
continue;
}
}
2021-05-07 02:56:34 +04:30
2020-07-28 05:10:27 -07:00
//
2021-03-22 18:19:39 +04:30
// Check if it's a process id or not
2020-07-28 05:10:27 -07:00
//
2021-03-22 18:19:39 +04:30
if (!SetProcId && !Section.compare("pid"))
{
NextIsProcId = TRUE;
continue;
}
if (!SetAddress)
{
2024-03-17 19:00:14 +09:00
if (!SymbolConvertNameOrExprToAddress(SplitCommandCaseSensitive.at(IndexInCommandCaseSensitive - 1),
&Address))
2021-03-22 18:19:39 +04:30
{
ShowMessages("err, couldn't resolve error at '%s'\n\n",
2024-03-17 19:00:14 +09:00
SplitCommandCaseSensitive.at(IndexInCommandCaseSensitive - 1).c_str());
2021-03-22 18:19:39 +04:30
CommandEditMemoryHelp();
return;
}
else
{
//
// Means that the address is set, next we should read value
//
SetAddress = TRUE;
continue;
}
}
if (SetAddress)
{
//
// Remove the hex notations
//
if (Section.rfind("0x", 0) == 0 || Section.rfind("0X", 0) == 0 ||
Section.rfind("\\x", 0) == 0 || Section.rfind("\\X", 0) == 0)
{
Section = Section.erase(0, 2);
}
else if (Section.rfind('x', 0) == 0 || Section.rfind('X', 0) == 0)
2021-03-22 18:19:39 +04:30
{
Section = Section.erase(0, 1);
}
Section.erase(remove(Section.begin(), Section.end(), '`'), Section.end());
//
// Check if the value is valid based on byte counts
//
if (EditMemoryRequest.ByteSize == EDIT_BYTE && Section.size() >= 3)
{
ShowMessages("please specify a byte (hex) value for 'eb' or '!eb'\n\n");
return;
}
if (EditMemoryRequest.ByteSize == EDIT_DWORD && Section.size() >= 9)
{
ShowMessages(
"please specify a dword (hex) value for 'ed' or '!ed'\n\n");
return;
}
if (EditMemoryRequest.ByteSize == EDIT_QWORD && Section.size() >= 17)
{
ShowMessages(
"please specify a qword (hex) value for 'eq' or '!eq'\n\n");
return;
}
//
// Qword is checked by the following function, no need to double
// check it above.
//
if (!ConvertStringToUInt64(Section, &Value))
{
ShowMessages("please specify a correct hex value to change the memory "
"content\n\n");
CommandEditMemoryHelp();
return;
}
else
{
//
// Add it to the list
//
ValuesToEdit.push_back(Value);
//
// Keep track of values to modify
//
CountOfValues++;
if (!SetValue)
{
//
// At least on value is there
//
SetValue = TRUE;
}
continue;
}
}
2020-07-28 05:10:27 -07:00
}
2021-03-22 18:19:39 +04:30
2021-05-07 02:56:34 +04:30
//
// Check to prevent using process id in e* commands
//
if (g_IsSerialConnectedToRemoteDebuggee && ProcId != 0)
{
ShowMessages(ASSERT_MESSAGE_CANNOT_SPECIFY_PID);
2021-05-07 02:56:34 +04:30
return;
}
if (ProcId == 0)
{
ProcId = GetCurrentProcessId();
}
2020-07-28 05:10:27 -07:00
//
2021-03-22 18:19:39 +04:30
// Fill the structure
2020-07-28 05:10:27 -07:00
//
2021-03-22 18:19:39 +04:30
EditMemoryRequest.ProcessId = ProcId;
EditMemoryRequest.Address = Address;
EditMemoryRequest.CountOf64Chunks = CountOfValues;
2020-07-28 05:10:27 -07:00
2021-03-22 18:19:39 +04:30
//
// Check if address and value are set or not
//
if (!SetAddress)
{
ShowMessages("please specify a correct hex address\n\n");
2020-07-28 05:10:27 -07:00
CommandEditMemoryHelp();
return;
}
2021-03-22 18:19:39 +04:30
if (!SetValue)
{
2020-07-28 05:10:27 -07:00
ShowMessages(
2021-03-22 18:19:39 +04:30
"please specify a correct hex value as the content to edit\n\n");
CommandEditMemoryHelp();
2020-07-28 05:10:27 -07:00
return;
2021-03-22 18:19:39 +04:30
}
if (NextIsProcId)
{
ShowMessages("please specify a correct hex value as the process id\n\n");
2020-07-28 05:10:27 -07:00
CommandEditMemoryHelp();
return;
}
2021-03-22 18:19:39 +04:30
//
// Now it's time to put everything together in one structure
//
FinalSize = (CountOfValues * sizeof(UINT64)) + SIZEOF_DEBUGGER_EDIT_MEMORY;
2020-07-28 05:10:27 -07:00
2021-03-22 18:19:39 +04:30
//
// Set the size
//
EditMemoryRequest.FinalStructureSize = FinalSize;
2020-07-28 05:10:27 -07:00
2021-03-22 18:19:39 +04:30
//
// Allocate structure + buffer
//
FinalBuffer = (UINT64 *)malloc(FinalSize);
2020-07-28 05:10:27 -07:00
2021-03-22 18:19:39 +04:30
if (!FinalBuffer)
{
ShowMessages("unable to allocate memory\n\n");
return;
}
2020-07-28 05:10:27 -07:00
2021-03-22 18:19:39 +04:30
//
// Zero the buffer
//
ZeroMemory(FinalBuffer, FinalSize);
2020-07-28 05:10:27 -07:00
2021-03-22 18:19:39 +04:30
//
// Copy the structure on top of the allocated buffer
//
memcpy(FinalBuffer, &EditMemoryRequest, SIZEOF_DEBUGGER_EDIT_MEMORY);
2021-03-22 06:30:09 -07:00
2021-03-22 18:19:39 +04:30
//
// Put the values in 64 bit structures
//
std::copy(ValuesToEdit.begin(), ValuesToEdit.end(), (UINT64 *)((UINT64)FinalBuffer + SIZEOF_DEBUGGER_EDIT_MEMORY));
2021-03-22 06:30:09 -07:00
2021-03-22 18:19:39 +04:30
//
// send the request
//
if (g_IsSerialConnectedToRemoteDebuggee)
{
2021-03-24 14:19:01 +04:30
KdSendEditMemoryPacketToDebuggee((DEBUGGER_EDIT_MEMORY *)FinalBuffer, FinalSize);
2021-03-22 18:19:39 +04:30
return;
}
//
// It's on VMI mode
//
AssertShowMessageReturnStmt(g_DeviceHandle, ASSERT_MESSAGE_DRIVER_NOT_LOADED, AssertReturn);
2020-07-28 05:10:27 -07:00
2021-03-22 18:19:39 +04:30
Status = DeviceIoControl(
g_DeviceHandle, // Handle to device
IOCTL_DEBUGGER_EDIT_MEMORY, // IO Control Code (IOCTL)
2021-03-22 18:19:39 +04:30
FinalBuffer, // Input Buffer to driver.
FinalSize, // Input buffer length
&EditMemoryRequest, // Output Buffer from driver.
SIZEOF_DEBUGGER_EDIT_MEMORY, // Length of output buffer in bytes.
NULL, // Bytes placed in buffer.
NULL // synchronous call
);
if (!Status)
{
free(FinalBuffer);
ShowMessages("ioctl failed with code 0x%x\n", GetLastError());
return;
}
2022-10-06 17:45:07 +09:00
if (EditMemoryRequest.Result == DEBUGGER_OPERATION_WAS_SUCCESSFUL)
2021-03-22 18:19:39 +04:30
{
//
// Was successful, nothing to do
//
}
else
{
2021-03-24 14:19:01 +04:30
ShowErrorMessage(EditMemoryRequest.Result);
2021-03-22 18:19:39 +04:30
}
2020-08-28 04:03:12 -07:00
//
2021-03-22 18:19:39 +04:30
// Free the malloc buffer
//
2021-03-22 18:19:39 +04:30
free(FinalBuffer);
2020-07-28 05:10:27 -07:00
}