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

perform tests More...

#include "pch.h"

Functions

BOOLEAN SetupTestName (_Inout_updates_bytes_all_(BufferLength) PCHAR TestLocation, ULONG BufferLength)
 
BOOLEAN CreateProcessAndOpenPipeConnection (PHANDLE ConnectionPipeHandle, PHANDLE ThreadHandle, PHANDLE ProcessHandle)
 Create a Process And Open Pipe Connection object.
 
VOID CloseProcessAndClosePipeConnection (HANDLE ConnectionPipeHandle, HANDLE ThreadHandle, HANDLE ProcessHandle)
 Close the pipe connection and the remote process.
 

Variables

TCHAR g_TestLocation [MAX_PATH]
 Holds the location test-hyperdbg.exe.
 

Detailed Description

perform tests

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

Function Documentation

◆ CloseProcessAndClosePipeConnection()

VOID CloseProcessAndClosePipeConnection ( HANDLE ConnectionPipeHandle,
HANDLE ThreadHandle,
HANDLE ProcessHandle )

Close the pipe connection and the remote process.

Parameters
ConnectionPipeHandleHandle of remote pipe connection
ThreadHandleHandle of test thread
ProcessHandleHandle of test process
Returns
VOID
314{
315 //
316 // Close the connection and handles
317 //
318 NamedPipeServerCloseHandle(ConnectionPipeHandle);
319 CloseHandle(ThreadHandle);
320 CloseHandle(ProcessHandle);
321}
VOID NamedPipeServerCloseHandle(HANDLE PipeHandle)
Close handle of server's named pipe.
Definition namedpipe.cpp:156

◆ CreateProcessAndOpenPipeConnection()

BOOLEAN CreateProcessAndOpenPipeConnection ( PHANDLE ConnectionPipeHandle,
PHANDLE ThreadHandle,
PHANDLE ProcessHandle )

Create a Process And Open Pipe Connection object.

