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

Header for serial port connection from debuggee to debugger. More...

Go to the source code of this file.

Macros

#define CBR_110   110
 
#define CBR_300   300
 
#define CBR_600   600
 
#define CBR_1200   1200
 
#define CBR_2400   2400
 
#define CBR_4800   4800
 
#define CBR_9600   9600
 
#define CBR_14400   14400
 
#define CBR_19200   19200
 
#define CBR_38400   38400
 
#define CBR_56000   56000
 
#define CBR_57600   57600
 
#define CBR_115200   115200
 
#define CBR_128000   128000
 
#define CBR_256000   256000
 
#define COM1_PORT   0x03F8
 
#define COM2_PORT   0x02F8
 
#define COM3_PORT   0x03E8
 
#define COM4_PORT   0x02E8
 

Functions

VOID KdHyperDbgTest (UINT16 Byte)
 
VOID KdHyperDbgPrepareDebuggeeConnectionPort (UINT32 PortAddress, UINT32 Baudrate)
 
VOID KdHyperDbgSendByte (UCHAR Byte, BOOLEAN BusyWait)
 
BOOLEAN KdHyperDbgRecvByte (PUCHAR RecvByte)
 
VOID SerialConnectionTest ()
 A simple connection test.
 
NTSTATUS SerialConnectionPrepare (PDEBUGGER_PREPARE_DEBUGGEE DebuggeeRequest)
 Perform tasks relating to stepping (step-in & step-out) requests.
 
BOOLEAN SerialConnectionCheckPort (UINT32 SerialPort)
 Check if serial port address.
 
BOOLEAN SerialConnectionCheckBaudrate (DWORD Baudrate)
 Check if baud rate is valid or not.
 
BOOLEAN SerialConnectionSend (CHAR *Buffer, UINT32 Length)
 Perform sending buffer over serial.
 
BOOLEAN SerialConnectionRecvBuffer (CHAR *BufferToSave, UINT32 *LengthReceived)
 Receive packet from the debugger.
 
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.
 

Detailed Description

Header for 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

Macro Definition Documentation

◆ CBR_110

#define CBR_110   110

◆ CBR_115200

#define CBR_115200   115200

◆ CBR_1200

#define CBR_1200   1200

◆ CBR_128000

#define CBR_128000   128000

◆ CBR_14400

#define CBR_14400   14400

◆ CBR_19200

#define CBR_19200   19200

◆ CBR_2400

#define CBR_2400   2400

◆ CBR_256000

#define CBR_256000   256000

◆ CBR_300

#define CBR_300   300

◆ CBR_38400

#define CBR_38400   38400

◆ CBR_4800

#define CBR_4800   4800

◆ CBR_56000

#define CBR_56000   56000

◆ CBR_57600

#define CBR_57600   57600

◆ CBR_600

#define CBR_600   600

◆ CBR_9600

#define CBR_9600   9600

◆ COM1_PORT

#define COM1_PORT   0x03F8

◆ COM2_PORT

#define COM2_PORT   0x02F8

◆ COM3_PORT

#define COM3_PORT   0x03E8

◆ COM4_PORT

#define COM4_PORT   0x02E8

Function Documentation

◆ KdHyperDbgPrepareDebuggeeConnectionPort()

VOID KdHyperDbgPrepareDebuggeeConnectionPort ( UINT32 PortAddress,
UINT32 Baudrate )
105{
106 UINT64 PortAddressUChar = 0ULL;
107 PortAddressUChar = PortAddressUChar | PortAddress;
108
109 g_PortDetails.Address = (PUCHAR)PortAddressUChar;
110 g_PortDetails.BaudRate = Baudrate;
111 g_PortDetails.Flags = 0;
112 g_PortDetails.ByteWidth = 1;
113
114 g_PortDetails.Write = WritePortWithIndex8;
115 g_PortDetails.Read = ReadPortWithIndex8;
116}
unsigned __int64 UINT64
Definition BasicTypes.h:21
CPPORT g_PortDetails
Definition uart16550.c:43

◆ KdHyperDbgRecvByte()

BOOLEAN KdHyperDbgRecvByte ( PUCHAR RecvByte)
126{
127 if (Uart16550GetByte(&g_PortDetails, RecvByte) == UartSuccess)
128 {
129 return TRUE;
130 }
131 return FALSE;
132}
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54
UART_STATUS Uart16550GetByte(_Inout_ PCPPORT Port, _Out_ PUCHAR Byte)
Definition uart16550.c:546

◆ KdHyperDbgSendByte()

VOID KdHyperDbgSendByte ( UCHAR Byte,
BOOLEAN BusyWait )
120{
121 Uart16550PutByte(&g_PortDetails, Byte, BusyWait);
122}
UART_STATUS Uart16550PutByte(_Inout_ PCPPORT Port, UCHAR Byte, BOOLEAN BusyWait)
Definition uart16550.c:645

◆ KdHyperDbgTest()

VOID KdHyperDbgTest ( UINT16 Byte)
78{
79 UNREFERENCED_PARAMETER(Byte);
80 //
81 // *** This function is for internal use and test
82 // don't use it ***
83 //
84
85 CPPORT TempPort = {0};
86 TempPort.Address = (PUCHAR)0x2f8;
87 TempPort.BaudRate = 0x01c200; // 115200
88 TempPort.Flags = 0;
89 TempPort.ByteWidth = 1;
90
91 TempPort.Write = WritePortWithIndex8;
92 TempPort.Read = ReadPortWithIndex8;
93
94 Uart16550PutByte(&TempPort, 0x42, TRUE);
95
96 for (size_t i = 0; i < 100; i++)
97 {
98 // char RecvByte = 0;
99 // Uart16550GetByte(&TempPort,&RecvByte);
100 }
101}

◆ 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 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

◆ 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}
#define NULL_ZERO
Definition BasicTypes.h:51
unsigned char BYTE
Definition BasicTypes.h:24
unsigned char UCHAR
Definition BasicTypes.h:35
unsigned int UINT32
Definition BasicTypes.h:48
#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

◆ 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