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

Server and Client communication over NamedPipes. More...

#include "pch.h"

Functions

HANDLE NamedPipeServerCreatePipe (LPCSTR PipeName, UINT32 OutputBufferSize, UINT32 InputBufferSize)
 Create a named pipe server.
BOOLEAN NamedPipeServerWaitForClientConntection (HANDLE PipeHandle)
 wait for client connection
UINT32 NamedPipeServerReadClientMessage (HANDLE PipeHandle, CHAR *BufferToSave, INT MaximumReadBufferLength)
 read client message from the named pipe
BOOLEAN NamedPipeServerSendMessageToClient (HANDLE PipeHandle, CHAR *BufferToSend, INT BufferSize)
VOID NamedPipeServerCloseHandle (HANDLE PipeHandle)
 Close handle of server's named pipe.
HANDLE NamedPipeClientCreatePipe (LPCSTR PipeName)
 Create a client named pipe.
HANDLE NamedPipeClientCreatePipeOverlappedIo (LPCSTR PipeName)
 Create a client named pipe through overlapped I/O.
BOOLEAN NamedPipeClientSendMessage (HANDLE PipeHandle, CHAR *BufferToSend, INT BufferSize)
 send client message over named pipe
UINT32 NamedPipeClientReadMessage (HANDLE PipeHandle, CHAR *BufferToRead, INT MaximumSizeOfBuffer)
VOID NamedPipeClientClosePipe (HANDLE PipeHandle)
 close named pipe handle of client
INT NamedPipeServerExample ()
 and example of how to use named pipe as a server
INT NamedPipeClientExample ()
 and example of how to use named pipe as a client

Variables

OVERLAPPED g_OverlappedIoStructureForReadDebugger
OVERLAPPED g_OverlappedIoStructureForWriteDebugger

Detailed Description

Server and Client communication over NamedPipes.

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

Function Documentation

◆ NamedPipeClientClosePipe()

VOID NamedPipeClientClosePipe ( HANDLE PipeHandle)

close named pipe handle of client

Parameters
PipeHandle
Returns
VOID
366{
367 CloseHandle(PipeHandle);
368}

◆ NamedPipeClientCreatePipe()

HANDLE NamedPipeClientCreatePipe ( LPCSTR PipeName)

Create a client named pipe.

Pipe name format - \servername\pipe\pipename This pipe is for server on the same computer, however, pipes can be used to connect to a remote server

Parameters
PipeName
Returns
HANDLE
188{
189 HANDLE hPipe;
190
191 //
192 // Connect to the server pipe using CreateFile()
193 //
194 hPipe = CreateFileA(PipeName, // pipe name
195 GENERIC_READ | // read and write access
196 GENERIC_WRITE,
197 0, // no sharing
198 NULL, // default security attributes
199 OPEN_EXISTING, // opens existing pipe
200 0, // default attributes
201 NULL); // no template file
202
203 if (INVALID_HANDLE_VALUE == hPipe)
204 {
205 ShowMessages("err, occurred while connecting to the server (%x)\n",
206 GetLastError());
207 //
208 // One might want to check whether the server pipe is busy
209 // This sample will error out if the server pipe is busy
210 // Read on ERROR_PIPE_BUSY and WaitNamedPipe() for that
211 //
212
213 //
214 // Error
215 //
216 return NULL;
217 }
218 else
219 {
220 return hPipe;
221 }
222}
NULL()
Definition test-case-generator.py:530

◆ NamedPipeClientCreatePipeOverlappedIo()

HANDLE NamedPipeClientCreatePipeOverlappedIo ( LPCSTR PipeName)

Create a client named pipe through overlapped I/O.

Pipe name format - \servername\pipe\pipename This pipe is for server on the same computer, however, pipes can be used to connect to a remote server

