Buffer Overflow Examples, Overwriting a function pointer
文章推薦指數: 80 %
Another post where I overwrite a variable value which is used as a function pointer. (x32)
Skiptoprimarynavigation
Skiptocontent
Skiptofooter
0xRick'sBlog
About
Categories
Tags
Togglemenu
0xRick
Follow
Somewherebetween1'sand0's
HomePage
Twitter
Github
BufferOverflowExamples,Overwritingafunctionpointer-protostarstack3
Introduction
./Stack3
Findingthesizeofthebuffer
Findingthememoryaddressofthefunction
Applyingtheexploit
BufferOverflowExamples,Overwritingafunctionpointer-protostarstack3
Introduction
HeyI’mbackagainwithanotherarticle,todayI’mgoingtosolveprotostarstack3butthistimeit’sgoingtobeabitdifferent,InthelasttwoarticlesIsolvedstack0,stack1andstack2andIusedthesourcecodeofthebinariestoidentifywherethebufferoverflowhappensandwhatexploittouse.Nowwithstack3wearegiventhesourcecodebutWearenotgoingtouseit,Insteadofthatwewillusesomepracticaltechniquestosolvethischallenge.Becauseinarealsituationwewon’thavethesourceoftheprogramright?Let’ssee
Ifyouhaven’treadmypreviousarticlesaboutbufferoverflowIrecommendreadingthemfirst
./Stack3
Let’sfirstlookattheprogramandseewhatdoesitdo.
Wedon’tseeanyoutputsoweshouldgiveitanargument.Weneedtoconfirmthattheprogramisvulnerabletoabufferoverflowsowecanpassanargumentof100charsandseewhathappens
python-c"print'A'*100"|./stack3
Andweseeasegfaultwhichconfirmsthatabufferoverflowhappened,wealsoseethisline:“callingfunctionpointer,jumpingto0x41414141”
Sonowwehaveanideaaboutwhat’shappeninghere,There’safunctionpointerthatexecutesafunctionbasedonthegivenmemoryaddressofthatfunction.Thatmemoryaddressisstoredinavariableandwecanoverwritethatvariablewhenweexceedthebuffer.Weseethatthefunctionpointerwascallingtheaddress0x41414141and0x41isthehexof“A”.Nowwehavetodotwothings.Thefirstthingistoknowwherethebufferoverflowhappens,Becauseherewehavegiventheprogramanargumentof100charsbutwedon’tknowexactlythesizeofthebuffer.Thesecondthingistofindthememoryaddressofthefunctionthatweneedtoexecute.Let’sseehowtodothat.
Findingthesizeofthebuffer
TomakethingseasierIcompiledtheprogramonmykaliboxtostarttestingthere.
Metasploithastwoscriptscalledpattern_createandpattern_offset,youcanfindthemonkaliin/usr/share/metasploit-framework/tools/exploit
pattern_createcreatsauniquestringofadefinedlengthsowewillcreateapatternof100chars.
./pattern_create.rb-l100
Nowlet’sruntheprogramingdb,I’musinggdb-peda
Firstwesetabreakpointinmain.
break*main
Thismakestheprogrambreakafterthefirstinstructionofthefunctionmain()
Thenweruntheprogram.
Itstopsatthebreakpoint.Wedoctomakeitcontinuethenpassourargument
Thesegfaulthappensandweseewhereithappened:0x63413163
Nowwewillusepattern_offsettoknowwhatisthelocationof0x63413163
./pattern_offset-l100-q63413163
Andwegetexactmatchatoffset64,Thismeansthatthebuffersizeis64charsandafterthattheoverflowhappens.
Findingthememoryaddressofthefunction
Ifwedoinfofunctionsfromgdbitwilllistallthefunctionsandtheirmemoryaddresses,wecanalsodothatwithobjdump.Butwhatisthefunctionwe’relookingfor?
infofunctions
Weseealotoffunctionsbutthemostinterestingoneiscalled“win”,buttheaddressonmykaliboxwillbedifferentfromtheaddressontheprotostarmachine.Wewillreturntoprotostaranduseobjdumptofindit.
objdump-dstack3
Andwegottheaddress0x08048424
Applyingtheexploit
Nowwecaneasilybuildourexploit,weknowthatthebufferis64charsafterthatwecanpasstheaddressofthefunctionandthefunctionpointerwillexecuteit.
python-c"print'A'*64+'\x24\x84\x04\x08'"|./stack3
Andwegettheoutput“codeflowchangedsuccessfully”
Wesolveditwithoutthesource,nowlet’slookatthesource
#include
延伸文章資訊
- 1Program hijacking - Rutgers CS
The best-known set of attacks are based on buffer overflow. ... The function then adjusts the sta...
- 2Am not able to call C++ function pointers from inline assembly
Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. Create a fre...
- 3Buffer Overflow Examples, Overwriting a function pointer
Another post where I overwrite a variable value which is used as a function pointer. (x32)
- 4Jump Tables via Function Pointer Arrays in C/C++
Here's a look at the use of arrays of function pointers in C/C++ as jump ... the function that us...
- 5c++ - Explanation of function pointers - Stack Overflow
I have a problem with understanding some C++ syntax combined with function pointers and function ...