05 Exploit Payloads
05 Exploit Payloads
Course 2023/2024
1 A Little Recap
2 Payload Types
3 Filters
4 Encoders/Decoders
5 Payload Components
1 A Little Recap
2 Payload Types
3 Filters
4 Encoders/Decoders
5 Payload Components
Shellcode?
Shellcode: code that executes a shell
Exploit payload: executable code in exploits
Shellcode?
Shellcode: code that executes a shell
Exploit payload: executable code in exploits
Exploit payload
Snippets of code that are injected into a running process and
run from within that process
It must keep the injected process running
Otherwise the process will terminate and thus the exploit will terminate as well
Requirements
Position-independent code
Facilitates execution, regardless of the memory address or the segment
in which they are injected
1 A Little Recap
2 Payload Types
3 Filters
4 Encoders/Decoders
5 Payload Components
Null-free payloads
Payloads that have NO null bytes
Useful for string-based exploits
What if we need, for instance, a null value for the execution of the shellcode?
Alphanumeric
Only printable bits are valid
For instance, ASCII bytes
1 A Little Recap
2 Payload Types
3 Filters
4 Encoders/Decoders
5 Payload Components
After the last xor instruction, eax will contain the value 0xD0FF9090
How to use eax?
Encoding process:
Represents input bit octets as 2-character encoded output strings
Each octet is divided into two parts (nibble)
Each nibble is translated to a single character in the base16 alphabet
Encoding algorithm
For each input byte, divided it into its nibble parts
For each nibble, add the value ’A’ (0x41)
The result will be in the range 0x41...0x50 (’A’...’P’)
Mark the end of the payloads with some character greater than ’P’
Decoding algorithm
For each input byte, subtract the value ’A’ (0x41)
Shift the result to the left
Add the next input byte, after subtracting the value ’A’ (0x41)
For each nibble, add the value ’A’ (0x41)
INIT:
8A07 MOV AL ,BYTE PTR DS:[ EDI]
2C 41 SUB AL ,0 x41
C0E0 04 SHL AL ,0 x4
47 INC EDI edi: encoded shellcode buffer
0207 ADD AL ,BYTE PTR DS:[ EDI]
2C 41 SUB AL ,0 x41 esi: decoded shellcode buffer
8806 MOV BYTE PTR DS:[ ESI],AL
46 INC ESI Can they both be the same
47 INC EDI buffer?
803F 51 CMP BYTE PTR DS:[ EDI ],0 x51
72 EB JB @INIT
UNICODE filters
UNICODE character set
16 bits (instead of 8) to represent characters
UNICODE characters equivalent to ASCII character are named wide chars
This null byte is used for other alphabetic encodings, such as Chinese,
Russian, etc.
nop ; 0x90
nop ; 0x90 nop ; 0x90
nop ; 0x90
→ add byte ptr ds:[ eax + 90009000] , dl ; 0 x009000900090
nop ; 0x90
Valid instructions
Single opcode
0xNN 0x00 0xNN opcodes
0x00 0xNN 0x00 opcodes
0xNN 0x00 0xNN 0x00 0xNN opcodes
NOTE: ebp must point to a writable memory address (otherwise, it will crash)
Remember: you must first configure the EIP with a valid address
https://www.corelan.be/index.php/2009/11/06/exploit-writing-tutorial-part-7-unicode-from-0x00410041-to-calc
1 A Little Recap
2 Payload Types
3 Filters
4 Encoders/Decoders
5 Payload Components
XOR encoders
Take advantage of XOR properties
a ⊗ b = c; c ⊗ b = a; c ⊗ a = b
int encode ( unsigned char xorKey , unsigned char *buf , int shellcodelen )
{
for(int i = 0; i < shellcodelen ; i++)
if( xorKey != ( unsigned char) shellcode [i])
buf[i] = (( unsigned char) shellcode [i])^ xorKey ;
}
EB 02 JMP B
A:
EB 05 JMP C
B:
E8 F9FFFFFF CALL A
C:
5F POP EDI
83 C7 1A ADD EDI ,1A
57 PUSH EDI
5E POP ESI
33 C0 XOR EAX ,EAX
33 C9 XOR ECX ,ECX
B1 NN MOV CL , NNh # shellcode size
DEC:
8A07 MOV AL ,BYTE PTR DS :[ EDI]
3C 41 CMP AL ,41 # cipher key
74 02 JE G
34 41 XOR AL ,41 # cipher key
G:
8806 MOV BYTE PTR DS:[ ESI],AL
47 INC EDI
46 INC ESI
E2 F2 LOOPD DEC
Other variants:
XOR-ROR additive feedback (https://github.com/Re4son/slae-4)
...
Other variants:
XOR-ROR additive feedback (https://github.com/Re4son/slae-4)
...
Custom encoders/decoders
Customize your encoder/decoder!
Always following these steps:
1 Choose an encoding mechanism
2 Develop an encoder
3 Develop a decoder
4 Decoder must be located before the modified payload
1 A Little Recap
2 Payload Types
3 Filters
4 Encoders/Decoders
5 Payload Components
Restore privileges
Useful on Unix-like systems: effective user ID vs real user ID
eid governs what access the process has
uid determines who the user really is
Some programs may drop privileges before execution (e.g., /etc/sh in the
latest versions of GNU/Linux and macOS)
You can run seteuid(0) before the shellcode payload to get an elevated
shell (in a +s program)
vfork() is like fork(), except that the parent process is suspended until
the child process executes the execve() system call or exits
Shell execution
Minimal payload to run a shell
You have worked with this payload before, see the previous topic slides (or
lab workbooks)!
Note that on some systems, a drop of privileges may occur by default as a
good practice of security principles
On remote, variants: bind shell and reverse shell
Bind shell
Payload that opens a listening port
When the attacker connects, it automatically launches a shell
Think of a client/server architecture:
The attacker acts as a client, the target acts as a server
Attacker Target
(acts as a client) (acts as a server)
bind shell
Reverse shell
Payload that connects to a specific address
When connecting to the address, it automatically launches a shell
Think of a client/server architecture:
The attacker acts as a server, the target acts as a client
Attacker Target
(acts as a server) (acts as a client)
reverse shell
Staged payload
Useful to avoid payload size constraints
Each stage prepares the runtime environment for the next stage, allowing
the next stage to run with fewer constraints
For instance, the first stage can search for the subsequent stage somewhere else in
memory and decode it, or download it over the network, and then run it (or inject it into a
running process)
Course 2023/2024