Snippets

Alexander Hanel Execute Binary File

Created by Alexander Hanel
#include <iostream> 
#include <cstdlib> 
#include <fstream>
#include <Windows.h>
#include <stdio.h>

using namespace std;

int main(int argc, char * argv[])
{
	HANDLE hFile;
	DWORD dwFileSize;
	DWORD baseAddres = 0;
	BOOL readSuccess;
	DWORD bytesRead = 0;

	if (argc == 1)
	{
		cout << "\tArugments: execute_sc.exe sc.bin offset (optional)" << endl;
		return 0;
	}
	// Open up file passed as an argument 
	hFile = CreateFile(argv[1],		// name of the write
		GENERIC_READ,				// open for writing
		FILE_SHARE_READ,			// do not share
		NULL,						// default security
		OPEN_EXISTING,				// create new file only
		FILE_ATTRIBUTE_NORMAL,		// normal file
		NULL);						// no attr. template

	// check if handle is valid 
	if (hFile != INVALID_HANDLE_VALUE)
	{
		if (argc >= 3)
		{
			baseAddres = std::strtoul(argv[2], 0, 16);
		}

		dwFileSize = GetFileSize(hFile, NULL);
		void *exec = VirtualAlloc((LPVOID)baseAddres, dwFileSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
		cout << "Allocated at " << std::hex << exec << endl;
		if (exec != NULL)
		{
			readSuccess = ReadFile(hFile, exec, dwFileSize, &bytesRead, NULL);
			if (readSuccess == TRUE)
				((void(*)())exec)();
			else
				cout << "ERROR: ReadFile Failed Error: " << endl;
		}
		else
			cout << "ERROR: VirtualAlloc Failed" << endl;
		CloseHandle(hFile);
	}

	return 0;
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.