Objective Explain basic fuzzing with concrete coding example

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Exploring Security Vulnerabilities by Exploiting Buffer Overflow using the MIPS ISA Andrew T. Phillips Jack S. E. Tan Department of Computer Science University.
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
COSC 120 Computer Programming
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Netprog: Buffer Overflow1 Buffer Overflow Exploits Taken shamelessly from: netprog/overflow.ppt.
CSE 451 Section 4 Project 2 Design Considerations.
Guide To UNIX Using Linux Third Edition
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
University of Washington CSE 351 : The Hardware/Software Interface Section 5 Structs as parameters, buffer overflows, and lab 3.
Buffer overflows.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2011.
RjpSystem Level Programming Operating Systems 1 Having fun withy the Unix Operating System Praxis Week 7 Rob Pooley.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Exploiting Buffer Overflows on AIX/PowerPC HP-UX/PA-RISC Solaris/SPARC.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
Pointers OVERVIEW.
Buffer Overflow CS461/ECE422 Spring Reading Material Based on Chapter 11 of the text.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
File IO and command line input CSE 2451 Rong Shi.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Overflow Examples 01/13/2012. ACKNOWLEDGEMENTS These slides where compiled from the Malware and Software Vulnerabilities class taught by Dr Cliff Zou.
Smashing the Stack Overview The Stack Region Buffer Overflow
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Introduction to Systems Programming (CS 0449) C Preprocessing Makefile File I/O.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
JMU GenCyber Boot Camp Summer, Introduction to Penetration Testing Elevating privileges – Getting code run in a privileged context Exploiting misconfigurations.
CS 155 Section 1 PP1 Eu-Jin Goh. Setting up Environment Demo.
A Quick Look at C for C++ Programmers Noah Mendelsohn Tufts University Web: COMP.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 1 Scott Marino.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Files A collection of related data treated as a unit. Two types Text
C is a high level language (HLL)
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2014.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Error handling I/O Man pages
Content Coverity Static Analysis Use cases of Coverity Examples
Introduction to the C Language
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
CSC215 Lecture Input and Output.
Choice of Programming Language
CS111 Computer Programming
Hank Childs, University of Oregon
Objective Explain basic fuzzing with concrete coding example
Chapter 11 Introduction to Programming in C
Chapter 14 - Advanced C Topics
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Text and Binary File Processing
File Input and Output.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2015.
CNT4704: Analysis of Computer Communication Network Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Fall 2011.
Malware and Software Vulnerability Analysis Q&A of Fuzzing Programming Project 2 Cliff Zou University of Central Florida.
C Preprocessing File I/O
Malware and Software Vulnerability Analysis Fuzzing Test Example Cliff Zou University of Central Florida.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2016.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2013.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2010.
Presentation transcript:

CAP6135: Malware and Software Vulnerability Analysis Fuzzing Test Example Cliff Zou Spring 2014

Objective Explain basic fuzzing with concrete coding example Explain how the vulnerable code in programming project 2 is derived Introduce several useful techniques in doing the fuzzing test on project 2

Example Code $ fuzzTest-target 200 “what is this?” 2 Example code needs three inputs Int, string, Int int inputInteger; /* global variable */ void (*foo)(short); if (argc != 4){ fprintf(stderr, "fuzzTest needs 3 input parameters: int string int!\n"); exit(0); } sscanf(argv[1], "%d", &inputInteger); my_func(inputInteger, argv[2], argv[3]); Subfun my_func() introduces 3 man-made bugs

Bug # 1: Integer Overflow int my_func(short argLen, char *str, char *divStr) { int denominator; float x; char buf[bufLen]; if (argLen != inputInteger) { fprintf(stderr, "Bug #1: integer overflow triggered\n"); foo = (void *)0xbfffffff; foo(argLen); /* trigger illegal instruction fault */ exit(1); Int variable inputInteger changes to short Overflow happens when inputInteger>32767 foo() is a function pointer Give it an arbitrary address will cause illegal memory reference for executing code

Bug # 2: buffer Overflow char buf[10]; if (strlen(str) > 10){ fprintf(stderr, "Bug #2: buffer overflow triggered. strlen=%d\n", strlen(str)); strcpy(buf, str); /* trigger segmentation fault or stack smashing */ return 2; /*if overwriting return address, it will cause segmentation fault */ }

Bug #3: divide by zero sscanf(divStr, "%d", &denominator); int denominator; float x; sscanf(divStr, "%d", &denominator); if (denominator == 0){ x = argLen / denominator; fprintf(stderr, "Bug #3: division by zero triggered\n"); foo = (void *)0xbffbffff; foo(argLen); /* trigger illegal instruction fault */ }else return 0;

Fuzzer Outline Generate inputs (random or follow rules) firstInt = rand()%50000; secondInt = rand() % 2; arraySize = rand() % 20; charArray = (char *) malloc(arraySize); for (j=0; j< arraySize; j++) charArray[j] = 'A'; charArray[arraySize-1] = NULL; Generate execution command line sprintf(buffer, "./fuzzTest-target %d \"%s\" %d\n", firstInt, charArray, secondInt); free(charArray); /* must free memory for repeat testing! */

Fuzzer Outline Execute target code ret = system(buffer); Obtain target execution exit code wait(&status); retCode = WEXITSTATUS(ret); Check abnormal exit code and record inputs that cause the abnormal if ( retCode == 128+11 || retCode ==128+4) /* segmentation fault (11) or illegal (4) */ { printf("retCode=%d ## Input: firstInt = %d, arraySize = %d, secondInt = %d\n", retCode, firstInt, arraySize, secondInt); fflush(stdout); /*make sure output is print out immediately ! */ } Repeat from start in generating inputs

