Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <windows.h>
- VOID Parent(VOID)
- {
- DWORD Written = 0;
- PCHAR DataToWrite = "0xBADFOOD";
- WCHAR ProcessCmdLine[] = L"pipetest.exe --child";
- BOOL Res = FALSE;
- HANDLE hInputReadEnd = INVALID_HANDLE_VALUE;
- HANDLE hInputWriteEnd = INVALID_HANDLE_VALUE;
- STARTUPINFO StartupInfo;
- PROCESS_INFORMATION ProcessInfo;
- printf("PARENT: Creating pipe...\n");
- Res = CreatePipe(&hInputReadEnd, &hInputWriteEnd, NULL, 0);
- if (Res) {
- printf("PARENT: Pipe created successfully:\nPARENT: Read end handle: 0x%p\nPARENT: Write end handle: 0x%p\n", hInputReadEnd, hInputWriteEnd);
- printf("PARENT: Duplicating pies's read end in order to make the handle inheritable...\n");
- Res = DuplicateHandle(GetCurrentProcess(), hInputReadEnd, GetCurrentProcess(), &hInputReadEnd, 0, TRUE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
- if (Res) {
- printf("PARENT: Duplicated handle value is 0x%p\n", hInputReadEnd);
- ZeroMemory(&StartupInfo, sizeof(StartupInfo));
- StartupInfo.cb = sizeof(StartupInfo);
- StartupInfo.hStdInput = hInputReadEnd;
- StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
- StartupInfo.dwFlags = STARTF_USESTDHANDLES;
- printf("PARENT: Creating child process...\n");
- Res = CreateProcessW(NULL, (LPWSTR)&ProcessCmdLine, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, L"E:\\Temp", &StartupInfo, &ProcessInfo);
- if (Res) {
- printf("PARENT: Closing read end handle...\n");
- Res = CloseHandle(hInputReadEnd);
- if (!Res)
- printf("PARENT: Close operation failed: %u\n", GetLastError());
- printf("PARENT: Writting data to the pipe...\n");
- Res = WriteFile(hInputWriteEnd, DataToWrite, strlen(DataToWrite), &Written, NULL);
- if (!Res)
- printf("PARENT: Write operation failed: %u\n", GetLastError());
- printf("PARENT: That's all, folks!\n");
- getchar();
- CloseHandle(ProcessInfo.hThread);
- CloseHandle(ProcessInfo.hProcess);
- CloseHandle(hInputWriteEnd);
- } else {
- printf("PARENT: Child creation failed: %u\n", GetLastError());
- CloseHandle(hInputReadEnd);
- CloseHandle(hInputWriteEnd);
- }
- } else {
- printf("PARENT: Failed to duplicate the handle: %u\n", GetLastError());
- CloseHandle(hInputWriteEnd);
- }
- } else printf("PARENT: Failed to create pipe: %u\n", GetLastError());
- return;
- }
- VOID Child(VOID)
- {
- DWORD Read = 0;
- CHAR Buffer[255];
- BOOL Res = FALSE;
- HANDLE hInput = INVALID_HANDLE_VALUE;
- Sleep(1000);
- printf("CHILD: Retrieving handle for standard input...\n");
- hInput = GetStdHandle(STD_INPUT_HANDLE);
- Res = hInput != NULL;
- if (Res) {
- printf("CHILD: Input handle is 0x%p\n", hInput);
- ZeroMemory(&Buffer, sizeof(Buffer));
- printf("CHILD: Reading input...\n");
- Res = ReadFile(hInput, &Buffer, strlen("0xBADFOOD"), &Read, NULL);
- if (Res) {
- printf("CHILD: Input data: %s\n", &Buffer);
- printf("CHILD: That's all, folks!\n");
- getchar();
- } else printf("CHILD: Failed to read input data: %u\n", GetLastError());
- } else printf("CHILD: Failed to get input handle: %u\n", GetLastError());
- return;
- }
- int main(int argc, char **argv)
- {
- if (argc == 2) {
- if (strncmp(argv[1], "--parent", strlen("--parent")) == 0)
- Parent();
- else if (strncmp(argv[1], "--child", strlen("--child")) == 0)
- Child();
- else printf("PIPETEST: Invalid argument \"%s\"\n", argv[1]);
- } else printf("PIPETEST: The process accepts one argument only\n");
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement