HyperDbg/hyperdbg/libhyperdbg/code/debugger/commands/extension-commands/monitor.cpp

342 lines
13 KiB
C++
Raw Permalink Normal View History

/**
* @file monitor.cpp
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
* @brief !monitor command
* @details
* @version 0.1
* @date 2020-05-27
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
2020-08-28 04:03:12 -07:00
/**
2023-07-13 16:05:42 +09:00
* @brief help of the !monitor command
*
* @return VOID
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
VOID
CommandMonitorHelp()
{
2022-04-10 22:24:56 +04:30
ShowMessages("!monitor : monitors address range for read and writes.\n\n");
ShowMessages("syntax : \t!monitor [MemoryType (vapa)] [Attribute (string)] [FromAddress (hex)] "
2022-02-11 02:53:28 +03:30
"[ToAddress (hex)] [pid ProcessId (hex)] [core CoreId (hex)] "
2023-07-31 15:05:14 +09:00
"[imm IsImmediate (yesno)] [sc EnableShortCircuiting (onoff)] [stage CallingStage (prepostall)] "
2024-07-19 17:03:43 +09:00
"[buffer PreAllocatedBuffer (hex)] [script { Script (string) }] [asm condition { Condition (assembly/hex) }] "
"[asm code { Code (assembly/hex) }] [output {OutputName (string)}]\n");
2021-03-22 18:19:39 +04:30
ShowMessages("syntax : \t!monitor [MemoryType (vapa)] [Attribute (string)] [FromAddress (hex)] "
"[l Length (hex)] [pid ProcessId (hex)] [core CoreId (hex)] "
"[imm IsImmediate (yesno)] [sc EnableShortCircuiting (onoff)] [stage CallingStage (prepostall)] "
2024-07-19 17:03:43 +09:00
"[buffer PreAllocatedBuffer (hex)] [script { Script (string) }] [asm condition { Condition (assembly/hex) }] "
"[asm code { Code (assembly/hex) }] [output {OutputName (string)}]\n");
ShowMessages("\n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : !monitor rw fffff801deadb000 fffff801deadbfff\n");
ShowMessages("\t\te.g : !monitor rw fffff801deadb000 l 1000\n");
ShowMessages("\t\te.g : !monitor pa rw c01000 l 1000\n");
2023-07-07 01:21:07 +09:00
ShowMessages("\t\te.g : !monitor rwx fffff801deadb000 fffff801deadbfff\n");
ShowMessages("\t\te.g : !monitor rwx fffff801deadb000 l 230d0\n");
ShowMessages("\t\te.g : !monitor rw nt!Kd_DEFAULT_Mask Kd_DEFAULT_Mask+5\n");
2022-02-11 02:53:28 +03:30
ShowMessages("\t\te.g : !monitor r fffff801deadb000 fffff801deadbfff pid 400\n");
ShowMessages("\t\te.g : !monitor w fffff801deadb000 fffff801deadbfff core 2 pid 400\n");
ShowMessages("\t\te.g : !monitor w c01000 c01000+2500 core 2 pid 400\n");
2023-07-07 01:21:07 +09:00
ShowMessages("\t\te.g : !monitor x fffff801deadb000 fffff801deadbfff core 2 pid 400\n");
ShowMessages("\t\te.g : !monitor x fffff801deadb000 l 500 core 2 pid 400\n");
2023-07-07 01:21:07 +09:00
ShowMessages("\t\te.g : !monitor wx fffff801deadb000 fffff801deadbfff core 2 pid 400\n");
2024-07-20 22:05:39 +09:00
ShowMessages("\t\te.g : !monitor rw fffff801deadb000 l 1000 script { printf(\"read/write occurred at the virtual address: %%llx\\n\", $context); }\n");
ShowMessages("\t\te.g : !monitor rw fffff801deadb000 l 1000 asm code { nop; nop; nop }\n");
}
2020-08-28 04:03:12 -07:00
/**
* @brief !monitor command handler
*
2024-07-30 18:44:15 +09:00
* @param CommandTokens
* @param Command
2024-07-30 18:44:15 +09:00
*
* @return VOID
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
VOID
CommandMonitor(vector<CommandToken> CommandTokens, string Command)
2021-03-22 18:19:39 +04:30
{
PDEBUGGER_GENERAL_EVENT_DETAIL Event = NULL;
PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger = NULL;
PDEBUGGER_GENERAL_ACTION ActionCustomCode = NULL;
PDEBUGGER_GENERAL_ACTION ActionScript = NULL;
UINT32 EventLength;
UINT32 ActionBreakToDebuggerLength = 0;
UINT32 ActionCustomCodeLength = 0;
UINT32 ActionScriptLength = 0;
UINT32 HookLength = 0;
UINT64 OptionalParam1 = 0; // Set the 'from' target address
UINT64 OptionalParam2 = 0; // Set the 'to' target address
BOOLEAN SetFrom = FALSE;
BOOLEAN SetTo = FALSE;
BOOLEAN IsNextLength = FALSE;
BOOLEAN LengthAlreadySet = FALSE;
BOOLEAN SetAttributes = FALSE;
BOOLEAN HookMemoryTypeSet = FALSE;
DEBUGGER_HOOK_MEMORY_TYPE HookMemoryType = DEBUGGER_MEMORY_HOOK_VIRTUAL_ADDRESS; // by default virtual address
DEBUGGER_EVENT_PARSING_ERROR_CAUSE EventParsingErrorCause;
2021-03-22 18:19:39 +04:30
2024-07-30 18:44:15 +09:00
if (CommandTokens.size() < 4)
2021-03-22 18:19:39 +04:30
{
2024-07-30 18:44:15 +09:00
ShowMessages("incorrect use of the '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
2021-03-22 18:19:39 +04:30
CommandMonitorHelp();
return;
}
2020-08-28 04:03:12 -07:00
2021-03-22 18:19:39 +04:30
//
// Interpret and fill the general event and action fields
//
// We use HIDDEN_HOOK_READ_AND_WRITE here but it might be changed to
2023-07-07 01:21:07 +09:00
// HIDDEN_HOOK_READ or HIDDEN_HOOK_WRITE or other events it is because
// we are not sure what kind event the user need
2021-03-22 18:19:39 +04:30
//
if (!InterpretGeneralEventAndActionsFields(
2024-07-30 18:44:15 +09:00
&CommandTokens,
2023-07-07 01:21:07 +09:00
HIDDEN_HOOK_READ_AND_WRITE_AND_EXECUTE,
2021-03-22 18:19:39 +04:30
&Event,
&EventLength,
&ActionBreakToDebugger,
&ActionBreakToDebuggerLength,
&ActionCustomCode,
&ActionCustomCodeLength,
&ActionScript,
&ActionScriptLength,
&EventParsingErrorCause))
2021-03-22 18:19:39 +04:30
{
return;
}
2020-08-28 04:03:12 -07:00
2021-03-22 18:19:39 +04:30
//
// Interpret command specific details (if any)
//
2024-07-30 18:44:15 +09:00
for (auto Section : CommandTokens)
2021-03-22 18:19:39 +04:30
{
2024-07-30 18:44:15 +09:00
if (CompareLowerCaseStrings(Section, "!monitor"))
2021-03-22 18:19:39 +04:30
{
continue;
}
else if (IsNextLength)
{
2024-07-30 18:44:15 +09:00
if (!ConvertTokenToUInt32(Section, &HookLength))
{
ShowMessages("err, you should enter a valid length\n\n");
return;
}
IsNextLength = FALSE;
LengthAlreadySet = TRUE;
SetTo = TRUE; // No longer need a second address
}
2024-07-30 18:44:15 +09:00
else if (CompareLowerCaseStrings(Section, "r") && !SetAttributes)
2021-03-22 18:19:39 +04:30
{
Event->EventType = HIDDEN_HOOK_READ;
2023-09-18 19:47:13 +09:00
SetAttributes = TRUE;
}
2024-07-30 18:44:15 +09:00
else if (CompareLowerCaseStrings(Section, "w") && !SetAttributes)
2021-03-22 18:19:39 +04:30
{
Event->EventType = HIDDEN_HOOK_WRITE;
2023-09-18 19:47:13 +09:00
SetAttributes = TRUE;
2021-03-22 18:19:39 +04:30
}
2024-07-30 18:44:15 +09:00
else if (CompareLowerCaseStrings(Section, "x") && !SetAttributes)
2023-07-07 01:21:07 +09:00
{
Event->EventType = HIDDEN_HOOK_EXECUTE;
2023-09-18 19:47:13 +09:00
SetAttributes = TRUE;
2023-07-07 01:21:07 +09:00
}
2024-07-30 18:44:15 +09:00
else if ((CompareLowerCaseStrings(Section, "rw") || CompareLowerCaseStrings(Section, "wr")) && !SetAttributes)
2021-03-22 18:19:39 +04:30
{
Event->EventType = HIDDEN_HOOK_READ_AND_WRITE;
2023-09-18 19:47:13 +09:00
SetAttributes = TRUE;
2021-03-22 18:19:39 +04:30
}
2024-07-30 18:44:15 +09:00
else if ((CompareLowerCaseStrings(Section, "rx") || CompareLowerCaseStrings(Section, "xr")) &&
2023-09-18 19:47:13 +09:00
!SetAttributes)
2023-07-07 01:21:07 +09:00
{
Event->EventType = HIDDEN_HOOK_READ_AND_EXECUTE;
2023-09-18 19:47:13 +09:00
SetAttributes = TRUE;
2023-07-07 01:21:07 +09:00
}
2024-07-30 18:44:15 +09:00
else if ((CompareLowerCaseStrings(Section, "wx") || CompareLowerCaseStrings(Section, "xw")) &&
2023-09-18 19:47:13 +09:00
!SetAttributes)
2023-07-07 01:21:07 +09:00
{
Event->EventType = HIDDEN_HOOK_WRITE_AND_EXECUTE;
2023-09-18 19:47:13 +09:00
SetAttributes = TRUE;
2023-07-07 01:21:07 +09:00
}
2024-07-30 18:44:15 +09:00
else if ((CompareLowerCaseStrings(Section, "rwx") ||
CompareLowerCaseStrings(Section, "rxw") ||
CompareLowerCaseStrings(Section, "wrx") ||
CompareLowerCaseStrings(Section, "wxr") ||
CompareLowerCaseStrings(Section, "xrw") ||
CompareLowerCaseStrings(Section, "xwr")) &&
2023-09-18 19:47:13 +09:00
!SetAttributes)
2023-07-07 01:21:07 +09:00
{
Event->EventType = HIDDEN_HOOK_READ_AND_WRITE_AND_EXECUTE;
2023-09-18 19:47:13 +09:00
SetAttributes = TRUE;
2023-07-07 01:21:07 +09:00
}
2024-07-30 18:44:15 +09:00
else if (CompareLowerCaseStrings(Section, "l") && !SetTo && !LengthAlreadySet)
{
IsNextLength = TRUE;
continue;
}
2024-07-30 18:44:15 +09:00
else if (CompareLowerCaseStrings(Section, "va") && !HookMemoryTypeSet)
{
HookMemoryType = DEBUGGER_MEMORY_HOOK_VIRTUAL_ADDRESS;
HookMemoryTypeSet = TRUE;
continue;
}
2024-07-30 18:44:15 +09:00
else if (CompareLowerCaseStrings(Section, "pa") && !HookMemoryTypeSet)
{
HookMemoryType = DEBUGGER_MEMORY_HOOK_PHYSICAL_ADDRESS;
HookMemoryTypeSet = TRUE;
continue;
}
2021-03-22 18:19:39 +04:30
else
{
//
// It's probably address
//
if (!SetFrom)
{
if (!SymbolConvertNameOrExprToAddress(
2024-07-30 18:44:15 +09:00
GetCaseSensitiveStringFromCommandToken(Section),
&OptionalParam1))
2021-03-22 18:19:39 +04:30
{
//
2024-03-17 01:01:22 +09:00
// couldn't resolve or unknown parameter
2021-03-22 18:19:39 +04:30
//
ShowMessages("err, couldn't resolve error at '%s'\n\n",
2024-07-30 18:44:15 +09:00
GetCaseSensitiveStringFromCommandToken(Section).c_str());
2021-03-22 18:19:39 +04:30
CommandMonitorHelp();
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
2021-03-22 18:19:39 +04:30
return;
}
SetFrom = TRUE;
}
else if (!SetTo && !LengthAlreadySet)
2021-03-22 18:19:39 +04:30
{
if (!SymbolConvertNameOrExprToAddress(
2024-07-30 18:44:15 +09:00
GetCaseSensitiveStringFromCommandToken(Section),
&OptionalParam2))
2021-03-22 18:19:39 +04:30
{
//
2024-03-17 01:01:22 +09:00
// Couldn't resolve or unknown parameter
2021-03-22 18:19:39 +04:30
//
ShowMessages("err, couldn't resolve error at '%s'\n\n",
2024-07-30 18:44:15 +09:00
GetCaseSensitiveStringFromCommandToken(Section).c_str());
2021-03-22 18:19:39 +04:30
CommandMonitorHelp();
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
2021-03-22 18:19:39 +04:30
return;
}
SetTo = TRUE;
}
else
{
//
2024-03-17 19:00:14 +09:00
// Unknown parameter
2021-03-22 18:19:39 +04:30
//
2024-07-30 18:44:15 +09:00
ShowMessages("unknown parameter '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(Section).c_str());
2021-03-22 18:19:39 +04:30
CommandMonitorHelp();
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
2021-03-22 18:19:39 +04:30
return;
}
}
}
2021-11-12 23:57:32 +03:30
//
// Check if all parameters are received
//
if (!SetFrom || !SetTo)
{
ShowMessages("please choose the 'from' or 'to' values or specify the length\n");
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
return;
}
//
// Check if user specified the 'l' rather than providing two addresses
//
if (LengthAlreadySet)
{
//
// Because when the user specifies length, the last byte should be ignored
//
OptionalParam2 = OptionalParam1 + HookLength - 1;
}
2021-11-12 23:57:32 +03:30
//
// Check for invalid order of address
//
2021-03-22 18:19:39 +04:30
if (OptionalParam1 > OptionalParam2)
{
//
2021-03-22 18:19:39 +04:30
// 'from' is greater than 'to'
//
ShowMessages("please choose the 'from' value first, then choose the 'to' "
2021-04-18 23:22:28 +04:30
"value\n");
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
return;
}
2020-08-28 04:03:12 -07:00
2021-11-12 23:57:32 +03:30
//
2023-09-18 19:47:13 +09:00
// Check if user set the attributes of !monitor or not
2021-11-12 23:57:32 +03:30
//
2023-09-18 19:47:13 +09:00
if (!SetAttributes)
2021-11-12 23:57:32 +03:30
{
2023-09-18 19:47:13 +09:00
ShowMessages("please specify the attribute(s) that you want to monitor (r, w, x, rw, rx, wx, rwx)\n");
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
2021-11-12 23:57:32 +03:30
return;
}
//
2021-03-22 18:19:39 +04:30
// Set the optional parameters
//
2023-10-14 21:28:08 +09:00
Event->Options.OptionalParam1 = OptionalParam1;
Event->Options.OptionalParam2 = OptionalParam2;
Event->Options.OptionalParam3 = HookMemoryType;
2020-08-28 04:03:12 -07:00
//
2022-05-08 22:13:44 +04:30
// Send the ioctl to the kernel for event registration
2020-08-28 04:03:12 -07:00
//
2021-03-22 18:19:39 +04:30
if (!SendEventToKernel(Event, EventLength))
{
//
// There was an error, probably the handle was not initialized
// we have to free the Action before exit, it is because, we
// already freed the Event and string buffers
//
2022-05-08 22:13:44 +04:30
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
2021-03-22 18:19:39 +04:30
return;
}
2020-05-27 12:09:57 -07:00
2020-08-28 04:03:12 -07:00
//
2021-03-22 18:19:39 +04:30
// Add the event to the kernel
2020-08-28 04:03:12 -07:00
//
if (!RegisterActionToEvent(Event,
ActionBreakToDebugger,
2022-03-13 01:33:21 +03:30
ActionBreakToDebuggerLength,
ActionCustomCode,
ActionCustomCodeLength,
ActionScript,
ActionScriptLength))
2021-03-22 18:19:39 +04:30
{
//
// There was an error
//
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
2021-03-22 18:19:39 +04:30
return;
}
}