Understand what the assemble code for <getbuf> does

时间:2019-04-17 02:51:59

标签: assembly buffer overflow

Questions 1: I am reading about buffer overflow attacks and saw some examples, however I am having issues grasping what's going on the assemble code level. Can someone please help me understand this assemble code?

Questions 2: In this attack we did a 24 padding bits then the destination address, which address here gets overwritten by the new destination address.

Questions 3: I know the first column is for address, but I am confused what's the second column for example in the 1st line we have 1 48 38 ec

Dummy code:

<getbuf>
4019a1   48 38 ec     sub $0x18, rsp    // 24 bit for buffer? but what's rsp?
4019a8   48 38 ec 18  mov %rsp, %rdi    // rdi == arg, but what's rsp?
4019ac   48 89 e7     call <gets>       // not sure what does <gets> do
4019af   e8 8c 02 00  add  $0x18, %rsp  // not sure what's going on.
4019b4   48 83 c4 18  retq              // return?
4019be   c3           xchg %ax %ax      // I thought we returned already

this is not the actual code, but just a close representation.

1 个答案:

答案 0 :(得分:0)

  

有人可以帮助我理解此汇编代码吗?

     

24位用于缓冲区?但是什么是rsp?

rsp是堆栈指针。堆栈是存储器中用于按功能临时存储数据的某些区域。在许多CPU(包括x86)上,call指令还将在retretq指令之后执行的指令的地址存储到堆栈中。

在x86 CPU上,rsp(32位:esp)寄存器包含一个地址。该地址之前的内存是可用的,该地址之后和该地址之后的内存都已使用。

如果需要100个字节的临时内存,请从rsp寄存器中减去100;这样做,表明您使用了这100个字节的内存(由您的代码)。一旦您不再需要该内存,就可以恢复旧的rsp值。

由于retq假设rsp指向call存储地址的内存,因此您必须在执行{{1}之前恢复rsp的旧值。 }。

  

不确定retq的作用

gets是用于C编程的函数。它读一行文本(例如从键盘)。在您的情况下,该行将写入堆栈。与gets不同,此功能将检查内存中是否有足够的空间!

如果行读取的长度超过24个字节,则fgets函数将覆盖gets指令之前“已使用”的内存中的数据。

正如我已经写过的那样,由sub $0x18, %rsp写入并由call读取的地址存储在那里!

换句话说:如果retq读入的行太长,则gets写入的地址将被覆盖,call指令将不会返回到调用函数,但是将跳到内存中的某个错误地址。

(我希望这能回答问题2。)

  

我以为我们已经回来了

retq并不是真正的指令,而只是一些伪数据。它是由某些编译器插入的,因为这些编译器始终会生成(例如)两个字节长的倍数的函数。

  

但是我很困惑第二列是什么,例如第一行中有1 48 38 ec

这是RAM中的字节(以十六进制表示),代表第三列中的汇编程序指令。

示例:RAM中的十六进制字节xchg %ax %ax将被CPU解释为48 38 ec

(请注意tum_的注释:示例中显示的数据显然是错误的:sub $0x18, %rsp显然不是表示指令48 38 ec的字节。)

相关问题