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.
 
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
 

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
303{
304 CloseHandle(PipeHandle);
305}

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

◆ NamedPipeClientExample()

int NamedPipeClientExample ( )

and example of how to use named pipe as a client

Returns
int
398{
399 HANDLE PipeHandle;
400 BOOLEAN SentMessageResult;
401 UINT32 ReadBytes;
402 const int BufferSize = 1024;
403 char Buffer[BufferSize] = "test message to send from client !!!";
404
405 PipeHandle = NamedPipeClientCreatePipe("\\\\.\\Pipe\\HyperDbgTests");
406
407 if (!PipeHandle)
408 {
409 //
410 // Unable to create handle
411 //
412 return 1;
413 }
414
415 SentMessageResult =
416 NamedPipeClientSendMessage(PipeHandle, Buffer, (int)strlen(Buffer) + 1);
417
418 if (!SentMessageResult)
419 {
420 //
421 // Sending error
422 //
423 return 1;
424 }
425
426 ReadBytes = NamedPipeClientReadMessage(PipeHandle, Buffer, BufferSize);
427
428 if (!ReadBytes)
429 {
430 //
431 // Nothing to read
432 //
433 return 1;
434 }
435
436 printf("Server sent the following message: %s\n", Buffer);
437
438 NamedPipeClientClosePipe(PipeHandle);
439
440 return 0;
441}
UCHAR BOOLEAN
Definition BasicTypes.h:39
unsigned int UINT32
Definition BasicTypes.h:48
UINT32 NamedPipeClientReadMessage(HANDLE PipeHandle, char *BufferToRead, int MaximumSizeOfBuffer)
Definition namedpipe.cpp:268
HANDLE NamedPipeClientCreatePipe(LPCSTR PipeName)
Create a client named pipe.
Definition namedpipe.cpp:179
VOID NamedPipeClientClosePipe(HANDLE PipeHandle)
close named pipe handle of client
Definition namedpipe.cpp:302
BOOLEAN NamedPipeClientSendMessage(HANDLE PipeHandle, char *BufferToSend, int BufferSize)
send client message over named pipe
Definition namedpipe.cpp:225

◆ NamedPipeClientReadMessage()

UINT32 NamedPipeClientReadMessage ( HANDLE PipeHandle,
char * BufferToRead,
int MaximumSizeOfBuffer )
269{
270 DWORD cbBytes;
271
272 //
273 // Read server response
274 //
275 BOOL bResult = ReadFile(PipeHandle, // handle to pipe
276 BufferToRead, // buffer to receive data
277 MaximumSizeOfBuffer, // size of buffer
278 &cbBytes, // number of bytes read
279 NULL); // not overlapped I/O
280
281 if ((!bResult) || (0 == cbBytes))
282 {
283 printf("err, occurred while reading from the server (%x)\n",
284 GetLastError());
285 CloseHandle(PipeHandle);
286 return NULL; // Error
287 }
288
289 //
290 // Success
291 //
292 return cbBytes;
293}
int BOOL
Definition BasicTypes.h:23
unsigned long DWORD
Definition BasicTypes.h:22

◆ NamedPipeClientSendMessage()

BOOLEAN NamedPipeClientSendMessage ( HANDLE PipeHandle,
char * BufferToSend,
int BufferSize )

send client message over named pipe

Parameters
PipeHandle
BufferToSend
BufferSize
Returns
BOOLEAN
226{
227 //
228 // We are done connecting to the server pipe,
229 // we can start communicating with
230 // the server using ReadFile()/WriteFile()
231 // on handle - hPipe
232 //
233
234 DWORD cbBytes;
235
236 //
237 // Send the message to server
238 //
239 BOOL bResult =
240 WriteFile(PipeHandle, // handle to pipe
241 BufferToSend, // buffer to write from
242 BufferSize, // number of bytes to write, include the NULL
243 &cbBytes, // number of bytes written
244 NULL); // not overlapped I/O
245
246 if ((!bResult) || (BufferSize != cbBytes))
247 {
248 printf("err, occurred while writing to the server (%x)\n",
249 GetLastError());
250 CloseHandle(PipeHandle);
251
252 //
253 // Error
254 //
255 CloseHandle(PipeHandle);
256 return FALSE;
257 }
258 else
259 {
260 return TRUE;
261 }
262}
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54

◆ NamedPipeServerCloseHandle()

VOID NamedPipeServerCloseHandle ( HANDLE PipeHandle)

Close handle of server's named pipe.

Parameters
PipeHandle
Returns
VOID
157{
158 CloseHandle(PipeHandle);
159}

