HyperDbg Debugger
Loading...
Searching...
No Matches
output.cpp File Reference

output command More...

#include "pch.h"

Functions

VOID CommandOutputHelp ()
 help of the output command
 
VOID CommandOutput (vector< string > SplitCommand, string Command)
 output command handler
 

Variables

LIST_ENTRY g_OutputSources
 Holds a list of output sources created by output command.
 
BOOLEAN g_OutputSourcesInitialized
 it shows whether the debugger started using output sources or not or in other words, is g_OutputSources initialized with a variable or it is empty
 

Detailed Description

output command

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

Function Documentation

◆ CommandOutput()

VOID CommandOutput ( vector< string > SplitCommand,
string Command )

output command handler

Parameters
SplitCommand
Command
Returns
VOID
55{
56 PDEBUGGER_EVENT_FORWARDING EventForwardingObject;
59 string DetailsOfSource;
60 UINT32 IndexToShowList;
61 PLIST_ENTRY TempList = 0;
62 BOOLEAN OutputSourceFound = FALSE;
63 HANDLE SourceHandle = INVALID_HANDLE_VALUE;
64 SOCKET Socket = NULL;
65 HMODULE Module = NULL;
66 vector<string> SplitCommandCaseSensitive {Split(Command, ' ')};
67
68 if (SplitCommand.size() <= 2)
69 {
70 ShowMessages("incorrect use of the 'output'\n\n");
72 return;
73 }
74
75 //
76 // Check if the user needs a list of outputs or not
77 //
78 if (SplitCommand.size() == 1)
79 {
80 IndexToShowList = 0;
81
83 {
84 TempList = &g_OutputSources;
85
86 while (&g_OutputSources != TempList->Blink)
87 {
88 TempList = TempList->Blink;
89
90 PDEBUGGER_EVENT_FORWARDING CurrentOutputSourceDetails =
91 CONTAINING_RECORD(TempList, DEBUGGER_EVENT_FORWARDING, OutputSourcesList);
92
93 //
94 // Increase index
95 //
96 IndexToShowList++;
97
98 string TempStateString = "";
99 string TempTypeString = "";
100
101 if (CurrentOutputSourceDetails->State ==
103 {
104 TempStateString = "not opened";
105 }
106 else if (CurrentOutputSourceDetails->State ==
108 {
109 TempStateString = "opened ";
110 }
111 else if (CurrentOutputSourceDetails->State ==
113 {
114 TempStateString = "closed ";
115 }
116
117 if (CurrentOutputSourceDetails->Type == EVENT_FORWARDING_NAMEDPIPE)
118 {
119 TempTypeString = "namedpipe";
120 }
121 else if (CurrentOutputSourceDetails->Type == EVENT_FORWARDING_FILE)
122 {
123 TempTypeString = "file ";
124 }
125 else if (CurrentOutputSourceDetails->Type == EVENT_FORWARDING_TCP)
126 {
127 TempTypeString = "tcp ";
128 }
129 else if (CurrentOutputSourceDetails->Type == EVENT_FORWARDING_MODULE)
130 {
131 TempTypeString = "module ";
132 }
133
134 ShowMessages("%x %s %s\t%s\n", IndexToShowList, TempTypeString.c_str(), TempStateString.c_str(), CurrentOutputSourceDetails->Name);
135 }
136 }
137 else
138 {
139 ShowMessages("output forwarding list is empty\n");
140 }
141
142 return;
143 }
144
145 //
146 // Check if it's a create, open, or close
147 //
148 if (!SplitCommand.at(1).compare("create"))
149 {
150 //
151 // It's a create
152 //
153
154 //
155 // check if the parameters are okay for a create or not
156 //
157 if (SplitCommand.size() <= 4)
158 {
159 ShowMessages("incorrect use of the 'output'\n\n");
161 return;
162 }
163
164 //
165 // Check for the type of the output source
166 //
167 if (!SplitCommand.at(3).compare("file"))
168 {
170 }
171 else if (!SplitCommand.at(3).compare("namedpipe"))
172 {
174 }
175 else if (!SplitCommand.at(3).compare("tcp"))
176 {
178 }
179 else if (!SplitCommand.at(3).compare("module"))
180 {
182 }
183 else
184 {
185 ShowMessages("incorrect type near '%s'\n\n",
186 SplitCommand.at(3).c_str());
188 return;
189 }
190
191 //
192 // Check to make sure that the name doesn't exceed the maximum character
193 //
194 if (SplitCommand.at(2).size() >=
196 {
197 ShowMessages("name of the output cannot exceed form %d characters\n\n",
200 return;
201 }
202
203 //
204 // Search to see if there is another output source with the
205 // same name which we don't want to create two or more output
206 // sources with the same name
207 //
209 {
210 TempList = &g_OutputSources;
211
212 while (&g_OutputSources != TempList->Flink)
213 {
214 TempList = TempList->Flink;
215
216 PDEBUGGER_EVENT_FORWARDING CurrentOutputSourceDetails =
217 CONTAINING_RECORD(TempList, DEBUGGER_EVENT_FORWARDING, OutputSourcesList);
218
219 if (strcmp(CurrentOutputSourceDetails->Name,
220 SplitCommandCaseSensitive.at(2).c_str()) == 0)
221 {
222 //
223 // Indicate that we found this item
224 //
225 OutputSourceFound = TRUE;
226
227 //
228 // No need to search through the list anymore
229 //
230 break;
231 }
232 }
233
234 //
235 // Check whether the entered name already exists
236 //
237 if (OutputSourceFound)
238 {
239 ShowMessages("err, the name you entered, already exists, please choose "
240 "another name\n");
241 return;
242 }
243 }
244
245 //
246 // try to open the source and get the handle
247 //
248 DetailsOfSource = Command.substr(Command.find(SplitCommandCaseSensitive.at(3)) +
249 SplitCommandCaseSensitive.at(3).size() + 1,
250 Command.size());
251
252 SourceHandle = ForwardingCreateOutputSource(Type, DetailsOfSource, &Socket, &Module);
253
254 //
255 // Check if it's a valid handle or not
256 //
257 if (SourceHandle == INVALID_HANDLE_VALUE)
258 {
260 "err, invalid address or cannot open or find the address\n");
261 return;
262 }
263
264 //
265 // allocate the buffer for storing the event forwarding details
266 //
267 EventForwardingObject =
269
270 if (EventForwardingObject == NULL)
271 {
272 ShowMessages("err, in allocating memory for event forwarding\n");
273 return;
274 }
275
276 RtlZeroMemory(EventForwardingObject, sizeof(DEBUGGER_EVENT_FORWARDING));
277
278 //
279 // Set the state
280 //
281 EventForwardingObject->State = EVENT_FORWARDING_STATE_NOT_OPENED;
282
283 //
284 // Set the type
285 //
286 EventForwardingObject->Type = Type;
287
288 //
289 // Get a new tag
290 //
291 EventForwardingObject->OutputUniqueTag = ForwardingGetNewOutputSourceTag();
292
293 //
294 // Set the handle or in the case of TCP, set the socket
295 // or if it's a module the set the module handle
296 //
297 if (Type == EVENT_FORWARDING_TCP)
298 {
299 EventForwardingObject->Socket = Socket;
300 }
301 else if (Type == EVENT_FORWARDING_MODULE)
302 {
303 EventForwardingObject->Module = Module;
304
305 //
306 // Handle is the function address
307 //
308 EventForwardingObject->Handle = SourceHandle;
309 }
310 else
311 {
312 EventForwardingObject->Handle = SourceHandle;
313 }
314
315 //
316 // Move the name of the output source to the buffer
317 //
318 strcpy_s(EventForwardingObject->Name, SplitCommandCaseSensitive.at(2).c_str());
319
320 //
321 // Check if list is initialized or not
322 //
324 {
327 }
328
329 //
330 // Add the source to the trace list
331 //
333 &(EventForwardingObject->OutputSourcesList));
334 }
335 else if (!SplitCommand.at(1).compare("open"))
336 {
337 //
338 // It's an open
339 //
341 {
342 ShowMessages("err, the name you entered, not found\n");
343 return;
344 }
345
346 //
347 // Now we should find the corresponding object in the memory and
348 // pass it to the global open functions
349 //
350 TempList = &g_OutputSources;
351
352 while (&g_OutputSources != TempList->Flink)
353 {
354 TempList = TempList->Flink;
355
356 PDEBUGGER_EVENT_FORWARDING CurrentOutputSourceDetails = CONTAINING_RECORD(
357 TempList,
359 OutputSourcesList);
360
361 if (strcmp(CurrentOutputSourceDetails->Name,
362 SplitCommandCaseSensitive.at(2).c_str()) == 0)
363 {
364 //
365 // Indicate that we found this item
366 //
367 OutputSourceFound = TRUE;
368
369 //
370 // Open the output
371 //
372 Status = ForwardingOpenOutputSource(CurrentOutputSourceDetails);
373
375 {
376 ShowMessages("err, the name you entered was already closed\n");
377 return;
378 }
380 {
381 ShowMessages("err, the name you entered was already opened\n");
382 return;
383 }
384 else if (Status !=
386 {
387 ShowMessages("err, unable to open the output source\n");
388 return;
389 }
390
391 //
392 // No need to search through the list anymore
393 //
394 break;
395 }
396 }
397
398 if (!OutputSourceFound)
399 {
400 ShowMessages("err, the name you entered, not found\n");
401 return;
402 }
403 }
404 else if (!SplitCommand.at(1).compare("close"))
405 {
406 //
407 // It's a close
408 //
410 {
411 ShowMessages("err, the name you entered, not found\n");
412 return;
413 }
414
415 //
416 // Now we should find the corresponding object in the memory and
417 // pass it to the global close functions
418 //
419 TempList = &g_OutputSources;
420
421 while (&g_OutputSources != TempList->Flink)
422 {
423 TempList = TempList->Flink;
424
425 PDEBUGGER_EVENT_FORWARDING CurrentOutputSourceDetails = CONTAINING_RECORD(
426 TempList,
428 OutputSourcesList);
429
430 if (strcmp(CurrentOutputSourceDetails->Name,
431 SplitCommandCaseSensitive.at(2).c_str()) == 0)
432 {
433 //
434 // Indicate that we found this item
435 //
436 OutputSourceFound = TRUE;
437
438 //
439 // Close the output if it's not already closed
440 //
441 Status = ForwardingCloseOutputSource(CurrentOutputSourceDetails);
442
444 {
445 ShowMessages("err, the name you entered was already closed\n");
446 return;
447 }
449 {
450 ShowMessages("err, unable to close the source\n");
451 return;
452 }
453 else if (Status !=
455 {
456 ShowMessages("err, unable to close the source\n");
457 return;
458 }
459
460 //
461 // No need to search through the list anymore
462 //
463 break;
464 }
465 }
466
467 if (!OutputSourceFound)
468 {
469 ShowMessages("err, the name you entered, not found\n");
470 return;
471 }
472 }
473 else
474 {
475 //
476 // Invalid argument
477 //
478 ShowMessages("incorrect option at '%s'\n\n", SplitCommand.at(1).c_str());
480 return;
481 }
482}
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
FORCEINLINE VOID InitializeListHead(_Out_ PLIST_ENTRY ListHead)
Definition Windows.h:41
FORCEINLINE VOID InsertHeadList(_Inout_ PLIST_ENTRY ListHead, _Inout_ PLIST_ENTRY Entry)
Definition Windows.h:115
const vector< string > Split(const string &s, const char &c)
general split command
Definition common.cpp:117
DEBUGGER_OUTPUT_SOURCE_STATUS ForwardingCloseOutputSource(PDEBUGGER_EVENT_FORWARDING SourceDescriptor)
Closes the output source.
Definition forwarding.cpp:110
DEBUGGER_OUTPUT_SOURCE_STATUS ForwardingOpenOutputSource(PDEBUGGER_EVENT_FORWARDING SourceDescriptor)
Opens the output source.
Definition forwarding.cpp:40
VOID * ForwardingCreateOutputSource(DEBUGGER_EVENT_FORWARDING_TYPE SourceType, const string &Description, SOCKET *Socket, HMODULE *Module)
Create a new source (create handle from the source)
Definition forwarding.cpp:215
UINT64 ForwardingGetNewOutputSourceTag()
Get the output source tag and increase the global variable for tag.
Definition forwarding.cpp:28
@ 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_TYPE DEBUGGER_EVENT_FORWARDING_TYPE
event forwarding type
#define MAXIMUM_CHARACTERS_FOR_EVENT_FORWARDING_NAME
maximum characters for event forwarding source names
Definition forwarding.h:32
@ 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
@ 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
struct _DEBUGGER_EVENT_FORWARDING * PDEBUGGER_EVENT_FORWARDING
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96
NULL()
Definition test-case-generator.py:530
BOOLEAN g_OutputSourcesInitialized
it shows whether the debugger started using output sources or not or in other words,...
Definition globals.h:408
VOID CommandOutputHelp()
help of the output command
Definition output.cpp:26
LIST_ENTRY g_OutputSources
Holds a list of output sources created by output command.
Definition globals.h:417
structures hold the detail of event forwarding
Definition forwarding.h:80
LIST_ENTRY OutputSourcesList
Definition forwarding.h:88
DEBUGGER_EVENT_FORWARDING_TYPE Type
Definition forwarding.h:81
DEBUGGER_EVENT_FORWARDING_STATE State
Definition forwarding.h:82
CHAR Name[MAXIMUM_CHARACTERS_FOR_EVENT_FORWARDING_NAME]
Definition forwarding.h:89
SOCKET Socket
Definition forwarding.h:84
HMODULE Module
Definition forwarding.h:85
UINT64 OutputUniqueTag
Definition forwarding.h:86
VOID * Handle
Definition forwarding.h:83

◆ CommandOutputHelp()

VOID CommandOutputHelp ( )

help of the output command

Returns
VOID
27{
28 ShowMessages("output : creates an output instance that can be used in event "
29 "forwarding.\n\n");
30
31 ShowMessages("syntax : \toutput [create Name (string)] [file|namedpipe|tcp Address (string)]\n");
32 ShowMessages("syntax : \toutput [open|close Name (string)]\n");
33
34 ShowMessages("\n");
35 ShowMessages("\t\te.g : output create MyOutputName1 file "
36 "c:\\rev\\output.txt\n");
37 ShowMessages("\t\te.g : output create MyOutputName2 tcp 192.168.1.10:8080\n");
38 ShowMessages("\t\te.g : output create MyOutputName3 namedpipe "
39 "\\\\.\\Pipe\\HyperDbgOutput\n");
40 ShowMessages("\t\te.g : output create MyOutputName1 module "
41 "c:\\rev\\event_forwarding.dll\n");
42 ShowMessages("\t\te.g : output open MyOutputName1\n");
43 ShowMessages("\t\te.g : output close MyOutputName1\n");
44}

Variable Documentation

◆ g_OutputSources

LIST_ENTRY g_OutputSources
extern

Holds a list of output sources created by output command.

user-mode events and output sources are two separate things in HyperDbg

417{0};

◆ g_OutputSourcesInitialized

BOOLEAN g_OutputSourcesInitialized
extern

it shows whether the debugger started using output sources or not or in other words, is g_OutputSources initialized with a variable or it is empty