SlideShare a Scribd company logo
CNIT 127: Exploit Development




Ch 3: Shellcode
Updated 2-22-21
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
Understanding System Calls
Shellcode
• Written in assembler


• Translated into hexadecimal opcodes


• Intended to inject into a system by
exploiting a vulnerability


• Typically spawns a root shell, but may do
something else
System Calls (or Syscalls)
• Syscalls directly access the kernel, to:


– Get input


– Produce output


– Exit a process


– Execute a binary file


– And more


• They are the interface between protected
kernel mode and user mode
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
Protecting the Kernel
• Protected kernel mode


– Prevents user applications from compromising
the OS


• If a user mode program attempts to
access kernel memory, this generates an
access exception


• Syscalls are the interface between user
mode and kernel mode
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
Syscalls use INT 0x80
1. Load syscall number into EAX


2. Put arguments in other registers


3. Execute INT 0x80


4. CPU switches to kernel mode


5. Syscall function executes
Syscall Number and Arguments
• Syscall number is an integer in EAX


• Up to six arguments are loaded into


– EBX, ECX, EDX, ESI, EDI, and EBP


• For more than six arguments, the first
argument holds a pointer to a data
structure
Demonstration
Using Debian 10 64-Bit
exit()
• The libc exit function does a lot of
preparation, carefully covering many
possible situations, and then calls SYSCALL
to exit
Disassembling exit
• gdb -q e


– disassemble main


– main calls exit


– exit calls
__run_exit_handlers


– __run_exit_handlers
calls _exit


– disassemble _exit


• int 0x80


– call *$gs:10


– int 0x80
Four Ways to Do Syscall
• Link Ch 3o
Disassembling _exit
• syscall 252 (0xfc), exit_group() (kill all threads)


• syscall 1, exit()
	
(kill calling thread)


– Link Ch 3e
Writing Shellcode for the
exit() Syscall
Shellcode Size
• Shellcode should be as simple and
compact as possible


• Because vulnerabilities often only allow a
small number of injected bytes


– It therefore lacks error-handling, and will
crash easily
sys_exit Syscall
• Two arguments: eax=1, ebx is return value
(0 in our case)


• Link Ch 3m
Simplest code for exit(0)
nasm and ld
• sudo apt install nasm


• nasm creates object file


• gcc links it, creating an executable ELF file
objdump
• Shows the contents of object files
C Code to Test Shellcode
• From link Ch 3k


• Textbook version explained at link Ch 3i
Compile and Run
• Textbook omits the "-z execstack" option


• It's required now or you get a segfault


• Next, we'll use "strace" to see all system
calls when this program runs


• That shows a lot of complex calls, and
"exit(0)" at the end
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
Five Steps to Shellcode
1. Write high-level code


2. Compile and disassemble


3. Analyze the assembly


4. Clean up assembly, remove nulls


5. Extract commands and create shellcode
fork() and execve()
• Two ways to create a new process in Linux


• Replace a running process


– Uses execve()


• Copy a running process to create a new
one


– Uses fork() and execve() together
man execve
C Program to Use execve()
• Static linking preserves our execve syscall
In gdb, disassemble main
• Pushes 3 Arguments


• Calls __execve
disassemble execve
• Puts four parameters into edx, ecx, ebx, and eax
Versions of syscall
• Link Ch 3n
CNIT 127 Ch 3: Shellcode
Final Shellcode
CNIT 127 Ch 3: Shellcode

More Related Content

PDF
127 Ch 2: Stack overflows on Linux
PDF
CNIT 127: Ch 2: Stack Overflows in Linux
PDF
CNIT 127: 3: Shellcode
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 Ch Ch 1: Before you Begin
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 Ch 6: The Wild World of Windows
PDF
CNIT 127 Lecture 7: Intro to 64-Bit Assembler
127 Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack Overflows in Linux
CNIT 127: 3: Shellcode
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch Ch 1: Before you Begin
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch 6: The Wild World of Windows
CNIT 127 Lecture 7: Intro to 64-Bit Assembler

What's hot (20)

PDF
CNIT 127 Ch 8: Windows overflows (Part 1)
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 14: Protection Mechanisms
PDF
CNIT 127: Ch 8: Windows overflows (Part 1)
PDF
CNIT 126 Ch 7: Analyzing Malicious Windows Programs
PDF
CNIT 127: Ch 18: Source Code Auditing
PDF
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
PDF
CNIT 127 Ch 4: Introduction to format string bugs
PDF
CNIT 127 14: Protection Mechanisms
PDF
CNIT 127 Ch 4: Introduction to format string bugs (rev. 2-9-17)
PDF
CNIT 127 Ch 2: Stack overflows on Linux
PDF
CNIT 127 Ch 1: Before you Begin
PDF
CNIT 126 12: Covert Malware Launching
PDF
CNIT 126 7: Analyzing Malicious Windows Programs
PDF
CNIT 127: Ch 3: Shellcode
PDF
CNIT 126: 10: Kernel Debugging with WinDbg
PDF
CNIT 126 9: OllyDbg
PDF
CNIT 127 Ch 2: Stack overflows on Linux
PDF
Practical Malware Analysis Ch13
PDF
CNIT 127: 8: Windows overflows (Part 2)
CNIT 127 Ch 8: Windows overflows (Part 1)
CNIT 127 Ch 3: Shellcode
CNIT 127 14: Protection Mechanisms
CNIT 127: Ch 8: Windows overflows (Part 1)
CNIT 126 Ch 7: Analyzing Malicious Windows Programs
CNIT 127: Ch 18: Source Code Auditing
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
CNIT 127 Ch 4: Introduction to format string bugs
CNIT 127 14: Protection Mechanisms
CNIT 127 Ch 4: Introduction to format string bugs (rev. 2-9-17)
CNIT 127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 1: Before you Begin
CNIT 126 12: Covert Malware Launching
CNIT 126 7: Analyzing Malicious Windows Programs
CNIT 127: Ch 3: Shellcode
CNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126 9: OllyDbg
CNIT 127 Ch 2: Stack overflows on Linux
Practical Malware Analysis Ch13
CNIT 127: 8: Windows overflows (Part 2)
Ad

Similar to CNIT 127 Ch 3: Shellcode (20)

PDF
Shellcoding in linux
PDF
Shellcode Disassembling - Reverse Engineering
PDF
Linux Shellcode disassembling
PDF
NDC TechTown 2023_ Return Oriented Programming an introduction.pdf
PDF
N_Asm Assembly system calls (sol)
PDF
Return Oriented Programming, an introduction
PDF
Software Vulnerabilities in C and C++ (CppCon 2018)
PPTX
OS SERVICES.pptxJGHHHHHHHHHHHHHHHHGGGGGGGG
PDF
Shellcoding, an Introduction
PDF
System Calls
PDF
Rootkit on linux_x86_v2.6
ODP
null Pune meet - Application Security: Code injection
PPTX
Advanced Bulkification Strategies in Apex Triggers
PDF
3. System Calls A system call is simply a kernel function that a wier.pdf
PPT
Advanced c programming in Linux
PDF
X86 assembly nasm syntax
PDF
L03SystemCalls.pdf all about system call in os
ODP
Design and implementation_of_shellcodes
PPT
LINUX Device Drivers
PPTX
System calls in operating sytems incluses operating sytem process
Shellcoding in linux
Shellcode Disassembling - Reverse Engineering
Linux Shellcode disassembling
NDC TechTown 2023_ Return Oriented Programming an introduction.pdf
N_Asm Assembly system calls (sol)
Return Oriented Programming, an introduction
Software Vulnerabilities in C and C++ (CppCon 2018)
OS SERVICES.pptxJGHHHHHHHHHHHHHHHHGGGGGGGG
Shellcoding, an Introduction
System Calls
Rootkit on linux_x86_v2.6
null Pune meet - Application Security: Code injection
Advanced Bulkification Strategies in Apex Triggers
3. System Calls A system call is simply a kernel function that a wier.pdf
Advanced c programming in Linux
X86 assembly nasm syntax
L03SystemCalls.pdf all about system call in os
Design and implementation_of_shellcodes
LINUX Device Drivers
System calls in operating sytems incluses operating sytem process
Ad

More from Sam Bowne (20)

