1 15-BufferOverflow


Previous: 14-MaliciousSoftware.html

1.1 Program inputs??

Besides user-input, what should be considered program inputs?

A bio-centric attack:
https://www.technologyreview.com/s/608596/scientists-hack-a-computer-using-dna/
http://dnasec.cs.washington.edu/dnasec.pdf
http://dnasec.cs.washington.edu/

1.2 Extra reading

13b-ReverseEngineering/assembly64.pdf
https://www.hacksplaining.com/exercises/buffer-overflows
https://www.hacksplaining.com/prevention/buffer-overflows

1.3 Screencasts

1.4 Background

https://en.wikipedia.org/wiki/Buffer_overflow

1.4.1 Address space

1.4.2 Virtual memory

Mapping virtual addresses to real addresses
15-BufferOverflow/virt_mem.png

1.4.3 Memory organization

15-BufferOverflow/memory_diagram_stack_heap.png
15-BufferOverflow/stack00.png

15-BufferOverflow/mem.png

1.4.4 Stack

Stack growing downward here
15-BufferOverflow/call_stack.png

1.4.5 Stack and heap growth

Stack grows down, heap grows up
15-BufferOverflow/f4-crop.png

1.4.6 Frame

Each frame on the stack:
15-BufferOverflow/f3-crop.png

1.5 Buffer overflow

1.5.1 Exploits

What is an Exploit?

1.5.2 Definition

Buffer Overflow Attack

1.5.3 History

Buffer overflow attack history
15-BufferOverflow/image4.png
Still quite common due to legacy code in C/C++/asm!

A buffer overflow, also known as a buffer overrun, is defined in the NIST Glossary of Key Information Security Terms as follows:
“A condition at an interface under which more input can be placed into a buffer or data holding area than the capacity allocated, overwriting other information. Attackers exploit such a condition to crash a system or to insert specially crafted code that allows them to gain control of the system.”

1.5.4 Buffer Overflow Basics

1.5.5 Example in C

Contiguous memory in C

unsigned short B = 1979;
char A[8] = "";

Lower in stack15-BufferOverflow/over00.pngHigher in stack
Contiguous memory in C

Unsafe:

strcpy(A, "excessive");

Lower in stack15-BufferOverflow/over01.pngHigher in stack

Safer: To prevent the buffer overflow from happening in this example, the call to strcpy could be replaced with strncpy, which takes the maximum capacity of A as an additional parameter and ensures that no more than this amount of data is written to A:

strncpy(A, "excessive", sizeof(A));

Safest: Use a modern fast systems-capable language like Rust.

1.5.6 strcpy() vs. strncpy()

Explicit is better than implicit!

1.5.7 Attacks

Buffer Overflow Attacks

1.5.7.1 Fuzzing

https://en.wikipedia.org/wiki/Fuzzing
https://www.fuzzingbook.org/

1.5.8 Programming language

Memory organization
15-BufferOverflow/memory_diagram_stack_heap.png
15-BufferOverflow/stack00.png

1.6 Stack buffer overflow

1.6.1 Simple example

Stack buffer overflow vulnerability example in C

#include <string.h>

void foo(char *bar)
{
    char  c[12];
    strcpy(c, bar);  // no bounds checking
}

int main(int argc, char **argv)
{
    foo(argv[1]);
    return 0;
}
// g++ source.cpp
// ./a.out arg1tooooloooooongg

+++++++++++++++ Cahoot15-1

Stack buffer overflow vulnerability example in C

Stack buffer overflow vulnerability example Before data is copied
NOTE: this image series is “upside-down” compared to most stack diagrams
15-BufferOverflow/overflow3.png

Stack buffer overflow vulnerability example Hello is the first command line argument
15-BufferOverflow/overflow1.png

Stack buffer overflow vulnerability example
“AAAAAAAAAAAAAAAAAAAA
is the first command line argument (address of what?)
15-BufferOverflow/overflow2.png

1.6.2 Another example

Buffer overflow attack
15-BufferOverflow/image6.png

