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, BOOLEAN PauseAfterConnection)
 Connect to a remote serial device (Debugger).
BOOLEAN HyperDbgDebugRemoteDeviceUsingNamedPipe (const CHAR *NamedPipe, BOOLEAN PauseAfterConnection)
 Connect to a remote named pipe (Debugger).
BOOLEAN HyperDbgDebugCurrentDeviceUsingComPort (const CHAR *PortName, DWORD Baudrate)
 Connect to a remote serial device (Debuggee).
BOOLEAN HyperDbgDebugCloseRemoteDebugger ()
 Connect to a remote serial device (Debuggee).
VOID CommandDebug (vector< CommandToken > CommandTokens, string Command)
 .debug command handler

Variables

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

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< CommandToken > CommandTokens,
string Command )

.debug command handler

Parameters
CommandTokens
Command
Returns
VOID
236{
237 UINT32 Baudrate;
238 UINT32 Port;
239 BOOLEAN IsFirstCommand = TRUE;
240 BOOLEAN IsRemote = FALSE;
241 BOOLEAN IsPrepare = FALSE;
242 BOOLEAN IsSerial = FALSE;
243 BOOLEAN IsNamedPipe = FALSE;
244 BOOLEAN IsPause = FALSE;
245 BOOLEAN IsNamedPipeAddressKnown = FALSE;
246 string NamedPipeAddress;
247 BOOLEAN IsComPortAddressKnown = FALSE;
248 string ComAddress;
249 BOOLEAN IsComPortBaudrateKnown = FALSE;
250
251 if (CommandTokens.size() == 2 && CompareLowerCaseStrings(CommandTokens.at(1), "close"))
252 {
253 //
254 // Check if the debugger is attached to a debuggee
255 //
257 {
258 ShowMessages("err, debugger is not attached to any instance of debuggee\n");
259 }
260
261 return;
262 }
263 else if (CommandTokens.size() <= 3)
264 {
265 ShowMessages("incorrect use of the '%s'\n\n",
266 GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
268 return;
269 }
270
271 for (auto Section : CommandTokens)
272 {
273 if (IsFirstCommand)
274 {
275 IsFirstCommand = FALSE;
276 continue;
277 }
278 else if (!IsRemote && CompareLowerCaseStrings(Section, "remote"))
279 {
280 IsRemote = TRUE;
281 continue;
282 }
283 else if (!IsPrepare && CompareLowerCaseStrings(Section, "prepare"))
284 {
285 IsPrepare = TRUE;
286 continue;
287 }
288 else if (!IsSerial && CompareLowerCaseStrings(Section, "serial"))
289 {
290 IsSerial = TRUE;
291 continue;
292 }
293 else if (!IsNamedPipe && CompareLowerCaseStrings(Section, "namedpipe"))
294 {
295 IsNamedPipe = TRUE;
296 continue;
297 }
298 else if (!IsPause && CompareLowerCaseStrings(Section, "pause"))
299 {
300 IsPause = TRUE;
301 continue;
302 }
303 else if (!IsNamedPipeAddressKnown && IsNamedPipe)
304 {
305 IsNamedPipeAddressKnown = TRUE;
306 NamedPipeAddress = GetCaseSensitiveStringFromCommandToken(Section);
307 continue;
308 }
309 else if (!IsComPortBaudrateKnown && IsSerial && IsNumber(GetCaseSensitiveStringFromCommandToken(Section)))
310 {
311 IsComPortBaudrateKnown = TRUE;
312 Baudrate = stoi(GetCaseSensitiveStringFromCommandToken(Section));
313
314 //
315 // Check if baudrate is valid or not
316 //
317 if (!CommandDebugCheckBaudrate(Baudrate))
318 {
319 //
320 // Baud-rate is invalid
321 //
322 ShowMessages("err, baud rate is invalid\n\n");
324 return;
325 }
326
327 continue;
328 }
329 else if (!IsComPortAddressKnown && IsSerial)
330 {
331 IsComPortAddressKnown = TRUE;
332 ComAddress = GetCaseSensitiveStringFromCommandToken(Section);
333
334 //
335 // check if com port address is valid or not
336 //
337 if (!CommandDebugCheckComPort(ComAddress.c_str(), &Port))
338 {
339 //
340 // com port is invalid
341 //
342 ShowMessages("err, COM port is invalid\n\n");
344 return;
345 }
346
347 continue;
348 }
349 else
350 {
351 ShowMessages("err, couldn't resolve error at '%s'\n\n",
354 return;
355 }
356 }
357
358 //
359 // Validate parameters
360 //
361 if (IsRemote && IsPrepare)
362 {
363 ShowMessages("err, both 'remote' and 'prepare' can't be used together\n\n");
365 return;
366 }
367
368 if (!IsRemote && !IsPrepare)
369 {
370 ShowMessages("err, either 'remote' or 'prepare' should be used\n\n");
372 return;
373 }
374
375 //
376 // Prepare cannot be specified with the 'pause'
377 //
378 if (IsPrepare && IsPause)
379 {
380 ShowMessages("err, 'pause' cannot be used with 'prepare'\n\n");
382 return;
383 }
384
385 //
386 // Named pipe cannot be used with the 'prepare'
387 //
388 if (IsNamedPipe && IsPrepare)
389 {
390 ShowMessages("err, named pipe cannot be used with 'prepare'\n\n");
392 return;
393 }
394
395 //
396 // Check if named pipe is empty or not if it's a named pipe
397 //
398 if (IsNamedPipe && NamedPipeAddress.empty())
399 {
400 ShowMessages("err, named pipe address is empty\n\n");
402 return;
403 }
404
405 //
406 // If it's serial, COM address and bausrate should be known
407 //
408 if (IsSerial && (!IsComPortAddressKnown || !IsComPortBaudrateKnown))
409 {
410 ShowMessages("err, COM port address or baudrate is unknown\n\n");
412 return;
413 }
414
415 //
416 // Perform connecting to the remote machine or prepare to send
417 //
418 if (IsPrepare)
419 {
420 //
421 // Everything is okay, prepare to send (debuggee)
422 //
423 HyperDbgDebugCurrentDeviceUsingComPort(ComAddress.c_str(), Baudrate);
424 }
425 else
426 {
427 //
428 // Everything is okay, connect to the remote machine to send (debugger)
429 //
430 if (IsNamedPipe)
431 {
432 HyperDbgDebugRemoteDeviceUsingNamedPipe(NamedPipeAddress.c_str(), IsPause);
433 }
434 else if (IsSerial)
435 {
436 HyperDbgDebugRemoteDeviceUsingComPort(ComAddress.c_str(), Baudrate, IsPause);
437 }
438 }
439}
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
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 HyperDbgDebugRemoteDeviceUsingComPort(const CHAR *PortName, DWORD Baudrate, BOOLEAN PauseAfterConnection)
Connect to a remote serial device (Debugger).
Definition debug.cpp:116
BOOLEAN HyperDbgDebugRemoteDeviceUsingNamedPipe(const CHAR *NamedPipe, BOOLEAN PauseAfterConnection)
Connect to a remote named pipe (Debugger).
Definition debug.cpp:157
BOOLEAN CommandDebugCheckComPort(const CHAR *ComPort, UINT32 *Port)
Check if COM port is valid or not.
Definition debug.cpp:59
BOOLEAN HyperDbgDebugCurrentDeviceUsingComPort(const CHAR *PortName, DWORD Baudrate)
Connect to a remote serial device (Debuggee).
Definition debug.cpp:171
BOOLEAN CommandDebugCheckBaudrate(DWORD Baudrate)
Check if baud rate is valid or not.
Definition debug.cpp:92
VOID CommandDebugHelp()
help of the .debug command
Definition debug.cpp:25
BOOLEAN HyperDbgDebugCloseRemoteDebugger()
Connect to a remote serial device (Debuggee).
Definition debug.cpp:211
BOOLEAN IsNumber(const string &str)

◆ CommandDebugCheckBaudrate()

BOOLEAN CommandDebugCheckBaudrate ( DWORD Baudrate)

Check if baud rate is valid or not.

Parameters
Baudrate
Returns
BOOLEAN
93{
94 if (Baudrate == CBR_110 || Baudrate == CBR_300 || Baudrate == CBR_600 ||
95 Baudrate == CBR_1200 || Baudrate == CBR_2400 || Baudrate == CBR_4800 ||
96 Baudrate == CBR_9600 || Baudrate == CBR_14400 || Baudrate == CBR_19200 ||
97 Baudrate == CBR_38400 || Baudrate == CBR_56000 || Baudrate == CBR_57600 ||
98 Baudrate == CBR_115200 || Baudrate == CBR_128000 ||
99 Baudrate == CBR_256000)
100 {
101 return TRUE;
102 }
103 return FALSE;
104}
#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
60{
61 if (_stricmp(ComPort, "com1") == 0)
62 {
63 *Port = COM1_PORT;
64 return TRUE;
65 }
66 else if (_stricmp(ComPort, "com2") == 0)
67 {
68 *Port = COM2_PORT;
69 return TRUE;
70 }
71 else if (_stricmp(ComPort, "com3") == 0)
72 {
73 *Port = COM3_PORT;
74 return TRUE;
75 }
76 else if (_stricmp(ComPort, "com4") == 0)
77 {
78 *Port = COM4_PORT;
79 return TRUE;
80 }
81
82 return FALSE;
83}
#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
26{
27 ShowMessages(
28 ".debug : debugs a target machine or makes this machine a debuggee.\n\n");
29
30 ShowMessages(
31 "syntax : \t.debug [remote] [serial|namedpipe] [pause] [Baudrate (decimal)] [Address (string)]\n");
32 ShowMessages(
33 "syntax : \t.debug [prepare] [serial] [Baudrate (decimal)] [Address (string)]\n");
34 ShowMessages("syntax : \t.debug [close]\n");
35
36 ShowMessages("\n");
37 ShowMessages("\t\te.g : .debug remote serial 115200 com2\n");
38 ShowMessages("\t\te.g : .debug remote pause serial 115200 com2\n");
39 ShowMessages("\t\te.g : .debug remote namedpipe \\\\.\\pipe\\HyperDbgPipe\n");
40 ShowMessages("\t\te.g : .debug remote pause namedpipe \\\\.\\pipe\\HyperDbgPipe\n");
41 ShowMessages("\t\te.g : .debug remote namedpipe \"\\\\.\\pipe\\HyperDbg Pipe\"\n");
42 ShowMessages("\t\te.g : .debug prepare serial 115200 com1\n");
43 ShowMessages("\t\te.g : .debug prepare serial 115200 com2\n");
44 ShowMessages("\t\te.g : .debug close\n");
45
46 ShowMessages(
47 "\nvalid baud rates (decimal) : 110, 300, 600, 1200, 2400, 4800, 9600, "
48 "14400, 19200, 38400, 56000, 57600, 115200, 128000, 256000\n");
49 ShowMessages("valid COM ports : COM1, COM2, COM3, COM4 \n");
50}

◆ HyperDbgDebugCloseRemoteDebugger()

BOOLEAN HyperDbgDebugCloseRemoteDebugger ( )

Connect to a remote serial device (Debuggee).

Returns
BOOLEAN
212{
213 //
214 // Check if the debugger is attached to a debuggee
215 //
217 {
219 return TRUE;
220 }
221 else
222 {
223 return FALSE;
224 }
225}
BOOLEAN g_IsSerialConnectedToRemoteDebuggee
Shows if the debugger was connected to remote debuggee over (A remote guest).
Definition globals.h:253
BOOLEAN KdCloseConnection()
Send close packet to the debuggee and debugger.
Definition kd.cpp:3056

◆ HyperDbgDebugCurrentDeviceUsingComPort()

BOOLEAN HyperDbgDebugCurrentDeviceUsingComPort ( const CHAR * PortName,
DWORD Baudrate )

Connect to a remote serial device (Debuggee).

Parameters
PortName
Baudrate
Returns
BOOLEAN
172{
173 UINT32 Port;
174
175 //
176 // Check if baudrate is valid or not
177 //
178 if (!CommandDebugCheckBaudrate(Baudrate))
179 {
180 //
181 // Baud-rate is invalid
182 //
183 return FALSE;
184 }
185
186 //
187 // check if com port address is valid or not
188 //
189 if (!CommandDebugCheckComPort(PortName, &Port))
190 {
191 //
192 // com port is invalid
193 //
194 return FALSE;
195 }
196
197 //
198 // Everything is okay, connect to the remote machine to send (debuggee)
199 // Note: Pause after connect is set to FALSE, because it doesn't make sense for the
200 // deubuggee to pause after connecting to the debugger
201 //
202 return KdPrepareAndConnectDebugPort(PortName, Baudrate, Port, TRUE, FALSE, FALSE);
203}
BOOLEAN KdPrepareAndConnectDebugPort(const CHAR *PortName, DWORD Baudrate, UINT32 Port, BOOLEAN IsPreparing, BOOLEAN IsNamedPipe, BOOLEAN PauseAfterConnection)
Prepare and initialize COM port.
Definition kd.cpp:2468

◆ HyperDbgDebugRemoteDeviceUsingComPort()

BOOLEAN HyperDbgDebugRemoteDeviceUsingComPort ( const CHAR * PortName,
DWORD Baudrate,
BOOLEAN PauseAfterConnection )

Connect to a remote serial device (Debugger).

Parameters
PortName
Baudrate
PauseAfterConnection
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, PauseAfterConnection);
146}

◆ HyperDbgDebugRemoteDeviceUsingNamedPipe()

BOOLEAN HyperDbgDebugRemoteDeviceUsingNamedPipe ( const CHAR * NamedPipe,
BOOLEAN PauseAfterConnection )

Connect to a remote named pipe (Debugger).

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

Variable Documentation

◆ g_IsSerialConnectedToRemoteDebuggee

BOOLEAN g_IsSerialConnectedToRemoteDebuggee
extern

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