(C++) NOP Sled by Unprotect

Created the Tuesday 06 December 2022. Updated 4 weeks, 1 day ago.


This code creates a NOP slide with eight NOP instructions and then inserts the shellcode at the end of the slide. When the program branches to the start of the code section, it will slide through the NOP instructions until it reaches the shellcode, which will then be executed. This demonstrates how a NOP slide can be used to direct program execution to a specific location when the exact branch target is not known.


            #include <stdio.h>
#include <windows.h>

// Shellcode to spawn a cmd.exe process
unsigned char shellcode[] = "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

int main() {
    // Insert a NOP slide at the start of the code section

    // Insert the shellcode at the end of the NOP slide
    __asm__("jmp shellcode");

    // Allocate memory for the shellcode and copy it into place
    void *shellcode_mem = VirtualAlloc(0, sizeof(shellcode), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    memcpy(shellcode_mem, shellcode, sizeof(shellcode));

    return 0;