Shellcode in PE File
Embedding shellcode in a Portable Executable File and testing shellcode in C++
This short article is a quick look into how to embed shellcode generated from MSFVENOM into an EXE file. Shellcode is machine code generally used to exploit vulnerabilities. In the earlier days, shellcodes were primarily used in Linux and Unix systems to pop a shell. Often the aim was not just to get access to a shell, but rather the idea was to get a privileged shell. Malware authors often create Trojan viruses by embedding shellcode, thus essentially ‘backdooring’ an ‘.exe’ file.
For this simple demonstration, ‘putty.exe’ will need to be downloaded, and a new section will be added to that file manually via ‘CFF Explorer’. Please see the appendix for download links for all applications discussed in this article.
Here we are adding a new section with ‘1000’ d(ec) data, as seen in the image below. There is also another way to do this using ‘code caves.’ Code caves are empty spaces inside an ‘exe’ file and can be used to inject Shellcode (provided there is enough space for the Shellcode in question).
However, this demonstration uses an explicitly created header space rather than code caves.
Please select the following three options before proceeding to the next step.
The newly created header section is labeled as ‘.pwn,’ enabling us to quickly find this artificial section when we disassemble the file in the disassembler.
Next, open the altered program in the ‘Xdbg64’ debugger, and select the program’s entry point. Here the first instruction can be changed to jump to the ‘.pwn’ section that we created earlier via the CFF explorer. A quick search in the disassembler’s ‘memory map’ should show the starting address of that section. With this information in hand, we can manually instruct the program to JMP to that section upon invocation.
As shown in the image below, please change the program entry instruction to jump <<address of the .pwn section>>. Leave the “Fill with NOPs” checked.
Let us look at the manually added section by following the jump instruction in the disassembler.
There is enough space (since we added the new section with 1000 d(ec) data) to embed a decent size shellcode, so it is time to get one.
The following command instructs ‘Msfvenom’ to spit out a shell code, i.e., machine code, and in this case, to launch the ‘Regedit.exe’ program.
Copy the shellcode and right-click, and Binary paste the contents. Please ensure the pushad and pushfd instructions are added before binary pasting the shellcode.
These two instructions indicate the state should be preserved by saving the current value of the CPU registers and flags, so we can restore the normal flow of the application after executing our shellcode.
This article only deals with injecting the shellcode and does not delve into restoring the normal flow of the application. The idea here is to divert the program flow to a predetermined location where the machine code is located. The machine code will instruct the OS to open ‘Regedit.exe.’
We will do a Binary paste to embed the shellcode from “MSFVenom” into the executable ‘.pwn’ header section.
00476000 60 pushad
00476001 9C pushfd
00476002 FC cld [start of shellcode]
00476003 E8 82......
00476004 ...........After binary pasting the shellcode, please select the ‘patch file’ and save the file with all the changes. If everything goes well, the user will see the program ‘Regedit.exe’ launch upon execution of the putty.exe file.
Commit the change by selecting the ‘Patch File’ option, which should save all changes. Invoking ‘putty.exe’ now should launch the ‘Regedit.exe’ program.
Can we execute the shellcode directly to test its effectiveness before injecting it into a ‘.exe’ file?
Of course, we can. We can paste shellcode into a C or C++ program to test it out.
Get Rajanikanth Sugumar’s stories in your inbox
Join Medium for free to get updates from this writer.
Let us try to plug this machine code into a C++ program and assess the operation firsthand. To accomplish this, we will need a raw output from ‘Msfvenom’ instead of the hex output we got earlier with the ‘-f hex’ option. The raw output can then be appropriately formatted for consumption by a C++ program.
This is the raw output format.
Shellcode formatter is an excellent script that spits out shellcodes in various formats, leaving us to pick the most appropriate one for the use case at hand. We need to feed the raw shellcode through the ‘Shellcode formatter’ script to get the format that is necessary for the C++ program. Since we will be using the C++ inline asm directive, we need the shellcode to be in the ‘0x<hex value>’ format.
Manually adding either ‘0x’ or ‘/x’s to the shellcode can be time-consuming, and running a script such as the shellcode formatter script to get the raw shellcode in different formats is beneficial.
Once the PowerShell execution policy is changed, please execute the powershell script, as seen in the image below.
Now that we have the desired shellcode format ‘payload.txt.b64’, we can paste that into the C++ code where shown, and then this code can be compiled and executed, and if everything works, we should then see the ‘Regedit.exe’ pop up as it did when we injected the shellcode directly into the ‘Putty.exe’ file. For this C++ code, we need to use the ‘0x’ format, called C# formatted shellcode by the script.
#include <windows.h>
#include <stdio.h>
int main() {
asm(".byte 0xfc,0xe8,0x82,0x00,0x00,0x00,0x60,0x89,0xe5,0x31,0xc0,0x64,0x8b,0x50,0x30,0x8b,0x52,0x0c,0x8b,0x52,0x14,0x8b,0x72,0x28,0x0f,0xb7,0x4a,0x26,0x31,0xff,0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0xc1,0xcf,0x0d,0x01,0xc7,0xe2,0xf2,0x52,0x57,0x8b,0x52,0x10,0x8b,0x4a,0x3c,0x8b,0x4c,0x11,0x78,0xe3,0x48,0x01,0xd1,0x51,0x8b,0x59,0x20,0x01,0xd3,0x8b,0x49,0x18,0xe3,0x3a,0x49,0x8b,0x34,0x8b,0x01,0xd6,0x31,0xff,0xac,0xc1,0xcf,0x0d,0x01,0xc7,0x38,0xe0,0x75,0xf6,0x03,0x7d,0xf8,0x3b,0x7d,0x24,0x75,0xe4,0x58,0x8b,0x58,0x24,0x01,0xd3,0x66,0x8b,0x0c,0x4b,0x8b,0x58,0x1c,0x01,0xd3,0x8b,0x04,0x8b,0x01,0xd0,0x89,0x44,0x24,0x24,0x5b,0x5b,0x61,0x59,0x5a,0x51,0xff,0xe0,0x5f,0x5f,0x5a,0x8b,0x12,0xeb,0x8d,0x5d,0x6a,0x01,0x8d,0x85,0xb2,0x00,0x00,0x00,0x50,0x68,0x31,0x8b,0x6f,0x87,0xff,0xd5,0xbb,0xf0,0xb5,0xa2,0x56,0x68,0xa6,0x95,0xbd,0x9d,0xff,0xd5,0x3c,0x06,0x7c,0x0a,0x80,0xfb,0xe0,0x75,0x05,0xbb,0x47,0x13,0x72,0x6f,0x6a,0x00,0x53,0xff,0xd5,0x72,0x65,0x67,0x65,0x64,0x69,0x74,0x2e,0x65,0x78,0x65,0x00\n\t"
"ret\n\t");
return 0;
}Conclusion
In this article, we looked into embedding a shellcode to launch the windows ‘Regedit.exe’ program, but this can obviously be changed to a shellcode that throws a reverse shell when the victim double clicks on the file, and to make detection more complicated, malware authors often restore execution back to the main program so after the shellcode is run the control is relinquished back to the original program. This way, the victim may not notice that anything is amiss.
Furthermore, to bolster AV evasion, malware authors usually exploit code caves and tend to use encoders.
A code cave in the executable itself is used instead of adding a section header. An antivirus program can flag additional section headers as having overlays — indicating something odd about that program. Programs of this nature are usually flagged by antivirus software. Encoders such as ‘Shikata Ga Nai’ are also used to evade AV detection.
Finally, the detection ratio is evaluated against Virustotal before and after incorporating various obfuscation techniques to evaluate the strategy’s overall effectiveness.
Shellcodes have many uses and are generally used in hacking and malware creation. Although, it is usually easier to create and leverage custom shellcodes in Linux due to its use of system interrupts such as INT 80. On the other hand, making shellcodes for windows can be frustrating and doesn’t always work as planned.
‘Wiley’s The Shellcoder’s Handbook’ comes highly recommended if you are interested in knowing more about shellcodes and also explains the pitfalls of windows shellcodes.
References
- PUTTY
https://www.chiark.greenend.org.uk/~sgtatham/putty/releases/0.60.htm - X64DBG
https://x64dbg.com/ - CFF EXPLORER
https://ntcore.com/?page_id=345 - METASPOILT (MSFVenom)
https://docs.metasploit.com/docs/using-metasploit/getting-started/nightly-installers.html - (Script) Shellcode Formatter
https://github.com/FortyNorthSecurity/RandomScripts/blob/main/Cobalt%20Scripts/shellcode_formatter.ps1 - Shellcode Formatter Article
https://fortynorthsecurity.com/blog/quick-shellcode-modification/ - Orwell Dev-C++ — Chocolatey Repo
https://community.chocolatey.org/packages/orwelldevcpp - C++ Code Inline — executing-shellcode-with-inline-assembly-in-c-c++
https://www.ired.team/offensive-security/code-injection-process-injection/executing-shellcode-with-inline-assembly-in-c-c++ - Advanced Backdooring — Includes restoring control back to program
https://fluidattacks.com/blog/backdooring-putty/ - PE File Format — Microsoft
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format









