Skip to content

Instantly share code, notes, and snippets.

@TheDuchy
Last active March 7, 2019 23:05
Show Gist options
  • Save TheDuchy/e5228562db7aa065098b93a56bfd010f to your computer and use it in GitHub Desktop.
Save TheDuchy/e5228562db7aa065098b93a56bfd010f to your computer and use it in GitHub Desktop.
// Written with <3 by Jan "Duchy" Neduchal 2019
// as a part of an article on medium.com about RE
// Note for the future me: add a link to the article you dumbass
#include <windows.h>
#include <iostream>
int super_secret_function(LPSTR cmd, STARTUPINFOA& si, PROCESS_INFORMATION& pi) {
// Start the child process.
if (!CreateProcessA(
NULL, // No module name (use command line)
cmd, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
std::cout << "CreateProcess failed (" << GetLastError() << ").\n";
return -1;
}
else
{
return 0;
}
}
// https://stackoverflow.com/questions/13801517/createprocess-to-execute-windows-command
int main(int argc, char *argv[])
{
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
LPSTR cmd = new char[64];
strcpy(cmd, "cmd.exe /C echo HELLO WORLD && pause>nul");
super_secret_function(cmd, si, pi);
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process, thread handles and free heap
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
delete cmd;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment