Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #include <Windows.h>
  2.  
  3. HANDLE child_STDOUT_read; //read handle to child process' rerouted STDOUT pipe
  4. HANDLE child_STDOUT_write; //write handle to -||-
  5. HANDLE child_STDIN_read; //read handle to child process' rerouted STDIN pipe
  6. HANDLE child_STDIN_write; //write handle to -||-
  7.  
  8. { //set up pipes for communication with child process
  9. SECURITY_ATTRIBUTES security;
  10. security.nLength = sizeof(SECURITY_ATTRIBUTES);
  11. security.bInheritHandle = true; //allow the child process to inherit any of the handles at all
  12. security.lpSecurityDescriptor = NULL;
  13.  
  14.  
  15. //may include a success check to catch creation errors ( if(!CreatePipe(..){error} )
  16. CreatePipe(&child_STDIN_read, &child_STDIN_write, &security, 0); //creates pipe with child_STDIN_read and child_STDIN_write as read and write handles
  17. SetHandleInformation(child_STDIN_write, HANDLE_FLAG_INHERIT, 0); //prevents inheritance of the STDIN_write handle access (child shouldnt be able to write to its own STDIN)
  18.  
  19. CreatePipe(&child_STDOUT_read, &child_STDOUT_write, &security, 0); //creates pipe with child_STDOUT_read and child_STDOUT_write as read and write handles
  20. SetHandleInformation(child_STDOUT_read, HANDLE_FLAG_INHERIT, 0); //prevents inheritance of the STDOUT_write handle access (child shouldnt be able to read its own STDOUT)
  21. }
  22.  
  23. PROCESS_INFORMATION Process_Info; //contains information about the launched child process
  24. { //set up child process
  25. TCHAR path_to_child[] = TEXT("Path\\to\\child\\"); //!!BEWARE OF SPACES IN PATH!!, can also just be the name of the executable or module (gnuplot), but thats prone to tampering.
  26. TCHAR command_to_execute[] = TEXT("child.exe -param"); //!!BEWARE OF SPACES IN PATH!!, executable to start and parameters, can include full path
  27. //theres a lot of overlap between the two. first one is more suited to programs that are part of the local folder hierarchy/program suite. second one is more suited to execute system wide modules like ls, cat, gnuplot, etc
  28.  
  29.  
  30. STARTUPINFO Start_Info; //contains information about how to launch the process
  31.  
  32. BOOL child_creation_success = false;
  33.  
  34. ZeroMemory(&Process_Info, sizeof(PROCESS_INFORMATION)); //initialise a PROCESS_INFORMATION instance with zeroes
  35.  
  36. ZeroMemory(&Start_Info, sizeof(STARTUPINFO)); //initialise Start_info with all zero
  37. Start_Info.cb = sizeof(STARTUPINFO);
  38. Start_Info.hStdError = child_STDOUT_write; //replaces the STDEerr pipe of the child process with the STDOUT pipe defined in the parent process
  39. Start_Info.hStdOutput = child_STDOUT_write; //replaces the STDOUT pipe of the child process with the STDOUT pipe defined in the parent process(both STDERR and STDOUT appear in the pipe the parent process reads from)
  40. Start_Info.hStdInput = child_STDIN_read; //replaces the STDIN pope of the child process with the STDOUT pipe defined in the parent process
  41. Start_Info.dwFlags |= STARTF_USESTDHANDLES; //enables pipe replacement
  42.  
  43. child_creation_success = CreateProcess(
  44. path_to_child, //as outlined above
  45. command_to_execute, //as outlined above
  46. NULL, //process security attributes
  47. NULL, //primary thread security attributes
  48. TRUE, //If this parameter is TRUE, each inheritable handle in the calling process is inherited by the new process. If the parameter is FALSE, the handles are not inherited.
  49. 0, //defines process execution priorities
  50. NULL, //defines user environment to use, NULL uses same environment as parent
  51. NULL, //defines directory to run in, NULL uses same directory as parent
  52. &Start_Info, //pointer to startupinfo
  53. &Process_Info //receives PROCESS_INFORMATION
  54. );
  55.  
  56. if (!child_creation_success)
  57. {
  58. //do some error handling
  59. }
  60. }
  61.  
  62. //Use ReadFile and WriteFile to access the pipe handles
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement