HyperDbg Debugger
Loading...
Searching...
No Matches
SerialConnection.c File Reference

Serial port connection from debuggee to debugger. More...

#include "pch.h"

Functions

VOID SerialConnectionTest ()
 A simple connection test.
 
VOID SerialConnectionSendEndOfBuffer ()
 Send end of buffer packet.
 
BOOLEAN SerialConnectionCheckForTheEndOfTheBuffer (PUINT32 CurrentLoopIndex, BYTE *Buffer)
 compares the buffer with a string
 
BOOLEAN SerialConnectionRecvBuffer (CHAR *BufferToSave, UINT32 *LengthReceived)
 Receive packet from the debugger.
 
BOOLEAN SerialConnectionSend (CHAR *Buffer, UINT32 Length)
 Perform sending buffer over serial.
 
BOOLEAN SerialConnectionSendTwoBuffers (CHAR *Buffer1, UINT32 Length1, CHAR *Buffer2, UINT32 Length2)
 Perform sending 2 not appended buffers over serial.
 
BOOLEAN SerialConnectionSendThreeBuffers (CHAR *Buffer1, UINT32 Length1, CHAR *Buffer2, UINT32 Length2, CHAR *Buffer3, UINT32 Length3)
 Perform sending 3 not appended buffers over serial.
 
BOOLEAN SerialConnectionCheckBaudrate (DWORD Baudrate)
 Check if baud rate is valid or not.
 
BOOLEAN SerialConnectionCheckPort (UINT32 SerialPort)
 Check if serial port address.
 
NTSTATUS SerialConnectionPrepare (PDEBUGGER_PREPARE_DEBUGGEE DebuggeeRequest)
 Perform tasks relating to stepping (step-in & step-out) requests.
 

Detailed Description

Serial port connection from debuggee to debugger.

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

Function Documentation

◆ SerialConnectionCheckBaudrate()

BOOLEAN SerialConnectionCheckBaudrate ( DWORD Baudrate)

Check if baud rate is valid or not.

Parameters
Baudrate
Returns
BOOLEAN return TRUE if it's correct and returns FALSE if it's not correct
302{
303 if (Baudrate == CBR_110 || Baudrate == CBR_300 || Baudrate == CBR_600 ||
304 Baudrate == CBR_1200 || Baudrate == CBR_2400 || Baudrate == CBR_4800 ||
305 Baudrate == CBR_9600 || Baudrate == CBR_14400 || Baudrate == CBR_19200 ||
306 Baudrate == CBR_38400 || Baudrate == CBR_56000 || Baudrate == CBR_57600 ||
307 Baudrate == CBR_115200 || Baudrate == CBR_128000 ||
308 Baudrate == CBR_256000)
309 {
310 return TRUE;
311 }
312 return FALSE;
313}
#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

◆ SerialConnectionCheckForTheEndOfTheBuffer()

BOOLEAN SerialConnectionCheckForTheEndOfTheBuffer ( PUINT32 CurrentLoopIndex,
BYTE * Buffer )

compares the buffer with a string

Parameters
CurrentLoopIndexNumber of previously read bytes
Buffer
Returns
BOOLEAN
54{
55 UINT32 ActualBufferLength;
56
57 ActualBufferLength = *CurrentLoopIndex;
58
59 //
60 // End of buffer is 4 character long
61 //
62 if (*CurrentLoopIndex <= 3)
63 {
64 return FALSE;
65 }
66
67 if (Buffer[ActualBufferLength] == SERIAL_END_OF_BUFFER_CHAR_4 &&
68 Buffer[ActualBufferLength - 1] == SERIAL_END_OF_BUFFER_CHAR_3 &&
69 Buffer[ActualBufferLength - 2] == SERIAL_END_OF_BUFFER_CHAR_2 &&
70 Buffer[ActualBufferLength - 3] == SERIAL_END_OF_BUFFER_CHAR_1)
71 {
72 //
73 // Clear the end character
74 //
75 Buffer[ActualBufferLength - 3] = NULL_ZERO;
76 Buffer[ActualBufferLength - 2] = NULL_ZERO;
77 Buffer[ActualBufferLength - 1] = NULL_ZERO;
78 Buffer[ActualBufferLength] = NULL_ZERO;
79
80 //
81 // Set the new length
82 //
83 *CurrentLoopIndex = ActualBufferLength - 3;
84
85 return TRUE;
86 }
87 return FALSE;
88}
#define NULL_ZERO
Definition BasicTypes.h:51
unsigned int UINT32
Definition BasicTypes.h:48
#define SERIAL_END_OF_BUFFER_CHAR_4
Definition Constants.h:435
#define SERIAL_END_OF_BUFFER_CHAR_2
Definition Constants.h:433
#define SERIAL_END_OF_BUFFER_CHAR_1
characters of the buffer that we set at the end of buffers for serial
Definition Constants.h:432
#define SERIAL_END_OF_BUFFER_CHAR_3
Definition Constants.h:434

◆ SerialConnectionCheckPort()

BOOLEAN SerialConnectionCheckPort ( UINT32 SerialPort)

Check if serial port address.

Parameters
SerialPort
Returns
BOOLEAN return TRUE if it's correct and returns FALSE if it's not correct
324{
325 if (SerialPort == COM1_PORT || SerialPort == COM2_PORT || SerialPort == COM3_PORT ||
326 SerialPort == COM4_PORT)
327 {
328 return TRUE;
329 }
330 return FALSE;
331}
#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

◆ SerialConnectionPrepare()

NTSTATUS SerialConnectionPrepare ( PDEBUGGER_PREPARE_DEBUGGEE DebuggeeRequest)

Perform tasks relating to stepping (step-in & step-out) requests.

Parameters
DebuggerPrintRequestRequest to prepare debuggee
Returns
NTSTATUS
342{
343 //
344 // Check if baud rate is valid or not
345 //
346 if (!SerialConnectionCheckBaudrate(DebuggeeRequest->Baudrate))
347 {
348 //
349 // Baud rate is invalid, set the status and return
350 //
352 return STATUS_UNSUCCESSFUL;
353 }
354
355 //
356 // Check if port address is valid or not
357 //
358 if (!SerialConnectionCheckPort(DebuggeeRequest->PortAddress))
359 {
360 //
361 // Port address is invalid, set the status and return
362 //
364 return STATUS_UNSUCCESSFUL;
365 }
366
367 //
368 // Prepare the structures needed for connecting remote port
369 //
370 KdHyperDbgPrepareDebuggeeConnectionPort(DebuggeeRequest->PortAddress, DebuggeeRequest->Baudrate);
371
372 //
373 // Initialize kernel debugger
374 //
376
377 //
378 // Send "Start" packet along with Windows Name
379 //
382 (CHAR *)DebuggeeRequest,
384
385 //
386 // Set status to successful
387 //
388 DebuggeeRequest->Result = DEBUGGER_OPERATION_WAS_SUCCESSFUL;
389
390 return STATUS_SUCCESS;
391}
char CHAR
Definition BasicTypes.h:31
@ DEBUGGER_REMOTE_PACKET_TYPE_DEBUGGEE_TO_DEBUGGER
Definition Connection.h:164
@ DEBUGGER_REMOTE_PACKET_REQUESTED_ACTION_DEBUGGEE_STARTED
Definition Connection.h:104
#define MAXIMUM_CHARACTER_FOR_OS_NAME
maximum name for OS name buffer
Definition Constants.h:459
#define DEBUGGER_ERROR_PREPARING_DEBUGGEE_INVALID_SERIAL_PORT
error, serial port address is invalid
Definition ErrorCodes.h:152
#define DEBUGGER_ERROR_PREPARING_DEBUGGEE_INVALID_BAUDRATE
error, baud rate is invalid
Definition ErrorCodes.h:146
#define DEBUGGER_OPERATION_WAS_SUCCESSFUL
General value to indicate that the operation or request was successful.
Definition ErrorCodes.h:23
VOID KdInitializeKernelDebugger()
initialize kernel debugger
Definition Kd.c:22
_Use_decl_annotations_ BOOLEAN KdResponsePacketToDebugger(DEBUGGER_REMOTE_PACKET_TYPE PacketType, DEBUGGER_REMOTE_PACKET_REQUESTED_ACTION Response, CHAR *OptionalBuffer, UINT32 OptionalBufferLength)
Sends a HyperDbg response packet to the debugger.
Definition Kd.c:294
BOOLEAN SerialConnectionCheckBaudrate(DWORD Baudrate)
Check if baud rate is valid or not.
Definition SerialConnection.c:301
BOOLEAN SerialConnectionCheckPort(UINT32 SerialPort)
Check if serial port address.
Definition SerialConnection.c:323
VOID KdHyperDbgPrepareDebuggeeConnectionPort(UINT32 PortAddress, UINT32 Baudrate)
Definition uart16550.c:104
#define STATUS_UNSUCCESSFUL
Definition Windows.h:172
UINT32 Result
Definition RequestStructures.h:586
UINT32 Baudrate
Definition RequestStructures.h:584
UINT32 PortAddress
Definition RequestStructures.h:583

