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

!crwrite command More...

#include "pch.h"

Functions

VOID CommandCrwriteHelp ()
 help of the !crwrite command
VOID CommandCrwrite (vector< CommandToken > CommandTokens, string Command)
 !crwrite command handler

Detailed Description

!crwrite command

Author
angel_killah
Version
0.1
Date
2022-07-05

Function Documentation

◆ CommandCrwrite()

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

!crwrite command handler

Parameters
CommandTokens
Command
Returns
VOID
48{
50 PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger = NULL;
51 PDEBUGGER_GENERAL_ACTION ActionCustomCode = NULL;
52 PDEBUGGER_GENERAL_ACTION ActionScript = NULL;
53 UINT32 EventLength;
54 UINT32 ActionBreakToDebuggerLength = 0;
55 UINT32 ActionCustomCodeLength = 0;
56 UINT32 ActionScriptLength = 0;
57 UINT64 TargetRegister = 1;
58 UINT64 MaskRegister = 0xFFFFFFFFFFFFFFFF;
59 BOOLEAN GetRegister = FALSE;
60 BOOLEAN GetMask = FALSE;
61 DEBUGGER_EVENT_PARSING_ERROR_CAUSE EventParsingErrorCause;
62
63 if (CommandTokens.size() < 2)
64 {
65 ShowMessages("incorrect use of the '%s'\n\n",
66 GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
68 return;
69 }
70
71 //
72 // Interpret and fill the general event and action fields
73 //
74 //
76 &CommandTokens,
78 &Event,
79 &EventLength,
80 &ActionBreakToDebugger,
81 &ActionBreakToDebuggerLength,
82 &ActionCustomCode,
83 &ActionCustomCodeLength,
84 &ActionScript,
85 &ActionScriptLength,
86 &EventParsingErrorCause))
87 {
88 return;
89 }
90
91 //
92 // Interpret command specific details (if any)
93 //
94 for (auto Section : CommandTokens)
95 {
96 if (CompareLowerCaseStrings(Section, "!crwrite"))
97 {
98 continue;
99 }
100 else if (!GetRegister)
101 {
102 //
103 // It's probably a CR
104 //
105 if (!ConvertTokenToUInt64(Section, &TargetRegister))
106 {
107 //
108 // Unknown parameter
109 //
110 ShowMessages("unknown parameter '%s'\n\n",
113
114 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
115 return;
116 }
117 else
118 {
119 GetRegister = TRUE;
120 }
121 }
122 else if (!GetMask)
123 {
124 if (!ConvertTokenToUInt64(Section, &MaskRegister))
125 {
126 //
127 // Unknown parameter
128 //
129 ShowMessages("unknown parameter '%s'\n\n",
132
133 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
134 return;
135 }
136 else
137 {
138 GetMask = TRUE;
139 }
140 }
141 else
142 {
143 //
144 // Unknown parameter
145 //
146 ShowMessages("unknown parameter '%s'\n\n",
149
150 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
151 return;
152 }
153 }
154
155 if (TargetRegister != VMX_EXIT_QUALIFICATION_REGISTER_CR0 && TargetRegister != VMX_EXIT_QUALIFICATION_REGISTER_CR4)
156 {
157 ShowMessages("please choose either 0 for cr0 or 4 for cr4\n");
159
160 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
161 return;
162 }
163
164 //
165 // Set the target control register
166 //
167 Event->Options.OptionalParam1 = TargetRegister;
168
169 //
170 // Set the mask to filter control register bits
171 //
172 Event->Options.OptionalParam2 = MaskRegister;
173
174 //
175 // Send the ioctl to the kernel for event registration
176 //
177 if (!SendEventToKernel(Event, EventLength))
178 {
179 //
180 // There was an error, probably the handle was not initialized
181 // we have to free the Action before exit, it is because, we
182 // already freed the Event and string buffers
183 //
184 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
185 return;
186 }
187
188 //
189 // Add the event to the kernel
190 //
191 if (!RegisterActionToEvent(Event,
192 ActionBreakToDebugger,
193 ActionBreakToDebuggerLength,
194 ActionCustomCode,
195 ActionCustomCodeLength,
196 ActionScript,
197 ActionScriptLength))
198 {
199 //
200 // There was an error
201 //
202 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
203 return;
204 }
205}
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
@ CONTROL_REGISTER_MODIFIED
Definition Events.h:162
struct _DEBUGGER_GENERAL_EVENT_DETAIL * PDEBUGGER_GENERAL_EVENT_DETAIL
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
BOOLEAN ConvertTokenToUInt64(CommandToken TargetToken, PUINT64 Result)
add ` between 64 bit values and convert them to string
Definition common.cpp:447
VOID CommandCrwriteHelp()
help of the !crwrite command
Definition crwrite.cpp:20
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.
NULL()
Definition test-case-generator.py:530
UINT64 OptionalParam2
Definition Events.h:278
UINT64 OptionalParam1
Definition Events.h:277
DEBUGGER_EVENT_OPTIONS Options
Definition Events.h:396

◆ CommandCrwriteHelp()

VOID CommandCrwriteHelp ( )

help of the !crwrite command

Returns
VOID
21{
22 ShowMessages("!crwrite : monitors modification of control registers (CR0 / CR4).\n\n");
23
24 ShowMessages("syntax : \t!crwrite [Cr (hex)] [mask Mask (hex)] [pid ProcessId (hex)] "
25 "[core CoreId (hex)] [imm IsImmediate (yesno)] [sc EnableShortCircuiting (onoff)] "
26 "[stage CallingStage (prepostall)] [buffer PreAllocatedBuffer (hex)] [script { Script (string) }] "
27 "[asm condition { Condition (assembly/hex) }] [asm code { Code (assembly/hex) }] [output {OutputName (string)}]\n");
28
29 ShowMessages("\n");
30 ShowMessages("\t\te.g : !crwrite 0\n");
31 ShowMessages("\t\te.g : !crwrite 0 0x10000\n");
32 ShowMessages("\t\te.g : !crwrite 4 pid 400\n");
33 ShowMessages("\t\te.g : !crwrite 4 core 2 pid 400\n");
34 ShowMessages("\t\te.g : !crwrite 4 script { printf(\"4th control register is modified at: %%llx\\n\", @rip); }\n");
35 ShowMessages("\t\te.g : !crwrite 4 asm code { nop; nop; nop }\n");
36}