Format String.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

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.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Breno de MedeirosFlorida State University Fall 2005 Buffer overflow and stack smashing attacks Principles of application software security.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
University of Washington CSE 351 : The Hardware/Software Interface Section 5 Structs as parameters, buffer overflows, and lab 3.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
1 - buttons Click “Step Forward” to execute one line of the program. Click “Reset” to start over. “Play,” “Stop,” and “Step Back” are disabled in this.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
Computer Security and Penetration Testing
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
Brian E. Brzezicki. This tutorial just illustrates the underlying concepts of buffer overflows by way of an extremely simple stack overflow  Most buffer.
CNIT 127: Exploit Development Ch 4: Introduction to Format String Bugs.
Chapter 18 I/O in C.
Buffer overflow and stack smashing attacks Principles of application software security.
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.
Slides by Kent Seamons and Tim van der Horst Last Updated: Nov 11, 2011.
Embedding Assembly Code in C Programs תרגול 7 שילוב קוד אסמבלי בקוד C.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
CS 1704 Introduction to Data Structures and Software Engineering.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
VM: Chapter 7 Buffer Overflows. csci5233 computer security & integrity (VM: Ch. 7) 2 Outline Impact of buffer overflows What is a buffer overflow? Types.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
CSC 482/582: Computer Security
C Formatted Input/Output
Protecting Memory What is there to protect in memory?
User-Written Functions
Udaya Shyama Pallathadka Ganapathi Bhat CSCE 548 Student Presentation
Protecting Memory What is there to protect in memory?
Module 30 (Unix/Linux Security Issues II)
Protecting Memory What is there to protect in memory?
Chapter 2 - Introduction to C Programming
Chapter 18 I/O in C.
CSC 495/583 Topics of Software Security Stack Overflows (2)
Pointers.
Chapter 2 - Introduction to C Programming
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
CS 465 Buffer Overflow Slides by Kent Seamons and Tim van der Horst
Lecture 13 Input/Output Files.
Chapter 2 - Introduction to C Programming
Chapter 11 Introduction to Programming in C
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
CSCE Fall 2013 Prof. Jennifer L. Welch.
Chapter 2 - Introduction to C Programming
Software Security Lesson Introduction
Chapter 11 Introduction to Programming 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
Chapter 2 - Introduction to C Programming
A Simple Two-Pass Assembler
CSCE Fall 2012 Prof. Jennifer L. Welch.
Buffer Overflows.
Chapter 2 - Introduction to C Programming
Understanding and Preventing Buffer Overflow Attacks in Unix
Week 3: Format String Vulnerability
Format String Vulnerability
Return-to-libc Attacks
Presentation transcript:

Format String

printf ("The magic number is: %d\n", 1911); Output: 1911

The function retrieves the parameters requested by the format string from the stack

the format string asks for 3 arguments, but the program actually provides only two (i.e. a and b). Can this program pass the compiler? The function printf() is defined as function with variable length of arguments. To find the miss-match, compiler needs to understand how printf() works and what the meaning of the format string is. Sometimes, the format string is generated during the execution of the program. Can printf() detect the miss-match? Unless the stack is marked with a boundary, printf()does not know that it runs out of the arguments that are provided to it. In a miss-match case, printf() will fetch some data that do not belong to this function call.

Attacks on Format String Vulnerability Crashing the program -> printf ("%s%s%s%s%s%s%s%s%s%s%s%s"); the number fetched by printf() might not be an address, the memory pointed by this number might not exist (i.e. no physical memory has been assigned to such an address), and the program will crash. It is also possible that the number happens to be a good address, but the address space is protected (e.g. it is reserved for kernel memory). In this case, the program will also crash. Viewing the Stack printf ("%08x %08x %08x %08x %08x\n"); A possible output may look like: 40012980 080628c4 bffff7a4 00000005 08059c04 Viewing memory at any location We have to supply an address of the memory. However, we cannot change the code; we can only supply the format string If we use printf(%s) without specifying a memory address, the target address will be obtained from the stack anyway by the printf() function. The function maintains an initial stack pointer, so it knows the location of the parameters in the stack. Observation: the format string is usually located on the stack. If we can encode the target address in the format string, the target address will be in the stack.

the format string is stored in a buffer, which is located on the stack If we can force the printf to obtain the address from the format string (also on the stack), we can control the address. printf ("\x10\x01\x48\x08 %x %x %x %x %s"); \x10\x01\x48\x08 are the four bytes of the target address. In C language, \x10 in a string tells the compiler to put a hexadecimal value 0x10 in the current position. The value will take up just one byte. Without using \x, if we directly put "10" in a string, the ASCII values of the characters ’1’ and ’0’ will be stored. Their ASCII values are 49 and 48, respectively.

%x causes the stack pointer to move towards the format string %x causes the stack pointer to move towards the format string. Here is how the attack works if user input[] contains the following format string: "\x10\x01\x48\x08 %x %x %x %x %s". The key challenge in this attack is to figure out the distance between the user input[] and the address passed to the printf() function. This distance decides how many %x you need to insert into the format string, before giving %s.

Writing Integer Writing an integer to nearly any location in the process memory %n: The number of characters written so far is stored into the integer indicated by the corresponding argument. Just replace the %s in the above example with %n, and the contents at the address 0x10014808 will be overwritten Using this attack, attackers can do the following: Overwrite important program flags that control access privileges Overwrite return addresses on the stack, function pointers, etc.

Writing Integer Is it really possible to write arbitrary integer values? Use dummy output characters. To write a value of 1000, a simple padding of 1000 dummy characters would do. To avoid long format strings, we can use a width specification of the format indicators. Countermeasures. Address randomization: just like the countermeasures used to protect against buffer-overflow attacks, address randomization makes it difficult for the attackers to find out what address they want to read/write.