Parameters
PipeName
Returns
HANDLE
235{
236 HANDLE hPipe;
237
238 //
239 // Connect to the server pipe using CreateFile()
240 //
241 hPipe = CreateFileA(PipeName, // pipe name
242 GENERIC_READ | // read and write access
243 GENERIC_WRITE,
244 0, // no sharing
245 NULL, // default security attributes
246 OPEN_EXISTING, // opens existing pipe
247 FILE_FLAG_OVERLAPPED, // Overlapped I/O
248 NULL); // no template file
249
250 if (INVALID_HANDLE_VALUE == hPipe)
251 {
252 ShowMessages("err, occurred while connecting to the server (%x)\n",
253 GetLastError());
254 //
255 // One might want to check whether the server pipe is busy
256 // This sample will error out if the server pipe is busy
257 // Read on ERROR_PIPE_BUSY and WaitNamedPipe() for that
258 //
259
260 //
261 // Error
262 //
263 return NULL;
264 }
265 else
266 {
267 //
268 // Create event for overlapped I/O (Read)
269 //
271 CreateEvent(NULL, TRUE, FALSE, NULL);
272
273 //
274 // Create event for overlapped I/O (Write)
275 //
277 CreateEvent(NULL, TRUE, FALSE, NULL);
278
279 return hPipe;
280 }
281}
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
OVERLAPPED g_OverlappedIoStructureForWriteDebugger
OVERLAPPED g_OverlappedIoStructureForReadDebugger

◆ NamedPipeClientExample()

INT NamedPipeClientExample ( )

and example of how to use named pipe as a client

Returns
INT
454{
455 HANDLE PipeHandle;
456 BOOLEAN SentMessageResult;
457 UINT32 ReadBytes;
458 const INT BufferSize = 1024;
459 CHAR Buffer[BufferSize] = "test message to send from client !!!";
460
461 PipeHandle = NamedPipeClientCreatePipe("\\\\.\\Pipe\\HyperDbgTests");
462
463 if (!PipeHandle)
464 {
465 //
466 // Unable to create handle
467 //
468 return 1;
469 }
470
471 SentMessageResult =
472 NamedPipeClientSendMessage(PipeHandle, Buffer, (UINT32)strlen(Buffer) + 1);
473
474 if (!SentMessageResult)
475 {
476 //
477 // Sending error
478 //
479 return 1;
480 }
481
482 ReadBytes = NamedPipeClientReadMessage(PipeHandle, Buffer, BufferSize);
483
484 if (!ReadBytes)
485 {
486 //
487 // Nothing to read
488 //
489 return 1;
490 }
491
492 ShowMessages("server sent the following message: %s\n", Buffer);
493
494 NamedPipeClientClosePipe(PipeHandle);
495
496 return 0;
497}
UCHAR BOOLEAN
Definition BasicTypes.h:35
int INT
Definition BasicTypes.h:43
unsigned int UINT32
Definition BasicTypes.h:54
char CHAR
Definition BasicTypes.h:33
BOOLEAN NamedPipeClientSendMessage(HANDLE PipeHandle, CHAR *BufferToSend, INT32 BufferSize)
Send client message over named pipe.
Definition namedpipe.cpp:382
HANDLE NamedPipeClientCreatePipe(LPCSTR PipeName)
Create a client named pipe.
Definition namedpipe.cpp:336
UINT32 NamedPipeClientReadMessage(HANDLE PipeHandle, CHAR *BufferToRead, INT32 MaximumSizeOfBuffer)
Read a message from the server over named pipe.
Definition namedpipe.cpp:430
VOID NamedPipeClientClosePipe(HANDLE PipeHandle)
close named pipe handle of client
Definition namedpipe.cpp:464

◆ NamedPipeClientReadMessage()

UINT32 NamedPipeClientReadMessage ( HANDLE PipeHandle,
CHAR * BufferToRead,
INT MaximumSizeOfBuffer )
332{
333 DWORD cbBytes;
334
335 //
336 // Read server response
337 //
338 BOOL bResult = ReadFile(PipeHandle, // handle to pipe
339 BufferToRead, // buffer to receive data
340 MaximumSizeOfBuffer, // size of buffer
341 &cbBytes, // number of bytes read
342 NULL); // not overlapped I/O
343
344 if ((!bResult) || (0 == cbBytes))
345 {
346 ShowMessages("err, occurred while reading from the server (%x)\n",
347 GetLastError());
348 CloseHandle(PipeHandle);
349 return NULL; // Error
350 }
351
352 //
353 // Success
354 //
355 return cbBytes;
356}
int BOOL
Definition BasicTypes.h:25
unsigned long DWORD
Definition BasicTypes.h:38

◆ NamedPipeClientSendMessage()

BOOLEAN NamedPipeClientSendMessage ( HANDLE PipeHandle,
CHAR * BufferToSend,
INT BufferSize )

send client message over named pipe

Parameters
PipeHandle
BufferToSend
BufferSize
Returns
BOOLEAN
293{
294 //
295 // We are done connecting to the server pipe,
296 // we can start communicating with
297 // the server using ReadFile()/WriteFile()
298 // on handle - hPipe
299 //
300
301 DWORD cbBytes;
302
303 //
304 // Send the message to server
305 //
306 BOOL bResult =
307 WriteFile(PipeHandle, // handle to pipe
308 BufferToSend, // buffer to write from
309 BufferSize, // number of bytes to write, include the NULL
310 &cbBytes, // number of bytes written
311 NULL); // not overlapped I/O
312
313 if ((!bResult) || (BufferSize != cbBytes))
314 {
315 ShowMessages("err, occurred while writing to the server (%x)\n",
316 GetLastError());
317 CloseHandle(PipeHandle);
318
319 return FALSE;
320 }
321 else
322 {
323 return TRUE;
324 }
325}

◆ NamedPipeServerCloseHandle()

VOID NamedPipeServerCloseHandle ( HANDLE PipeHandle)

Close handle of server's named pipe.

Parameters
PipeHandle
Returns
VOID
165{
166 CloseHandle(PipeHandle);
167}

◆ NamedPipeServerCreatePipe()

HANDLE NamedPipeServerCreatePipe ( LPCSTR PipeName,
UINT32 OutputBufferSize,
UINT32 InputBufferSize )

Create a named pipe server.

Parameters
PipeName
OutputBufferSize
InputBufferSize
Returns
HANDLE
36{
37 HANDLE hPipe;
38
39 hPipe = CreateNamedPipeA(PipeName, // pipe name
40 PIPE_ACCESS_DUPLEX, // read/write access
41 PIPE_TYPE_MESSAGE | // message type pipe
42 PIPE_READMODE_MESSAGE | // message-read mode
43 PIPE_WAIT, // blocking mode
44 PIPE_UNLIMITED_INSTANCES, // max. instances
45 OutputBufferSize, // output buffer size
46 InputBufferSize, // input buffer size
47 NMPWAIT_USE_DEFAULT_WAIT, // client time-out
48 NULL); // default security attribute
49
50 if (INVALID_HANDLE_VALUE == hPipe)
51 {
52 ShowMessages("err, rror occurred while creating the pipe (%x)\n",
53 GetLastError());
54 return NULL;
55 }
56 return hPipe;
57}

◆ NamedPipeServerExample()

INT NamedPipeServerExample ( )

and example of how to use named pipe as a server

Returns
INT
383{
384 HANDLE PipeHandle;
385 BOOLEAN SentMessageResult;
386 UINT32 ReadBytes;
387 const INT BufferSize = 1024;
388 CHAR BufferToRead[BufferSize] = {0};
389 CHAR BufferToSend[BufferSize] = "test message to send from server !!!";
390
391 PipeHandle = NamedPipeServerCreatePipe("\\\\.\\Pipe\\HyperDbgTests",
392 BufferSize,
393 BufferSize);
394 if (!PipeHandle)
395 {
396 //
397 // Error in creating handle
398 //
399 return 1;
400 }
401
403 {
404 //
405 // Error in connection
406 //
407 return 1;
408 }
409
410 ReadBytes =
411 NamedPipeServerReadClientMessage(PipeHandle, BufferToRead, BufferSize);
412
413 if (!ReadBytes)
414 {
415 //
416 // Nothing to read
417 //
418 return 1;
419 }
420
421 ShowMessages("message from client : %s\n", BufferToRead);
422
423 SentMessageResult = NamedPipeServerSendMessageToClient(
424 PipeHandle,
425 BufferToSend,
426 (UINT32)strlen(BufferToSend) + 1);
427
428 if (!SentMessageResult)
429 {
430 //
431 // error in sending
432 //
433 return 1;
434 }
435
436 NamedPipeServerCloseHandle(PipeHandle);
437
438 return 0;
439}
HANDLE NamedPipeServerCreatePipe(LPCSTR PipeName, UINT32 OutputBufferSize, UINT32 InputBufferSize)
Create a named pipe server.
Definition namedpipe.cpp:176
BOOLEAN NamedPipeServerSendMessageToClient(HANDLE PipeHandle, CHAR *BufferToSend, INT32 BufferSize)
Send a message to the client over named pipe.
Definition namedpipe.cpp:280
VOID NamedPipeServerCloseHandle(HANDLE PipeHandle)
Close handle of server's named pipe.
Definition namedpipe.cpp:313
UINT32 NamedPipeServerReadClientMessage(HANDLE PipeHandle, CHAR *BufferToSave, INT32 MaximumReadBufferLength)
read client message from the named pipe
Definition namedpipe.cpp:237
BOOLEAN NamedPipeServerWaitForClientConntection(HANDLE PipeHandle)
wait for client connection
Definition namedpipe.cpp:66