Parameters
ConnectionPipeHandlePointer to receive Pipe Handle
ThreadHandlePointer to receive Thread Handle
ProcessHandlePointer to receive Process Handle
Returns
BOOLEAN
114{
115 HANDLE PipeHandle;
116 BOOLEAN SentMessageResult;
117 UINT32 ReadBytes;
118 char * BufferToRead;
119 char * BufferToSend;
120 char HandshakeBuffer[] = "Hello, Dear Test Process... Yes, I'm HyperDbg Debugger :)";
121 PROCESS_INFORMATION ProcessInfo;
122 STARTUPINFO StartupInfo;
123 char CmdArgs[] = TEST_PROCESS_NAME " im-hyperdbg";
124
125 PipeHandle = NamedPipeServerCreatePipe("\\\\.\\Pipe\\HyperDbgTests",
128 if (!PipeHandle)
129 {
130 //
131 // Error in creating handle
132 //
133 return FALSE;
134 }
135
136 BufferToRead = (char *)malloc(TEST_CASE_MAXIMUM_BUFFERS_TO_COMMUNICATE);
137
138 if (!BufferToRead)
139 {
140 //
141 // Enable to allocate buffer
142 //
143 return FALSE;
144 }
145
146 BufferToSend = (char *)malloc(TEST_CASE_MAXIMUM_BUFFERS_TO_COMMUNICATE);
147
148 if (!BufferToSend)
149 {
150 //
151 // Enable to allocate buffer
152 //
153 free(BufferToSend);
154 return FALSE;
155 }
156
157 RtlZeroMemory(BufferToRead, TEST_CASE_MAXIMUM_BUFFERS_TO_COMMUNICATE);
158 RtlZeroMemory(BufferToSend, TEST_CASE_MAXIMUM_BUFFERS_TO_COMMUNICATE);
159
160 strcpy(BufferToSend, HandshakeBuffer);
161
162 //
163 // Create the Test Process
164 //
165
166 ZeroMemory(&StartupInfo, sizeof(StartupInfo));
167
168 //
169 // the only compulsory field
170 //
171 StartupInfo.cb = sizeof StartupInfo;
172
173 //
174 // Set-up path
175 //
177 {
178 //
179 // Test process not found
180 //
181 free(BufferToRead);
182 free(BufferToSend);
183
184 return FALSE;
185 }
186
187 if (CreateProcess(g_TestLocation, CmdArgs, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &StartupInfo, &ProcessInfo))
188 {
189 //
190 // Wait for message from the target process
191 //
193 {
194 //
195 // Error in connection
196 //
197 free(BufferToRead);
198 free(BufferToSend);
199
200 return FALSE;
201 }
202
203 ReadBytes =
205
206 if (!ReadBytes)
207 {
208 //
209 // Nothing to read
210 //
211
212 free(BufferToRead);
213 free(BufferToSend);
214
215 return FALSE;
216 }
217
218 if (strcmp(BufferToRead, "Hey there, Are you HyperDbg?") == 0)
219 {
220 //
221 // *** Connected to the remote process ***
222 //
223
224 //
225 // Send the hand shake message
226 //
227 SentMessageResult = NamedPipeServerSendMessageToClient(
228 PipeHandle,
229 BufferToSend,
230 (UINT32)strlen(BufferToSend) + 1);
231
232 if (!SentMessageResult)
233 {
234 //
235 // error in sending
236 //
237
238 free(BufferToRead);
239 free(BufferToSend);
240
241 return FALSE;
242 }
243
244 //
245 // Receive the request for the test case number
246 //
247 RtlZeroMemory(BufferToRead, TEST_CASE_MAXIMUM_BUFFERS_TO_COMMUNICATE);
248
250
251 if (!ReadBytes)
252 {
253 //
254 // Nothing to read
255 //
256
257 free(BufferToRead);
258 free(BufferToSend);
259
260 return FALSE;
261 }
262
263 if (strcmp(BufferToRead, "Wow! I miss you... Would you plz send test cases?") == 0)
264 {
265 //
266 // Set the output handles
267 //
268 *ConnectionPipeHandle = PipeHandle;
269 *ThreadHandle = ProcessInfo.hThread;
270 *ProcessHandle = ProcessInfo.hProcess;
271
272 free(BufferToRead);
273 free(BufferToSend);
274
275 return TRUE;
276 }
277
278 ShowMessages("err, could not handshake with the test process\n");
279
280 free(BufferToRead);
281 free(BufferToSend);
282
283 return FALSE;
284 }
285 else
286 {
287 ShowMessages("err, the process could not be started\n");
288
289 free(BufferToRead);
290 free(BufferToSend);
291
292 return FALSE;
293 }
294 }
295
296 free(BufferToRead);
297 free(BufferToSend);
298
299 return FALSE;
300}
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
#define TEST_CASE_MAXIMUM_BUFFERS_TO_COMMUNICATE
Maximum buffer to communicate between debugger and debuggee process.
Definition Definition.h:60
UINT32 NamedPipeServerReadClientMessage(HANDLE PipeHandle, char *BufferToSave, int MaximumReadBufferLength)
read client message from the named pipe
Definition namedpipe.cpp:88
BOOLEAN NamedPipeServerSendMessageToClient(HANDLE PipeHandle, char *BufferToSend, int BufferSize)
Definition namedpipe.cpp:123
BOOLEAN NamedPipeServerWaitForClientConntection(HANDLE PipeHandle)
wait for client connection
Definition namedpipe.cpp:58
HANDLE NamedPipeServerCreatePipe(LPCSTR PipeName, UINT32 OutputBufferSize, UINT32 InputBufferSize)
Create a named pipe server.
Definition namedpipe.cpp:27
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96
TCHAR g_TestLocation[MAX_PATH]
Holds the location test-hyperdbg.exe.
Definition globals.h:441
BOOLEAN SetupTestName(_Inout_updates_bytes_all_(BufferLength) PCHAR TestLocation, ULONG BufferLength)
Definition tests.cpp:26
#define TEST_PROCESS_NAME
exe name of test process
Definition tests.h:22

◆ SetupTestName()

BOOLEAN SetupTestName ( _Inout_updates_bytes_all_(BufferLength) PCHAR TestLocation,
ULONG BufferLength )
Parameters
BufferLengthSetup test process name
Returns
BOOLEAN
28{
29 HANDLE fileHandle;
30 DWORD driverLocLen = 0;
31 HMODULE ProcHandle = GetModuleHandle(NULL);
32 char * Pos;
33
34 //
35 // Get the current directory.
36 //
37
38 //
39 // We use the location of running exe instead of
40 // finding driver based on current directory
41 //
42
43 /*
44 driverLocLen = GetCurrentDirectory(BufferLength, DriverLocation);
45
46 if (driverLocLen == 0) {
47
48 ShowMessages("err, GetCurrentDirectory failed (%x)\n", GetLastError());
49
50 return FALSE;
51 }
52 */
53
54 GetModuleFileName(ProcHandle, TestLocation, BufferLength);
55
56 Pos = strrchr(TestLocation, '\\');
57 if (Pos != NULL)
58 {
59 //
60 // this will put the null terminator here. you can also copy to
61 // another string if you want, we can also use PathCchRemoveFileSpec
62 //
63 *Pos = '\0';
64 }
65
66 //
67 // Setup path name to driver file.
68 //
69 if (FAILED(StringCbCat(TestLocation, BufferLength, "\\" TEST_PROCESS_NAME)))
70 {
71 return FALSE;
72 }
73
74 //
75 // Insure driver file is in the specified directory.
76 //
77 if ((fileHandle = CreateFile(TestLocation, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) ==
78 INVALID_HANDLE_VALUE)
79 {
80 ShowMessages("%s.exe is not loaded.\n", TEST_PROCESS_NAME);
81
82 //
83 // Indicate failure.
84 //
85 return FALSE;
86 }
87
88 //
89 // Close open file handle.
90 //
91 if (fileHandle)
92 {
93 CloseHandle(fileHandle);
94 }
95
96 //
97 // Indicate success.
98 //
99 return TRUE;
100}
unsigned long DWORD
Definition BasicTypes.h:22

Variable Documentation

◆ g_TestLocation

TCHAR g_TestLocation[MAX_PATH]
extern

Holds the location test-hyperdbg.exe.

441{0};