这段代码会发生什么?(执行char缓冲区)

asq*_*red 17 c c++ function-pointers

有人能给我一个关于第二行代码中发生的事情的完整解释吗?

我知道包含shellcode的缓冲区的地址被转换为一个执行的函数指针.但我对所涉及的所有括号和步骤感到困惑,所以我需要更详细的解释.

unsigned char buf[] = "\x90\x90\x90\x90\x90\x90\x90\x90";

((void(*)())buf)();
Run Code Online (Sandbox Code Playgroud)

我试着用这种方式解释一下:

buf                     //address of the buffer containing code
void(*)()               //"type" function pointer returning void, no parameters
(void(*)()) buf         //cast buf to said type
( (void(*)()) buf )()   //take the outcome of the cast and execute it by appending ()
Run Code Online (Sandbox Code Playgroud)

它是否正确?

编辑: 我知道DEP会阻止执行,即使它会执行,程序也会崩溃,因为它会在NOP之后执行"随机垃圾".我的问题只是函数调用的语法.

Mar*_* A. 6

  1. buf(数组名称转换为指针)转换为void(*)()函数指针

    (void(*)())buf
    
    Run Code Online (Sandbox Code Playgroud)
  2. 通过指针调用该函数

    (function_pointer)();
    
    Run Code Online (Sandbox Code Playgroud)

请注意,由于运算符优先级规则,这是错误的

(void(*)()) buf() // Function call has a higher precedence over type cast
Run Code Online (Sandbox Code Playgroud)

所以另一对括号是必要的.

最终执行它(如果DEP允许它,这是系统相关的)和(如果x86)Nop-Nop-Nop等.

你是对的.

作为旁注:NOP代码也会使您的应用程序崩溃:没有返回语句,当有效负载完成时,IP将无法恢复.