Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <windows.h>
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #include <sstream>
- #include <consoleapi.h>
- #include <Shlwapi.h>
- #include <atlstr.h>
- #pragma hdrstop
- void ErrorMessage(wchar_t *str) //display detailed error info
- {
- LPVOID msg;
- FormatMessageW(
- FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- GetLastError(),
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR)&msg,
- 0,
- NULL
- );
- wprintf_s(L"pcLog=\"%s: %s\";\n", str, msg);
- LocalFree(msg);
- }
- int _tmain(int argc, _TCHAR** argv)
- {
- if (argc < 3)
- {
- wprintf_s(L"pcLog=\"Too few arguments specified.\";\n");
- return 2;
- }
- wchar_t* username = argv[1];
- wchar_t* password = argv[2];
- wchar_t* executable = argv[3];
- //Before we bother with anything else, let's make sure we can log on as the specified user.
- HANDLE logonToken;
- if (!LogonUserExW(username, NULL, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_WINNT50, &logonToken, NULL, NULL, NULL, NULL))
- {
- wprintf_s(L"pcLog=\"Failed to log in as user %s.\";\n", username);
- return 2;
- }
- wprintf_s(L"pcLog=\"Logged in as user %s.\";\n", username);
- CloseHandle(logonToken);
- //Make argument string to pass to new process, without our username and password.
- std::wstringstream ss;
- ss << executable;
- for (int i = 4; i < argc; i++)
- {
- ss << " " << argv[i];
- }
- std::wstring str = ss.str();
- LPWSTR cmdLine = const_cast<LPWSTR>(str.c_str());
- PROCESS_INFORMATION pi;
- HANDLE newstdin, newstdout, read_stdout, write_stdin; //pipe handles
- SECURITY_ATTRIBUTES sa;
- sa.nLength = sizeof(SECURITY_ATTRIBUTES);
- sa.lpSecurityDescriptor = NULL;
- sa.bInheritHandle = TRUE;
- if (!CreatePipe(&newstdin, &write_stdin, &sa, 0)) //create stdin pipe
- {
- ErrorMessage(L"CreatePipe");
- return 2;
- }
- if (!CreatePipe(&read_stdout, &newstdout, &sa, 0)) //create stdout pipe
- {
- ErrorMessage(L"CreatePipe");
- CloseHandle(newstdin);
- CloseHandle(write_stdin);
- return 2;
- }
- STARTUPINFO si;
- // Set up the startup info struct.
- ZeroMemory(&si, sizeof(STARTUPINFO));
- si.cb = sizeof(STARTUPINFO);
- si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
- si.hStdOutput = newstdout;
- si.hStdInput = newstdin;
- si.hStdError = newstdout;
- si.wShowWindow = SW_HIDE;
- ATL::CStringW currentPath(argv[3]);
- wprintf_s(L"pcLog=\"Executable:%s\";\n", currentPath.GetString());
- wchar_t* wd = const_cast<LPWSTR>(currentPath.GetString());
- PathRemoveFileSpecW(wd);
- ATL::CStringW wdWithSlash(wd);
- wdWithSlash += L"\\";
- wprintf_s(L"pcLog=\"Working Directory:%s\";\n", wdWithSlash.GetString());
- //spawn the child process as the specified user
- wprintf_s(L"pcLog=\"Starting %s as '%s' with arguments: %s\";\n", executable, username, cmdLine);
- if (!CreateProcessWithLogonW(username, NULL, password, LOGON_WITH_PROFILE, executable, cmdLine, CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT, NULL, wdWithSlash, &si, &pi))
- {
- DWORD err = GetLastError();
- wprintf_s(L"pcLog=\"Unable to launch application. Error %d\";\n", err);
- CloseHandle(newstdin);
- CloseHandle(newstdout);
- CloseHandle(read_stdout);
- CloseHandle(write_stdin);
- return 2;
- }
- unsigned long exit = 0; //process exit code
- unsigned long bread; //bytes read
- unsigned long avail; //bytes available
- _TCHAR buf[1024];
- SecureZeroMemory(buf,sizeof(buf));
- for (;;) //main program loop
- {
- GetExitCodeProcess(pi.hProcess, &exit);
- //Basically, block this thread until the child exits.
- if (exit != STILL_ACTIVE)
- break;
- PeekNamedPipe(read_stdout, buf, 1024, &bread, &avail, NULL);
- if (bread != 0)
- {
- SecureZeroMemory(buf,sizeof(buf));
- if (avail > 1024)
- {
- while (bread >= 1024)
- {
- ReadFile(read_stdout, buf, 1024, &bread, NULL); //read the stdout pipe
- wprintf_s(L"%s", buf);
- SecureZeroMemory(buf, sizeof(buf));
- }
- }
- else {
- ReadFile(read_stdout, buf, 1024, &bread, NULL);
- wprintf_s(L"%s", buf);
- }
- }
- }
- CloseHandle(pi.hThread);
- CloseHandle(pi.hProcess);
- CloseHandle(newstdin);
- CloseHandle(newstdout);
- CloseHandle(read_stdout);
- CloseHandle(write_stdin);
- wprintf_s(L"pcLog=\"Process exit code: %u\";\n", exit);
- return 1; //Because reasons
- }
Advertisement
Add Comment
Please, Sign In to add comment