◆ NamedPipeServerCreatePipe()

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

Create a named pipe server.

Parameters
PipeName
OutputBufferSize
InputBufferSize
Returns
HANDLE
28{
29 HANDLE hPipe;
30
31 hPipe = CreateNamedPipeA(PipeName, // pipe name
32 PIPE_ACCESS_DUPLEX, // read/write access
33 PIPE_TYPE_MESSAGE | // message type pipe
34 PIPE_READMODE_MESSAGE | // message-read mode
35 PIPE_WAIT, // blocking mode
36 PIPE_UNLIMITED_INSTANCES, // max. instances
37 OutputBufferSize, // output buffer size
38 InputBufferSize, // input buffer size
39 NMPWAIT_USE_DEFAULT_WAIT, // client time-out
40 NULL); // default security attribute
41
42 if (INVALID_HANDLE_VALUE == hPipe)
43 {
44 printf("err, occurred while creating the pipe (%x)\n",
45 GetLastError());
46 return NULL;
47 }
48 return hPipe;
49}

◆ NamedPipeServerExample()

int NamedPipeServerExample ( )

and example of how to use named pipe as a server

Returns
int
320{
321 HANDLE PipeHandle;
322 BOOLEAN SentMessageResult;
323 UINT32 ReadBytes;
324 const int BufferSize = 1024;
325 char BufferToRead[BufferSize] = {0};
326 char BufferToSend[BufferSize] = "test message to send from server !!!";
327
328 printf("create name pipe\n");
329 PipeHandle = NamedPipeServerCreatePipe("\\\\.\\Pipe\\HyperDbgTests",
330 BufferSize,
331 BufferSize);
332 if (!PipeHandle)
333 {
334 //
335 // Error in creating handle
336 //
337 return 1;
338 }
339
340 printf("success!\n");
341 printf("wait for the client connection\n");
342
344 {
345 //
346 // Error in connection
347 //
348 return 1;
349 }
350
351 printf("client connected\n");
352 printf("read client message\n");
353
354 ReadBytes =
355 NamedPipeServerReadClientMessage(PipeHandle, BufferToRead, BufferSize);
356
357 if (!ReadBytes)
358 {
359 //
360 // Nothing to read
361 //
362 return 1;
363 }
364
365 printf("Message from client : %s\n", BufferToRead);
366
367 SentMessageResult = NamedPipeServerSendMessageToClient(
368 PipeHandle,
369 BufferToSend,
370 (int)strlen(BufferToSend) + 1);
371
372 if (!SentMessageResult)
373 {
374 //
375 // error in sending
376 //
377 return 1;
378 }
379
380 NamedPipeServerCloseHandle(PipeHandle);
381
382 return 0;
383}
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 NamedPipeServerCloseHandle(HANDLE PipeHandle)
Close handle of server's named pipe.
Definition namedpipe.cpp:156

◆ NamedPipeServerReadClientMessage()

UINT32 NamedPipeServerReadClientMessage ( HANDLE PipeHandle,
char * BufferToSave,
int MaximumReadBufferLength )

read client message from the named pipe

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

◆ NamedPipeServerSendMessageToClient()

BOOLEAN NamedPipeServerSendMessageToClient ( HANDLE PipeHandle,
char * BufferToSend,
int BufferSize )
126{
127 DWORD cbBytes;
128
129 //
130 // Reply to client
131 //
132 BOOLEAN bResult =
133 WriteFile(PipeHandle, // handle to pipe
134 BufferToSend, // buffer to write from
135 BufferSize, // number of bytes to write, include the NULL
136 &cbBytes, // number of bytes written
137 NULL); // not overlapped I/O
138
139 if ((!bResult) || (BufferSize != cbBytes))
140 {
141 printf("Error occurred while writing to the client (%x)\n",
142 GetLastError());
143 CloseHandle(PipeHandle);
144 return FALSE;
145 }
146 return TRUE;
147}

◆ NamedPipeServerWaitForClientConntection()

BOOLEAN NamedPipeServerWaitForClientConntection ( HANDLE PipeHandle)

wait for client connection

Parameters
PipeHandle
Returns
BOOLEAN
59{
60 //
61 // Wait for the client to connect
62 //
63 BOOL bClientConnected = ConnectNamedPipe(PipeHandle, NULL);
64
65 if (FALSE == bClientConnected)
66 {
67 printf("err, occurred while connecting to the client (%x)\n",
68 GetLastError());
69 CloseHandle(PipeHandle);
70 return FALSE;
71 }
72
73 //
74 // Client connected
75 //
76 return TRUE;
77}