Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- //#include <tchar.h>
- #include <windows.h>
- #include <iostream>
- #include <fstream>
- #pragma warning(disable : 4800)
- #define BUFSIZE 4096
- PROCESS_INFORMATION CreateChildProcess(char szCmdline[], HANDLE hChildStdWr)
- {
- PROCESS_INFORMATION piProcInfo;
- STARTUPINFO siStartInfo;
- bool bSuccess = FALSE;
- ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
- ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
- siStartInfo.cb = sizeof(STARTUPINFO);
- siStartInfo.hStdOutput = hChildStdWr;
- siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
- bSuccess = CreateProcess(NULL, szCmdline, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &siStartInfo, &piProcInfo);
- if(!bSuccess)
- {
- exit(1);
- }
- else
- {
- CloseHandle(hChildStdWr);
- }
- return piProcInfo;
- }
- std::string ReadFromPipe(PROCESS_INFORMATION piProcInfo, HANDLE hChildStdRd)
- {
- DWORD dwRead;
- CHAR chBuf[BUFSIZE];
- bool bSuccess = FALSE;
- std::string out = "";
- for(;;)
- {
- bSuccess=ReadFile(hChildStdRd, chBuf, BUFSIZE, &dwRead, NULL);
- if(!bSuccess || dwRead == 0)
- {
- break;
- }
- std::string s(chBuf, dwRead);
- out += s;
- }
- return out;
- }
- bool fileExist(const char *fileName)
- {
- std::ifstream infile(fileName);
- return infile.good();
- }
- int main(int argc, char *argv[])
- {
- std::string command;
- SECURITY_ATTRIBUTES sa;
- sa.nLength = sizeof(SECURITY_ATTRIBUTES);
- sa.bInheritHandle = TRUE;
- sa.lpSecurityDescriptor = NULL;
- if(!fileExist("C:\\Windows\\SysWOW64\\bash.exe"))
- {
- ShellExecuteW(NULL, L"open", L"cmd.exe", L" /C mklink C:\\Windows\\SysWOW64\\bash.exe C:\\Windows\\System32\\bash.exe", NULL, SW_HIDE);
- }
- HANDLE g_hChildStd_OUT_Rd = NULL;
- HANDLE g_hChildStd_OUT_Wr = NULL;
- if(!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &sa, 0))
- {
- exit(1);
- }
- if(!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
- {
- exit(1);
- }
- 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
- command = ReadFromPipe(piProcInfo, g_hChildStd_OUT_Rd);
- std::cout<<command<<std::endl;
- CloseHandle(piProcInfo.hProcess);
- CloseHandle(piProcInfo.hThread);
- HANDLE g_hChildStd_OUT_RdB = NULL;
- HANDLE g_hChildStd_OUT_WrB = NULL;
- if(!CreatePipe(&g_hChildStd_OUT_RdB, &g_hChildStd_OUT_WrB, &sa, 0))
- {
- exit(1);
- }
- if(!SetHandleInformation(g_hChildStd_OUT_RdB, HANDLE_FLAG_INHERIT, 0))
- {
- exit(1);
- }
- PROCESS_INFORMATION piProcInfoB = CreateChildProcess("C:\\Windows\\SysWOW64\\bash.exe -c ls", g_hChildStd_OUT_WrB);
- command = ReadFromPipe(piProcInfoB, g_hChildStd_OUT_RdB);
- std::cout<<command<<std::endl;
- CloseHandle(piProcInfoB.hProcess);
- CloseHandle(piProcInfoB.hThread);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement