ch10-2025
ch10-2025
Chapter 10
Buffer Overflow
Information Security
2025 Fall Semester
Younho Lee
Learning Objective
❖ Define what a buffer overflow is, and list possible consequences
❖ Describe how a stack buffer overflow works in detail
❖ Define shellcode and describe its use in a buffer overflow attack
❖ List various defences against buffer overflow attacks
❖ List a range of other types of buffer overflow attacks
Introduction
❖ Widely used since Morris Worm at 1988
❖ Techniques for prevention is well known, however it is still major
concern due to:
▪ Legacy buggy codes
▪ Failure to patch and update existing codes
▪ Continuing careless programming practices
❖ Brief History of Some buffer overflow attacks
Years Attacks
2014 Heartbleed (TLS protocol in OPENSSL Library)
2016 Adobe Flash Player
2019 WhatsApp VoIP (FaceBook)
10.1 Stack Overflows
next_tag(str1);
gets(str2);
if (strncmp(str1, str2, 8) == 0)
valid = TRUE;
printf(“buffer1: str1(%s), str2(%s), valid(%d)\n”, str1, str2, valid);
}
“BADINPUTBADINPUT”
10.1 Stack Overflows
❖Q
3) Pushes the current frame pointer value (which points to
the calling routine’s frame) onto the stack
4) Sets the frame pointer to be the current stack pointer
value (that is the address of the old frame pointer), which
now identifies the new stack frame location for the called
function * Example Stack
5) Allocates space for local variables by moving the stack Frame with
pointer down to leave sufficient room for them
Functions P and Q
6) Run the body of the called function
10.1 Stack Overflows - Stack Buffer Overflow (3/15)
$ ./buffer2
Enter value for name: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Segmentation fault (core dumped)
Enter value for name:
Hello your Re?pyy]uEA is ABCDEFGHQRSTUVWXabcdefguyu
$ perl –e ‘print pack(“H*”,
Enter value for Kyyu:
“41424344454647485152535455565758616263646566676808fcffbf948304080a4e4e4e4e0a”);’
Hello your Kyyu is NNNN
| ./buffer2
Segmentation fault (core dumped)
void main(void)
{
char tag[5]=“name”; // (A) Frame pointer indicates 0xbfffbf8e
hello(tag);
printf(“Buffer2 is done”); // (B) at 0x0804830f
}
10.1 Stack Overflows - Stack Buffer Overflow (6/15)
Shellcode
❖ Code supplied by attacker
▪ Often saved in buffer being overflowed
▪ Traditionally transferred control to a user command-line interpreter (shell)
• Shellcode includes the instructions such as execve(“/bin/sh”) or
system(“command.exe”)
▪ Attackers generate shellcode using machine code
❖ Machine code
▪ Specific to processor and operating system
▪ Traditionally needed good assembly language skills to create
▪ More recently a number of sites and tools have been developed that
automate this process
10.1 Stack Overflows - Stack Buffer Overflow (10/15)
Shellcode development
❖ Example UNIX shellcode
NOP
JMPSHORT POPA %esi
(Go ahead by 26 XOR From mov %al,
(0x58+0x06)
byte addresses) eax to 0x7(%esi)
eax
MOV src, dest copy (move) value from src into dest
LEA src, dest copy the address (load effective address) of src into dest
ADD / SUB src, dest add / sub value in src from dest leaving result in dest
AND / OR / XOR src, dest logical and / or / xor value in src with dest leaving result in dest
CMP val1, val2 compare val1 and val2, setting CPU flags as a result
JMP / JZ / JNZ addr jump / if zero / if not zero to addr
PUSH src push the value in src onto the stack
POP dest pop the value on the top of the stack into dest
CALL addr call function at addr
LEAVE clean up stack frame before leaving function
RET return from function
INT num software interrupt to access operating system function
NOP no operation or do nothing instruction
10.1 Stack Overflows - Stack Buffer Overflow (13/15)
❖ Attack
10.1 Stack Overflows - Stack Buffer Overflow (15/15)
Saved frame
pointer
Return Back up before
Address function begins
Buffer Overflow Defenses (8/10)
{ return x + y; }
return F(3) + F(4);
}
Buffer Overflow Defenses (9/10)
2nd execution:
a.out heap libc.so.6 stack
Buffer Overflow Defenses (10/10)
Original
R
R
1 0
F
P
R Stack
address
▪ Randomization of the stack in memory and
of system libraries
Normal After
case attack
Other Forms of Overflow Attacks (3/5)
Heap Overflow
❖ Attack buffer located in heap
▪ Typically located above program code
▪ Memory is requested by programs to use in dynamic data structures (such
as linked lists of records)
❖ No return address
▪ Hence no easy transfer of control
▪ May have function pointers can exploit
▪ Or manipulate management data structures
Defenses
• Making the heap non-executable
• Randomizing the allocation of memory on
the heap
Other Forms of Overflow Attacks (5/5)