HyperDbg Debugger
Loading...
Searching...
No Matches
remote-connection.cpp File Reference

handle remote connections command More...

#include "pch.h"

Functions

VOID RemoteConnectionListen (PCSTR Port)
 Listen of a port and wait for a client connection.
 
DWORD WINAPI RemoteConnectionThreadListeningToDebuggee (LPVOID lpParam)
 A thread that listens for server (debuggee) messages and show it by using ShowMessages wrapper.
 
VOID RemoteConnectionConnect (PCSTR Ip, PCSTR Port)
 Connect to a remote debuggee (guest) as a client (host)
 
int RemoteConnectionSendCommand (const char *sendbuf, int len)
 send the command as a client (debugger, host) to the server (debuggee, guest)
 
int RemoteConnectionSendResultsToHost (const char *sendbuf, int len)
 Send the results of executing a command from deubggee (server, guest) to the debugger (client, host)
 
int RemoteConnectionCloseTheConnectionWithDebuggee ()
 Close the connect from client side to the debuggee.
 

Variables

BYTE g_EndOfBufferCheckTcp [TCP_END_OF_BUFFER_CHARS_COUNT]
 the buffer that we set at the end of buffers for tcp connection
 
BOOLEAN g_IsConnectedToHyperDbgLocally
 Shows whether the user is allowed to use 'load' command to load modules locally in VMI (virtual machine introspection) mode.
 
BOOLEAN g_IsConnectedToRemoteDebuggee
 Shows whether the current debugger is the host and connected to a remote debuggee (guest)
 
BOOLEAN g_IsConnectedToRemoteDebugger
 Shows whether the current system is a guest (debuggee) and a remote debugger is connected to this system.
 
BOOLEAN g_BreakPrintingOutput
 Shows whether the pause command or CTRL+C or CTRL+Break is executed or not.
 
BOOLEAN g_IsEndOfMessageReceived
 variable to keep track if the end of the message received (for showing signature)
 
SOCKET g_SeverSocket
 The socket object of guest debuggee (not debugger) it is because in HyperDbg, debugger is client and debuggee is a server.
 
SOCKET g_ServerListenSocket
 Server in debuggee needs an extra socket.
 
SOCKET g_ClientConnectSocket
 The socket object of host debugger (not debuggee) it is because in HyperDbg, debuggee is server and debugger is a client.
 
HANDLE g_RemoteDebuggeeListeningThread
 In debugger (not debuggee), we save the ip of server debuggee in this variable to use it later e.g, in signature.
 
HANDLE g_EndOfMessageReceivedEvent
 Handle to if the end of the message received (for showing signature)
 

Detailed Description

handle remote connections command

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

Function Documentation

◆ RemoteConnectionCloseTheConnectionWithDebuggee()

int RemoteConnectionCloseTheConnectionWithDebuggee ( )

Close the connect from client side to the debuggee.

Returns
int returning 0 means that there was no error in executing the function and 1 shows there was an error
505{
508
509 return 0;
510}
SOCKET g_ClientConnectSocket
The socket object of host debugger (not debuggee) it is because in HyperDbg, debuggee is server and d...
Definition globals.h:89
int CommunicationClientShutdownConnection(SOCKET ConnectSocket)
shutdown the connection as a client
Definition tcpclient.cpp:137
int CommunicationClientCleanup(SOCKET ConnectSocket)
cleanup the connection as client
Definition tcpclient.cpp:216

◆ RemoteConnectionConnect()

VOID RemoteConnectionConnect ( PCSTR Ip,
PCSTR Port )

Connect to a remote debuggee (guest) as a client (host)

this routine is supposed to be called by .connect command

