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

.debug command More...

#include "pch.h"

Functions

VOID CommandDebugHelp ()
 help of the .debug command
 
BOOLEAN CommandDebugCheckComPort (const CHAR *ComPort, UINT32 *Port)
 Check if COM port is valid or not.
 
BOOLEAN CommandDebugCheckBaudrate (DWORD Baudrate)
 Check if baud rate is valid or not.
 
BOOLEAN HyperDbgDebugRemoteDeviceUsingComPort (const CHAR *PortName, DWORD Baudrate)
 Connect to a remote serial device (Debugger)
 
BOOLEAN HyperDbgDebugRemoteDeviceUsingNamedPipe (const CHAR *NamedPipe)
 Connect to a remote named pipe (Debugger)
 
BOOLEAN HyperDbgDebugCurrentDeviceUsingComPort (const CHAR *PortName, DWORD Baudrate)
 Connect to a remote serial device (Debuggee)
 
VOID CommandDebug (vector< string > SplitCommand, string Command)
 .debug command handler
 

Variables

HANDLE g_SerialListeningThreadHandle
 In debuggee and debugger, we save the handle of the user-mode listening thread for pauses here.
 
HANDLE g_SerialRemoteComPortHandle
 In debugger (not debuggee), we save the handle of the user-mode listening thread for remote system here.
 
BOOLEAN g_IsSerialConnectedToRemoteDebuggee
 Shows if the debugger was connected to remote debuggee over (A remote guest)
 
BOOLEAN g_IsSerialConnectedToRemoteDebugger
 Shows if the debugger was connected to remote debugger (A remote host)
 
BOOLEAN g_IsDebuggeeRunning
 Shows if the debuggee is running or not.
 

Detailed Description

.debug command

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

Function Documentation

◆ CommandDebug()

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

.debug command handler

