这段代码中发生了什么?(执行一个字符缓冲区)
What happens in this code? (Executing a char buffer)
谁能给我一个完整的解释在这第二行代码中发生了什么?
我知道包含shellcode的缓冲区的地址被强制转换为执行的函数指针。但是我对所涉及的所有大括号和步骤有点困惑,所以我需要更详细的解释。
unsigned char buf[] = "x90x90x90x90x90x90x90x90";
((void(*)())buf)();
我试着这样向自己解释:
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 ()
正确吗?
编辑:我知道DEP会阻止执行,即使它执行,程序也会崩溃,因为它会在nop之后执行"随机垃圾"。我的问题是关于函数调用的语法
-
将
buf
(数组名转换为指针)转换为void(*)()
函数指针(void(*)())buf
-
通过指针
调用该函数(function_pointer)();
注意这是错误的,因为操作符优先级规则
(void(*)()) buf() // Function call has a higher precedence over type cast
所以需要另一对括号
最终执行它(如果DEP允许,这取决于系统)和(如果x86) Nop-Nop-Nop,等等。
你是对的。
作为旁注:NOP代码也会使你的应用程序崩溃:没有返回语句,IP不会在负载完成时恢复。
正确吗?我希望能得到更详细/正确的解释。
这里有一个更简洁的替代:
unsigned char buf[] = "x90x90x90x90x90x90x90x90";
// ((void(*)())buf)();
// equivalent code:
typedef void (*void_callback)(); // declare the function pointer as a named type
void_callback callback = reinterpret_cast<void_callback>(buf);
callback();
相关文章:
- 是否可以在每次使用std::cout时执行一个函数
- 如何执行一个函数 x% 的执行次数?
- 在另一个线程中执行一个方法
- CLION 调试器在执行一个步骤后停止工作
- 每次使用不同的fixture多次执行一个测试用例
- OpenMP 一次只执行一个线程
- 想要用cuda内核执行一个循环直到用户取消
- 编写并执行一个C或C++程序,该程序可创建无限数量的进程
- C++自执行一个标准:size_t
- 如何从另一个函数执行一个函数
- 执行一个新的完全独立的进程,该进程封装在一个可执行文件中,而不需要为独立进程将文件写入磁盘
- 希望定期执行一个函数,但继续程序循环的其余部分
- 从另一个进程内的内存执行一个进程
- Fencepost问题:我需要一个递归函数,当它第一次被调用时,它会跳过执行一个部分
- 我应该使用什么标志来强制执行一个好的C++11样式
- 如何从C++程序中执行一个简单的Applescript
- 如何从lldb执行一个函数(由指针标识)
- 执行一个假语句
- 从c++启动一个c#应用程序,并在该应用程序上执行一个任务
- 嵌套的if-else语句只执行一个条件(c++)