127 ch3
127 ch3
Ch 3: Shellcode
Updated 2-8-22
Topics
• Protection rings
• Syscalls
• Shellcode
• nasm Assembler
• ld GNU Linker
• objdump to see contents of object files
• strace System Call Tracer
• Removing Nulls
• Spawning a Shell
Protection Rings
• Although the x86
provides four
rings, only rings 0
and 3 are used by
Windows or Unix
• Ring 3 is user-
land
• Ring 0 is kernel-
land
• Links Ch 3a-3c
Libc
• C library wrapper
• C functions that perform syscalls
• Advantages of libc
– Allows programs to continue to function
normally even if a syscall is changed
– Provides useful functions, like malloc
– (malloc allocates space on the heap)
• See link Ch 3d
Demonstration
Using Debian 11 64-Bit
exit()
– exit calls
__run_exit_handlers
– __run_exit_handlers
calls _exit
– disassemble _exit
• int 0x80
– call *$gs:10
– int 0x80
• Link Ch 3o
Disassembling _exit
sys_exit Syscall
• Two arguments: eax=1, ebx is return value
(0 in our case)
• Link Ch 3m
objdump
• Shows the contents of object files
C Code to Test Shellcode
Using strace
• sudo apt install strace
Injectable Shellcode
Getting Rid of Nulls
• We have null bytes, which will terminate
a string and break the exploit
Replacing Instructions
• This instruction contains nulls
– mov ebx,0
• This one doesn't
– xor ebx,ebx
• This instruction contains nulls, because it
moves 32 bits
– mov eax,1
• This one doesn't, moving only 8 bits
– mov al, 1
OLD
NEW
objdump of New Exit Shellcode
Spawning a Shell
Beyond exit()
• The exit() shellcode stops the program, so
it's just a DoS attack
• Any illegal instruction can make the
program crash, so that's of little use
• We want shellcode that offers the
attacker a shell, so the attacker can type
in arbitrary commands
man execve
C Program to Use execve()
disassemble execve
• Puts four parameters into edx, ecx, ebx, and eax
Versions of syscall
• Link Ch 3n
Final Shellcode