CNIT 127: Exploit Development. Ch 18: Source Code Auditing. Updated

Similar documents
CNIT 127: Exploit Development. Ch 14: Protection Mechanisms. Updated

Buffer overflow background

Software Security II: Memory Errors - Attacks & Defenses

Outline. Classic races: files in /tmp. Race conditions. TOCTTOU example. TOCTTOU gaps. Vulnerabilities in OS interaction

Verification & Validation of Open Source

Programmer s Points to Remember:

ECS 153 Discussion Section. April 6, 2015

Software Security Buffer Overflows more countermeasures. Erik Poll. Digital Security Radboud University Nijmegen

Reflections on using C(++) Root Cause Analysis

CSE 565 Computer Security Fall 2018

Software Security Buffer Overflows more countermeasures. Erik Poll. Digital Security Radboud University Nijmegen

Program Security and Vulnerabilities Class 2

2 Sadeghi, Davi TU Darmstadt 2012 Secure, Trusted, and Trustworthy Computing Chapter 6: Runtime Attacks

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

Hugbúnaðarverkefni 2 - Static Analysis

Beyond Stack Smashing: Recent Advances in Exploiting. Jonathan Pincus(MSR) and Brandon Baker (MS)

Secure Programming I. Steven M. Bellovin September 28,

Software Security: Buffer Overflow Defenses

C and C++ Secure Coding 4-day course. Syllabus

Buffer overflow prevention, and other attacks

Other array problems. Integer overflow. Outline. Integer overflow example. Signed and unsigned

Security and Privacy in Computer Systems. Lecture 5: Application Program Security

Oracle Developer Studio Code Analyzer

Copyright (2004) Purdue Research Foundation. All rights reserved.

The Shellcoder's Handbook Discovering and Exploiting Security Holes Second Edition

A program execution is memory safe so long as memory access errors never occur:

Agenda. Security and Standard C Libraries. Martyn Lovell Visual C++ Libraries Microsoft Corporation

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated

Black Hat Webcast Series. C/C++ AppSec in 2014

Fundamentals of Computer Security

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

Secure Software Programming and Vulnerability Analysis

Software Security: Buffer Overflow Attacks

Buffer Overflow Defenses

1.1 For Fun and Profit. 1.2 Common Techniques. My Preferred Techniques

Hackveda Training - Ethical Hacking, Networking & Security

CNIT 129S: Securing Web Applications. Ch 12: Attacking Users: Cross-Site Scripting (XSS) Part 2

Buffer Overflows: Attacks and Defenses for the Vulnerability of the Decade Review

Secure Coding in C and C++

Topics in Software Security Vulnerability

Software and Web Security 1. Root Cause Analysis. Abstractions Assumptions Trust. sws1 1

Runtime Defenses against Memory Corruption

CYSE 411/AIT681 Secure Software Engineering Topic #10. Secure Coding: Integer Security

Secure Programming. Buffer Overflows Learning objectives. Type. 3 Buffer Overflows. 4 An Important Vulnerability. 5 Example Overflow

2/9/18. Readings. CYSE 411/AIT681 Secure Software Engineering. Introductory Example. Secure Coding. Vulnerability. Introductory Example.

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

CSCE 548 Building Secure Software Buffer Overflow. Professor Lisa Luo Spring 2018

CNIT 129S: Securing Web Applications. Ch 10: Attacking Back-End Components

Bypassing Browser Memory Protections

Lecture 05 Integer overflow. Stephen Checkoway University of Illinois at Chicago

Memory Corruption 101 From Primitives to Exploit

CIS 551 / TCOM 401 Computer and Network Security. Spring 2007 Lecture 2

The Shellcoder's Handbook:

DAY 3. CS3600, Northeastern University. Alan Mislove

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

CMSC 414 Computer and Network Security

Software Tools for Source Code Analysis

SoK: Eternal War in Memory

System Administration and Network Security

Secure Coding Techniques

Limitations of the stack

(In columns, of course.)