Buffer overflow attack
15-BufferOverflow/image7.png

1.6.3 Recall

Password example buffer_overflow.cpp uploaded during the last section.

1.7 Shellcode injection

Side note:

1.7.1 Stack Overflow Variants

Target

Shellcode functions

1.8 Defenses

Two approaches to buffer overflow defense

Compile time
Aim to harden programs to resist attacks in new programs.

Run time
Aim to detect and abort attacks in existing programs.

1.8.1 Compile-Time Defenses: Programming Language

Solution:
Use a modern high-level language (python, rust, ruby, lua, go etc).

Advantages

Disadvantages

1.8.2 Compile-Time Defenses: Safe Coding Techniques

1.8.3 Run-time defenses

1.9 Heap overflow

1.10 Defenses

Two approaches to buffer overflow defense

Compile time
Aim to harden programs to resist attacks in new programs.

Run time
Aim to detect and abort attacks in existing programs.

1.10.1 Compile time

1.10.1.1 Programming language

Examples
15-BufferOverflow/languages.png

1.10.1.1.1 Classic overflow
void copyData(char *userId)
{
    char smallBuffer[10]; // size of 10
    strcpy(smallBuffer, userId);
}

int main(int argc, char *argv[])
{
    // Payload of 11
    char *userId = "01234567890";
    // this shall cause a buffer overload
    copyData(userId);
    return 0;
}

Unsafe functions
15-BufferOverflow/image14.png

Range checking with strncopy, strncat, cin.getline, etc., is often suggested, as human-factors hand-holding / mommying you.

Classic overflow fix?

#include <iostream>
#include <cstring>

int main(void)
{
    char strDest[3]= "hi";
    char strSrc[] = "Welcome";
    char anotherCstring[] = "Hello";
    strncpy(strDest, strSrc, 5);
    std::cout << strDest;
    return 0;
}

strncopy can cause another overflow too (no NULL check)

Unsafe functions
15-BufferOverflow/funcs.png

1.10.1.1.2 Format Strings

Some format parameters:

%x  // hexadecimal (unsigned int)
%s  // string ((const) (unsigned) char *)
%n  // number of bytes written so far, (* int)
%d  // decimal (int)
%u  // unsigned decimal (unsigned int)

Example:
printf ("Hello:%s\n", a273150);

Issues:

printf (User_Input); // Vulnerability:
// Attack:
"%s%s%s%s%s%s%s%s%s%s%s%s"
printf("%s", str); //Fix:

For every % in the argument the printf function finds, it assumes that there is an associated value on the stack.
In this way, the function walks the stack downwards, reading the corresponding values from the stack, and printing them to the user.

1.10.1.1.3 Integer overflow
int main(void)
{
    int val;
    val = 0x7fffffff;     /* 2147483647*/
    printf("val=%d(0x%x)\n", val, val);
    /*Overflow the int*/
    printf("val+1=%d(0x%x)\n", val + 1, val + 1);
    return 0;
}

Integer overflow

#include <limits.h>

int safe_add(int a, int b)
{
    if(a > 0 && b > INT_MAX - a)
    {
        /* handle overflow */
    }
    else if(a < 0 && b < INT_MIN - a)
    {
        /* handle underflow */
    }
    return a + b;
}
1.10.1.1.4 Array bounds checking

1.10.1.2 Language Extensions/Safe Libraries

1.10.1.3 Stack protection

1.10.2 Run-time defenses

1.10.2.1 Executable address space protection

Overview

Issues

1.10.2.2 Address space randomization

1.10.2.3 Random dynamic memory allocation

1.10.2.4 Random standard library locations

1.10.2.5 Guard pages

1.11 Other attack types

1.11.1 Replacement stack frame

1.11.2 Return to system call

Stack overflow variant replaces return address with standard library function

Defenses

1.11.3 Heap overflow

1.11.4 Global data overflow

Can attack buffer located in global data

Defenses

+++++++++++++++++++++++ Cahoot15-2

Next: 16-Databases.html