Parameters
SplitCommand
Command
Returns
VOID
211{
212 UINT32 Baudrate;
213 UINT32 Port;
214
215 if (SplitCommand.size() == 2 && !SplitCommand.at(1).compare("close"))
216 {
217 //
218 // Check if the debugger is attached to a debuggee
219 //
221 {
223 }
224 else
225 {
227 "err, debugger is not attached to any instance of debuggee\n");
228 }
229 return;
230 }
231 else if (SplitCommand.size() <= 3)
232 {
233 ShowMessages("incorrect use of the '.debug'\n\n");
235 return;
236 }
237
238 //
239 // Check the main command
240 //
241 if (!SplitCommand.at(1).compare("remote"))
242 {
243 //
244 // in the case of the 'remote'
245 //
246
247 if (!SplitCommand.at(2).compare("serial"))
248 {
249 //
250 // Connect to a remote serial device
251 //
252 if (SplitCommand.size() != 5)
253 {
254 ShowMessages("incorrect use of the '.debug'\n\n");
256 return;
257 }
258
259 //
260 // Set baudrate
261 //
262 if (!IsNumber(SplitCommand.at(3)))
263 {
264 //
265 // Unknown parameter
266 //
267 ShowMessages("unknown parameter '%s'\n\n",
268 SplitCommand.at(3).c_str());
270 return;
271 }
272
273 Baudrate = stoi(SplitCommand.at(3));
274
275 //
276 // Check if baudrate is valid or not
277 //
278 if (!CommandDebugCheckBaudrate(Baudrate))
279 {
280 //
281 // Baud-rate is invalid
282 //
283 ShowMessages("err, baud rate is invalid\n\n");
285 return;
286 }
287
288 //
289 // check if com port address is valid or not
290 //
291 if (!CommandDebugCheckComPort(SplitCommand.at(4).c_str(), &Port))
292 {
293 //
294 // com port is invalid
295 //
296 ShowMessages("err, COM port is invalid\n\n");
298 return;
299 }
300
301 //
302 // Everything is okay, connect to the remote machine to send (debugger)
303 //
304 HyperDbgDebugRemoteDeviceUsingComPort(SplitCommand.at(4).c_str(), Baudrate);
305 }
306 else if (!SplitCommand.at(2).compare("namedpipe"))
307 {
308 //
309 // Connect to a remote namedpipe
310 //
311 string Delimiter = "namedpipe";
312 string Token = Command.substr(
313 Command.find(Delimiter) + Delimiter.size() + 1,
314 Command.size());
315
316 //
317 // Connect to a namedpipe (it's probably a Virtual Machine debugging)
318 //
320 }
321 else
322 {
323 //
324 // Unknown parameter
325 //
326 ShowMessages("unknown parameter '%s'\n\n", SplitCommand.at(2).c_str());
328 return;
329 }
330 }
331 else if (!SplitCommand.at(1).compare("prepare"))
332 {
333 if (SplitCommand.size() != 5)
334 {
335 ShowMessages("incorrect use of the '.debug'\n\n");
337 return;
338 }
339
340 //
341 // in the case of the 'prepare'
342 // currently we only support serial
343 //
344 if (!SplitCommand.at(2).compare("serial"))
345 {
346 //
347 // Set baudrate
348 //
349 if (!IsNumber(SplitCommand.at(3)))
350 {
351 //
352 // Unknown parameter
353 //
354 ShowMessages("unknown parameter '%s'\n\n",
355 SplitCommand.at(3).c_str());
357 return;
358 }
359
360 Baudrate = stoi(SplitCommand.at(3));
361
362 //
363 // Check if baudrate is valid or not
364 //
365 if (!CommandDebugCheckBaudrate(Baudrate))
366 {
367 //
368 // Baud-rate is invalid
369 //
370 ShowMessages("err, baud rate is invalid\n\n");
372 return;
373 }
374
375 //
376 // check if com port address is valid or not
377 //
378 if (!CommandDebugCheckComPort(SplitCommand.at(4).c_str(), &Port))
379 {
380 //
381 // com port is invalid
382 //
383 ShowMessages("err, COM port is invalid\n\n");
385 return;
386 }
387
388 //
389 // Everything is okay, prepare to send (debuggee)
390 //
391 HyperDbgDebugCurrentDeviceUsingComPort(SplitCommand.at(4).c_str(), Baudrate);
392 }
393 else
394 {
395 ShowMessages("invalid parameter '%s'\n\n", SplitCommand.at(2));
397 return;
398 }
399 }
400 else
401 {
402 ShowMessages("invalid parameter '%s'\n\n", SplitCommand.at(1));
404 return;
405 }
406}
unsigned int UINT32
Definition BasicTypes.h:48
BOOLEAN IsNumber(const string &str)
check if given string is a numeric string or not
Definition common.cpp:145
BOOLEAN HyperDbgDebugRemoteDeviceUsingNamedPipe(const CHAR *NamedPipe)
Connect to a remote named pipe (Debugger)
Definition debug.cpp:156
BOOLEAN HyperDbgDebugRemoteDeviceUsingComPort(const CHAR *PortName, DWORD Baudrate)
Connect to a remote serial device (Debugger)
Definition debug.cpp:116
BOOLEAN CommandDebugCheckComPort(const CHAR *ComPort, UINT32 *Port)
Check if COM port is valid or not.
Definition debug.cpp:60
BOOLEAN HyperDbgDebugCurrentDeviceUsingComPort(const CHAR *PortName, DWORD Baudrate)
Connect to a remote serial device (Debuggee)
Definition debug.cpp:170
BOOLEAN CommandDebugCheckBaudrate(DWORD Baudrate)
Check if baud rate is valid or not.
Definition debug.cpp:93
VOID CommandDebugHelp()
help of the .debug command
Definition debug.cpp:29
BOOLEAN g_IsSerialConnectedToRemoteDebuggee
Shows if the debugger was connected to remote debuggee over (A remote guest)
Definition globals.h:231
BOOLEAN KdCloseConnection()
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96

