Writing and Compiling Shellcode in C
Last updated
Last updated
This is a quick lab to get familiar with the process of writing and compiling shellcode in C and is merely a personal conspectus of the paper by for - go check it out for a deep dive on all the subtleties involved in this process, that will not be covered in these notes.
For the sake of this lab, we are going to turn a simple C program (that is provided by in the aforementioned paper) that pops a message box, to shellcode and execute it by manually injecting it into an RWX memory location inside notepad.
Below is a quick overview of how writing and compiling shellcode in C works:
Shellcode is written in C
C code is compiled to a list of assembly instructions
Assembly instructions are cleaned up and external dependencies removed
Assembly is linked to a binary
Shellcode is extracted from the binary
This shellcode can now be injected/executed by leveraging
First of, let's start the Developer Command Prompt for VS 2019, which will set up our dev environment required for compiling and linking the C code used in this lab:
In my case, the said console is located here:
Let's start it like so:
Below are two C files that make up the program we will be converting to shellcode:
c-shellcode.cpp
- the program that pops a message box
peb-lookup.h
- header file required by the c-shellcode.cpp
, which contains functions for resolving addresses for LoadLibraryA
and GetProcAddress
We can now convert the C code in c-shellcode.cpp
to assembly instructions like so:
The switches' instruct the compiler to:
/c
- Prevent the automatic call to LINK
/FA
- Create a listing file containing assembler code for the provided C code
/GS-
- Turn off detection of some buffer overruns
Below shows how we compile the c-shellcode.cpp
into c-shellcode.asm
:
Now that our C code has been convered to assembly in c-shellcode.asm
, we need to clean up the file a bit, so we can link it to an .exe without errors and to avoid the shellcode from crashing. Specifically, we need to:
Remove dependencies from external libraries
Align stack
Fix a simple syntax issue
First off, we need to comment out or remove instructions to link this module with libraries libcmt
and oldnames
:
Add procedure AlignRSP
right at the top of the first _TEXT
segment in our c-shellcode.asm
:
Below shows how it should look like in the c-shellcode.asm
:
Remove or comment out PDATA
and XDATA
segments as shown below:
We need to change line mov rax, QWORD PTR gs:96
to mov rax, QWORD PTR gs:[96]
:
We are now ready to link the assembly listings inside c-shellcode.asm
to get an executable c-shellcode.exe
:
We can now check that if c-shellcode.exe
does what it was meant to - pops a message box:
Let's copy out the shellcode from the .text
section, which in our case starts at 0x200 into the raw file:
If you are wondering how we found the shellcode location, look at the .text
section - you can extract if from there too:
Once the shellcode is copied, let's paste it to an RWX memory area (you can set any memory location to have permissions RWX with xdbg64) inside notepad, set RIP to that location and resume code execution in that location. If we did all the previous steps correctly, we should see our shellcode execute and pop the message box:
Once we have the c-shellcode.exe
binary, we can extract the shellcode and execute it using any technique, but for the sake of this lab, we will copy it out as a list of hex values and simply paste them into an RWX memory slot inside a notepad.exe.