cnit 127: exploit development ch 16: fault injection

67
CNIT 127: Exploit Development Ch 16: Fault Injection

Upload: holly-sparks

Post on 18-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CNIT 127: Exploit Development Ch 16: Fault Injection

CNIT 127: Exploit Development

Ch 16: Fault Injection

Page 2: CNIT 127: Exploit Development Ch 16: Fault Injection

Fault Injection

• Long used to verify the fault tolerance of hardware, such as– Automobile and airplane components– Coffee makers

• Faults are injected through– Pins of integrated circuits– Bursts of EMI (Electromagnetic Interference)– Altered voltage levels, etc.

Page 3: CNIT 127: Exploit Development Ch 16: Fault Injection

QA (Quality Assurance)

• Engineers test software for weaknesses with fault injection

• Automating these tests makes their work much more efficient

• They also use manual auditing techniques– Reverse engineering– Source code auditing

Page 4: CNIT 127: Exploit Development Ch 16: Fault Injection

Topics

• Design Overview• Fault Monitoring• Putting It Together

Page 5: CNIT 127: Exploit Development Ch 16: Fault Injection

Design Overview

Page 6: CNIT 127: Exploit Development Ch 16: Fault Injection

Input Generation

• Select input that uses esoteric and untested software features

• This request uses the uncommon .ida filetype– An ISAPI filter included in IIS web server

Page 7: CNIT 127: Exploit Development Ch 16: Fault Injection

Generating Input

• Manual generation– Build inputs in a text editor– Time-consuming, but produces best results

• Automated generation– Creating fake input with a program– May waste time on buggy input

Page 8: CNIT 127: Exploit Development Ch 16: Fault Injection

Generating Input

• Live capture– Inject faults directly into live network traffic– Requires complex adjustment of data size fields,

checksums, etc.

• Fuzz generation– Researchers noticed core dumps when using a

dial-up modem during a thunderstorm– Random data injection found many new faults

Page 9: CNIT 127: Exploit Development Ch 16: Fault Injection

Fault Injection

• Open-source apps– Can be recompiled with special added code to

improve fuzzing– Such as American Fuzzy Lop (link Fuzz 15)

• Closed-source apps– Only input data is modified

Page 10: CNIT 127: Exploit Development Ch 16: Fault Injection

Modification Engines

• To find buffer overflows– Inject variable-sized data to elements– Use non-alphanumeric characters to delimit

elements– Inject into elements, without altering delimiters

Page 11: CNIT 127: Exploit Development Ch 16: Fault Injection
Page 12: CNIT 127: Exploit Development Ch 16: Fault Injection

Defeating Input Sanitization

• Repeat existing characters instead of injecting new ones

Page 13: CNIT 127: Exploit Development Ch 16: Fault Injection

Fault Delivery

• Nagel algorithm– Delays transmission of small datagrams so they can

be grouped together– Enabled by default in Windows– Must be disabled with NO_DELAY flag

• Link Ch 16a

Page 14: CNIT 127: Exploit Development Ch 16: Fault Injection

Fault Monitoring

Page 15: CNIT 127: Exploit Development Ch 16: Fault Injection

Using a Debugger

• Good for interactive fault testing• Capture every exception, if possible

– Instead of passing them to the application first ("First chance")

• Access-violation exceptions are the most important– Indicate that data structures used to read or write

to RAM were corrupted

Page 16: CNIT 127: Exploit Development Ch 16: Fault Injection

CNIT 127: Exploit Development

Ch 17: The Art of Fuzzing

Page 17: CNIT 127: Exploit Development Ch 16: Fault Injection

Static Analysis

• Analyzing code that is not running• Source code or binary• Many bugs found this way are unimportant in

practice– Because there is no input from the user that

"reaches" the buggy code– There's no easy way to determine the reachability

of a bug from static analysis

Page 18: CNIT 127: Exploit Development Ch 16: Fault Injection

Fuzzing is Scalable

• An SMTP fuzzer can test any SMTP server• No need to rewrite it• Very simple strings may apply to many

protocols– Such as "../" * 5000

Page 19: CNIT 127: Exploit Development Ch 16: Fault Injection

Weaknesses in Fuzzers

• Some parts of code won't be hit by a fuzzer– Because it requires special input values we don't

know about

• Fuzzing gets very slow if many parameters vary

• Fuzzing should be supplemented by static analysis and runtime binary analysis

Page 20: CNIT 127: Exploit Development Ch 16: Fault Injection

SPIKE

• Builds a network packet by adding data one field at a time to a "spike" data structure

• Automatically fills in size fields, checksums, etc.

• Has various sending programs– Such as generic_send_tcp

Page 21: CNIT 127: Exploit Development Ch 16: Fault Injection

SPIKE Functions

• s_string("Hello, world!");– Adds the literal string Hello World! to the spike

• s_string_variable("MESSAGE");– Adds a series of varying strings to the spike– The first one is MESSAGE

• s_readline();– Reads a message from the server

Page 22: CNIT 127: Exploit Development Ch 16: Fault Injection

Very Simple SPIKE Script

• Enough to fuzz "Vulnerable Server"

Page 23: CNIT 127: Exploit Development Ch 16: Fault Injection

X-Query (for Unix)

• Capture with WireShark

Page 24: CNIT 127: Exploit Development Ch 16: Fault Injection

Spike Script (Partial)

Page 25: CNIT 127: Exploit Development Ch 16: Fault Injection

CNIT 127: Exploit Development

Ch 18: Source Code Auditing

Page 26: CNIT 127: Exploit Development Ch 16: Fault Injection

Cscope

• A source code browsing tool• Useful for large code trees,