List of Unix Signal Number You can find it at: http://man7.org/linux/man-pages/man7/signal.7.html The WEXITSTATUS() returns a value that is the signal number that caused the termination plus 128

How to Record Fuzzing Result? When abnormal happens, record down inputs that cause the abnormal Record the corresponding abnormal message printout by target code Unix OS I/O definition: stdin (0), stdout (1), stderr (2) I/O redirection: $ Command < data.txt: let stdin get from file (instead of keyboard) $ Command > output.txt: let stdout redirect to file $ Command 2> error.txt: let stderr redirect to file $ Command &> output.txt: let stdout and stderr redirect to file For our example: $./fuzzTest100 &> output.txt

Program Project 2 Introduction

Manual Read Sample.jpg File To understand the jpeg file format and the project’s ‘sample.format’ description, you need a HEX Editor: In Unix: use “$hexdump sample.jpg > hex.txt” Each two-byte value is shown as ‘daff’ where the first byte is ‘ff’ and second byte is ‘da’ ! A bit confusing on the byte order HexEdit for Win: http://www.physics.ohio-state.edu/~prewett/hexedit/ This program shows each byte value, so no confusion on big-endian or little-endian stuff. You can use windows accessories “calculator” to translate between decimal and hexadecimal values Use ‘programmer’ option in ‘view’ menu

One-Round Fuzzing Outline In our fuzzer, we need to first read sample.jpg into a char buffer array Then, modify the buffer (randomly or follow some format rules) Then, write the content of the buffer to test.jpg file. Then, invoke jpegconv on test.jpg to do fuzz test

Read sample.jpg into Buffer char imageBuf[10000]; /*enough to hold sample.jpg */ int fSize; FILE *fin, *fout; fin = fopen(“./sample.jpg”, "rb"); fout = fopen(“./test.jpg”, “wb"); fseek(fin, 0, SEEK_END); /* set file pointer to the file end */ fSize=ftell(fin); /*get input file size */ fseek(fin, 0, SEEK_SET); /* rewind the pointer to the start of file fin */ fread(imageBuf, 1, fSize, fin); /* read byte stream of the file */ fclose(fin); /* then, modify imageBuf randomly, */ /* or follow jpeg format on the header section*/

Jpeg Header Format Now the ‘sample.jpg’ is in the char array imageBuf[] Check the ‘sample.format’ for the Jpeg format For example: imageBuf[0] = 0xff; imageBuf[1] = 0xd8;  SOI header imageBuf[158]=0xff; imageBuf[159]=0xc0;  SOF header imageBuf[609]=0xff; imageBuf[610]=0xda;  SOS header Simple fuzzing: Mutation-based fuzzing Only work on Jpeg Header section since all bugs are in here You may only be able to find a few bugs in this way Of course, trying millions of inputs may find all bugs if you are lucky Advanced fuzzing: Protocol-aware fuzzing Follow the guide in project description, modify format sections step-by-step Modify different section could trigger different bugs

Write fuzzed image to test.jpg fwrite (imageBuf , 1, fSize, fout ); /* if you modified the image size, then use the new fSize */ fclose (fout); /* then, invoke jpegconv on test.jpg for testing */ Note that the ‘test.jpg’ will only save the newest fuzzed file if you repeatedly do the above fuzzing! On the other hand, we cannot save all tested jpg files since most of them do not trigger bugs

Save Fuzzed Input That Causes Bug int status, ret, retCode; int crashNum = 0; char fileName[20]; /* saved fuzzed image file name */ char comBuf[200]; /* save the command line string */ sprintf(comBuf, “./jpegconv -ppm -outfile foo.ppm test.jpg"); ret = system(buffer); wait(&status); retCode = WEXITSTATUS(ret); if (retCode == 139){ /* Segmentation fault for a bug */ crashNum ++; sprintf(fileName, “./crashed-%d.jpg”, crashNum); fout = fopen(fileName, “wb"); fwrite (imageBuf, 1, fSize, fout ); fclose (fout); }

Notes Remember, do not save every fuzzed input into image files! There is no enough disk space for that on Eustis! You will still have multiple fuzzed images saved for the same bug. You can find smart way to only save one copy for each bug. When one or two bugs are repeatedly triggered Try to modify image on other format sections Mutate image file in different ways Change to different values random, negative, zero, upper-bound… Change different number of bytes Consecutive n bytes, randomly picked n bytes, change value of n, ….

Unsolved Task How to Match crashed-x.jpg to its bug ID? Hint: Jpegconv uses stderr to print out “BUG X TRIGGERED” I will leave this task to you

Notes Do not directly copy code in this slide to your code! The quotation mark has been changed by Word! How many runs should I do? No. of fuzzed input files No. of saved fuzzed image files In order to not blow your disk space quota in Eustis No. of different bugs found Need your code to process stderr message Your code needs to check if fopen() succeeds or not!

Working Environment You can do this project on Eustis, or any Linux machine you set up Make sure ‘jpegconv’ works on your computer (see project description) You can use any programming langrage in Linux for the project But your code must be able to run under Eustis for project submission in order for me to verify your code! Eustis support: Perl, Java, C, Python, Sbcl Your report must explain how I can run your code under Eustis!

Last Words After this detailed explanation and coding, the project should be not too hard My own mutation-based fuzzer only contains less than 60 lines in C Find two bugs in 1300 inputs Protocol-aware fuzzer will be longer