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

209 lines
6.9 KiB
C++
Raw Permalink Normal View History

2023-09-13 19:59:55 +09:00
/**
* @file mode.cpp
2023-09-13 19:59:55 +09:00
* @author Sina Karvandi (sina@hyperdbg.org)
2023-11-02 13:48:56 +09:00
* @brief !mode command
2023-09-13 19:59:55 +09:00
* @details
* @version 0.6
* @date 2023-09-13
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
/**
* @brief help of the !mode command
2023-09-13 19:59:55 +09:00
*
* @return VOID
*/
VOID
CommandModeHelp()
2023-09-13 19:59:55 +09:00
{
2023-09-18 19:47:13 +09:00
ShowMessages("!mode : traps (and possibly blocks) the execution of user-mode/kernel-mode instructions.\n\n");
2023-09-13 19:59:55 +09:00
2023-09-18 19:47:13 +09:00
ShowMessages("syntax : \t!mode [Mode (string)] [pid ProcessId (hex)] [core CoreId (hex)] [imm IsImmediate (yesno)] "
"[sc EnableShortCircuiting (onoff)] [buffer PreAllocatedBuffer (hex)] [script { Script (string) }] "
2024-07-19 17:03:43 +09:00
"[asm condition { Condition (assembly/hex) }] [asm code { Code (assembly/hex) }] [output {OutputName (string)}]\n");
2023-09-13 19:59:55 +09:00
ShowMessages("\n");
2023-09-18 19:47:13 +09:00
ShowMessages("\t\te.g : !mode u pid 1c0\n");
ShowMessages("\t\te.g : !mode k pid 1c0\n");
ShowMessages("\t\te.g : !mode ku pid 1c0\n");
ShowMessages("\t\te.g : !mode ku core 2 pid 400\n");
2024-07-20 22:05:39 +09:00
ShowMessages("\t\te.g : !mode u pid 1c0 script { printf(\"kernel -> user transition occurred!\\n\"); }\n");
ShowMessages("\t\te.g : !mode ku pid 1c0 asm code { nop; nop; nop }\n");
2023-09-18 19:47:13 +09:00
ShowMessages("\n");
ShowMessages("note: this event applies to the target process; thus, you need to specify the process id\n");
2023-09-13 19:59:55 +09:00
}
/**
* @brief !mode command handler
2023-09-13 19:59:55 +09:00
*
2024-07-30 18:44:15 +09:00
* @param CommandTokens
* @param Command
2024-07-30 18:44:15 +09:00
*
2023-09-13 19:59:55 +09:00
* @return VOID
*/
VOID
CommandMode(vector<CommandToken> CommandTokens, string Command)
2023-09-13 19:59:55 +09:00
{
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;
DEBUGGER_EVENT_PARSING_ERROR_CAUSE EventParsingErrorCause;
2023-09-18 19:47:13 +09:00
BOOLEAN SetMode = FALSE;
DEBUGGER_EVENT_MODE_TYPE TargetInterceptionMode = DEBUGGER_EVENT_MODE_TYPE_INVALID;
2023-09-13 19:59:55 +09:00
//
// Interpret and fill the general event and action fields
//
//
if (!InterpretGeneralEventAndActionsFields(
2024-07-30 18:44:15 +09:00
&CommandTokens,
TRAP_EXECUTION_MODE_CHANGED,
2023-09-13 19:59:55 +09:00
&Event,
&EventLength,
&ActionBreakToDebugger,
&ActionBreakToDebuggerLength,
&ActionCustomCode,
&ActionCustomCodeLength,
&ActionScript,
&ActionScriptLength,
&EventParsingErrorCause))
{
return;
}
2023-09-18 19:47:13 +09:00
//
// Check here to make sure that the user didn't specified the calling stages for this mode change execution trap
//
if (Event->EventStage != VMM_CALLBACK_CALLING_STAGE_PRE_EVENT_EMULATION)
{
ShowMessages("the utilization of 'post' or 'all' event calling stages is not meaningful "
"for the mode (user-mode/kernel-mode) change traps; therefore, this command does not support them\n");
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
return;
}
2023-09-13 19:59:55 +09:00
//
// Check for size
//
2024-07-30 18:44:15 +09:00
if (CommandTokens.size() > 2)
2023-09-13 19:59:55 +09:00
{
ShowMessages("incorrect use of the '%s'\n\n",
2024-07-30 18:44:15 +09:00
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
CommandModeHelp();
2023-09-13 19:59:55 +09:00
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
return;
}
2023-09-18 19:47:13 +09:00
//
// Interpret command specific details (if any)
//
2024-07-30 18:44:15 +09:00
for (auto Section : CommandTokens)
2023-09-18 19:47:13 +09:00
{
2024-07-30 18:44:15 +09:00
if (CompareLowerCaseStrings(Section, "!mode"))
2023-09-18 19:47:13 +09:00
{
continue;
}
2024-07-30 18:44:15 +09:00
else if (CompareLowerCaseStrings(Section, "u") && !SetMode)
2023-09-18 19:47:13 +09:00
{
TargetInterceptionMode = DEBUGGER_EVENT_MODE_TYPE_USER_MODE;
SetMode = TRUE;
}
2024-07-30 18:44:15 +09:00
else if (CompareLowerCaseStrings(Section, "k") && !SetMode)
2023-09-18 19:47:13 +09:00
{
TargetInterceptionMode = DEBUGGER_EVENT_MODE_TYPE_KERNEL_MODE;
SetMode = TRUE;
}
2024-07-30 18:44:15 +09:00
else if ((CompareLowerCaseStrings(Section, "uk") || CompareLowerCaseStrings(Section, "ku")) && !SetMode)
2023-09-18 19:47:13 +09:00
{
TargetInterceptionMode = DEBUGGER_EVENT_MODE_TYPE_USER_MODE_AND_KERNEL_MODE;
SetMode = TRUE;
}
else
{
//
2024-03-17 01:01:22 +09:00
// Couldn't resolve or unknown parameter
2023-09-18 19:47:13 +09:00
//
ShowMessages("err, couldn't resolve error at '%s'\n\n",
2024-07-30 18:44:15 +09:00
GetCaseSensitiveStringFromCommandToken(Section).c_str());
2023-09-18 19:47:13 +09:00
CommandModeHelp();
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
}
}
//
// Check if user specified the execution mode or not
//
if (!SetMode)
{
ShowMessages("please specify the mode(s) that you want to intercept their execution (u, k, ku)\n");
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
return;
}
//
// Check if user specified the process id or not
//
if (Event->ProcessId == DEBUGGER_EVENT_APPLY_TO_ALL_PROCESSES)
{
ShowMessages("this event only applies to the selected process(es). please specify "
"the 'pid' or the process id of the target process that you want to trap its execution\n");
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
return;
}
//
// Set the first parameter to the required execution mode
//
2023-10-14 21:28:08 +09:00
Event->Options.OptionalParam1 = (UINT64)TargetInterceptionMode;
2023-09-18 19:47:13 +09:00
2023-09-13 19:59:55 +09:00
//
// Send the ioctl to the kernel for event registration
//
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
//
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
return;
}
//
// Add the event to the kernel
//
if (!RegisterActionToEvent(Event,
ActionBreakToDebugger,
ActionBreakToDebuggerLength,
ActionCustomCode,
ActionCustomCodeLength,
ActionScript,
ActionScriptLength))
{
//
// There was an error
//
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
return;
}
}