-
Notifications
You must be signed in to change notification settings - Fork 121
/
UsePipeToExeCmd.cpp
61 lines (48 loc) · 1.22 KB
/
UsePipeToExeCmd.cpp
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
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "User32.lib")
char *ExeCmd(WCHAR *pszCmd)
{
SECURITY_ATTRIBUTES sa;
HANDLE hRead, hWrite;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&hRead, &hWrite, &sa, 0))
{
return ("[!] CreatePipe failed.");
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = hWrite;
si.hStdOutput = hWrite;
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
WCHAR command[MAX_PATH];
wsprintf(command, L"cmd.exe /c %ws", pszCmd);
if (!CreateProcess(NULL, command, NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi))
return ("[!] CreateProcess failed.");
CloseHandle(hWrite);
char buffer[4096] = { 0 };
DWORD bytesRead;
char strText[32768] = { 0 };
while (true)
{
if (ReadFile(hRead, buffer, 4096 - 1, &bytesRead, NULL) == NULL)
break;
sprintf_s(strText, "%s\r\n%s", strText, buffer);
memset(buffer, 0, sizeof(buffer));
}
// printf("%s\n", strText);
return strText;
}
int main()
{
WCHAR *Command = L"ipconfig /all";
char *data = ExeCmd(Command);
printf("%s\n", data);
return 0;
}