在缓冲区注入恶意代码后,如何维护运行时堆栈?

时间:2012-08-07 16:14:58

标签: callstack buffer-overflow

我正在阅读以下有关缓冲区溢出的文章:http://www1.telhai.ac.il/sources/private/academic/cs/557/2659/Materials/Smashing.pdf

根据本文中的示例,攻击者将代码注入溢出的缓冲区中,并修改返回地址以指向缓冲区的地址。因为注入的代码和局部变量都位于堆栈中,攻击者是否应该确保在使用代码溢出缓冲区之前,她/他是否为局部变量留下了足够的空间?从广义上讲,我很困惑当代码和局部变量都存在于堆栈中时如何维护堆栈....是否有可能局部变量覆盖注入的代码???

1 个答案:

答案 0 :(得分:0)

一种解决方案是不使用任何局部变量。这不一定是对代码的一个很大的限制,它会变得很小并且通过调用其他东西来完成它的工作。 (非常相对说它不是一个很大的约束,它超出了我的范围,但由于这不是最容易编写的代码,因此在这种情况下它不是一个很大的约束。)

但这甚至不是必要的。堆栈看起来像:

Function A Arg 2
Function A Arg 1
Function A Arg 0
Return Address into caller
Function A Local 1
Function A Local 0
Function B Arg 1
Function B Arg 0
Return Address into place in Function A's executable code.
Function B Local 2
Function B Local 1
Function B Local 0
Function C Arg 0
Return Address into place in Function B's executable code.
Function C Local 3
Function C Local 2
Function C Local 1
Function C Local 0
...

缓冲区溢出欺骗它看起来像:

Function A Arg 2
Function A Arg 1
Function A Arg 0
Return Address into caller
Function A Local 1
NastyCode bytes 0-3
NastyCode bytes 4-7
NastyCode bytes 8-B
NastyCode bytes C-E
NastyCode bytes F-10
NastyCode bytes 10-13
NastyCode bytes 14-17
NastyCode bytes 18-1B
NastyCode bytes 1F-20
...
NastyCode arg 0 (etc.)
Return Address (of NastyCode [may not even be valid, may return to original caller, or may jump into something it'll set up]).
NastyCode Local 2
NastyCode Local 1
NastyCode Local 0
Return Address (should be of Function F, into a point in Function E but it's changed to point to byte 0 of NastyCode)
Function F Local 2
Function F Local 1
Function F Local 0

因此,写入堆栈的可执行代码可能远离该代码的本地变量。