◆ SerialConnectionRecvBuffer()

BOOLEAN SerialConnectionRecvBuffer ( CHAR * BufferToSave,
UINT32 * LengthReceived )

Receive packet from the debugger.

Parameters
BufferToSave
LengthReceived
Returns
BOOLEAN
101{
102 UINT32 Loop = 0;
103
104 //
105 // Read data and store in a buffer
106 //
107 while (TRUE)
108 {
109 UCHAR RecvChar = NULL_ZERO;
110
111 if (!KdHyperDbgRecvByte(&RecvChar))
112 {
113 continue;
114 }
115
116 //
117 // We already now that the maximum packet size is MaxSerialPacketSize
118 // Check to make sure that we don't pass the boundaries
119 //
120 if (!(MaxSerialPacketSize > Loop))
121 {
122 //
123 // Invalid buffer (size of buffer exceeds the limitation)
124 //
125 LogError("Err, a buffer received in debuggee which exceeds the buffer limitation");
126 return FALSE;
127 }
128
129 BufferToSave[Loop] = RecvChar;
130
131 if (SerialConnectionCheckForTheEndOfTheBuffer(&Loop, (BYTE *)BufferToSave))
132 {
133 break;
134 }
135
136 Loop++;
137 }
138
139 //
140 // Set the length
141 //
142 *LengthReceived = Loop;
143
144 return TRUE;
145}
unsigned char BYTE
Definition BasicTypes.h:24
unsigned char UCHAR
Definition BasicTypes.h:35
#define MaxSerialPacketSize
size of buffer for serial
Definition Constants.h:194
#define LogError(format,...)
Log in the case of error.
Definition HyperDbgHyperLogIntrinsics.h:113
BOOLEAN SerialConnectionCheckForTheEndOfTheBuffer(PUINT32 CurrentLoopIndex, BYTE *Buffer)
compares the buffer with a string
Definition SerialConnection.c:53
BOOLEAN KdHyperDbgRecvByte(PUCHAR RecvByte)
Definition uart16550.c:125

◆ SerialConnectionSend()

BOOLEAN SerialConnectionSend ( CHAR * Buffer,
UINT32 Length )

Perform sending buffer over serial.

