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

!mode command More...

#include "pch.h"

Functions

VOID CommandModeHelp ()
 help of the !mode command
VOID CommandMode (vector< CommandToken > CommandTokens, string Command)
 !mode command handler

Detailed Description

!mode command

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.6
Date
2023-09-13

Function Documentation

◆ CommandMode()

VOID CommandMode ( vector< CommandToken > CommandTokens,
string Command )

!mode command handler

Parameters
CommandTokens
Command
Returns
VOID
50{
52 PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger = NULL;
53 PDEBUGGER_GENERAL_ACTION ActionCustomCode = NULL;
54 PDEBUGGER_GENERAL_ACTION ActionScript = NULL;
55 UINT32 EventLength;
56 UINT32 ActionBreakToDebuggerLength = 0;
57 UINT32 ActionCustomCodeLength = 0;
58 UINT32 ActionScriptLength = 0;
59 DEBUGGER_EVENT_PARSING_ERROR_CAUSE EventParsingErrorCause;
60 BOOLEAN SetMode = FALSE;
62
63 //
64 // Interpret and fill the general event and action fields
65 //
66 //
68 &CommandTokens,
70 &Event,
71 &EventLength,
72 &ActionBreakToDebugger,
73 &ActionBreakToDebuggerLength,
74 &ActionCustomCode,
75 &ActionCustomCodeLength,
76 &ActionScript,
77 &ActionScriptLength,
78 &EventParsingErrorCause))
79 {
80 return;
81 }
82
83 //
84 // Check here to make sure that the user didn't specified the calling stages for this mode change execution trap
85 //
87 {
88 ShowMessages("the utilization of 'post' or 'all' event calling stages is not meaningful "
89 "for the mode (user-mode/kernel-mode) change traps; therefore, this command does not support them\n");
90
91 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
92 return;
93 }
94
95 //
96 // Check for size
97 //
98 if (CommandTokens.size() > 2)
99 {
100 ShowMessages("incorrect use of the '%s'\n\n",
101 GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
102
104
105 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
106 return;
107 }
108
109 //
110 // Interpret command specific details (if any)
111 //
112 for (auto Section : CommandTokens)
113 {
114 if (CompareLowerCaseStrings(Section, "!mode"))
115 {
116 continue;
117 }
118 else if (CompareLowerCaseStrings(Section, "u") && !SetMode)
119 {
120 TargetInterceptionMode = DEBUGGER_EVENT_MODE_TYPE_USER_MODE;
121 SetMode = TRUE;
122 }
123 else if (CompareLowerCaseStrings(Section, "k") && !SetMode)
124 {
125 TargetInterceptionMode = DEBUGGER_EVENT_MODE_TYPE_KERNEL_MODE;
126 SetMode = TRUE;
127 }
128 else if ((CompareLowerCaseStrings(Section, "uk") || CompareLowerCaseStrings(Section, "ku")) && !SetMode)
129 {
131 SetMode = TRUE;
132 }
133 else
134 {
135 //
136 // Couldn't resolve or unknown parameter
137 //
138 ShowMessages("err, couldn't resolve error at '%s'\n\n",
140
142
143 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
144 }
145 }
146
147 //
148 // Check if user specified the execution mode or not
149 //
150 if (!SetMode)
151 {
152 ShowMessages("please specify the mode(s) that you want to intercept their execution (u, k, ku)\n");
153
154 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
155 return;
156 }
157
158 //
159 // Check if user specified the process id or not
160 //
162 {
163 ShowMessages("this event only applies to the selected process(es). please specify "
164 "the 'pid' or the process id of the target process that you want to trap its execution\n");
165
166 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
167 return;
168 }
169
170 //
171 // Set the first parameter to the required execution mode
172 //
173 Event->Options.OptionalParam1 = (UINT64)TargetInterceptionMode;
174
175 //
176 // Send the ioctl to the kernel for event registration
177 //
178 if (!SendEventToKernel(Event, EventLength))
179 {
180 //
181 // There was an error, probably the handle was not initialized
182 // we have to free the Action before exit, it is because, we
183 // already freed the Event and string buffers
184 //
185
186 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
187 return;
188 }
189
190 //
191 // Add the event to the kernel
192 //
193 if (!RegisterActionToEvent(Event,
194 ActionBreakToDebugger,
195 ActionBreakToDebuggerLength,
196 ActionCustomCode,
197 ActionCustomCodeLength,
198 ActionScript,
199 ActionScriptLength))
200 {
201 //
202 // There was an error
203 //
204
205 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
206 return;
207 }
208}
UCHAR BOOLEAN
Definition BasicTypes.h:35
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
unsigned int UINT32
Definition BasicTypes.h:54
#define DEBUGGER_EVENT_APPLY_TO_ALL_PROCESSES
Apply the event to all the processes.
Definition Constants.h:745
@ VMM_CALLBACK_CALLING_STAGE_PRE_EVENT_EMULATION
Definition DataTypes.h:126
@ TRAP_EXECUTION_MODE_CHANGED
Definition Events.h:169
@ DEBUGGER_EVENT_MODE_TYPE_KERNEL_MODE
Definition Events.h:210
@ DEBUGGER_EVENT_MODE_TYPE_USER_MODE_AND_KERNEL_MODE
Definition Events.h:208
@ DEBUGGER_EVENT_MODE_TYPE_INVALID
Definition Events.h:211
@ DEBUGGER_EVENT_MODE_TYPE_USER_MODE
Definition Events.h:209
struct _DEBUGGER_GENERAL_EVENT_DETAIL * PDEBUGGER_GENERAL_EVENT_DETAIL
enum _DEBUGGER_EVENT_MODE_TYPE DEBUGGER_EVENT_MODE_TYPE
Type of mode change traps.
struct _DEBUGGER_GENERAL_ACTION * PDEBUGGER_GENERAL_ACTION
std::string GetCaseSensitiveStringFromCommandToken(CommandToken TargetToken)
Get case sensitive string from command token.
Definition common.cpp:467
BOOLEAN CompareLowerCaseStrings(CommandToken TargetToken, const CHAR *StringToCompare)
Compare lower case strings.
Definition common.cpp:503
VOID FreeEventsAndActionsMemory(PDEBUGGER_GENERAL_EVENT_DETAIL Event, PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger, PDEBUGGER_GENERAL_ACTION ActionCustomCode, PDEBUGGER_GENERAL_ACTION ActionScript)
Deallocate buffers relating to events and actions.
Definition debugger.cpp:1688
BOOLEAN InterpretGeneralEventAndActionsFields(vector< CommandToken > *CommandTokens, VMM_EVENT_TYPE_ENUM EventType, PDEBUGGER_GENERAL_EVENT_DETAIL *EventDetailsToFill, PUINT32 EventBufferLength, PDEBUGGER_GENERAL_ACTION *ActionDetailsToFillBreakToDebugger, PUINT32 ActionBufferLengthBreakToDebugger, PDEBUGGER_GENERAL_ACTION *ActionDetailsToFillCustomCode, PUINT32 ActionBufferLengthCustomCode, PDEBUGGER_GENERAL_ACTION *ActionDetailsToFillScript, PUINT32 ActionBufferLengthScript, PDEBUGGER_EVENT_PARSING_ERROR_CAUSE ReasonForErrorInParsing)
Interpret general event fields.
Definition debugger.cpp:1736
BOOLEAN SendEventToKernel(PDEBUGGER_GENERAL_EVENT_DETAIL Event, UINT32 EventBufferLength)
Register the event to the kernel.
Definition debugger.cpp:1367
BOOLEAN RegisterActionToEvent(PDEBUGGER_GENERAL_EVENT_DETAIL Event, PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger, UINT32 ActionBreakToDebuggerLength, PDEBUGGER_GENERAL_ACTION ActionCustomCode, UINT32 ActionCustomCodeLength, PDEBUGGER_GENERAL_ACTION ActionScript, UINT32 ActionScriptLength)
Register the action to the event.
Definition debugger.cpp:1483
enum _DEBUGGER_EVENT_PARSING_ERROR_CAUSE DEBUGGER_EVENT_PARSING_ERROR_CAUSE
Reason for error in parsing commands.
VOID CommandModeHelp()
help of the !mode command
Definition mode.cpp:20
NULL()
Definition test-case-generator.py:530
UINT64 OptionalParam1
Definition Events.h:277
DEBUGGER_EVENT_OPTIONS Options
Definition Events.h:396
VMM_CALLBACK_EVENT_CALLING_STAGE_TYPE EventStage
Definition Events.h:374
UINT32 ProcessId
Definition Events.h:365

◆ CommandModeHelp()

VOID CommandModeHelp ( )

help of the !mode command

Returns
VOID
21{
22 ShowMessages("!mode : traps (and possibly blocks) the execution of user-mode/kernel-mode instructions.\n\n");
23
24 ShowMessages("syntax : \t!mode [Mode (string)] [pid ProcessId (hex)] [core CoreId (hex)] [imm IsImmediate (yesno)] "
25 "[sc EnableShortCircuiting (onoff)] [buffer PreAllocatedBuffer (hex)] [script { Script (string) }] "
26 "[asm condition { Condition (assembly/hex) }] [asm code { Code (assembly/hex) }] [output {OutputName (string)}]\n");
27
28 ShowMessages("\n");
29 ShowMessages("\t\te.g : !mode u pid 1c0\n");
30 ShowMessages("\t\te.g : !mode k pid 1c0\n");
31 ShowMessages("\t\te.g : !mode ku pid 1c0\n");
32 ShowMessages("\t\te.g : !mode ku core 2 pid 400\n");
33 ShowMessages("\t\te.g : !mode u pid 1c0 script { printf(\"kernel -> user transition occurred!\\n\"); }\n");
34 ShowMessages("\t\te.g : !mode ku pid 1c0 asm code { nop; nop; nop }\n");
35
36 ShowMessages("\n");
37 ShowMessages("note: this event applies to the target process; thus, you need to specify the process id\n");
38}