such as the whole Linux kernel

• Many useful search functions• Links Ch 18a, 18b

Page 27: CNIT 127: Exploit Development Ch 16: Fault Injection
Page 28: CNIT 127: Exploit Development Ch 16: Fault Injection

Ctags

• Indexes source code• Works in many

languages– Link Ch 18c

Page 29: CNIT 127: Exploit Development Ch 16: Fault Injection

Automated Source Code Analysis Tools

Page 30: CNIT 127: Exploit Development Ch 16: Fault Injection

Splint

• Badly out-of date (last revised in 2007)• Output a little hard to understand

– Links Ch 18d, 18e

Page 31: CNIT 127: Exploit Development Ch 16: Fault Injection

• Many available, specialized by language• Link Ch 18f

Page 32: CNIT 127: Exploit Development Ch 16: Fault Injection

• Easy to use• Finds about half the obvious vulnerabilities

we've exploited

Page 33: CNIT 127: Exploit Development Ch 16: Fault Injection

Heap Overflow

Page 34: CNIT 127: Exploit Development Ch 16: Fault Injection

Finds Some Vulnerabilities

• But not the overflow!

Page 35: CNIT 127: Exploit Development Ch 16: Fault Injection

Format String Vulnerability

• It doesn't find it at all!

Page 36: CNIT 127: Exploit Development Ch 16: Fault Injection

Vulnerability Classes

Page 37: CNIT 127: Exploit Development Ch 16: Fault Injection

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

Page 38: CNIT 127: Exploit Development Ch 16: Fault Injection

(Almost) Extinct Bug Classes

• Unbounded memory copy functions– strcpy(), sprintf(), strcat(), gets(), …

• Hunted nearly to extinction

Page 39: CNIT 127: Exploit Development Ch 16: Fault Injection

Root Cause (from Microsoft)

Page 40: CNIT 127: Exploit Development Ch 16: Fault Injection

Bypassing ASLR & DEP

Page 41: CNIT 127: Exploit Development Ch 16: Fault Injection

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

Page 42: CNIT 127: Exploit Development Ch 16: Fault Injection

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

Page 43: CNIT 127: Exploit Development Ch 16: Fault Injection

Snort RCP Processor (2003)

Page 44: CNIT 127: Exploit Development Ch 16: Fault Injection

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

Page 45: CNIT 127: Exploit Development Ch 16: Fault Injection

Demonstration Exploit

• Link Ch 18i

Page 46: CNIT 127: Exploit Development Ch 16: Fault Injection

Off-by-One Vulnerabilities

• Often caused by improper null-termination of strings

• Frequently found in loops or introduced by common string functions

• Can lead to arbitrary code execution

Page 47: CNIT 127: Exploit Development Ch 16: Fault Injection

Example from Apache

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

Page 48: CNIT 127: Exploit Development Ch 16: Fault Injection

OpenBSD ftp Daemon

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

Page 49: CNIT 127: Exploit Development Ch 16: Fault Injection

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

Page 50: CNIT 127: Exploit Development Ch 16: Fault Injection

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

Page 51: CNIT 127: Exploit Development Ch 16: Fault Injection

strncpy()• If there's not enough space in the destination

buffer– strncpy() won't null-terminate the string it writes

• Example– strncpy won't null-terminate buffer if input is long– strcpy command is unsafe

Page 52: CNIT 127: Exploit Development Ch 16: Fault Injection

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

Page 53: CNIT 127: Exploit Development Ch 16: Fault Injection

Example from Apache

• This line is intended to skip past :// in a URL– cp += 3

Page 54: CNIT 127: Exploit Development Ch 16: Fault Injection

But Not All Schemes End in ://

• If the URI is ldap:a – The null byte is skipped

Page 55: CNIT 127: Exploit Development Ch 16: Fault Injection

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

Page 56: CNIT 127: Exploit Development Ch 16: Fault Injection

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

Page 57: CNIT 127: Exploit Development Ch 16: Fault Injection

Integer Conversions

Page 58: CNIT 127: Exploit Development Ch 16: Fault Injection

• Link Ch 18l

Page 59: CNIT 127: Exploit Development Ch 16: Fault Injection

• A hashed password can begin with 0e– Like 0e123dca2343f869ab478374cb358274

• PHP reads that as scientific notation– 0^123…– Always zero (link Ch 18j)

Page 60: CNIT 127: Exploit Development Ch 16: Fault Injection

Free Login

• 1/256 passwords start with 0e• You can use any password like this to log in to

any account with a hash like that• You'll get in after 256 tries if you can predict

the hash– Or 65,536 tries if you can't

Page 61: CNIT 127: Exploit Development Ch 16: Fault Injection

Double Free Vulnerabilities

• Freeing the same memory chunk twice• Can lead to memory corruption and arbitrary

code execution

Page 62: CNIT 127: Exploit Development Ch 16: Fault Injection

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)

Page 63: CNIT 127: Exploit Development Ch 16: Fault Injection

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

Page 64: CNIT 127: Exploit Development Ch 16: Fault Injection

Example

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

Page 65: CNIT 127: Exploit Development Ch 16: Fault Injection

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

Page 66: CNIT 127: Exploit Development Ch 16: Fault Injection

Use After Free Vulnerabilities

• Heap buffers are temporary– Released with free()

• But a program may use a pointer after free()– If there are more than one variable used to point

to the same object

• Allows an attacker to write to RAM– Possible arbitrary code execution

Page 67: CNIT 127: Exploit Development Ch 16: Fault Injection

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 issued won't appear until the server is under heavy load– May remain as intermittent software bugs that are

never verified