◆ NamedPipeServerReadClientMessage()

UINT32 NamedPipeServerReadClientMessage ( HANDLE PipeHandle,
CHAR * BufferToSave,
INT MaximumReadBufferLength )

read client message from the named pipe

Parameters
PipeHandle
BufferToSave
MaximumReadBufferLength
Returns
UINT32
97{
98 DWORD cbBytes;
99
100 //
101 // We are connected to the client.
102 // To communicate with the client
103 // we will use ReadFile()/WriteFile()
104 // on the pipe handle - hPipe
105 //
106
107 //
108 // Read client message
109 //
110 BOOL bResult = ReadFile(PipeHandle, // handle to pipe
111 BufferToSave, // buffer to receive data
112 MaximumReadBufferLength, // size of buffer
113 &cbBytes, // number of bytes read
114 NULL); // not overlapped I/O
115
116 if ((!bResult) || (0 == cbBytes))
117 {
118 ShowMessages("err, occurred while reading from the client (%x)\n",
119 GetLastError());
120 CloseHandle(PipeHandle);
121 return 0;
122 }
123
124 //
125 // Number of bytes that the client sends to us
126 //
127 return cbBytes;
128}

◆ NamedPipeServerSendMessageToClient()

BOOLEAN NamedPipeServerSendMessageToClient ( HANDLE PipeHandle,
CHAR * BufferToSend,
INT BufferSize )
134{
135 DWORD cbBytes;
136
137 //
138 // Reply to client
139 //
140 BOOLEAN bResult =
141 WriteFile(PipeHandle, // handle to pipe
142 BufferToSend, // buffer to write from
143 BufferSize, // number of bytes to write, include the NULL
144 &cbBytes, // number of bytes written
145 NULL); // not overlapped I/O
146
147 if ((!bResult) || (BufferSize != cbBytes))
148 {
149 ShowMessages("err, occurred while writing to the client (%x)\n",
150 GetLastError());
151 CloseHandle(PipeHandle);
152 return FALSE;
153 }
154 return TRUE;
155}

◆ NamedPipeServerWaitForClientConntection()

BOOLEAN NamedPipeServerWaitForClientConntection ( HANDLE PipeHandle)

wait for client connection

Parameters
PipeHandle
Returns
BOOLEAN
67{
68 //
69 // Wait for the client to connect
70 //
71 BOOL ClientConnected = ConnectNamedPipe(PipeHandle, NULL);
72
73 if (FALSE == ClientConnected)
74 {
75 ShowMessages("err, occurred while connecting to the client (%x)\n",
76 GetLastError());
77 CloseHandle(PipeHandle);
78 return FALSE;
79 }
80
81 //
82 // Client connected
83 //
84 return TRUE;
85}

Variable Documentation

◆ g_OverlappedIoStructureForReadDebugger

OVERLAPPED g_OverlappedIoStructureForReadDebugger
extern

◆ g_OverlappedIoStructureForWriteDebugger

OVERLAPPED g_OverlappedIoStructureForWriteDebugger
extern