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

Headers for event source forwarding. More...

Go to the source code of this file.

Classes

struct  _DEBUGGER_EVENT_FORWARDING
 structures hold the detail of event forwarding More...
 

Macros

#define MAXIMUM_CHARACTERS_FOR_EVENT_FORWARDING_NAME   50
 maximum characters for event forwarding source names
 

Typedefs

typedef void(* hyperdbg_event_forwarding_t) (const char *, unsigned int)
 maximum characters for event forwarding source names
 
typedef enum _DEBUGGER_EVENT_FORWARDING_TYPE DEBUGGER_EVENT_FORWARDING_TYPE
 event forwarding type
 
typedef enum _DEBUGGER_EVENT_FORWARDING_STATE DEBUGGER_EVENT_FORWARDING_STATE
 event forwarding states
 
typedef enum _DEBUGGER_OUTPUT_SOURCE_STATUS DEBUGGER_OUTPUT_SOURCE_STATUS
 output source status
 
typedef struct _DEBUGGER_EVENT_FORWARDING DEBUGGER_EVENT_FORWARDING
 structures hold the detail of event forwarding
 
typedef struct _DEBUGGER_EVENT_FORWARDINGPDEBUGGER_EVENT_FORWARDING
 

Enumerations

enum  _DEBUGGER_EVENT_FORWARDING_TYPE { EVENT_FORWARDING_NAMEDPIPE , EVENT_FORWARDING_FILE , EVENT_FORWARDING_TCP , EVENT_FORWARDING_MODULE }
 event forwarding type More...
 
enum  _DEBUGGER_EVENT_FORWARDING_STATE { EVENT_FORWARDING_STATE_NOT_OPENED , EVENT_FORWARDING_STATE_OPENED , EVENT_FORWARDING_CLOSED }
 event forwarding states More...
 
enum  _DEBUGGER_OUTPUT_SOURCE_STATUS {
  DEBUGGER_OUTPUT_SOURCE_STATUS_SUCCESSFULLY_OPENED , DEBUGGER_OUTPUT_SOURCE_STATUS_SUCCESSFULLY_CLOSED , DEBUGGER_OUTPUT_SOURCE_STATUS_ALREADY_OPENED , DEBUGGER_OUTPUT_SOURCE_STATUS_ALREADY_CLOSED ,
  DEBUGGER_OUTPUT_SOURCE_STATUS_UNKNOWN_ERROR
}
 output source status More...
 

Functions

UINT64 ForwardingGetNewOutputSourceTag ()
 Get the output source tag and increase the global variable for tag.
 
DEBUGGER_OUTPUT_SOURCE_STATUS ForwardingOpenOutputSource (PDEBUGGER_EVENT_FORWARDING SourceDescriptor)
 Opens the output source.
 
DEBUGGER_OUTPUT_SOURCE_STATUS ForwardingCloseOutputSource (PDEBUGGER_EVENT_FORWARDING SourceDescriptor)
 Closes the output source.
 
BOOLEAN ForwardingCheckAndPerformEventForwarding (UINT32 OperationCode, CHAR *Message, UINT32 MessageLength)
 Check and send the event result to the corresponding sources.
 
BOOLEAN ForwardingWriteToFile (HANDLE FileHandle, CHAR *Message, UINT32 MessageLength)
 Write the output results to the file.
 
BOOLEAN ForwardingSendToNamedPipe (HANDLE NamedPipeHandle, CHAR *Message, UINT32 MessageLength)
 Send the output results to the namedpipe.
 
BOOLEAN ForwardingSendToTcpSocket (SOCKET TcpSocket, CHAR *Message, UINT32 MessageLength)
 Send the output results to the tcp socket.
 
VOIDForwardingCreateOutputSource (DEBUGGER_EVENT_FORWARDING_TYPE SourceType, const string &Description, SOCKET *Socket, HMODULE *Module)
 Create a new source (create handle from the source)
 

