• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Buffer OverFlow
 

Buffer OverFlow

on

  • 1,230 views

 

Statistics

Views

Total Views
1,230
Views on SlideShare
906
Embed Views
324

Actions

Likes
1
Downloads
23
Comments
0

11 Embeds 324

http://flexaired.blogspot.com 254
http://flexaired.blogspot.in 52
http://flexaired.blogspot.co.uk 5
http://flexaired.blogspot.ca 3
http://flexaired.blogspot.nl 3
http://flexaired.blogspot.de 2
http://flexaired.blogspot.pt 1
http://flexaired.blogspot.co.nz 1
http://flexaired.blogspot.com.ar 1
http://flexaired.blogspot.com.au 1
http://flexaired.blogspot.co.il 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Buffer OverFlow Buffer OverFlow Presentation Transcript

    • Buffer Over-run
    • Introduction
      • What is Buffer Overrun?
        • Buffer Over-run refers to problem where we can make program to use more than allotted ‘buffer’.
        • All buffer overruns cannot be exploited as security vulnerability.
      • What it could lead to?
        • Buffer overflows can be triggered by inputs that are designed to execute code, or alter the way the program operates.
        • You get an access violation (AV).
        • Your site becomes unstable.
        • The attacker injects code into your application, executes it, and makes everyone an administrator of your site.
    • The π Program
      • int main(int argc, char* argv[])
      • {
      • char buff[1024];
      • if (strcmp (argv[1], "3.1415926") == 0) {
      • strcpy (buff, argv[2]);
      • printf ("thanks, %sn", buff);
      • }
      • else {
      • strcpy(buff, argv[1]);
      • printf("sorry, %s is not correct.n", buff);
      • }
      • }
      Buffer Overflow #1 Buffer Overflow #2
    • Types of Buffer Overrun
      • Stack Overruns
      • Heap Overruns
      • Array Indexing Errors
      • Format String
      • Unicode and ANSI Buffer size mismatch
    • Stack Overrun
      • Occurs when a buffer declared on the stack is overwritten by copying data lager than buffer.
      • This can be exploited, by modifying return address (ESP) in stack.
      Local Var1 Param1 Local Var2 ESP Arg1 If we can change this return to desired address. Then execution after returning from this function, Execution would continue from modified address
    • Stack Overflow Example
      • Sample Execution for argument “Hello”–
      • foo = 00401000
      • bar = 00401045
      • Stack:
        • 6C6C6548  Hello
        • 0000006F
        • 7FFDF000
        • 0012FF80
        • 0040108A  Return Address of foo
        • 00410EDE
      • Execution for argument “ABCDEFGHIJKLMNOPx45x10x40”–
      • foo = 00401000
      • bar = 00401045
      • Stack:
      • 44434241
      • 48474645
      • 4C4B4A49
      • 504F4E4D
      • 00401045  Return address changed to bar()
      • 00410ECA
      • Hacked!!
      • void foo (const char *input)
      • {
      • char buf[10];
      • strcpy(buf, input);
      • printf(“Stack:%p %p %p %p %p %p}
      • }
      • void bar(){
      • printf(“Hacked!!’);
      • }
      • int main(int argc, char* argv[])
      • {
        • ……
        • printf(“foo = %p”,foo);
        • printf(“bar = %p”,bar);
        • foo(argv[1]);
        • ……
      • }
    • Stack Overrun
      • Off-by-One –
        • Programming error, which boundary while handling buffers.
        • void foo(char *in)
        • {
        • char buff[64];
        • strcpy(buff,in,sizeof(buff));
        • buff[sizeof(buff)] = “0”;  Off-by-one
        • }
      • To improve security , Intel in Itanium Processor(ia64) starts storing return address in Register. This make it difficult to exploit Stack Overrun on IA64, but not impossible.
      • Stack Overruns are usually discovered by attackers by using fuzzers
    • Stack BOs at Work
      • void func(char *p, int i)
      • {
        • int j = 0;
        • CFoo foo;
        • int (*fp)(char *, int) = &func;
        • char b[128];
        • strcpy(b,p);
      • }
    • Heap Overruns
      • Occurs by copying data lager than buffer size, for a buffer whose memory is allotted from heap (using malloc family functions).
      • Difficult to exploit as compared to Stack Overruns, as no program execution register can be modified.
    • Heap Overrun Example
      • void foo(char *input1, char *input2)
      • {
      • char *buff,buff2;
      • buff = (char*)malloc(16);
      • buff2= (char*)malloc(16);
      • strcpy(buff,input1);
      • strcpy(buff2,input2);
      • printf(“input 1 = %sn input 2 = %sn”, buff, buff2);
      • free(buff); free(buff2);
      • }
      • void bar(){
      • printf(“Hacked!!’);
      • }
      • int main(int argc, char* argv[])
      • {
        • char arg1[], arg2[]; // map to input
        • printf(“bar = %p”,bar);
        • foo(arg1,arg2);
        • ……
      • }
      • Sample Execution for argument “Hello Temp”–
      • bar = 41414141
      • input 1 = Hello
      • Input 2 = Temp
      • Now change parameter to “DDDDDDDDDDDDDCCC Temp”-
      • bar = 41414141
      • Warning Message = Memory “0x44434343” could not be read
      • With debugger, we can get to know that return address in stack while calling foo() is saved at 0x41424344
      • Now change parameter to “DDDDDDDDDDDDABCD AAAA”-
      • bar = 41414141
      • input 1 = Hello
      • Input 2 = AAAA
      • Hacked!!!
    • Array Indexing Errors
      • Manipulation can be done easily by passing non-valid index, which points to memory out of range of allocated array memory.
      • Ex. Char Array defined as arr[10].
        • char arr[10];
        • SetValue(index i, value x)
        • {
        • arr[i] = x;
        • }
      • Any of these operation would modify memory not allocated for buffer –
        • SetValue(-20,’x’) => Arr[-20] = ‘x’
        • SetValue(26,’x’) => Arr[26] = ‘x’
      • Variant – Truncation error. Where due to truncation of value, unexpected may happen. E.g.-
        • On a UNIX system ID of super user is ‘0’. Network File System Daemon accepts user id as integer, check with non-zero and then truncate to unsigned short.
        • So user with ID ‘10000’, on truncation would become ‘0’
    • Format String Bugs
      • The Format String exploit occurs in functions which expects variable number of inputs. E.g. – printf.
      • This problem arises because there isn’t any realistic way for a function which takes variable number of arguments to determine how many arguments where passed to it. Sample functions – printf, sprintf, fprintf
      • If a Format String has parameter , like %x, is inserted into the posted data, the string is parsed by the Format Function, and the conversion specified in the parameters is executed. The Format Function is expecting more arguments as input, and if these arguments are not supplied, the function could read or write the stack.
    • Format String Attack
      • int main (int argc, char **argv)
      • {
      • char buf [100];
      • int x = 1 ;
      • snprintf(buf, sizeof buf, argv [1] ) ;
      • buf [ sizeof(buf) -1 ] = 0 ;
      • printf ( “Buffer size is: (%d) nData
      • input:%sn” , strlen (buf) , buf ) ;
      • printf ( “X equals: %d/ in
      • hex: %#xnMemory address for x: (%p)
      • n” , x, x, &x) ;
      • return 0 ;
      • }
      • ./test.exe “aaaa”
      • Buffer size is (4)
      • Data input : aaaa
      • X equals: 1/ in hex: 0x1
      • Memory address for x (0xbffff73c)
      • ./test.exe “aaaa %x %x”
      • Buffer size is (15)
      • Data input : aaaa 1 61616161
      • X equals: 1/ in hex: 0x1
      • Memory address for x (0xbffff73c)
    • Unicode and ANSI Buffer Size Mismatches
      • Buffer overrun during conversion of Unicode and ANSI is very common problem on Windows.
      • Reason for conversion is due to variable bytes representation of characters in Unicode.
    • Arithmetic Overflow / Underflow Does it Look Familiar?
      • Algorithm(A[0..N-1], value)
      • {
      • low = 0
      • high = N - 1
      • while (low <= high)
      • {
      • mid = (low + high) / 2
      • if (A[mid] > value)
      • high = mid - 1
      • else if (A[mid] < value)
      • low = mid + 1
      • else
      • return mid // found
      • }
      • return -1 // not found
      • }
    • The ‘n’-function are safe? Right?! Be extra cautious when using ‘n’ functions! // Example #1 (code verifies pszSrc is <= 50 chars) #define MAX (50) char *pszDest = malloc(sizeof(pszSrc)); strncpy(pszDest,pszSrc,MAX); sizeof is 4-bytes, not 50 String not null-terminated if len(pszSrc) >= MAX // Example #2 #define MAX (50) char szDest[MAX]; strncpy(szDest,pszSrc,MAX); // Example #3 #define MAX (50) char szDest[MAX]; strncpy(szDest,pszSrc,strlen(pszSrc)); Wrong buffer size!
    • Preventing Buffer Overrun
      • Do not write user input to arbitrary location in memory.
      • Special care while doing string operations in code.
      • Choice of programming language. E.g. C++ STL provides in-built memory handling and checks.
      • Avoid standard library functions which are not bounds checked, such as gets , scanf and strcpy .
      • Use Strsafe.h
      • Use GS Flags while compilation.
      • Questions?