◆ CommandDebugCheckBaudrate()

BOOLEAN CommandDebugCheckBaudrate ( DWORD Baudrate)

Check if baud rate is valid or not.

Parameters
Baudrate
Returns
BOOLEAN
94{
95 if (Baudrate == CBR_110 || Baudrate == CBR_300 || Baudrate == CBR_600 ||
96 Baudrate == CBR_1200 || Baudrate == CBR_2400 || Baudrate == CBR_4800 ||
97 Baudrate == CBR_9600 || Baudrate == CBR_14400 || Baudrate == CBR_19200 ||
98 Baudrate == CBR_38400 || Baudrate == CBR_56000 || Baudrate == CBR_57600 ||
99 Baudrate == CBR_115200 || Baudrate == CBR_128000 ||
100 Baudrate == CBR_256000)
101 {
102 return TRUE;
103 }
104 return FALSE;
105}
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54
#define CBR_56000
Definition SerialConnection.h:81
#define CBR_300
Definition SerialConnection.h:72
#define CBR_57600
Definition SerialConnection.h:82
#define CBR_115200
Definition SerialConnection.h:83
#define CBR_2400
Definition SerialConnection.h:75
#define CBR_600
Definition SerialConnection.h:73
#define CBR_128000
Definition SerialConnection.h:84
#define CBR_1200
Definition SerialConnection.h:74
#define CBR_14400
Definition SerialConnection.h:78
#define CBR_19200
Definition SerialConnection.h:79
#define CBR_256000
Definition SerialConnection.h:85
#define CBR_4800
Definition SerialConnection.h:76
#define CBR_38400
Definition SerialConnection.h:80
#define CBR_9600
Definition SerialConnection.h:77
#define CBR_110
Definition SerialConnection.h:71

◆ CommandDebugCheckComPort()

BOOLEAN CommandDebugCheckComPort ( const CHAR * ComPort,
UINT32 * Port )

Check if COM port is valid or not.

Parameters
ComPort
Returns
BOOLEAN
61{
62 if (_stricmp(ComPort, "com1") == 0)
63 {
64 *Port = COM1_PORT;
65 return TRUE;
66 }
67 else if (_stricmp(ComPort, "com2") == 0)
68 {
69 *Port = COM2_PORT;
70 return TRUE;
71 }
72 else if (_stricmp(ComPort, "com3") == 0)
73 {
74 *Port = COM3_PORT;
75 return TRUE;
76 }
77 else if (_stricmp(ComPort, "com4") == 0)
78 {
79 *Port = COM4_PORT;
80 return TRUE;
81 }
82
83 return FALSE;
84}
#define COM4_PORT
Definition SerialConnection.h:93
#define COM3_PORT
Definition SerialConnection.h:92
#define COM2_PORT
Definition SerialConnection.h:91
#define COM1_PORT
Definition SerialConnection.h:90

◆ CommandDebugHelp()

VOID CommandDebugHelp ( )

help of the .debug command

Returns
VOID
30{
32 ".debug : debugs a target machine or makes this machine a debuggee.\n\n");
33
35 "syntax : \t.debug [remote] [serial|namedpipe] [Baudrate (decimal)] [Address (string)]\n");
37 "syntax : \t.debug [prepare] [serial] [Baudrate (decimal)] [Address (string)]\n");
38 ShowMessages("syntax : \t.debug [close]\n");
39
40 ShowMessages("\n");
41 ShowMessages("\t\te.g : .debug remote serial 115200 com2\n");
42 ShowMessages("\t\te.g : .debug remote namedpipe \\\\.\\pipe\\HyperDbgPipe\n");
43 ShowMessages("\t\te.g : .debug prepare serial 115200 com1\n");
44 ShowMessages("\t\te.g : .debug prepare serial 115200 com2\n");
45 ShowMessages("\t\te.g : .debug close\n");
46
48 "\nvalid baud rates (decimal) : 110, 300, 600, 1200, 2400, 4800, 9600, "
49 "14400, 19200, 38400, 56000, 57600, 115200, 128000, 256000\n");
50 ShowMessages("valid COM ports : COM1, COM2, COM3, COM4 \n");
51}

