Function Pointer (No API)
We can execute shellcode from a local process without relying on well-known Windows APIs like VirtualAlloc
, CreateThread
, or similar functions. To do this, you can use section
pragma with the allocate
declarator specifier. this tells the compiler to place the shellcode directly into the .text
section of the PE, eliminating the need to allocate a RW (read-write) memory region specifically for storing the shellcode. by casting the array holding the shellcode as a function pointer and invoking it, you can execute the shellcode without calling CreateThread
or similar APIs that are traditionally used for this purpose.
However, while this method allows shellcode execution, it is not generally recommended. Shellcode generated by tools like Msfvenom typically terminates the calling thread after execution. If the shellcode is executed via the function pointer method, the main thread becomes the calling thread, which results in the termination of the entire process once the shellcode finishes. Using a new thread to run the shellcode avoids this issue—once the shellcode completes, only the newly created worker thread terminates, preventing the process from exiting entirely.
Another problem with this technique is that the shellcode is stored in .text
which is a read-only section, meaning that anything we write in this section will not be modifiable.
In real-world scenarios where we have to encrypt/obfuscate our payloads, we cant use the same memory section for decryption / deobfuscation tasks.
Execution Flow:
Store shellcode array in .text section
Create a function pointer that points to the start of shellcode array.
Call the function pointer to change execution direction to shellcode array
Code:
(
(void(
)())(&goodcode))();
translates to this in simple syntax:
If we prefer not to store the shellcode in .text
section, the same execution technique can be combined using Win API for memory allocation and copyin the shellcode. execution can still happen using function pointers.
Code Samples
Code snippets are available on GitHub:
Last updated
Was this helpful?