Parameters
Bufferbuffer to send
Lengthlength of buffer to send
Returns
BOOLEAN
156{
157 //
158 // Check if buffer not pass the boundary
159 //
161 {
162 LogError("Err, buffer is above the maximum buffer size that can be sent to debuggee (%d > %d), "
163 "for more information, please visit https://docs.hyperdbg.org/tips-and-tricks/misc/increase-communication-buffer-size",
166 return FALSE;
167 }
168
169 for (size_t i = 0; i < Length; i++)
170 {
171 KdHyperDbgSendByte(Buffer[i], TRUE);
172 }
173
174 //
175 // Send the end buffer
176 //
178
179 return TRUE;
180}
#define SERIAL_END_OF_BUFFER_CHARS_COUNT
count of characters for serial end of buffer
Definition Constants.h:426
VOID SerialConnectionSendEndOfBuffer()
Send end of buffer packet.
Definition SerialConnection.c:34
VOID KdHyperDbgSendByte(UCHAR Byte, BOOLEAN BusyWait)
Definition uart16550.c:119

◆ SerialConnectionSendEndOfBuffer()

VOID SerialConnectionSendEndOfBuffer ( )

◆ SerialConnectionSendThreeBuffers()

BOOLEAN SerialConnectionSendThreeBuffers ( CHAR * Buffer1,
UINT32 Length1,
CHAR * Buffer2,
UINT32 Length2,
CHAR * Buffer3,
UINT32 Length3 )

Perform sending 3 not appended buffers over serial.

Parameters
Buffer1buffer to send
Length1length of buffer to send
Buffer2buffer to send
Length2length of buffer to send
Buffer3buffer to send
Length3length of buffer to send
Returns
BOOLEAN
248{
249 //
250 // Check if buffer not pass the boundary
251 //
252 if ((Length1 + Length2 + Length3 + SERIAL_END_OF_BUFFER_CHARS_COUNT) > MaxSerialPacketSize)
253 {
254 LogError("Err, buffer is above the maximum buffer size that can be sent to debuggee (%d > %d), "
255 "for more information, please visit https://docs.hyperdbg.org/tips-and-tricks/misc/increase-communication-buffer-size",
256 Length1 + Length2 + Length3 + SERIAL_END_OF_BUFFER_CHARS_COUNT,
258 return FALSE;
259 }
260
261 //
262 // Send first buffer
263 //
264 for (size_t i = 0; i < Length1; i++)
265 {
266 KdHyperDbgSendByte(Buffer1[i], TRUE);
267 }
268
269 //
270 // Send second buffer
271 //
272 for (size_t i = 0; i < Length2; i++)
273 {
274 KdHyperDbgSendByte(Buffer2[i], TRUE);
275 }
276
277 //
278 // Send third buffer
279 //
280 for (size_t i = 0; i < Length3; i++)
281 {
282 KdHyperDbgSendByte(Buffer3[i], TRUE);
283 }
284
285 //
286 // Send the end buffer
287 //
289
290 return TRUE;
291}

◆ SerialConnectionSendTwoBuffers()

BOOLEAN SerialConnectionSendTwoBuffers ( CHAR * Buffer1,
UINT32 Length1,
CHAR * Buffer2,
UINT32 Length2 )

Perform sending 2 not appended buffers over serial.

Parameters
Buffer1buffer to send
Length1length of buffer to send
Buffer2buffer to send
Length2length of buffer to send
Returns
BOOLEAN
193{
194 //
195 // Check if buffer not pass the boundary
196 //
197 if ((Length1 + Length2 + SERIAL_END_OF_BUFFER_CHARS_COUNT) > MaxSerialPacketSize)
198 {
199 LogError("Err, buffer is above the maximum buffer size that can be sent to debuggee (%d > %d), "
200 "for more information, please visit https://docs.hyperdbg.org/tips-and-tricks/misc/increase-communication-buffer-size",
201 Length1 + Length2 + SERIAL_END_OF_BUFFER_CHARS_COUNT,
203 return FALSE;
204 }
205
206 //
207 // Send first buffer
208 //
209 for (size_t i = 0; i < Length1; i++)
210 {
211 KdHyperDbgSendByte(Buffer1[i], TRUE);
212 }
213
214 //
215 // Send second buffer
216 //
217 for (size_t i = 0; i < Length2; i++)
218 {
219 KdHyperDbgSendByte(Buffer2[i], TRUE);
220 }
221
222 //
223 // Send the end buffer
224 //
226
227 return TRUE;
228}

◆ SerialConnectionTest()

VOID SerialConnectionTest ( )

A simple connection test.

Returns
VOID
21{
22 for (size_t i = 0; i < 100; i++)
23 {
25 }
26}
unsigned short UINT16
Definition BasicTypes.h:47
VOID KdHyperDbgTest(UINT16 Byte)
Definition uart16550.c:77