BUFFER OVERFLOWS EXAMPLES

identifies whether a vulnerability exists in programs and exploiting those vulnerabilities

VULNERABLE C FUNCTIONS

these functions have no bounds checking. these functions will consume, create & write data until a null terminating string is encountered

strcpy(), strcat(), strpringf(), vsprintf(), gets(), scanf()

SECURE C FUNCTIONS (ALTERNATIVES)

strncpy(), strncat(), snprintf(), fgets()

SAMPLE VULNERABLE PROGRAM FLOW

int main()
{ 
  //function prototype
  void vulnFunc();

  //greet our Trojan friends
  printf("Hello, DSU!\n");
  
  //do something interesting
  vulnFunc();
  
  //close
  return 0;
  
  //function implementation
  void vulnFunc(){
    //local variables
    int a = 1;
    int b = 20;
    int c = 123;
    char buffer [8];
  
    //get user input, print it
    gets(buffer)                          //the gets() will not pay any attention to the "8" character buffer limit and will accept input until a null terminator is encountered
                                          //gets() does not know or care about how 
                                          //big buffer is.It reads characters until 
                                          //it sees a newline (\n) or EOF, and then 
                                          //adds a null terminator (\0) — even if 
                                          //the input is longer than the buffer. This
                                          //causes a buffer overflow, which can 
                                          //Corrupt memory,Crash programs, Be 
                                          //exploited for code execution (classic vuln)
                                          //the function header
    printf("%s\n", buffer);
  
    return;
  }
}

SIMPLE BUFFER OVERFLOW PROGRAM

EXPLOITATION

STEP 1: CODE REVIEW (IF SOURCES ARE AVAILBLE)

STEP 2: DEBUGGING (IF SOURCES AREN'T AVAILABLE)

this method requires determining how many characters will break the program

UNDERSTANDING THE PROGRAM CRASH

Last updated