◆ HyperDbgDebugCurrentDeviceUsingComPort()

BOOLEAN HyperDbgDebugCurrentDeviceUsingComPort ( const CHAR * PortName,
DWORD Baudrate )

Connect to a remote serial device (Debuggee)

Parameters
PortName
Baudrate
Returns
BOOLEAN
171{
172 UINT32 Port;
173
174 //
175 // Check if baudrate is valid or not
176 //
177 if (!CommandDebugCheckBaudrate(Baudrate))
178 {
179 //
180 // Baud-rate is invalid
181 //
182 return FALSE;
183 }
184
185 //
186 // check if com port address is valid or not
187 //
188 if (!CommandDebugCheckComPort(PortName, &Port))
189 {
190 //
191 // com port is invalid
192 //
193 return FALSE;
194 }
195
196 //
197 // Everything is okay, connect to the remote machine to send (debuggee)
198 //
199 return KdPrepareAndConnectDebugPort(PortName, Baudrate, Port, TRUE, FALSE);
200}
BOOLEAN KdPrepareAndConnectDebugPort(const char *PortName, DWORD Baudrate, UINT32 Port, BOOLEAN IsPreparing, BOOLEAN IsNamedPipe)

◆ HyperDbgDebugRemoteDeviceUsingComPort()

BOOLEAN HyperDbgDebugRemoteDeviceUsingComPort ( const CHAR * PortName,
DWORD Baudrate )

Connect to a remote serial device (Debugger)

Parameters
PortName
Baudrate
Returns
BOOLEAN
117{
118 UINT32 Port;
119
120 //
121 // Check if baudrate is valid or not
122 //
123 if (!CommandDebugCheckBaudrate(Baudrate))
124 {
125 //
126 // Baud-rate is invalid
127 //
128 return FALSE;
129 }
130
131 //
132 // check if com port address is valid or not
133 //
134 if (!CommandDebugCheckComPort(PortName, &Port))
135 {
136 //
137 // com port is invalid
138 //
139 return FALSE;
140 }
141
142 //
143 // Everything is okay, connect to the remote machine to send (debugger)
144 //
145 return KdPrepareAndConnectDebugPort(PortName, Baudrate, Port, FALSE, FALSE);
146}

◆ HyperDbgDebugRemoteDeviceUsingNamedPipe()

BOOLEAN HyperDbgDebugRemoteDeviceUsingNamedPipe ( const CHAR * NamedPipe)

Connect to a remote named pipe (Debugger)

Parameters
NamedPipe
Returns
BOOLEAN
157{
158 return KdPrepareAndConnectDebugPort(NamedPipe, NULL, NULL, FALSE, TRUE);
159}

Variable Documentation

◆ g_IsDebuggeeRunning

BOOLEAN g_IsDebuggeeRunning
extern

Shows if the debuggee is running or not.

◆ g_IsSerialConnectedToRemoteDebuggee

BOOLEAN g_IsSerialConnectedToRemoteDebuggee
extern

Shows if the debugger was connected to remote debuggee over (A remote guest)

◆ g_IsSerialConnectedToRemoteDebugger

BOOLEAN g_IsSerialConnectedToRemoteDebugger
extern

Shows if the debugger was connected to remote debugger (A remote host)

◆ g_SerialListeningThreadHandle

HANDLE g_SerialListeningThreadHandle
extern

In debuggee and debugger, we save the handle of the user-mode listening thread for pauses here.

◆ g_SerialRemoteComPortHandle

HANDLE g_SerialRemoteComPortHandle
extern

In debugger (not debuggee), we save the handle of the user-mode listening thread for remote system here.