-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathCPP-Basic-PipeClient.cpp
More file actions
80 lines (73 loc) · 2.16 KB
/
CPP-Basic-PipeClient.cpp
File metadata and controls
80 lines (73 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <windows.h>
#include <iostream>
typedef enum _csIL {
ANONYMOUS,
IDENTIFICATION,
IMPERSONATION,
DELEGATION
} CSIL;
const int MESSAGE_SIZE = 512;
int main()
{
LPCWSTR pwsPipePrefix = L"\\\\.\\pipe\\";
LPCWSTR pwsPipeName = NULL;
HANDLE hFile = NULL;
BOOL bPipeRead = TRUE;
LPWSTR pReadBuf[MESSAGE_SIZE] = { 0 };
LPDWORD pdwBytesRead = { 0 };
DWORD dwFlags = 0;
BOOL bEnforceSQOS = TRUE;
CSIL csImpersonationLevel = IMPERSONATION;
// explicit set SQOS if enabled
if(bEnforceSQOS) dwFlags = SECURITY_SQOS_PRESENT;
// set Impersonation Level
switch (csImpersonationLevel)
{
case ANONYMOUS:
dwFlags |= SECURITY_ANONYMOUS;
break;
case IDENTIFICATION:
dwFlags |= SECURITY_IDENTIFICATION;
break;
case IMPERSONATION:
dwFlags |= SECURITY_IMPERSONATION;
break;
case DELEGATION:
dwFlags |= SECURITY_DELEGATION;
break;
default:
dwFlags |= SECURITY_IDENTIFICATION;
break;
}
// Read name pipe to from STDIN
wchar_t wcInputPipeName[200];
wprintf(L"[*] Pipe name to connect to (max 200 chars): ");
std::wcin.getline(wcInputPipeName, 200);
int iInputLen = wcslen(wcInputPipeName);
if ( iInputLen > 200) {
wprintf(L"[-] More than 200 chars (%d).. exiting.\n", iInputLen);
return 1;
}
std::wstring wsConcatPipeName = pwsPipePrefix + (std::wstring)wcInputPipeName;
pwsPipeName = wsConcatPipeName.c_str();
// Connect to pipe server
wprintf(L"[*] Connecting to %s ...", pwsPipeName);
//hFile = CreateFile(pipeName, GENERIC_READ , 0, NULL, OPEN_EXISTING, 0, NULL);
hFile = CreateFile(pwsPipeName, GENERIC_READ, 0, NULL, OPEN_EXISTING, dwFlags, NULL);
if (hFile != INVALID_HANDLE_VALUE) wprintf(L"Success.\n");
else wprintf(L"Error: %d.", GetLastError());
// Read from pipe
while (bPipeRead) {
wprintf(L"[*] Reading from pipe...");
bPipeRead = ReadFile(hFile, pReadBuf, MESSAGE_SIZE, pdwBytesRead, NULL);
if (!bPipeRead) wprintf(L"Error: %d.\n", GetLastError());
else {
wprintf(L"Success. Read %d bytes, Received: %s\n", pdwBytesRead, pReadBuf);
}
// Sleep for some time just to add some latency
Sleep(2000);
}
// Close handle
CloseHandle(hFile);
return 0;
}