Class_Buffer_overflow_2024
Class_Buffer_overflow_2024
Buffer Overflow
• Buffer: contiguous allocated chunk of memory, such as an
array or a pointer in C.
1
29-01-2024
A Small Example
• Malicious user enters >
1024 chars, but buf can
only store 1024 chars;
extra chars overflow
buffer
void get_input() {
char buf[1024];
gets(buf);
}
void main(int argc, char*argv[]){
get_input();
}
4
2
29-01-2024
The Problem
void try1(char *s) {
char buf[10];
strcpy(buf,s);
printf(“buf is %s\n”,s);
}
…
try1(“thisstringistolong”);
Continued…
int main()
{ Writing past the buffer
int buffer[10];
buffer[20]=10;
}
3
29-01-2024
Buffer Overflows
• Applications running on the host in Internet have certain
privileges.
• They can access part of the system resources like system
variables, system files and even execute certain commands
that a remote user/application cannot.
• From an attacker’s view point if he can take control of a
vulnerable application running on target on a target
computer he can misuse the privileges of victim
application.
• This happens in buffer overflow. 7
4
29-01-2024
checkPassword() Bugs
• Execution stack: maintains current function state
and address of return function
10
5
29-01-2024
6
29-01-2024
StackGuard
• Canary: random value, unpredictable to attacker
• Compiler technique: inserts canary before return
address on stack
• Corrupt Canary: code halts
program to thwart a
possible attack
• Not comprehensive
protection
Source: C. Cowan et. al., StackGuard,
13
Stack Direction
• On Linux (x86) the stack grows from high
addresses to low.
14
7
29-01-2024
A Stack Frame
Parameters
Return Address
Calling Frame Pointer
SP+offset
Local Variables
SP
Addresses
00000000
15
18
addressof(y=3) return address
Sample saved stack pointer
y
Stack x
buf
8
29-01-2024
9
29-01-2024
NOPs
• Most CPUs have a No-Operation
instruction – it does nothing but advance the
instruction pointer.
• Usually we can put a bunch of these ahead
of our program (in the string).
• As long as the new return-address points to
a NOP we are OK.
20
10
29-01-2024
Using NOPs
new return address
Real program
(exec /bin/ls or whatever)
nop instructions
21
22
11
29-01-2024
Solutions
Checking of
bounds
24
12
29-01-2024
Vulnerability!!
26
13
29-01-2024
Continued…
• Instead of giving a string if we give a
format string as below for the input of the
vulnfun() function:
“%08x.%08x.%08x.%08x.%08x”
• The printf below prints the values of five
consecutive words in the stack (as eight-
digit padded hexadecimal numbers). We
can walk up the stack this way and view the
27
contents of the stack.
Countermeasure…
Allow printf to print
only string values
28
14
29-01-2024
Countermeasures
• Secure coding:
– Bound checking mandatory
29
– All user inputs should be checked or validated for
malicious code.
15
29-01-2024
Conclusion
• Buffer Overflow attacks are not only very
dangerous but once executed,they are also quite
easy to execute.
• It is very important to take adequate security
countermeasures against buffer overflows.
• It is suggested to use programming languages like
java which do not give direct access on addresses.
• Buffer Overflow attacks can be countered by
Programming Applications efficiently & securely. 31
References
• Network Security: A Hacker’s Perspective-Ankit Fadia
• https://www.owasp.org/index.php/Buffer_Overflow
• www.linuxjournal.com/article/6701
• www.windowsecurity.com/.../analysis_of_buffer_overflow
_attacks
• Operating System #37 Buffer Overflow Attacks Explained
in Detail Best Programming Courses @
https://goo.gl/MVVDXR Complete Operating Systems
Lecture/ Tutorials from IIT @ https://goo.gl/GMr3if
32
16