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

!hide command More...

#include "pch.h"

Functions

VOID CommandHideHelp ()
 help of the !hide command
 
VOID CommandHide (vector< string > SplitCommand, string Command)
 !hide command handler
 

Variables

UINT64 g_CpuidAverage
 The average calculated from the measurements of cpuid '!measure' command.
 
UINT64 g_CpuidStandardDeviation
 The standard deviation calculated from the measurements of cpuid '!measure' command.
 
UINT64 g_CpuidMedian
 The median calculated from the measurements of cpuid '!measure' command.
 
UINT64 g_RdtscAverage
 The average calculated from the measurements of rdtsc/p '!measure' command.
 
UINT64 g_RdtscStandardDeviation
 The standard deviation calculated from the measurements of rdtsc/p '!measure' command.
 
UINT64 g_RdtscMedian
 The median calculated from the measurements of rdtsc/p '!measure' command.
 
BOOLEAN g_TransparentResultsMeasured
 Shows whether the user executed and mesaured '!measure' command or not, it is because we want to use these measurements later in '!hide' command.
 
ACTIVE_DEBUGGING_PROCESS g_ActiveProcessDebuggingState
 State of active debugging thread.
 

Detailed Description

!hide command

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-07-07

Function Documentation

◆ CommandHide()

VOID CommandHide ( vector< string > SplitCommand,
string Command )

!hide command handler

