Download Fuzzing Test Example - Malware and Software Vulnerability Analysis - Lecture Slides and more Slides Software Engineering in PDF only on Docsity!
Fuzzing Test Example
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
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>
- 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 */ }
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
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+6) /* segmentation fault (11) or Abort (6) / { 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
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
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
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 image 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, randomly picked….
Notes
• Do not directly copy code in the slide!
- 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
- Eustis support: Perl, Java, C, Python, Sbcl
- Your report must explain how we can run your code in
Eustis!