Identifying Memory Corruption Bugs with Compiler Instrumentations. 이병영 ( 조지아공과대학교

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Software Vulnerabilities August 31, 2011 / CS261 Computer Security

EURECOM 6/2/2012 SYSTEM SECURITY Σ

Lecture 1: Buffer Overflows

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Hands-on Ethical Hacking: Preventing & Writing Buffer Overflow Exploits

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

CSCE 548 Building Secure Software Integers & Integer-related Attacks & Format String Attacks. Professor Lisa Luo Spring 2018

Advanced Programming & C++ Language

Software Security: Buffer Overflow Attacks (continued)

Ethical Hacking: Preventing & Writing Buffer Overflow Exploits

Foreword by Katie Moussouris... Acknowledgments... xvii. Introduction...xix. Chapter 1: The Basics of Networking... 1

20: Exploits and Containment

A Heap of Trouble Exploiting the Linux Kernel SLOB Allocator

Reversed Buffer Overflow Cross Stack Attacks. Kris Kaspersky Endeavor Security, Inc.

CS-527 Software Security

Buffer Overflows Defending against arbitrary code insertion and execution

[0569] p 0318 garbage

Stack Overflow. Faculty Workshop on Cyber Security May 23, 2012

Secure Systems Engineering

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

Dynamic Memory Allocation. Zhaoguo Wang

Computer Programming: Skills & Concepts (CP) Strings

Overview AEG Conclusion CS 6V Automatic Exploit Generation (AEG) Matthew Stephen. Department of Computer Science University of Texas at Dallas

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

Making C Less Dangerous

Is Exploitation Over? Bypassing Memory Protections in Windows 7

Attacking the Code: Source Code Auditing

Foundations of Network and Computer Security

Lecture 4 September Required reading materials for this class

We will focus on Buffer overflow attacks SQL injections. See book for other examples

Lecture Embedded System Security A. R. Darmstadt, Runtime Attacks

Secure Coding in C and C++

Transcription:

CNIT 127: Exploit Development Ch 18: Source Code Auditing Updated 4-10-17

Why Audit Source Code? Best way to discover vulnerabilities Can be done with just source code and grep Specialized tools make it much easier

Cscope A source code browsing tool Useful for large code trees, such as the whole Linux kernel Many useful search functions Cbrowser: GUI front-end Links Ch 18a, 18b

Ctags Indexes source code Creates a tag file with locations for language tags in files scanned Works in many languages, including C and C++ Link Ch 18c

Text Editor Vim and Emacs have features that make writing and searching though large amounts of code easy Bracket-matching: find matching ([{

Automated Source Code Analysis Tools

Splint Badly out-of date (last revised in 2007) Output a little hard to understand Links Ch 18d, 18e

Many available, specialized by language Link Ch 18f

Easy to use Finds about half the obvious vulnerabilities we've exploited

Heap Overflow

Finds Some Vulnerabilities But not the overflow!

Format String Vulnerability It doesn't find it at all!

Flawfinder Much better In Kali apt-get update apt-get install flawfinder

Methodology

Top-Down (Specific) Approach Search for specific lines of vulnerable code, such as format string errors Auditor doesn't have to understand application in depth Misses vulnerabilities that span more than one part of the code

Bottom-Up Approach Auditor reads large portion of code Starting at main() Time-consuming but can reveal subtle bugs

Selective Approach Most auditors use this approach Locate code that can be reached with attacker-defined input Focus energy on that code Learn the purpose of that code thoroughly

Vulnerability Classes

Generic Logic Errors Requires good understanding of an application And internal structures and classes Example: wildcard certificates Pascal-based CA will sell a certificate for * \0.evil.com C-based browser will see it as *, a wildcard Link Ch 18g

(Almost) Extinct Bug Classes Unbounded memory copy functions strcpy(), sprintf(), strcat(), gets(), Hunted nearly to extinction

Root Cause (from Microsoft)

Bypassing ASLR & DEP

Format Strings Easy to find with a code audit Although cppcheck failed Often found in logging code Vulnerable only if attacker controls the format string

Generic Incorrect Bounds-Checking Coder attempts to check limits, but does it incorrectly Example: Snort RCP Processor (2003) Processes a series of RPC fragments Checks each fragment to make sure it's not larger than the buffer But it should check the total size of all combined fragments

Snort RCP Processor (2003)

Loop Constructs Coders often use intricate loops, and loops within loops Complex interactions can lead to insecurities Led to a buffer overflow in Sendmail Link Ch 18h

Link Ch 18i Demonstration Exploit

Off-by-One Vulnerabilities Often caused by improper nulltermination of strings Frequently found in loops or introduced by common string functions Can lead to arbitrary code execution

Example from Apache When both if statements are true Space allocated is one byte too small memcpy will write one null out of bounds

OpenBSD ftp Daemon If last character is a quote, it can be written past the bounds of the input buffer

strncat() Strncat always null-terminates its output string Will write a null byte out of bounds unless the third argument is equal to the remaining space in the buffer minus one byte

Non-Null Termination Issues If a string is not terminated with a null Memory after the string is interpreted as part of the string May increase length of string String writes may corrupt memory outside the string buffer Can lead to arbitrary code execution

strncpy() If there's not enough space in the destination buffer strncpy() won't null-terminate the string it writes

strncpy() Example First strncpy won't null-terminate not_term_buf Second strcpy is unsafe, even though both buffers are the same size Fix it by adding this line of code after the first strcpy

Skipping Past Null-Termination String-processing loops that process more than one character at a time Or where assumptions about string length are made Can make it possible to write past end of a buffer Possible arbitrary code execution

Example from Apache This line is intended to skip past :// in a URL cp += 3

But Not All Schemes End in :// If the URI is ldap:a The null byte is skipped

Signed Comparison Vulnerabilities Coder attempts to check input length But uses a signed integer variable Or two different integer types or sizes C sometimes converts them both to signed integers before comparing them Following example from Apache Led to code execution on Windows and BSD Unix

Example from Apache bufsize is a signed integer Remaining space in the buffer r->remaining is signed Chunk size from the request len_to_read should be the smaller of the two Negative chunk size tricks the code into performing a large memcpy later, because it's cast to unsigned

Integer Conversions

Link Ch 18l

A hashed password can begin with 0e and contain only digits (very rare) Like 0e12353589661821035685 PHP reads that as scientific notation 0^123 Always zero (link Ch 18j)

Double Free Vulnerabilities Freeing the same memory chunk twice Can lead to memory corruption and arbitrary code execution Most common when heap buffers are stored in pointers with global scope Good practice: when a global pointer is freed, set it to Null to prevent it being reused Prevents dangling pointers

Out-of-Scope Memory Usage Vulnerabilities Use of a memory region before or after it is valid Also called "Dangling Pointer" Image from Wikipedia Link Ch 18k)

Uninitialized Variable Usage Static memory in the.data or.bss sections of an executable are initialized to null on program startup But memory on the stack or heap is not Uninitializes variables will contain data from previous function calls Argument data, saved registers, or local variables from previous function calls

Uninitialized Variable Usage Rare, because they can lead to immediate program crashes So they get fixed Look for them in code that is rarely used Such as handlers for uncommon errors Compilers attempt to prevent these errors

Example If data is null test is never assigned any value But test is still freed

Exploitation The "uninitialized" data in test is not random It comes from previous variables and function calls It may be controlled by the attacker So the free() leads to a controllable memory write Arbitrary code execution

Use After Free Vulnerabilities Heap buffers are temporary Released with free() But a program may use a pointer after free() If more than one variable points to the same object Allows an attacker to write to RAM Possible arbitrary code execution

Multithreaded Issues and Re-Entrant Safe Code A global variable is used by more than one thread, without proper locking A variable might be changed unexpectedly by another thread Such issues won't appear until the server is under heavy load May remain as intermittent software bugs that are never verified