Advertisement
Combreal

PipedForks02.cpp

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