Parameters
Ip
Port
Returns
VOID
316{
317 DWORD ThreadId;
318 CHAR Recv[3] = {0};
319 UINT32 BuffRecv = 0;
320
321 //
322 // Check if the debugger or debuggee is already active
323 //
325 {
326 return;
327 }
328
329 //
330 // Connect to server
331 //
333 {
334 //
335 // There was an error
336 //
337
338 //
339 // Indicate that debugger is not connected
340 //
342
343 //
344 // Indicate that it's not a remote debuggee
345 //
347
348 //
349 // Shutdown connection
350 //
352
353 //
354 // Cleanup
355 //
357 }
358 else
359 {
360 //
361 // Connection was successful
362 //
363
364 //
365 // Check to see whether the version of debugger and debuggee matches together or not
366 //
368 {
369 //
370 // Failed
371 //
372 ShowMessages("err, failed to communicate with debuggee\n");
373 return;
374 }
375
376 //
377 // Receive the handshake results
378 //
379 if (CommunicationClientReceiveMessage(g_ClientConnectSocket, Recv, sizeof(Recv), &BuffRecv) != 0)
380 {
381 //
382 // Failed, break
383 //
384 ShowMessages("err, failed to receive message from debuggee\n");
385 return;
386 }
387
388 //
389 // Check if the handshake was successful or not
390 //
391 if (strcmp((const char *)"OK", Recv) != 0)
392 {
393 //
394 // Build version not matched
395 //
397 return;
398 }
399
400 //
401 // Indicate that local debugger is not connected
402 //
404
405 //
406 // Indicate that it's a remote debuggee
407 //
409
410 //
411 // Create an event to show signature when the messages finished
412 //
413 if (g_EndOfMessageReceivedEvent == NULL)
414 {
415 g_EndOfMessageReceivedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
416 }
417
418 //
419 // Now, we should create a thread, which always listens to
420 // the remote debuggee for new messages
421 // Listen for upcoming messages
422 //
423 g_RemoteDebuggeeListeningThread = CreateThread(
424 NULL,
425 0,
427 NULL,
428 0,
429 &ThreadId);
430
431 ShowMessages("connected to %s:%s\n", Ip, Port);
432 }
433}
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54
unsigned long DWORD
Definition BasicTypes.h:22
unsigned int UINT32
Definition BasicTypes.h:48
char CHAR
Definition BasicTypes.h:31
const unsigned char BuildSignature[]
Definition Constants.h:130
BOOLEAN IsConnectedToAnyInstanceOfDebuggerOrDebuggee()
Shows whether the debugger is connected to a debugger or debuggee connected to a debugger.
Definition debugger.cpp:668
#define ASSERT_MESSAGE_BUILD_SIGNATURE_DOESNT_MATCH
Definition common.h:27
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96
BOOLEAN g_IsConnectedToRemoteDebuggee
Shows whether the current debugger is the host and connected to a remote debuggee (guest)
Definition globals.h:74
DWORD WINAPI RemoteConnectionThreadListeningToDebuggee(LPVOID lpParam)
A thread that listens for server (debuggee) messages and show it by using ShowMessages wrapper.
Definition remote-connection.cpp:211
HANDLE g_RemoteDebuggeeListeningThread
In debugger (not debuggee), we save the ip of server debuggee in this variable to use it later e....
Definition globals.h:124
HANDLE g_EndOfMessageReceivedEvent
Handle to if the end of the message received (for showing signature)
Definition globals.h:137
BOOLEAN g_IsConnectedToHyperDbgLocally
Shows whether the user is allowed to use 'load' command to load modules locally in VMI (virtual machi...
Definition globals.h:67
int CommunicationClientReceiveMessage(SOCKET ConnectSocket, CHAR *RecvBuf, UINT32 MaxBuffLen, PUINT32 BuffLenRecvd)
Receive message as a client.
Definition tcpclient.cpp:173
int CommunicationClientConnectToServer(PCSTR Ip, PCSTR Port, SOCKET *ConnectSocketArg)
communication for client, connecting to the server
Definition tcpclient.cpp:23
int CommunicationClientSendMessage(SOCKET ConnectSocket, const char *sendbuf, int buflen)
Send message a client.
Definition tcpclient.cpp:111

◆ RemoteConnectionListen()

VOID RemoteConnectionListen ( PCSTR Port)

Listen of a port and wait for a client connection.

this routine is supposed to be called by .listen command

Parameters
Port
Returns
VOID
41{
42 char recvbuf[COMMUNICATION_BUFFER_SIZE] = {0};
43
44 //
45 // Check if the debugger or debuggee is already active
46 //
48 {
49 return;
50 }
51
52 //
53 // Start server and wait for client
54 //
56
57 //
58 // Check the version of debuggee and debugger
59 //
61 {
62 //
63 // Failed
64 //
65 ShowMessages("err, unable to handshake with the remote debugger\n");
66
67 //
68 // Close the connection
69 //
71
72 return;
73 }
74
75 //
76 // Check whether the signature of debuggee and debugger match or not
77 //
78 if (strcmp((const char *)BuildSignature, recvbuf) != 0)
79 {
80 //
81 // Build version not matched
82 //
84
85 //
86 // Send unsuccessful handshake (version match) message
87 //
89 {
90 //
91 // Failed
92 //
93 ShowMessages("err, unable to handshake with the remote debugger\n");
94 }
95
96 //
97 // Close the connection
98 //
100
101 return;
102 }
103 else
104 {
105 //
106 // Send successful handshake (version match) message
107 //
109 {
110 //
111 // Failed
112 //
113 ShowMessages("err, unable to handshake with the remote debugger\n");
114
115 //
116 // Close the connection
117 //
119
120 return;
121 }
122 }
123
124 //
125 // Indicate that it's a remote debugger
126 //
128
129 //
130 // And also, make it a local debugger
131 //
133
134 //
135 // Zero the buffer for next command
136 //
137 RtlZeroMemory(recvbuf, COMMUNICATION_BUFFER_SIZE);
138
139 while (true)
140 {
141 //
142 // Receive message (this loop works as a command executer,
143 // we don't send the results to the remote machine by using
144 // this tools
145 //
147 {
148 //
149 // Failed, break
150 //
151 break;
152 }
153
154 //
155 // Execute the command
156 //
157 int CommandExecutionResult = HyperDbgInterpreter(recvbuf);
158
159 //
160 // Send end of buffer
161 //
163
164 //
165 // if the debugger encounters an exit state then the return will be 1
166 //
167 if (CommandExecutionResult == 1)
168 {
169 //
170 // Exit from the debugger
171 //
172 exit(0);
173 }
174
175 //
176 // Zero the buffer for next command
177 //
178 RtlZeroMemory(recvbuf, COMMUNICATION_BUFFER_SIZE);
179 }
180
181 //
182 // Indicate that debugger is not connected
183 //
185
186 //
187 // Indicate that it's note a remote debugger
188 //
190
191 //
192 // Indicate that we're not in remote debugger anymore
193 //
194 ShowMessages("closing the conntection...\n");
195
196 //
197 // Close the connection
198 //
201}
#define COMMUNICATION_BUFFER_SIZE
Packet size for TCP connections.
Definition Constants.h:330
INT HyperDbgInterpreter(CHAR *Command)
Interpret commands.
Definition interpreter.cpp:280
BYTE g_EndOfBufferCheckTcp[TCP_END_OF_BUFFER_CHARS_COUNT]
the buffer that we set at the end of buffers for tcp connection
Definition globals.h:56
SOCKET g_ServerListenSocket
Server in debuggee needs an extra socket.
Definition globals.h:103
SOCKET g_SeverSocket
The socket object of guest debuggee (not debugger) it is because in HyperDbg, debugger is client and ...
Definition globals.h:97
BOOLEAN g_IsConnectedToRemoteDebugger
Shows whether the current system is a guest (debuggee) and a remote debugger is connected to this sys...
Definition globals.h:81
int RemoteConnectionSendResultsToHost(const char *sendbuf, int len)
Send the results of executing a command from deubggee (server, guest) to the debugger (client,...
Definition remote-connection.cpp:481
int CommunicationServerShutdownAndCleanupConnection(SOCKET ClientSocket, SOCKET ListenSocket)
Shutdown and cleanup connection as server.
Definition tcpserver.cpp:216
int CommunicationServerCreateServerAndWaitForClient(PCSTR Port, SOCKET *ClientSocketArg, SOCKET *ListenSocketArg)
Create server and wait for a client to connect.
Definition tcpserver.cpp:31
int CommunicationServerReceiveMessage(SOCKET ClientSocket, char *recvbuf, int recvbuflen)
listen and receive message as the server
Definition tcpserver.cpp:147
int CommunicationServerSendMessage(SOCKET ClientSocket, const char *sendbuf, int length)
send message as the server
Definition tcpserver.cpp:188

◆ RemoteConnectionSendCommand()

int RemoteConnectionSendCommand ( const char * sendbuf,
int len )

send the command as a client (debugger, host) to the server (debuggee, guest)

Parameters
sendbufaddress of message buffer
lenlength of buffer
Returns
int returning 0 means that there was no error in executing the function and 1 shows there was an error
446{
447 //
448 // Send Message
449 //
451 {
452 //
453 // Failed
454 //
455 return 1;
456 }
457
458 //
459 // We wait for the debuggee to send the message
460 //
461 WaitForSingleObject(
463 INFINITE);
464
465 //
466 // Successful
467 //
468 return 0;
469}

◆ RemoteConnectionSendResultsToHost()

int RemoteConnectionSendResultsToHost ( const char * sendbuf,
int len )

Send the results of executing a command from deubggee (server, guest) to the debugger (client, host)

Parameters
sendbufbuffer address
lenlength of buffer
Returns
int returning 0 means that there was no error in executing the function and 1 shows there was an error
482{
483 //
484 // Send the message
485 //
486 if (CommunicationServerSendMessage(g_SeverSocket, sendbuf, len) != 0)
487 {
488 //
489 // Failed
490 //
491 return 1;
492 }
493
494 return 0;
495}

◆ RemoteConnectionThreadListeningToDebuggee()

DWORD WINAPI RemoteConnectionThreadListeningToDebuggee ( LPVOID lpParam)

A thread that listens for server (debuggee) messages and show it by using ShowMessages wrapper.

Parameters
lpParam
Returns
DWORD
212{
214 UINT32 BuffLenReceived = 0;
215
217 {
218 //
219 // Receive message
220 //
222 {
223 //
224 // Failed, break
225 //
226 break;
227 }
228
229 //
230 // Check if it's end of the buffer
231 //
232 for (size_t i = 0; i < BuffLenReceived; i++)
233 {
234 if (RecvBuf[i] == g_EndOfBufferCheckTcp[0] &&
235 RecvBuf[i + 1] == g_EndOfBufferCheckTcp[1] &&
236 RecvBuf[i + 2] == g_EndOfBufferCheckTcp[2] &&
237 RecvBuf[i + 3] == g_EndOfBufferCheckTcp[3])
238 {
240
241 //
242 // Cut the last string using \x00 \x00
243 //
244 RecvBuf[i] = '\x00';
245 RecvBuf[i + 1] = '\x00';
246 break;
247 }
248 }
249
250 //
251 // This is just because we want to show a correct signature
252 //
254 {
255 //
256 // Show message from remote debuggee
257 //
258 ShowMessages("%s", RecvBuf);
259 }
260
261 //
262 // Show the signature
263 //
265 {
266 //
267 // it's not end of message anymore
268 //
270
271 //
272 // Trigger the event
273 //
275 }
276
277 //
278 // Clear the buffer
279 //
280 RtlZeroMemory(RecvBuf, COMMUNICATION_BUFFER_SIZE);
281 }
282
283 //
284 // The connection was aborted
285 // Uinitialize every connections
286 //
287
288 //
289 // Indicate that debugger is not connected
290 //
292
293 //
294 // Indicate that it's a remote debuggee
295 //
297
298 //
299 // Show the signature
300 //
302
303 return 0;
304}
#define TCP_END_OF_BUFFER_CHARS_COUNT
count of characters for tcp end of buffer
Definition Constants.h:440
VOID HyperDbgShowSignature()
Show signature of HyperDbg.
Definition interpreter.cpp:566
BOOLEAN g_IsEndOfMessageReceived
variable to keep track if the end of the message received (for showing signature)
Definition globals.h:144
BOOLEAN g_BreakPrintingOutput
Shows whether the pause command or CTRL+C or CTRL+Break is executed or not.
Definition globals.h:499

Variable Documentation

◆ g_BreakPrintingOutput

BOOLEAN g_BreakPrintingOutput
extern

Shows whether the pause command or CTRL+C or CTRL+Break is executed or not.

◆ g_ClientConnectSocket

SOCKET g_ClientConnectSocket
extern

The socket object of host debugger (not debuggee) it is because in HyperDbg, debuggee is server and debugger is a client.

89{0};

◆ g_EndOfBufferCheckTcp

BYTE g_EndOfBufferCheckTcp[TCP_END_OF_BUFFER_CHARS_COUNT]
extern

the buffer that we set at the end of buffers for tcp connection

56 {
#define TCP_END_OF_BUFFER_CHAR_2
Definition Constants.h:447
#define TCP_END_OF_BUFFER_CHAR_1
characters of the buffer that we set at the end of buffers for tcp
Definition Constants.h:446
#define TCP_END_OF_BUFFER_CHAR_4
Definition Constants.h:449
#define TCP_END_OF_BUFFER_CHAR_3
Definition Constants.h:448

◆ g_EndOfMessageReceivedEvent

HANDLE g_EndOfMessageReceivedEvent
extern

Handle to if the end of the message received (for showing signature)

◆ g_IsConnectedToHyperDbgLocally

BOOLEAN g_IsConnectedToHyperDbgLocally
extern

Shows whether the user is allowed to use 'load' command to load modules locally in VMI (virtual machine introspection) mode.

◆ g_IsConnectedToRemoteDebuggee

BOOLEAN g_IsConnectedToRemoteDebuggee
extern

Shows whether the current debugger is the host and connected to a remote debuggee (guest)

◆ g_IsConnectedToRemoteDebugger

BOOLEAN g_IsConnectedToRemoteDebugger
extern

Shows whether the current system is a guest (debuggee) and a remote debugger is connected to this system.

◆ g_IsEndOfMessageReceived

BOOLEAN g_IsEndOfMessageReceived
extern

variable to keep track if the end of the message received (for showing signature)

◆ g_RemoteDebuggeeListeningThread

HANDLE g_RemoteDebuggeeListeningThread
extern

In debugger (not debuggee), we save the ip of server debuggee in this variable to use it later e.g, in signature.

◆ g_ServerListenSocket

SOCKET g_ServerListenSocket
extern

Server in debuggee needs an extra socket.

103{0};

◆ g_SeverSocket

SOCKET g_SeverSocket
extern

The socket object of guest debuggee (not debugger) it is because in HyperDbg, debugger is client and debuggee is a server.

97{0};