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

test command More...

#include "pch.h"

Functions

VOID CommandTestHelp ()
 help of the test command
BOOLEAN CommandTestPerformKernelTestsIoctl ()
 Send an IOCTL to the kernel to run the.
VOID CommandTestAllFunctionalities ()
 perform test on for all functionalities
VOID CommandTestAllHwdbg ()
 perform test for all functionalities of hwdbg
BOOLEAN CommandTestPerformTest ()
 perform test on the remote process
VOID CommandTestQueryState ()
 test command for query the state
VOID CommandTestQueryTrapState ()
 test command for query the trap state
VOID CommandTestQueryPreAllocPoolsState ()
 test command for query the state of pre-allocated pools
VOID CommandTestSetTargetTaskToHaltedCores (BOOLEAN Synchronous)
 test command for setting target tasks to halted cores
VOID CommandTestSetTargetTaskToTargetCore (UINT32 CoreNumber)
 test command for setting target task to the specified core
VOID CommandTestSetBreakpointState (BOOLEAN State)
 test command for turning on/off the breakpoints (#DB)
VOID CommandTestSetDebugBreakState (BOOLEAN State)
 test command for turning on/off the debug breaks (#DB)
VOID CommandTest (vector< CommandToken > CommandTokens, string Command)
 test command handler

Variables

BOOLEAN g_IsKdModuleLoaded
 shows whether the kernel debugger (KD) module is loaded or not
BOOLEAN g_IsSerialConnectedToRemoteDebuggee
 Shows if the debugger was connected to remote debuggee over (A remote guest).

Detailed Description

test command

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

Function Documentation

◆ CommandTest()

VOID CommandTest ( vector< CommandToken > CommandTokens,
string Command )

test command handler

Parameters
CommandTokens
Command
Returns
VOID
435{
436 UINT64 Context = NULL;
437
438 UINT32 CommandSize = (UINT32)CommandTokens.size();
439
440 if (CommandSize == 1)
441 {
442 ShowMessages("incorrect use of the '%s'\n\n",
443 GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
445 }
446 else if (CommandSize == 2 && CompareLowerCaseStrings(CommandTokens.at(1), "query"))
447 {
448 //
449 // Query the state of debuggee in debugger mode
450 //
452 }
453 else if (CommandSize == 2 && CompareLowerCaseStrings(CommandTokens.at(1), "trap-status"))
454 {
455 //
456 // Query the state of trap flag in debugger mode
457 //
459 }
460 else if (CommandSize == 2 && CompareLowerCaseStrings(CommandTokens.at(1), "pool"))
461 {
462 //
463 // Query the state of pre-allocated pools in debugger mode
464 //
466 }
467 else if (CommandSize == 2 && CompareLowerCaseStrings(CommandTokens.at(1), "sync-task"))
468 {
469 //
470 // Send target task to the halted cores in debugger mode (synchronous)
471 //
473 }
474 else if (CommandSize == 2 && CompareLowerCaseStrings(CommandTokens.at(1), "async-task"))
475 {
476 //
477 // Send target task to the halted cores in debugger mode (asynchronous)
478 //
480 }
481 else if (CommandSize == 3 && CompareLowerCaseStrings(CommandTokens.at(1), "target-core-task"))
482 {
483 if (!ConvertTokenToUInt64(CommandTokens.at(2), &Context))
484 {
485 ShowMessages("err, you should enter a valid hex number as the core id\n\n");
486 return;
487 }
488
489 //
490 // Send target task to the specific halted core in debugger mode
491 //
493 }
494 else if (CommandSize == 3 && CompareLowerCaseStrings(CommandTokens.at(1), "breakpoint"))
495 {
496 //
497 // Change breakpoint state
498 //
499 if (CompareLowerCaseStrings(CommandTokens.at(2), "on"))
500 {
502 }
503 else if (CompareLowerCaseStrings(CommandTokens.at(2), "off"))
504 {
506 }
507 else
508 {
509 ShowMessages("err, couldn't resolve error at '%s'\n\n",
510 GetCaseSensitiveStringFromCommandToken(CommandTokens.at(2)).c_str());
511 return;
512 }
513 }
514 else if (CommandSize == 3 && CompareLowerCaseStrings(CommandTokens.at(1), "trap"))
515 {
516 //
517 // Change debug break state
518 //
519 if (CompareLowerCaseStrings(CommandTokens.at(2), "on"))
520 {
522 }
523 else if (CompareLowerCaseStrings(CommandTokens.at(2), "off"))
524 {
526 }
527 else
528 {
529 ShowMessages("err, couldn't resolve error at '%s'\n\n",
530 GetCaseSensitiveStringFromCommandToken(CommandTokens.at(2)).c_str());
531 return;
532 }
533 }
534 else if (CommandSize == 2 && CompareLowerCaseStrings(CommandTokens.at(1), "all"))
535 {
536 //
537 // For testing functionalities
538 //
540 }
541 else if (CommandSize == 2 && CompareLowerCaseStrings(CommandTokens.at(1), "hwdbg"))
542 {
543 //
544 // For testing functionalities of hwdbg
545 //
547 }
548 else
549 {
550 ShowMessages("incorrect use of the '%s'\n\n",
551 GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
553 return;
554 }
555}
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
unsigned int UINT32
Definition BasicTypes.h:54
std::string GetCaseSensitiveStringFromCommandToken(CommandToken TargetToken)
Get case sensitive string from command token.
Definition common.cpp:467
BOOLEAN CompareLowerCaseStrings(CommandToken TargetToken, const CHAR *StringToCompare)
Compare lower case strings.
Definition common.cpp:503
BOOLEAN ConvertTokenToUInt64(CommandToken TargetToken, PUINT64 Result)
add ` between 64 bit values and convert them to string
Definition common.cpp:447
NULL()
Definition test-case-generator.py:530
VOID CommandTestSetTargetTaskToTargetCore(UINT32 CoreNumber)
test command for setting target task to the specified core
Definition test.cpp:354
VOID CommandTestSetTargetTaskToHaltedCores(BOOLEAN Synchronous)
test command for setting target tasks to halted cores
Definition test.cpp:332
VOID CommandTestQueryPreAllocPoolsState()
test command for query the state of pre-allocated pools
Definition test.cpp:310
VOID CommandTestAllFunctionalities()
perform test on for all functionalities
Definition test.cpp:105
VOID CommandTestSetDebugBreakState(BOOLEAN State)
test command for turning on/off the debug breaks (DB)
Definition test.cpp:403
VOID CommandTestQueryState()
test command for query the state
Definition test.cpp:268
VOID CommandTestAllHwdbg()
perform test for all functionalities of hwdbg
Definition test.cpp:153
VOID CommandTestSetBreakpointState(BOOLEAN State)
test command for turning on/off the breakpoints (DB)
Definition test.cpp:375
VOID CommandTestHelp()
help of the test command
Definition test.cpp:26
VOID CommandTestQueryTrapState()
test command for query the trap state
Definition test.cpp:289

◆ CommandTestAllFunctionalities()

VOID CommandTestAllFunctionalities ( )

perform test on for all functionalities

Returns
VOID
106{
107 HANDLE ThreadHandle;
108 HANDLE ProcessHandle;
109
110 //
111 // Test command parser
112 //
113 if (!OpenHyperDbgTestProcess(&ThreadHandle, &ProcessHandle, (CHAR *)TEST_CASE_PARAMETER_FOR_MAIN_COMMAND_PARSER))
114 {
115 ShowMessages("err, start HyperDbg test process for testing the main command parser\n");
116 return;
117 }
118
119 //
120 // Test PE parser helpers
121 //
122 if (!OpenHyperDbgTestProcess(&ThreadHandle, &ProcessHandle, (CHAR *)TEST_CASE_PARAMETER_FOR_PE_PARSER))
123 {
124 ShowMessages("err, start HyperDbg test process for testing the PE parser\n");
125 return;
126 }
127
128 //
129 // Test CodeView RSDS parser helpers
130 //
131 if (!OpenHyperDbgTestProcess(&ThreadHandle, &ProcessHandle, (CHAR *)TEST_CASE_PARAMETER_FOR_CODEVIEW_RSDS_PARSER))
132 {
133 ShowMessages("err, start HyperDbg test process for testing the CodeView RSDS parser\n");
134 return;
135 }
136
137 //
138 // Test script engine (script parser) using semantic tests
139 //
141 {
142 ShowMessages("err, start HyperDbg test process for testing semantic tests\n");
143 return;
144 }
145}
#define TEST_CASE_PARAMETER_FOR_CODEVIEW_RSDS_PARSER
Test case parameter for testing CodeView RSDS parser.
Definition Definition.h:90
#define TEST_CASE_PARAMETER_FOR_MAIN_COMMAND_PARSER
Test case parameter for testing main command parser.
Definition Definition.h:80
#define TEST_CASE_PARAMETER_FOR_SCRIPT_SEMANTIC_TEST_CASES
Test case parameter for testing semantic script tests.
Definition Definition.h:95
#define TEST_CASE_PARAMETER_FOR_PE_PARSER
Test case parameter for testing PE parser.
Definition Definition.h:85
char CHAR
Definition BasicTypes.h:33
BOOLEAN OpenHyperDbgTestProcess(PHANDLE ThreadHandle, PHANDLE ProcessHandle, CHAR *Args)
Opens test process.
Definition tests.cpp:312

◆ CommandTestAllHwdbg()

VOID CommandTestAllHwdbg ( )

perform test for all functionalities of hwdbg

Returns
VOID
154{
155 HANDLE ThreadHandle;
156 HANDLE ProcessHandle;
157
158 //
159 // Test hwdbg functionalities
160 //
161 if (!OpenHyperDbgTestProcess(&ThreadHandle, &ProcessHandle, (CHAR *)TEST_HWDBG_FUNCTIONALITIES))
162 {
163 ShowMessages("err, start HyperDbg test process for testing hwdbg functionalities\n");
164 return;
165 }
166}
#define TEST_HWDBG_FUNCTIONALITIES
Test case parameter for testing main command parser.
Definition Definition.h:75

◆ CommandTestHelp()

VOID CommandTestHelp ( )

help of the test command

Returns
VOID
27{
28 ShowMessages(
29 "test : tests essential features of HyperDbg in current machine.\n");
30
31 ShowMessages("syntax : \ttest [Task (string)]\n");
32
33 ShowMessages("\n");
34 ShowMessages("\t\te.g : test query\n");
35 ShowMessages("\t\te.g : test trap-status\n");
36 ShowMessages("\t\te.g : test pool\n");
37 ShowMessages("\t\te.g : test query\n");
38 ShowMessages("\t\te.g : test breakpoint on\n");
39 ShowMessages("\t\te.g : test breakpoint off\n");
40 ShowMessages("\t\te.g : test trap on\n");
41 ShowMessages("\t\te.g : test trap off\n");
42}

◆ CommandTestPerformKernelTestsIoctl()

BOOLEAN CommandTestPerformKernelTestsIoctl ( )

Send an IOCTL to the kernel to run the.

Returns
BOOLEAN
51{
52 BOOL Status;
53 ULONG ReturnedLength;
54 DEBUGGER_PERFORM_KERNEL_TESTS KernelTestRequest = {0};
55
57
58 //
59 // By the way, we don't need to send an input buffer
60 // to the kernel, but let's keep it like this, if we
61 // want to pass some other arguments to the kernel in
62 // the future
63 //
64 Status = DeviceIoControl(
65 g_DeviceHandle, // Handle to device
66 IOCTL_PERFORM_KERNEL_SIDE_TESTS, // IO Control Code (IOCTL)
67 &KernelTestRequest, // Input Buffer to driver.
68 SIZEOF_DEBUGGER_PERFORM_KERNEL_TESTS, // Input buffer length
69 &KernelTestRequest, // Output Buffer from driver.
70 SIZEOF_DEBUGGER_PERFORM_KERNEL_TESTS, // Length of output buffer in
71 // bytes.
72 &ReturnedLength, // Bytes placed in buffer.
73 NULL // synchronous call
74 );
75
76 if (!Status)
77 {
78 ShowMessages("ioctl failed with code 0x%x\n", GetLastError());
79 return FALSE;
80 }
81
82 if (KernelTestRequest.KernelStatus == DEBUGGER_OPERATION_WAS_SUCCESSFUL)
83 {
84 //
85 // Nothing to show
86 //
87 return TRUE;
88 }
89 else
90 {
91 //
92 // Show err message
93 //
94 ShowErrorMessage(KernelTestRequest.KernelStatus);
95 return FALSE;
96 }
97}
int BOOL
Definition BasicTypes.h:25
unsigned long ULONG
Definition BasicTypes.h:31
#define DEBUGGER_OPERATION_WAS_SUCCESSFUL
General value to indicate that the operation or request was successful.
Definition ErrorCodes.h:23
#define IOCTL_PERFORM_KERNEL_SIDE_TESTS
ioctl, to perform kernel-side tests
Definition Ioctls.h:270
#define SIZEOF_DEBUGGER_PERFORM_KERNEL_TESTS
Definition RequestStructures.h:366
struct _DEBUGGER_PERFORM_KERNEL_TESTS DEBUGGER_PERFORM_KERNEL_TESTS
request performing kernel tests
BOOLEAN ShowErrorMessage(UINT32 Error)
shows the error message
Definition debugger.cpp:40
#define ASSERT_MESSAGE_KD_NOT_LOADED
Definition common.h:29
#define AssertShowMessageReturnStmt(expr1, expr2, message1, message2, rc)
Definition common.h:59
#define ASSERT_MESSAGE_DRIVER_NOT_LOADED
Definition common.h:27
#define AssertReturnFalse
Definition common.h:21
HANDLE g_DeviceHandle
Holds the global handle of device which is used to send the request to the kernel by IOCTL,...
Definition globals.h:481
BOOLEAN g_IsKdModuleLoaded
shows whether the kernel debugger (KD) module is loaded or not
Definition globals.h:22
UINT32 KernelStatus
Definition RequestStructures.h:375

◆ CommandTestPerformTest()

BOOLEAN CommandTestPerformTest ( )

perform test on the remote process

Returns
BOOLEAN returns true if the results was true and false if the results was not ok
176{
177 BOOLEAN ResultOfTest = FALSE;
178 HANDLE PipeHandle;
179 HANDLE ThreadHandle;
180 HANDLE ProcessHandle;
181 UINT32 ReadBytes;
182 CHAR * Buffer = NULL;
183
184 //
185 // Allocate memory
186 //
188
189 if (!Buffer)
190 {
191 ShowMessages("err, enable allocate communication buffer\n");
192 return FALSE;
193 }
194
195 RtlZeroMemory(Buffer, TEST_CASE_MAXIMUM_BUFFERS_TO_COMMUNICATE);
196
197 //
198 // Create tests process to create a thread for us
199 //
200 if (!CreateProcessAndOpenPipeConnection(&PipeHandle,
201 &ThreadHandle,
202 &ProcessHandle))
203 {
204 ShowMessages("err, enable to connect to the test process\n");
205
206 free(Buffer);
207
208 return FALSE;
209 }
210
211 //
212 // ***** Perform test specific routines *****
213 //
214
215 //
216 // Wait for the result of test to be received
217 //
218
219SendCommandAndWaitForResponse:
220
221 CHAR TestCommand[] = "this is a test command";
222
224 PipeHandle,
225 TestCommand,
226 (UINT32)strlen(TestCommand) + 1);
227
228 if (!SentMessageResult)
229 {
230 //
231 // error in sending
232 //
233 return FALSE;
234 }
235
236 RtlZeroMemory(Buffer, TEST_CASE_MAXIMUM_BUFFERS_TO_COMMUNICATE);
237 ReadBytes =
239
240 if (!ReadBytes)
241 {
242 //
243 // Nothing to read
244 //
245 free(Buffer);
246
247 return FALSE;
248 }
249
250 goto SendCommandAndWaitForResponse;
251
252 //
253 // Close connection and remote process
254 //
255 CloseProcessAndClosePipeConnection(PipeHandle, ThreadHandle, ProcessHandle);
256
257 free(Buffer);
258
259 return ResultOfTest;
260}
#define TEST_CASE_MAXIMUM_BUFFERS_TO_COMMUNICATE
Maximum buffer to communicate between debugger and debuggee process.
Definition Definition.h:60
UCHAR BOOLEAN
Definition BasicTypes.h:35
BOOLEAN NamedPipeServerSendMessageToClient(HANDLE PipeHandle, CHAR *BufferToSend, INT32 BufferSize)
Send a message to the client over named pipe.
Definition namedpipe.cpp:280
UINT32 NamedPipeServerReadClientMessage(HANDLE PipeHandle, CHAR *BufferToSave, INT32 MaximumReadBufferLength)
read client message from the named pipe
Definition namedpipe.cpp:237
BOOLEAN CreateProcessAndOpenPipeConnection(PHANDLE ConnectionPipeHandle, PHANDLE ThreadHandle, PHANDLE ProcessHandle)
Create a Process And Open Pipe Connection object.
Definition tests.cpp:111
VOID CloseProcessAndClosePipeConnection(HANDLE ConnectionPipeHandle, HANDLE ThreadHandle, HANDLE ProcessHandle)
Close the pipe connection and the remote process.
Definition tests.cpp:368

◆ CommandTestQueryPreAllocPoolsState()

VOID CommandTestQueryPreAllocPoolsState ( )

test command for query the state of pre-allocated pools

Returns
VOID
311{
313 {
314 ShowMessages("err, query state of the debuggee is only possible when you connected "
315 "in debugger mode\n");
316 return;
317 }
318
319 //
320 // Send the query to the debuggee
321 //
323}
BOOLEAN g_IsSerialConnectedToRemoteDebuggee
Shows if the debugger was connected to remote debuggee over (A remote guest).
Definition globals.h:253
@ TEST_QUERY_PREALLOCATED_POOL_STATE
Definition RequestStructures.h:338
BOOLEAN KdSendTestQueryPacketToDebuggee(DEBUGGER_TEST_QUERY_STATE Type)
Send a test query request to the debuggee.
Definition kd.cpp:425

◆ CommandTestQueryState()

VOID CommandTestQueryState ( )

test command for query the state

Returns
VOID
269{
271 {
272 ShowMessages("err, query state of the debuggee is only possible when you connected "
273 "in debugger mode\n");
274 return;
275 }
276
277 //
278 // Send the query to the debuggee
279 //
281}
@ TEST_QUERY_HALTING_CORE_STATUS
Definition RequestStructures.h:337

◆ CommandTestQueryTrapState()

VOID CommandTestQueryTrapState ( )

test command for query the trap state

Returns
VOID
290{
292 {
293 ShowMessages("err, query state of the debuggee is only possible when you connected "
294 "in debugger mode\n");
295 return;
296 }
297
298 //
299 // Send the query to the debuggee
300 //
302}
@ TEST_QUERY_TRAP_STATE
Definition RequestStructures.h:339

◆ CommandTestSetBreakpointState()

VOID CommandTestSetBreakpointState ( BOOLEAN State)

test command for turning on/off the breakpoints (#DB)

Parameters
State
Returns
VOID
376{
378 {
379 ShowMessages("err, query state of the debuggee is only possible when you connected "
380 "in debugger mode\n");
381 return;
382 }
383
384 //
385 // Send the breakpoint settings to the debuggee
386 //
387 if (State)
388 {
390 }
391 else
392 {
394 }
395}
@ TEST_BREAKPOINT_TURN_OFF_BPS
Definition RequestStructures.h:340
@ TEST_BREAKPOINT_TURN_ON_BPS
Definition RequestStructures.h:341

◆ CommandTestSetDebugBreakState()

VOID CommandTestSetDebugBreakState ( BOOLEAN State)

test command for turning on/off the debug breaks (#DB)

Parameters
State
Returns
VOID
404{
406 {
407 ShowMessages("err, query state of the debuggee is only possible when you connected "
408 "in debugger mode\n");
409 return;
410 }
411
412 //
413 // Send the debug break settings to the debuggee
414 //
415 if (State)
416 {
418 }
419 else
420 {
422 }
423}
@ TEST_BREAKPOINT_TURN_OFF_DBS
Definition RequestStructures.h:347
@ TEST_BREAKPOINT_TURN_ON_DBS
Definition RequestStructures.h:348

◆ CommandTestSetTargetTaskToHaltedCores()

VOID CommandTestSetTargetTaskToHaltedCores ( BOOLEAN Synchronous)

test command for setting target tasks to halted cores

Parameters
Synchronous
Returns
VOID
333{
335 {
336 ShowMessages("err, query state of the debuggee is only possible when you connected "
337 "in debugger mode\n");
338 return;
339 }
340
341 //
342 // Send the target tasks to the halted cores
343 //
345}
@ TEST_SETTING_TARGET_TASKS_ON_HALTED_CORES_ASYNCHRONOUS
Definition RequestStructures.h:345
@ TEST_SETTING_TARGET_TASKS_ON_HALTED_CORES_SYNCHRONOUS
Definition RequestStructures.h:344

◆ CommandTestSetTargetTaskToTargetCore()

VOID CommandTestSetTargetTaskToTargetCore ( UINT32 CoreNumber)

test command for setting target task to the specified core

Parameters
CoreNumber
Returns
VOID
355{
357 {
358 ShowMessages("err, query state of the debuggee is only possible when you connected "
359 "in debugger mode\n");
360 return;
361 }
362
363 //
364 // Send the target task to the target halted core
365 //
367}
@ TEST_SETTING_TARGET_TASKS_ON_TARGET_HALTED_CORES
Definition RequestStructures.h:346
BOOLEAN KdSendTestQueryPacketWithContextToDebuggee(DEBUGGER_TEST_QUERY_STATE Type, UINT64 Context)
Send a test query request to the debuggee with the specified context.
Definition kd.cpp:460

Variable Documentation

◆ g_IsKdModuleLoaded

BOOLEAN g_IsKdModuleLoaded
extern

shows whether the kernel debugger (KD) module is loaded or not

◆ g_IsSerialConnectedToRemoteDebuggee

BOOLEAN g_IsSerialConnectedToRemoteDebuggee
extern

Shows if the debugger was connected to remote debuggee over (A remote guest).