PDF
Introduction to the Class & CISSP Certification
PDF
Cyberwar
PDF
3: DNS vulnerabilities
PDF
8. Software Development Security
PDF
4 Mapping the Application
PDF
3. Attacking iOS Applications (Part 2)
PDF
12 Elliptic Curves
PDF
11. Diffie-Hellman
PDF
2a Analyzing iOS Apps Part 1
PDF
9 Writing Secure Android Applications
PDF
12 Investigating Windows Systems (Part 2 of 3)
PDF
10 RSA
PDF
12 Investigating Windows Systems (Part 1 of 3
PDF
9. Hard Problems
PDF
8 Android Implementation Issues (Part 1)
PDF
11 Analysis Methodology
PDF
8. Authenticated Encryption
PDF
7. Attacking Android Applications (Part 2)
PDF
7. Attacking Android Applications (Part 1)
PDF
5. Stream Ciphers
Introduction to the Class & CISSP Certification
Cyberwar
3: DNS vulnerabilities
8. Software Development Security
4 Mapping the Application
3. Attacking iOS Applications (Part 2)
12 Elliptic Curves
11. Diffie-Hellman
2a Analyzing iOS Apps Part 1
9 Writing Secure Android Applications
12 Investigating Windows Systems (Part 2 of 3)
10 RSA
12 Investigating Windows Systems (Part 1 of 3
9. Hard Problems
8 Android Implementation Issues (Part 1)
11 Analysis Methodology
8. Authenticated Encryption
7. Attacking Android Applications (Part 2)
7. Attacking Android Applications (Part 1)
5. Stream Ciphers

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Institutional Correction lecture only . . .
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Pre independence Education in Inndia.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Microbial diseases, their pathogenesis and prophylaxis
human mycosis Human fungal infections are called human mycosis..pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Institutional Correction lecture only . . .
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Anesthesia in Laparoscopic Surgery in India
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
O7-L3 Supply Chain Operations - ICLT Program
Pre independence Education in Inndia.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Classroom Observation Tools for Teachers
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Renaissance Architecture: A Journey from Faith to Humanism
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

CNIT 127 Ch 3: Shellcode

  • 1. CNIT 127: Exploit Development 
 
 Ch 3: Shellcode Updated 2-22-21
  • 2. 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
  • 4. Shellcode • Written in assembler • Translated into hexadecimal opcodes • Intended to inject into a system by exploiting a vulnerability • Typically spawns a root shell, but may do something else
  • 5. System Calls (or Syscalls) • Syscalls directly access the kernel, to: – Get input – Produce output – Exit a process – Execute a binary file – And more • They are the interface between protected kernel mode and user mode
  • 6. 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
  • 7. Protecting the Kernel • Protected kernel mode – Prevents user applications from compromising the OS • If a user mode program attempts to access kernel memory, this generates an access exception • Syscalls are the interface between user mode and kernel mode
  • 8. 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
  • 9. Syscalls use INT 0x80 1. Load syscall number into EAX 2. Put arguments in other registers 3. Execute INT 0x80 4. CPU switches to kernel mode 5. Syscall function executes
  • 10. Syscall Number and Arguments • Syscall number is an integer in EAX • Up to six arguments are loaded into – EBX, ECX, EDX, ESI, EDI, and EBP • For more than six arguments, the first argument holds a pointer to a data structure
  • 12. exit() • The libc exit function does a lot of preparation, carefully covering many possible situations, and then calls SYSCALL to exit
  • 13. Disassembling exit • gdb -q e – disassemble main – main calls exit – exit calls __run_exit_handlers – __run_exit_handlers calls _exit – disassemble _exit • int 0x80 – call *$gs:10 – int 0x80
  • 14. Four Ways to Do Syscall • Link Ch 3o
  • 15. Disassembling _exit • syscall 252 (0xfc), exit_group() (kill all threads) • syscall 1, exit() (kill calling thread) – Link Ch 3e
  • 16. Writing Shellcode for the exit() Syscall
  • 17. Shellcode Size • Shellcode should be as simple and compact as possible • Because vulnerabilities often only allow a small number of injected bytes – It therefore lacks error-handling, and will crash easily
  • 18. sys_exit Syscall • Two arguments: eax=1, ebx is return value (0 in our case) • Link Ch 3m
  • 19. Simplest code for exit(0)
  • 20. nasm and ld • sudo apt install nasm • nasm creates object file • gcc links it, creating an executable ELF file
  • 21. objdump • Shows the contents of object files
  • 22. C Code to Test Shellcode • From link Ch 3k • Textbook version explained at link Ch 3i
  • 23. Compile and Run • Textbook omits the "-z execstack" option • It's required now or you get a segfault • Next, we'll use "strace" to see all system calls when this program runs • That shows a lot of complex calls, and "exit(0)" at the end
  • 24. Using strace • sudo apt install strace
  • 26. Getting Rid of Nulls • We have null bytes, which will terminate a string and break the exploit
  • 27. 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
  • 29. objdump of New Exit Shellcode
  • 31. 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
  • 32. Five Steps to Shellcode 1. Write high-level code 2. Compile and disassemble 3. Analyze the assembly 4. Clean up assembly, remove nulls 5. Extract commands and create shellcode
  • 33. fork() and execve() • Two ways to create a new process in Linux • Replace a running process – Uses execve() • Copy a running process to create a new one – Uses fork() and execve() together
  • 35. C Program to Use execve() • Static linking preserves our execve syscall
  • 36. In gdb, disassemble main • Pushes 3 Arguments • Calls __execve
  • 37. disassemble execve • Puts four parameters into edx, ecx, ebx, and eax