I'm trying to figure out how to execute arbitrary code on Windows 10. I feel like this code should work, but it's giving me an Access violation writing location error.
#include <iostream>
#include <vector>
#include <Windows.h>
// x64 assembly
#define ret (uint8_t)(0xc3)
#define nop (uint8_t)(0x90)
int main() {
std::vector<uint8_t> raw_code = {
nop, ret
};
LPVOID exec = VirtualAlloc(NULL, raw_code.size(), MEM_RESERVE, PAGE_READWRITE);
memcpy(exec, raw_code.data(), raw_code.size());
DWORD old_protect;
VirtualProtect(exec, raw_code.size(), PAGE_EXECUTE, &old_protect); // Line where error occurs
((void(*)())exec)();
VirtualProtect(exec, raw_code.size(), old_protect, &old_protect);
VirtualFree(exec, raw_code.size(), MEM_RELEASE);
return 0;
}