Parameters
SplitCommand
Command
Returns
VOID
61{
62 BOOLEAN Status;
63 ULONG ReturnedLength;
64 UINT64 TargetPid;
65 BOOLEAN TrueIfProcessIdAndFalseIfProcessName;
68 size_t RequestBufferSize = 0;
69
70 if (SplitCommand.size() <= 2 && SplitCommand.size() != 1)
71 {
72 ShowMessages("incorrect use of the '!hide'\n\n");
74 return;
75 }
76
77 //
78 // Find out whether the user enters pid or name
79 //
80 if (SplitCommand.size() == 1)
81 {
83 {
84 TrueIfProcessIdAndFalseIfProcessName = TRUE;
86 }
87 else
88 {
89 //
90 // There is no user-debugging process
91 //
92 ShowMessages("you're not attached to any user-mode process, "
93 "please explicitly specify the process id or process name\n");
94 return;
95 }
96 }
97 else if (!SplitCommand.at(1).compare("pid"))
98 {
99 TrueIfProcessIdAndFalseIfProcessName = TRUE;
100
101 //
102 // Check for the user to not add extra arguments
103 //
104 if (SplitCommand.size() != 3)
105 {
106 ShowMessages("incorrect use of the '!hide'\n\n");
108 return;
109 }
110
111 //
112 // It's just a pid for the process
113 //
114 if (!ConvertStringToUInt64(SplitCommand.at(2), &TargetPid))
115 {
116 ShowMessages("incorrect process id\n\n");
117 return;
118 }
119 }
120 else if (!SplitCommand.at(1).compare("name"))
121 {
122 TrueIfProcessIdAndFalseIfProcessName = FALSE;
123
124 //
125 // Trim the command
126 //
127 Trim(Command);
128
129 //
130 // Remove !hide from it
131 //
132 Command.erase(0, SplitCommand.at(0).size());
133
134 //
135 // remove name + space
136 //
137 Command.erase(0, 4 + 1);
138
139 //
140 // Trim it again
141 //
142 Trim(Command);
143 }
144 else
145 {
146 //
147 // Invalid argument for the second parameter to the command
148 //
149 ShowMessages("incorrect use of the '!hide'\n\n");
151 return;
152 }
153
154 //
155 // Check if the user used !measure or not
156 //
159 {
160 ShowMessages("the average, median and standard deviation is not measured. "
161 "Did you use '!measure' command?\n");
162 return;
163 }
164
165 //
166 // Check if debugger is loaded or not
167 //
169
170 //
171 // We wanna hide the debugger and make transparent vm-exits
172 //
173 HideRequest.IsHide = TRUE;
174
175 //
176 // Set the measured times cpuid
177 //
178 HideRequest.CpuidAverage = g_CpuidAverage;
179 HideRequest.CpuidMedian = g_CpuidMedian;
181
182 //
183 // Set the measured times rdtsc/p
184 //
185 HideRequest.RdtscAverage = g_RdtscAverage;
186 HideRequest.RdtscMedian = g_RdtscMedian;
188
190 TrueIfProcessIdAndFalseIfProcessName;
191
192 if (TrueIfProcessIdAndFalseIfProcessName)
193 {
194 //
195 // It's a process id
196 //
197 HideRequest.ProcId = (UINT32)TargetPid;
198
199 RequestBufferSize = sizeof(DEBUGGER_HIDE_AND_TRANSPARENT_DEBUGGER_MODE);
200 }
201 else
202 {
203 //
204 // It's a process name
205 //
206 HideRequest.LengthOfProcessName = (UINT32)Command.size() + 1;
207 RequestBufferSize = sizeof(DEBUGGER_HIDE_AND_TRANSPARENT_DEBUGGER_MODE) + Command.size() + 1;
208 }
209
210 //
211 // Allocate the requested buffer
212 //
213 FinalRequestBuffer =
214 (PDEBUGGER_HIDE_AND_TRANSPARENT_DEBUGGER_MODE)malloc(RequestBufferSize);
215
216 if (FinalRequestBuffer == NULL)
217 {
218 ShowMessages("insufficient space\n");
219 return;
220 }
221
222 //
223 // Zero the memory
224 //
225 RtlZeroMemory(FinalRequestBuffer, RequestBufferSize);
226
227 //
228 // Copy the buffer on the top of the final buffer
229 // to send the kernel
230 //
231 memcpy(FinalRequestBuffer, &HideRequest, sizeof(DEBUGGER_HIDE_AND_TRANSPARENT_DEBUGGER_MODE));
232
233 //
234 // If it's a name then we should add it to the end of the buffer
235 //
236 memcpy(((UINT64 *)((UINT64)FinalRequestBuffer +
238 Command.c_str(),
239 Command.size());
240
241 //
242 // Send the request to the kernel
243 //
244
245 Status = DeviceIoControl(
246 g_DeviceHandle, // Handle to device
248 // code
249 FinalRequestBuffer, // Input Buffer to driver.
250 (DWORD)RequestBufferSize, // Input buffer length
251 FinalRequestBuffer, // Output Buffer from driver.
253 // buffer in bytes.
254 &ReturnedLength, // Bytes placed in buffer.
255 NULL // synchronous call
256 );
257
258 if (!Status)
259 {
260 ShowMessages("ioctl failed with code 0x%x\n", GetLastError());
261 free(FinalRequestBuffer);
262 return;
263 }
264
265 if (FinalRequestBuffer->KernelStatus == DEBUGGER_OPERATION_WAS_SUCCESSFUL)
266 {
267 ShowMessages("transparent debugging successfully enabled :)\n");
268 }
269 else if (FinalRequestBuffer->KernelStatus ==
271 {
272 ShowMessages("unable to hide the debugger (transparent-debugging) :(\n");
273 free(FinalRequestBuffer);
274 return;
275 }
276 else
277 {
278 ShowMessages("unknown error occurred :(\n");
279 free(FinalRequestBuffer);
280 return;
281 }
282
283 //
284 // free the buffer
285 //
286 free(FinalRequestBuffer);
287}
UCHAR BOOLEAN
Definition BasicTypes.h:39
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54
unsigned __int64 UINT64
Definition BasicTypes.h:21
unsigned long DWORD
Definition BasicTypes.h:22
unsigned int UINT32
Definition BasicTypes.h:48
unsigned long ULONG
Definition BasicTypes.h:37
#define DEBUGGER_ERROR_UNABLE_TO_HIDE_OR_UNHIDE_DEBUGGER
error, unable to hide the debugger and enter to transparent-mode
Definition ErrorCodes.h:87
#define DEBUGGER_OPERATION_WAS_SUCCESSFUL
General value to indicate that the operation or request was successful.
Definition ErrorCodes.h:23
#define IOCTL_DEBUGGER_HIDE_AND_UNHIDE_TO_TRANSPARENT_THE_DEBUGGER
ioctl, request to enable or disable transparent-mode
Definition Ioctls.h:120
struct _DEBUGGER_HIDE_AND_TRANSPARENT_DEBUGGER_MODE * PDEBUGGER_HIDE_AND_TRANSPARENT_DEBUGGER_MODE
#define SIZEOF_DEBUGGER_HIDE_AND_TRANSPARENT_DEBUGGER_MODE
Definition RequestStructures.h:541
struct _DEBUGGER_HIDE_AND_TRANSPARENT_DEBUGGER_MODE DEBUGGER_HIDE_AND_TRANSPARENT_DEBUGGER_MODE
request for enable or disable transparent-mode
BOOLEAN ConvertStringToUInt64(string TextToConvert, PUINT64 Result)
check and convert string to a 64 bit unsigned integer and also check for special notations like 0x,...
Definition common.cpp:240
void Trim(std::string &s)
trim from both ends and start of a string (in place)
Definition common.cpp:594
UINT64 g_CpuidStandardDeviation
The standard deviation calculated from the measurements of cpuid '!measure' command.
Definition globals.h:532
UINT64 g_RdtscAverage
The average calculated from the measurements of rdtsc/p '!measure' command.
Definition globals.h:544
ACTIVE_DEBUGGING_PROCESS g_ActiveProcessDebuggingState
State of active debugging thread.
Definition globals.h:362
UINT64 g_RdtscMedian
The median calculated from the measurements of rdtsc/p '!measure' command.
Definition globals.h:556
BOOLEAN g_TransparentResultsMeasured
Shows whether the user executed and mesaured '!measure' command or not, it is because we want to use ...
Definition globals.h:520
VOID CommandHideHelp()
help of the !hide command
Definition hide.cpp:34
UINT64 g_CpuidMedian
The median calculated from the measurements of cpuid '!measure' command.
Definition globals.h:538
UINT64 g_RdtscStandardDeviation
The standard deviation calculated from the measurements of rdtsc/p '!measure' command.
Definition globals.h:550
UINT64 g_CpuidAverage
The average calculated from the measurements of cpuid '!measure' command.
Definition globals.h:526
#define AssertShowMessageReturnStmt(expr, message, rc)
Definition common.h:51
#define AssertReturn
Definition common.h:19
#define ASSERT_MESSAGE_DRIVER_NOT_LOADED
Definition common.h:25
HANDLE g_DeviceHandle
Holds the global handle of device which is used to send the request to the kernel by IOCTL,...
Definition globals.h:471
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96
UINT32 ProcessId
Definition ud.h:51
BOOLEAN IsActive
Definition ud.h:49
request for enable or disable transparent-mode
Definition RequestStructures.h:549
UINT64 CpuidStandardDeviation
Definition RequestStructures.h:553
UINT64 RdtscStandardDeviation
Definition RequestStructures.h:557
UINT64 CpuidAverage
Definition RequestStructures.h:552
UINT64 KernelStatus
Definition RequestStructures.h:565
UINT64 RdtscAverage
Definition RequestStructures.h:556
BOOLEAN IsHide
Definition RequestStructures.h:550
UINT64 CpuidMedian
Definition RequestStructures.h:554
UINT64 RdtscMedian
Definition RequestStructures.h:558
UINT32 LengthOfProcessName
Definition RequestStructures.h:562
BOOLEAN TrueIfProcessIdAndFalseIfProcessName
Definition RequestStructures.h:560
UINT32 ProcId
Definition RequestStructures.h:561

◆ CommandHideHelp()

VOID CommandHideHelp ( )

help of the !hide command

Returns
VOID
35{
36 ShowMessages("!hide : tries to make HyperDbg transparent from anti-debugging "
37 "and anti-hypervisor methods.\n\n");
38
39 ShowMessages("syntax : \t!hide\n");
40 ShowMessages("syntax : \t!hide [pid ProcessId (hex)]\n");
41 ShowMessages("syntax : \t!hide [name ProcessName (string)]\n");
42
43 ShowMessages("note : \tprocess names are case sensitive and you can use "
44 "this command multiple times.\n");
45
46 ShowMessages("\n");
47 ShowMessages("\t\te.g : !hide\n");
48 ShowMessages("\t\te.g : !hide pid b60 \n");
49 ShowMessages("\t\te.g : !hide name procexp.exe\n");
50}

Variable Documentation

◆ g_ActiveProcessDebuggingState

ACTIVE_DEBUGGING_PROCESS g_ActiveProcessDebuggingState
extern

State of active debugging thread.

362{0};

◆ g_CpuidAverage

UINT64 g_CpuidAverage
extern

The average calculated from the measurements of cpuid '!measure' command.

◆ g_CpuidMedian

UINT64 g_CpuidMedian
extern

The median calculated from the measurements of cpuid '!measure' command.

◆ g_CpuidStandardDeviation

UINT64 g_CpuidStandardDeviation
extern

The standard deviation calculated from the measurements of cpuid '!measure' command.

◆ g_RdtscAverage

UINT64 g_RdtscAverage
extern

The average calculated from the measurements of rdtsc/p '!measure' command.

◆ g_RdtscMedian

UINT64 g_RdtscMedian
extern

The median calculated from the measurements of rdtsc/p '!measure' command.

◆ g_RdtscStandardDeviation

UINT64 g_RdtscStandardDeviation
extern

The standard deviation calculated from the measurements of rdtsc/p '!measure' command.

◆ g_TransparentResultsMeasured

BOOLEAN g_TransparentResultsMeasured
extern

Shows whether the user executed and mesaured '!measure' command or not, it is because we want to use these measurements later in '!hide' command.