Detailed Description

Headers for event source forwarding.

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

Macro Definition Documentation

◆ MAXIMUM_CHARACTERS_FOR_EVENT_FORWARDING_NAME

#define MAXIMUM_CHARACTERS_FOR_EVENT_FORWARDING_NAME   50

maximum characters for event forwarding source names

Typedef Documentation

◆ DEBUGGER_EVENT_FORWARDING

structures hold the detail of event forwarding

◆ DEBUGGER_EVENT_FORWARDING_STATE

◆ DEBUGGER_EVENT_FORWARDING_TYPE

◆ DEBUGGER_OUTPUT_SOURCE_STATUS

output source status

this enum is used as the result returned from the functions that work with opening and closing sources

◆ hyperdbg_event_forwarding_t

typedef void(* hyperdbg_event_forwarding_t) (const char *, unsigned int)

maximum characters for event forwarding source names

◆ PDEBUGGER_EVENT_FORWARDING

Enumeration Type Documentation

◆ _DEBUGGER_EVENT_FORWARDING_STATE

event forwarding states

Enumerator
EVENT_FORWARDING_STATE_NOT_OPENED 
EVENT_FORWARDING_STATE_OPENED 
EVENT_FORWARDING_CLOSED 
52{
56
@ EVENT_FORWARDING_STATE_OPENED
Definition forwarding.h:54
@ EVENT_FORWARDING_CLOSED
Definition forwarding.h:55
@ EVENT_FORWARDING_STATE_NOT_OPENED
Definition forwarding.h:53
enum _DEBUGGER_EVENT_FORWARDING_STATE DEBUGGER_EVENT_FORWARDING_STATE
event forwarding states

◆ _DEBUGGER_EVENT_FORWARDING_TYPE

event forwarding type

Enumerator
EVENT_FORWARDING_NAMEDPIPE 
EVENT_FORWARDING_FILE 
EVENT_FORWARDING_TCP 
EVENT_FORWARDING_MODULE 
39{
44
enum _DEBUGGER_EVENT_FORWARDING_TYPE DEBUGGER_EVENT_FORWARDING_TYPE
event forwarding type
@ EVENT_FORWARDING_TCP
Definition forwarding.h:42
@ EVENT_FORWARDING_MODULE
Definition forwarding.h:43
@ EVENT_FORWARDING_FILE
Definition forwarding.h:41
@ EVENT_FORWARDING_NAMEDPIPE
Definition forwarding.h:40

◆ _DEBUGGER_OUTPUT_SOURCE_STATUS

output source status

this enum is used as the result returned from the functions that work with opening and closing sources

Enumerator
DEBUGGER_OUTPUT_SOURCE_STATUS_SUCCESSFULLY_OPENED 
DEBUGGER_OUTPUT_SOURCE_STATUS_SUCCESSFULLY_CLOSED 
DEBUGGER_OUTPUT_SOURCE_STATUS_ALREADY_OPENED 
DEBUGGER_OUTPUT_SOURCE_STATUS_ALREADY_CLOSED 
DEBUGGER_OUTPUT_SOURCE_STATUS_UNKNOWN_ERROR 
66{
72
@ DEBUGGER_OUTPUT_SOURCE_STATUS_SUCCESSFULLY_OPENED
Definition forwarding.h:67
@ DEBUGGER_OUTPUT_SOURCE_STATUS_UNKNOWN_ERROR
Definition forwarding.h:71
@ DEBUGGER_OUTPUT_SOURCE_STATUS_ALREADY_OPENED
Definition forwarding.h:69
@ DEBUGGER_OUTPUT_SOURCE_STATUS_SUCCESSFULLY_CLOSED
Definition forwarding.h:68
@ DEBUGGER_OUTPUT_SOURCE_STATUS_ALREADY_CLOSED
Definition forwarding.h:70
enum _DEBUGGER_OUTPUT_SOURCE_STATUS DEBUGGER_OUTPUT_SOURCE_STATUS
output source status

Function Documentation

◆ ForwardingCheckAndPerformEventForwarding()

BOOLEAN ForwardingCheckAndPerformEventForwarding ( UINT32 OperationCode,
CHAR * Message,
UINT32 MessageLength )

Check and send the event result to the corresponding sources.

Parameters
OperationCodeThe target operation code or tag
MessageLengthLength of the message

This function will not check whether the event has an output source or not, the caller if this function should make sure that the following event has valid output sources or not

Returns
BOOLEAN whether sending results was successful or not
442{
443 PLIST_ENTRY TempList;
444 BOOLEAN OutputSourceFound = FALSE;
445
446 //
447 // We should check whether the following flag matches
448 // with an output or not, also this is not where we want to
449 // check output resources
450 //
451 TempList = &g_EventTrace;
452 while (&g_EventTrace != TempList->Blink)
453 {
454 TempList = TempList->Blink;
455
456 PDEBUGGER_GENERAL_EVENT_DETAIL EventDetail = CONTAINING_RECORD(
457 TempList,
459 CommandsEventList);
460
461 if (EventDetail->HasCustomOutput && (UINT32)EventDetail->Tag == OperationCode)
462 {
463 //
464 // Output source found
465 //
466 OutputSourceFound = TRUE;
467
468 //
469 // Send the event to output sources
470 // Minus one (-1) is because we want to
471 // remove the null character at end of the message
472 //
474 EventDetail,
475 Message,
476 MessageLength))
477 {
478 ShowMessages("err, there was an error transferring the "
479 "message to the remote sources\n");
480 }
481
482 break;
483 }
484 }
485
486 return OutputSourceFound;
487}
UCHAR BOOLEAN
Definition BasicTypes.h:39
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54
unsigned int UINT32
Definition BasicTypes.h:48
LIST_ENTRY g_EventTrace
Holds a list of events in kernel and the state of events and the commands to show the state of each c...
Definition globals.h:400
BOOLEAN ForwardingPerformEventForwarding(PDEBUGGER_GENERAL_EVENT_DETAIL EventDetail, CHAR *Message, UINT32 MessageLength)
Send the event result to the corresponding sources.
Definition forwarding.cpp:343
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96
Each command is like the following struct, it also used for tracing works in user mode and sending it...
Definition Events.h:350
UINT64 Tag
Definition Events.h:388
BOOLEAN HasCustomOutput
Definition Events.h:372

◆ ForwardingCloseOutputSource()

DEBUGGER_OUTPUT_SOURCE_STATUS ForwardingCloseOutputSource ( PDEBUGGER_EVENT_FORWARDING SourceDescriptor)

Closes the output source.

Parameters
SourceDescriptorDescriptor of the source
Returns
DEBUGGER_OUTPUT_SOURCE_STATUS return status of the closing function
111{
112 //
113 // Check if already closed
114 //
115 if (SourceDescriptor->State == EVENT_FORWARDING_CLOSED)
116 {
118 }
119
120 //
121 // Check if not opened
122 //
123 if (SourceDescriptor->State == EVENT_FORWARDING_STATE_NOT_OPENED ||
124 SourceDescriptor->State != EVENT_FORWARDING_STATE_OPENED)
125 {
126 //
127 // Not opennd ? or state other than opened ?
128 //
130 }
131
132 //
133 // Set the state
134 //
135 SourceDescriptor->State = EVENT_FORWARDING_CLOSED;
136
137 //
138 // Now, it's time to close the source based on its type
139 //
140 if (SourceDescriptor->Type == EVENT_FORWARDING_FILE)
141 {
142 //
143 // Close the handle
144 //
145 CloseHandle(SourceDescriptor->Handle);
146
147 //
148 // Return the status
149 //
151 }
152 else if (SourceDescriptor->Type == EVENT_FORWARDING_TCP)
153 {
154 //
155 // Shutdown connection
156 //
158
159 //
160 // Cleanup
161 //
162 CommunicationClientCleanup(SourceDescriptor->Socket);
163
164 //
165 // Return the status
166 //
168 }
169 else if (SourceDescriptor->Type == EVENT_FORWARDING_NAMEDPIPE)
170 {
171 //
172 // Close the file
173 //
174 NamedPipeClientClosePipe(SourceDescriptor->Handle);
175
176 //
177 // Return the status
178 //
180 }
181 else if (SourceDescriptor->Type == EVENT_FORWARDING_MODULE)
182 {
183 //
184 // Free the library
185 //
186 FreeLibrary(SourceDescriptor->Module);
187
188 //
189 // Return the status
190 //
192 }
193
195}
VOID NamedPipeClientClosePipe(HANDLE PipeHandle)
close named pipe handle of client
Definition namedpipe.cpp:302
DEBUGGER_EVENT_FORWARDING_TYPE Type
Definition forwarding.h:81
DEBUGGER_EVENT_FORWARDING_STATE State
Definition forwarding.h:82
SOCKET Socket
Definition forwarding.h:84
HMODULE Module
Definition forwarding.h:85
VOID * Handle
Definition forwarding.h:83
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

◆ ForwardingCreateOutputSource()

VOID * ForwardingCreateOutputSource ( DEBUGGER_EVENT_FORWARDING_TYPE SourceType,
const string & Description,
SOCKET * Socket,
HMODULE * Module )

Create a new source (create handle from the source)

Parameters
SourceTypeType of the source
DescriptionDescription of the source
SocketSocket object in the case of TCP connection
ModuleModule object in the case of loading modules

If the target connection is a tcp connection then there is no handle and instead there is a socket, this way we pass a valid value for handle (TRUE) which is not a valid handle but it indicates that the operation was successful and the caller can use the pointer that it passed as the socket. On anything other than tcp sockets, the socket pointer in not modified; thus, it's not value

Returns
HANDLE returns handle of the source
219{
220 string IpPortDelimiter;
221 string Ip;
222 string Port;
223
224 if (SourceType == EVENT_FORWARDING_FILE)
225 {
226 //
227 // Create a new file
228 //
229 HANDLE FileHandle = CreateFileA(Description.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
230
231 //
232 // The handle might be INVALID_HANDLE_VALUE which will be
233 // checked by the caller
234 //
235 return (void *)FileHandle;
236 }
237 else if (SourceType == EVENT_FORWARDING_MODULE)
238 {
239 HMODULE ModuleHandle = LoadLibraryA(Description.c_str());
240
241 if (ModuleHandle == NULL)
242 {
243 ShowMessages("err, unable to load the module\n");
244 return INVALID_HANDLE_VALUE;
245 }
246
247 hyperdbg_event_forwarding_t hyperdbg_event_forwarding = (hyperdbg_event_forwarding_t)GetProcAddress(ModuleHandle, "hyperdbg_event_forwarding");
248
249 if (hyperdbg_event_forwarding == NULL)
250 {
251 ShowMessages("err, unable to find the 'hyperdbg_event_forwarding' function\n");
252 return INVALID_HANDLE_VALUE;
253 }
254
255 //
256 // Set the module handle
257 //
258 *Module = ModuleHandle;
259
260 //
261 // The handle is the location of the hyperdbg_event_forwarding function
262 //
263 return (void *)hyperdbg_event_forwarding;
264 }
265 else if (SourceType == EVENT_FORWARDING_NAMEDPIPE)
266 {
267 HANDLE PipeHandle = NamedPipeClientCreatePipe(Description.c_str());
268
269 if (!PipeHandle)
270 {
271 //
272 // Unable to create handle
273 //
274 return INVALID_HANDLE_VALUE;
275 }
276
277 return (void *)PipeHandle;
278 }
279 else if (SourceType == EVENT_FORWARDING_TCP)
280 {
281 //
282 // Check if the port is included in the description string or not
283 //
284 if (Description.find(':') != std::string::npos)
285 {
286 //
287 // Split the ip and port by : delimiter
288 //
289 IpPortDelimiter = ':';
290 size_t find = Description.find(IpPortDelimiter);
291 Ip = Description.substr(0, find);
292 Port = Description.substr(find + 1, find + Description.size());
293
294 //
295 // Connect to server, in this function 0 means there was no error
296 // and 1 means there was an error
297 //
298 if (CommunicationClientConnectToServer(Ip.c_str(), Port.c_str(), Socket) == 0)
299 {
300 //
301 // Send a fake handle just to avoid sending INVALID_HANDLE_VALUE
302 // because this functionality doesn't work with handlers; however,
303 // send 1 or TRUE is a valid handle
304 //
305 return (void *)TRUE;
306 }
307 else
308 {
309 //
310 // There was an error in connecting to the server
311 // so return an invalid handle
312 //
313 return INVALID_HANDLE_VALUE;
314 }
315 }
316 else
317 {
318 //
319 // Invalid address format
320 //
321 return INVALID_HANDLE_VALUE;
322 }
323 }
324
325 //
326 // By default, handle is invalid
327 //
328 return INVALID_HANDLE_VALUE;
329}
PHANDLE FileHandle
Definition Hooks.h:129
void(* hyperdbg_event_forwarding_t)(const char *, unsigned int)
maximum characters for event forwarding source names
Definition forwarding.h:22
HANDLE NamedPipeClientCreatePipe(LPCSTR PipeName)
Create a client named pipe.
Definition namedpipe.cpp:179
int CommunicationClientConnectToServer(PCSTR Ip, PCSTR Port, SOCKET *ConnectSocketArg)
communication for client, connecting to the server
Definition tcpclient.cpp:23

◆ ForwardingGetNewOutputSourceTag()

UINT64 ForwardingGetNewOutputSourceTag ( )

Get the output source tag and increase the global variable for tag.

Returns
UINT64
29{
30 return g_OutputSourceTag++;
31}
UINT64 g_OutputSourceTag
This variable holds the trace and generate numbers for unique tag of the output resources.
Definition globals.h:382

◆ ForwardingOpenOutputSource()

DEBUGGER_OUTPUT_SOURCE_STATUS ForwardingOpenOutputSource ( PDEBUGGER_EVENT_FORWARDING SourceDescriptor)

Opens the output source.

Parameters
SourceDescriptorDescriptor of the source
Returns
DEBUGGER_OUTPUT_SOURCE_STATUS return status of the opening function
41{
42 //
43 // Check if already closed
44 //
45 if (SourceDescriptor->State == EVENT_FORWARDING_CLOSED)
46 {
48 }
49
50 //
51 // check if already opened
52 //
53 if (SourceDescriptor->State == EVENT_FORWARDING_STATE_OPENED)
54 {
56 }
57
58 //
59 // Set the status to opened
60 //
61 SourceDescriptor->State = EVENT_FORWARDING_STATE_OPENED;
62
63 //
64 // Now, it's time to open the source based on its type
65 //
66 if (SourceDescriptor->Type == EVENT_FORWARDING_FILE)
67 {
68 //
69 // Nothing special to do here, file is opened with CreateFile
70 // and nothing should be called to open the handle
71 //
73 }
74 else if (SourceDescriptor->Type == EVENT_FORWARDING_NAMEDPIPE)
75 {
76 //
77 // Nothing special to do here, namedpipe is opened with CreateFile
78 // and nothing should be called to open the handle
79 //
81 }
82 else if (SourceDescriptor->Type == EVENT_FORWARDING_TCP)
83 {
84 //
85 // Nothing special to do here, tcp socket is opened with
86 // CommunicationClientConnectToServer and nothing should be
87 // called to open the socket
88 //
90 }
91 else if (SourceDescriptor->Type == EVENT_FORWARDING_MODULE)
92 {
93 //
94 // Nothing special to do here, function is found previously
95 // and nothing should be called to open the module
96 //
98 }
99
101}

◆ ForwardingSendToNamedPipe()

BOOLEAN ForwardingSendToNamedPipe ( HANDLE NamedPipeHandle,
CHAR * Message,
UINT32 MessageLength )

Send the output results to the namedpipe.

Parameters
NamedPipeHandleHandle of the target namedpipe
MessageThe message that should be sent to namedpipe
MessageLengthLength of the message

This function will not check whether the event has an output source or not, the caller if this function should make sure that the following event has valid output sources or not

Returns
BOOLEAN whether the sending to the namedpipe was successful or not
563{
564 BOOLEAN SentMessageResult;
565
566 SentMessageResult =
567 NamedPipeClientSendMessage(NamedPipeHandle, Message, MessageLength);
568
569 if (!SentMessageResult)
570 {
571 //
572 // Sending error
573 //
574 return FALSE;
575 }
576
577 //
578 // Successfully sent
579 //
580 return TRUE;
581}
BOOLEAN NamedPipeClientSendMessage(HANDLE PipeHandle, char *BufferToSend, int BufferSize)
send client message over named pipe
Definition namedpipe.cpp:225

◆ ForwardingSendToTcpSocket()

BOOLEAN ForwardingSendToTcpSocket ( SOCKET TcpSocket,
CHAR * Message,
UINT32 MessageLength )

Send the output results to the tcp socket.

Parameters
TcpSocketSocket object of the target tcp socket
MessageThe message that should be sent to the tcp socket
MessageLengthLength of the message

This function will not check whether the event has an output source or not, the caller if this function should make sure that the following event has valid output sources or not

Returns
BOOLEAN whether the sending to the tcp socket was successful or not
596{
597 if (CommunicationClientSendMessage(TcpSocket, Message, MessageLength) != 0)
598 {
599 //
600 // Failed to send
601 //
602 return FALSE;
603 }
604
605 //
606 // Successfully sent
607 //
608 return TRUE;
609}
int CommunicationClientSendMessage(SOCKET ConnectSocket, const char *sendbuf, int buflen)
Send message a client.
Definition tcpclient.cpp:111

◆ ForwardingWriteToFile()

BOOLEAN ForwardingWriteToFile ( HANDLE FileHandle,
CHAR * Message,
UINT32 MessageLength )

Write the output results to the file.

Parameters
FileHandleHandle of the target file
MessageThe message that should be written to file
MessageLengthLength of the message

This function will not check whether the event has an output source or not, the caller if this function should make sure that the following event has valid output sources or not

Returns
BOOLEAN whether the writing to the file was successful or not
502{
503 DWORD BytesWritten = 0;
504 BOOL ErrorFlag = FALSE;
505
506 ErrorFlag = WriteFile(FileHandle, // open file handle
507 Message, // start of data to write
508 MessageLength, // number of bytes to write
509 &BytesWritten, // number of bytes that were written
510 NULL); // no overlapped structure
511
512 return TRUE;
513
514 if (ErrorFlag == FALSE)
515 {
516 //
517 // Err, terminal failure: Unable to write to file
518 //
519
520 return FALSE;
521 }
522 else
523 {
524 if (BytesWritten != MessageLength)
525 {
526 //
527 // This is an error because a synchronous write that results in
528 // success (WriteFile returns TRUE) should write all data as
529 // requested. This would not necessarily be the case for
530 // asynchronous writes.
531 //
532
533 return FALSE;
534 }
535 else
536 {
537 //
538 // Successfully wrote
539 //
540 return TRUE;
541 }
542 }
543
544 //
545 // by default we assume there was an error
546 //
547 return FALSE;
548}
int BOOL
Definition BasicTypes.h:23
unsigned long DWORD
Definition BasicTypes.h:22