Shellcode
In
When creating shellcode, it is generally desirable to make it both small and executable, which allows it to be used in as wide a variety of situations as possible.[2] In assembly code, the same function can be performed in a multitude of ways and there is some variety in the lengths of opcodes that can be used for this purpose; good shellcode writers can put these small opcodes to use to create more compact shellcode.[3] Some have reached the smallest possible size while maintaining stability.[4]
Types of shellcode
Shellcode can either be local or remote, depending on whether it gives an attacker control over the machine it runs on (local) or over another machine through a network (remote).
Local
Local shellcode is used by an attacker who has limited access to a machine but can exploit a vulnerability, for example a buffer overflow, in a higher-privileged process on that machine. If successfully executed, the shellcode will provide the attacker access to the machine with the same higher privileges as the targeted process.
Remote
Remote shellcode is used when an attacker wants to target a vulnerable process running on another machine on a
A
Download and execute
Download and execute is a type of remote shellcode that
Staged
When the amount of data that an attacker can inject into the target process is too limited to execute useful shellcode directly, it may be possible to execute it in stages. First, a small piece of shellcode (stage 1) is executed. This code then downloads a larger piece of shellcode (stage 2) into the process's memory and executes it.
Egg-hunt
This is another form of staged shellcode, which is used if an attacker can inject a larger shellcode into the process but cannot determine where in the process it will end up. Small egg-hunt shellcode is injected into the process at a predictable location and executed. This code then searches the process's address space for the larger shellcode (the egg) and executes it.[8]
Omelette
This type of shellcode is similar to egg-hunt shellcode, but looks for multiple small blocks of data (eggs) and recombines them into one larger block (the omelette) that is subsequently executed. This is used when an attacker can only inject a number of small blocks of data into the process.[9]
Shellcode execution strategy
An exploit will commonly inject a shellcode into the target process before or at the same time as it exploits a vulnerability to gain control over the program counter. The program counter is adjusted to point to the shellcode, after which it gets executed and performs its task. Injecting the shellcode is often done by storing the shellcode in data sent over the network to the vulnerable process, by supplying it in a file that is read by the vulnerable process or through the command line or environment in the case of local exploits.
Shellcode encoding
Because most processes filter or restrict the data that can be injected, shellcode often needs to be written to allow for these restrictions. This includes making the code small, null-free or
- Design and implementation optimizations to decrease the size of the shellcode.
- Implementation modifications to get around limitations in the range of bytes used in the shellcode.
- Self-modifying code that modifies a number of the bytes of its own code before executing them to re-create bytes that are normally impossible to inject into the process.
Since
Percent encoding
Exploits that target browsers commonly encode shellcode in a JavaScript string using
For example, on the IA-32 architecture, here's how two NOP
(no-operation) instructions would look, first unencoded:
90 NOP 90 NOP
percent-encoding | unescape("%u9090")
|
---|---|
unicode literal | "\u9090"
|
HTML/XML entity | "邐" or "邐"
|
This instruction is used in NOP slides.
Null-free shellcode
Most shellcodes are written without the use of null bytes because they are intended to be injected into a target process through null-terminated strings. When a null-terminated string is copied, it will be copied up to and including the first null but subsequent bytes of the shellcode will not be processed. When shellcode that contains nulls is injected in this way, only part of the shellcode would be injected, making it incapable of running successfully.
To produce null-free shellcode from shellcode that contains null bytes, one can substitute machine instructions that contain zeroes with instructions that have the same effect but are free of nulls. For example, on the IA-32 architecture one could replace this instruction:
B8 01000000MOVEAX,1 // Set the register EAX to 0x00000001
which contains zeroes as part of the literal (1
expands to 0x00000001
) with these instructions:
33C0 XOR EAX,EAX // Set the register EAX to 0x00000000 40 INC EAX // Increase EAX to 0x00000001
which have the same effect but take fewer bytes to encode and are free of nulls.
Alphanumeric and printable shellcode
An alphanumeric shellcode is a shellcode that consists of or assembles itself on execution into entirely
In certain circumstances, a target process will filter any byte from the injected shellcode that is not a
Unicode proof shellcode
Modern programs use
Platforms
Most shellcode is written in
Shellcode analysis
Shellcode cannot be executed directly. In order to analyze what a shellcode attempts to do it must be loaded into another process. One common analysis technique is to write a small C program which holds the shellcode as a byte buffer, and then use a function pointer or use inline assembler to transfer execution to it. Another technique is to use an online tool, such as shellcode_2_exe, to embed the shellcode into a pre-made executable husk which can then be analyzed in a standard debugger. Specialized shellcode analysis tools also exist, such as the iDefense sclog project which was originally released in 2005 as part of the Malcode Analyst Pack. Sclog is designed to load external shellcode files and execute them within an API logging framework. Emulation-based shellcode analysis tools also exist such as the sctest application which is part of the cross-platform libemu package. Another emulation-based shellcode analysis tool, built around the libemu library, is scdbg which includes a basic debug shell and integrated reporting features.
See also
- Alphanumeric code
- Computer security
- Buffer overflow
- Exploit (computer security)
- Heap overflow
- Metasploit Project
- Shell (computing)
- Shell shoveling
- Stack buffer overflow
- Vulnerability (computing)
References
- ISBN 1-59749-005-9.
- OCLC 173682537.
- OCLC 57566682.
- ^ "Tiny Execve sh - Assembly Language - Linux/x86". GitHub. Retrieved 2021-02-01.
- ^ BHA (2013-06-06). "Shellcode/Socket-reuse". Retrieved 2013-06-07.
- ^ SkyLined (2010-01-11). "Download and LoadLibrary shellcode released". Archived from the original on 2010-01-23. Retrieved 2010-01-19.
- ^ "Download and LoadLibrary shellcode for x86 Windows". 2010-01-11. Retrieved 2010-01-19.
- ^ Skape (2004-03-09). "Safely Searching Process Virtual Address Space" (PDF). nologin. Retrieved 2009-03-19.
- ^ SkyLined (2009-03-16). "w32 SEH omelet shellcode". Skypher.com. Archived from the original on 2009-03-23. Retrieved 2009-03-19.
- ^ "JavaScript large number of unescape patterns detected". Archived from the original on 2015-04-03.
- ^ a b rix (2001-08-11). "Writing ia32 alphanumeric shellcodes". Phrack. 0x0b (57). Phrack Inc. #0x0f of 0x12. Archived from the original on 2022-03-08. Retrieved 2022-05-26.
- ^ a b obscou (2003-08-13). "Building IA32 'Unicode-Proof' Shellcodes". Phrack. 11 (61). Phrack Inc. #0x0b of 0x0f. Archived from the original on 2022-05-26. Retrieved 2008-02-29.
- ^ a b Mason, Joshua; Small, Sam; Monrose, Fabian; MacManus, Greg (November 2009). English Shellcode (PDF). Proceedings of the 16th ACM conference on Computer and Communications Security. New York, NY, USA. pp. 524–533. Archived (PDF) from the original on 2022-05-26. Retrieved 2010-01-10. (10 pages)
- ^ "Multi-architecture (x86) and 64-bit alphanumeric shellcode explained". Blackhat Academy. Archived from the original on 2012-06-21.
- ^ eugene (2001-08-11). "Architecture Spanning Shellcode". Phrack. Phrack Inc. #0x0e of 0x12. Archived from the original on 2021-11-09. Retrieved 2008-02-29.
- Full disclosure. Archivedfrom the original on 2022-05-26. Retrieved 2022-05-26.
- ISBN 978-1-4503-0244-9. Archived (PDF) from the original on 2022-05-26. Retrieved 2022-05-26. [1] (12 pages) (See also: [2])
External links
- Shell-Storm Database of shellcodes Multi-Platform.
- An introduction to buffer overflows and shellcode
- The Basics of Shellcoding (PDF) An overview of x86 shellcoding by Angelo Rosiello
- An introduction to shellcode development
- Contains x86 and non-x86 shellcode samples and an online interface for automatic shellcode generation and encoding, from the Metasploit Project
- a shellcode archive, sorted by Operating system.
- Microsoft Windows and Linux shellcode design tutorial going from basic to advanced.
- Windows and Linux shellcode tutorial containing step by step examples.
- Designing shellcode demystified[usurped]
- ALPHA3 A shellcode encoder that can turn any shellcode into both Unicode and ASCII, uppercase and mixedcase, alphanumeric shellcode.
- Writing Small shellcode by Dafydd Stuttard A whitepaper explaining how to make shellcode as small as possible by optimizing both the design and implementation.
- Writing IA32 Restricted Instruction Set Shellcode Decoder Loops by SkyLined Archived 2015-04-03 at the Wayback Machine A whitepaper explaining how to create shellcode when the bytes allowed in the shellcode are very restricted.
- BETA3 A tool that can encode and decode shellcode using a variety of encodings commonly used in exploits.
- Shellcode 2 Exe - Online converter to embed shellcode in exe husk
- Sclog - Updated build of the iDefense sclog shellcode analysis tool (Windows)
- Libemu - emulation based shellcode analysis library (*nix/Cygwin)
- Scdbg - shellcode debugger built around libemu emulation library (*nix/Windows)