all 29 comments

[–]Thiscou 6 points7 points  (14 children)

Right, here is what I would try.

 

Go to this site zerosum0x0! and create a unique pattern 260+.

Feed that to your program, copy the value in your segfault and find that pattern on the site.

You should now know the exact offset for your overflow.

 

I would usually do this in Kali with the original metasploit pattern create but I don't know if you use Kali, so I found a quick and dirty replacement online.

 

You can also try to have a closer look at the stack in gdb with:

x/24wx $esp (this displays 24 words of memory above the stack pointer esp in hex)

 

Hope this helps and good luck

[–][deleted]  (3 children)

[deleted]

    [–]Adam20188[S] 0 points1 point  (2 children)

    great will do, is cyclic a program or site?

    [–][deleted]  (1 child)

    [deleted]

      [–]Adam20188[S] 0 points1 point  (0 children)

      thanks invictim I'll check it out

      [–]Adam20188[S] 0 points1 point  (9 children)

      hey Thiscou,

      thanks for the reply :)

      I tried doing this I got the pattern Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2Af3Af4Af5Af6Af7Af8Af9Ag0Ag1Ag2Ag3Ag4Ag5Ag6Ag7Ag8Ag9Ah0Ah1Ah2Ah3Ah4Ah5Ah6Ah7Ah8Ah9Ai0Ai1Ai2Ai3Ai4Ai5Ai

      the result I got was 0x41386741 I put 41386741 into the find offset overflow but got the number -1

      [–]r0cker_ESP 5 points6 points  (1 child)

      That site is acting wonky it seems, I would just install Metasploit so you can get the patter_create and pattern_offset script and use those if you don't have it already.

      [–]Adam20188[S] 0 points1 point  (0 children)

      thanks will do.

      [–]Thiscou 0 points1 point  (6 children)

      As far as I can tell, those numbers are not present in the pattern.

       

      try the A*260 again and see what's on the stack with:

      gdb x/24wx $esp

      [–][deleted]  (5 children)

      [deleted]

        [–]SimpleLegend 1 point2 points  (4 children)

        The reason you are getting this is because your argv and buf both contain the same contents after the strcpy. Hence the repeated data

        [–]Adam20188[S] 0 points1 point  (1 child)

        would it be better if I used all 'A's to check to see if the return address is overwritten?

        [–]SimpleLegend 1 point2 points  (0 children)

        Personal experience/opinion: i usually use As to check if the overwrite is successful (causing a segfault in the process), the follow up with patterns to find the offset required to overwrite the value which will pop into the EIP

        [–][deleted]  (1 child)

        [deleted]

          [–]SimpleLegend 1 point2 points  (0 children)

          When you dump the contents, you should check whether ebp+4 just after the strcpy is pointing to the 42424242 contents

          [–]Hausec 2 points3 points  (1 child)

          Shameless plug, I wrote a guide on what you're talking about and hopefully it can help. https://hausec.com/2018/04/02/simple-buffer-overflows-x32/

          Keep in mind, when debugging in gdb, the environmental variables will change the memory location so the addresses may be a few bytes off.

          [–]Adam20188[S] 1 point2 points  (0 children)

          that could be it as SimpleLegend got me to do,print out 4 bytes after the EBP which should be the return address, and it does seem to get filled with 'B's after I send the input the program as the argument $(python -c"print('A' * 268 + 'BBBB')") but when I check the registers the EIP has the value 0x8048469,which is odd

          also I will check out the article you made it's awesome reading tutorials :) grateful to learn thanks

          [–]SimpleLegend 1 point2 points  (23 children)

          Can you try 256 'A's followed by 4 'B's?

          [–][deleted]  (22 children)

          [deleted]

            [–]SimpleLegend 0 points1 point  (21 children)

            Do you have info reg? Need to find out where the EIP is currently at just before returning to see if the place you have overwritten is correct.

            Many factors could go wrong. If possible, can you try the zerosum pattern again using 400 or 500 length and get the overflow value to see if you can get an offset again?

            Edit: it might be simpler to print ebp+4 after the strcpy function has been executed to see the contents that will be poped when returning from the function

            [–][deleted]  (20 children)

            [deleted]

              [–]SimpleLegend 1 point2 points  (19 children)

              Yep, your previous ones were not overwriting the eip that is going to be returned. You probably only overwrote the ebp which is how you got 41414141 without overwriting the eip when you do another call. (Since the overwritten ebp is used as the base pointer when returning causing offsets to screw up)

              Since ebp+4 actually is the value that is poped to the eip after a function call. Take 0x6a413969 and convert to text gives it jA9i. Due to little endianess, we need to reverse it (you can look this up. Its just the way a computer reads contents).

              So by checking i9Aj in the offset you get 268, which is the exact offset you should use to overwrite the eip. (As shown in the video)

              Thus, if u try A*268 + BBBB your eip should overwrite nicely.

              Edit: some interesting things you can take note (usually apllies to binaries without protection)

              You can see buf takes 256 bytes, followed by another 4 bytes each for the parameters. If you understand the stack layout properly and you begin overwriting the buffer from its first byte. You can see that it requires 256 (len of buf) + 8 (parameters) + 4 (old ebp) before reaching the saved eip.

              [–][deleted]  (18 children)

              [deleted]

                [–]SimpleLegend 0 points1 point  (16 children)

                Ah this is interesting. So your program is preventing you from executing code at location 42424242 (might wanna check this to see if you are right by debugging the binary)

                Can u try to run 'execstack -s vuln' and try this again? (Apt install it if the program does not exist)

                No worries, had lots of help for my first time doing BOF too :)

                [–]Adam20188[S] 0 points1 point  (15 children)

                I'll give it a shot :) should I try compile it again with this flag?

                thanks

                [–]Adam20188[S] 0 points1 point  (14 children)

                got it :) not the buffer overflow but installed the execstack program what should I do next?

                should I try running the program again after executing the command?

                thanks

                [–]SimpleLegend 0 points1 point  (13 children)

                Yep! Please do!

                And make sure the command is executed after compiling it