Advertisement
Combreal

PipedForks01.cpp

Jun 10th, 2018
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include <string>
  2. //#include <tchar.h>
  3. #include <windows.h>
  4. #include <iostream>
  5. #include <fstream>
  6. #pragma warning(disable : 4800)
  7. #define BUFSIZE 4096
  8.  
  9. PROCESS_INFORMATION CreateChildProcess(char szCmdline[], HANDLE hChildStdWr)
  10. {
  11.     PROCESS_INFORMATION piProcInfo;
  12.     STARTUPINFO siStartInfo;
  13.     bool bSuccess = FALSE;
  14.     ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
  15.     ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
  16.     siStartInfo.cb = sizeof(STARTUPINFO);
  17.     siStartInfo.hStdOutput = hChildStdWr;
  18.     siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
  19.     bSuccess = CreateProcess(NULL, szCmdline, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &siStartInfo, &piProcInfo);
  20.     if(!bSuccess)
  21.     {
  22.         exit(1);
  23.     }
  24.     else
  25.     {
  26.         CloseHandle(hChildStdWr);
  27.     }
  28.     return piProcInfo;
  29. }
  30.  
  31. std::string ReadFromPipe(PROCESS_INFORMATION piProcInfo, HANDLE hChildStdRd)
  32. {
  33.     DWORD dwRead;
  34.     CHAR chBuf[BUFSIZE];
  35.     bool bSuccess = FALSE;
  36.     std::string out = "";
  37.     for(;;)
  38.     {
  39.         bSuccess=ReadFile(hChildStdRd, chBuf, BUFSIZE, &dwRead, NULL);
  40.         if(!bSuccess || dwRead == 0)
  41.         {
  42.             break;
  43.         }
  44.         std::string s(chBuf, dwRead);
  45.         out += s;
  46.     }
  47.     return out;
  48. }
  49.  
  50. bool fileExist(const char *fileName)
  51. {
  52.     std::ifstream infile(fileName);
  53.     return infile.good();
  54. }
  55.  
  56. int main(int argc, char *argv[])
  57. {
  58.     std::string command;
  59.     SECURITY_ATTRIBUTES sa;
  60.     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  61.     sa.bInheritHandle = TRUE;
  62.     sa.lpSecurityDescriptor = NULL;
  63.     if(!fileExist("C:\\Windows\\SysWOW64\\bash.exe"))
  64.     {
  65.         ShellExecuteW(NULL, L"open", L"cmd.exe", L" /C mklink C:\\Windows\\SysWOW64\\bash.exe C:\\Windows\\System32\\bash.exe", NULL, SW_HIDE);
  66.     }
  67.     HANDLE g_hChildStd_OUT_Rd = NULL;
  68.     HANDLE g_hChildStd_OUT_Wr = NULL;
  69.     if(!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &sa, 0))
  70.     {
  71.         exit(1);
  72.     }
  73.     if(!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
  74.     {
  75.         exit(1);
  76.     }
  77.     PROCESS_INFORMATION piProcInfo = CreateChildProcess("cmd.exe /c dism /online /get-featureinfo /featurename:Microsoft-Windows-Subsystem-Linux | findstr /R /C:\"tat\"", g_hChildStd_OUT_Wr); //prend fr & en lol - État et State
  78.     command = ReadFromPipe(piProcInfo, g_hChildStd_OUT_Rd);
  79.     std::cout<<command<<std::endl;
  80.     CloseHandle(piProcInfo.hProcess);
  81.     CloseHandle(piProcInfo.hThread);
  82.     HANDLE g_hChildStd_OUT_RdB = NULL;
  83.     HANDLE g_hChildStd_OUT_WrB = NULL;
  84.     if(!CreatePipe(&g_hChildStd_OUT_RdB, &g_hChildStd_OUT_WrB, &sa, 0))
  85.     {
  86.         exit(1);
  87.     }
  88.     if(!SetHandleInformation(g_hChildStd_OUT_RdB, HANDLE_FLAG_INHERIT, 0))
  89.     {
  90.         exit(1);
  91.     }
  92.     PROCESS_INFORMATION piProcInfoB = CreateChildProcess("C:\\Windows\\SysWOW64\\bash.exe -c ls", g_hChildStd_OUT_WrB);
  93.     command = ReadFromPipe(piProcInfoB, g_hChildStd_OUT_RdB);
  94.     std::cout<<command<<std::endl;
  95.     CloseHandle(piProcInfoB.hProcess);
  96.     CloseHandle(piProcInfoB.hThread);
  97.     system("pause");
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement