Introduction To Shellcode Development
Introduction To Shellcode Development
Shellcode Development
Ionut Popescu
Penetration Tester @ KPMG Romania
http://www.kpmg.com/ro/en/Pages/default.aspx
1. Introduction
2. C/C++ compiling
3. Running shellcodes (do not)
4. Simple BOF example
5. Shellcode limitations
6. Linux syscalls 1. Windows shellcodes
7. Linux shellcode example 2. Disassemble shellcode
3. Find kernel32
4. Find GetProcAddress
5. Find LoadLibrary
6. Load a DLL
7. Call functions from DLL
8. Download and execute
9. More about shellcodes
10. Contact
Introduction
Shellcodes:
In computer security, a shellcode is a small piece of code used as the payload in the
exploitation of a software vulnerability. It is called "shellcode" because it typically starts a command
shell from which the attacker can control the compromised machine, but any piece of code that performs
a similar task can be called shellcode. Shellcode is commonly written in machine code.
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.
Omlette:
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 omelet) that is subsequently executed. This
is used when an attacker can only inject a number of small blocks of data into the process
C/C++ compiling
This code takes an argument from the command line and copies it to a local stack variable
c. This works fine for command line arguments smaller than 12 characters. Any arguments
larger than 11 characters long will result in corruption of the stack.
Shellcode limitations
Limitations:
What to do:
starter:
ender:
call starter ;put the address of the string on the stack
db 'hello'
Windows shellcodes
1. Find kernel32.dll
2. Find GetProcAddress
3. Find LoadLibrary
4. Load DLLs
5. Call “random” functions
Common shellcodes:
- calc.exe (WinExec)
- Download and execute (URLDownloadToFileA)
- MessageBox (user32.dll)
- Reverse TCP/Bind
PE File Format
The Portable Executable (PE) format is a file format for executables, object code, DLLs, and others used in 32-bit
and 64-bit versions of Windows operating systems. The PE format is a data structure that encapsulates the information
necessary for the Windows OS loader to manage the wrapped executable code. This includes dynamic library
references for linking, API export and import tables, resource management data and thread-local storage (TLS) data.
On NT operating systems, the PE format is used for EXE, DLL, SYS (device driver), and other file types.
General PE File Structure
MS-DOS Header
BYTE – 8 bits (1 byte), “unsigned char” LONG – 4 bytes (32 bits) “long”
CHAR – 8 bits (1 byte), “char” ULONGLONG – 8 bytes (64 bits) “unsigned long long”
DWORD – 4 bytes (32 bits) “unsigned long” WORD – 2 bytes (16 bits) “unsigned short”
PE Header
The .bss section represents uninitialized data for the application, including all variables declared as static within a function or source module.
The .rdata section represents read-only data, such as literal strings, constants, and debug directory information.
All other variables (except automatic variables, which appear on the stack) are stored in the .data section. Basically, these are application or module
global variables.
The .rsrc section contains resource information for a module. It begins with a resource directory structure like most other sections, but this section's
data is further structured into a resource tree. The IMAGE_RESOURCE_DIRECTORY, shown below, forms the root and nodes of the tree.
PE imports table
To parse the imports table, we need to iterate through all the functions with two pointers:
one for the name of the function and the other for the address of the function.
Verify shellcodes
"\x33\xC9\x64\x8B\x41\x30\x8B\x40\x0C\x8B"
"\x70\x14\xAD\x96\xAD\x8B\x58\x10\x8B\x53"
"\x3C\x03\xD3\x8B\x52\x78\x03\xD3\x8B\x72"
"\x20\x03\xF3\x33\xC9\x41\xAD\x03\xC3\x81"
"\x38\x47\x65\x74\x50\x75\xF4\x81\x78\x04"
"\x72\x6F\x63\x41\x75\xEB\x81\x78\x08\x64“
33 C9 64 8B 41 30 8B 40 0C 8B
70 14 AD 96 AD 8B 58 10 8B 53
3C 03 D3 8B 52 78 03 D3 8B 72
20 03 F3 33 C9 41 AD 03 C3 81
38 47 65 74 50 75 F4 81 78 04
72 6F 63 41 75 EB 81 78 08 64
C:\Users\Ionut\AppData\Local\nasm>ndisasm.exe -b 32 download.bin
..................................................
NASM: http://www.nasm.us/
Find kernel32.dll
typedef struct _PEB_LDR_DATA {
typedef struct _PEB { ...
... LIST_ENTRY InLoadOrderModuleList;
PPEB_LDR_DATA Ldr; // 0xC LIST_ENTRY InMemoryOrderModuleList; // 0x14
... LIST_ENTRY InInitializationOrderModuleList;
} PEB, *PPEB; ...
} PEB_LDR_DATA, *PPEB_LDR_DATA;
...
; http://bflow.security-portal.cz/down/xy.txt
Shellcodes: http://www.exploit-db.com/shellcode/
Windows x64 Shellcode: http://mcdermottcybersecurity.com/articles/windows-x64-shellcode
Shellcode on ARM Architecture: http://www.exploit-db.com/papers/15652/
64-bit Linux Shellcode: http://blog.markloiseau.com/2012/06/64-bit-linux-shellcode/
Shellcode 2 EXE: http://www.sandsprite.com/shellcode_2_exe.php
BETA3 - Multi-format shellcode encoding tool: http://code.google.com/p/beta3/
Shellcode/Socket-reuse: http://www.blackhatlibrary.net/Shellcode/Socket-reuse
Writing IA32 Restricted Instruction Set Shellcode : http://skypher.com/...shellcode.html.php
Building IA32 'Unicode-Proof' Shellcodes: http://phrack.org/issues/61/11.html#article
Shellcode/Egg hunt/w32 SEH omelet: http://skypher.com/...omelet_shellcode
What is polymorphic shell code: https://www.sans.org/.../polymorphic_shell.php
Shellcode to reverse bind a shell with netcat: http://morgawr.github.io/...with-netcat/
Omlette Egghunter Shellcode: http://www.thegreycorner.com/...shellcode.html
Shellcode/Alphanumeric: http://www.blackhatlibrary.net/Shellcode/Alphanumeric
A shellcode writing toolkit: https://github.com/reyammer/shellnoob
Windows Syscall Shellcode: http://www.symantec.com/...windows-syscall-shellcode
Contact information
Questions?
[email protected]