任务 1:调用 Shellcode
章节大纲
-
我们已从上述汇编代码中生成了二进制代码,并将其放在名为 call_shellcode.c 的 C 程序文件中的 shellcode 文件夹内。在本任务中,我们将测试 shellcode。
#include <stdlib.h> #include <stdio.h> #include <string.h> const char shellcode[] = #if __x86_64__ "\x48\x31\xd2\x52\x48\xb8\x2f\x62\x69\x6e" "\x2f\x2f\x73\x68\x50\x48\x89\xe7\x52\x57" "\x48\x89\xe6\x48\x31\xc0\xb0\x3b\x0f\x05" #else "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f" "\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31" "\xd2\x31\xc0\xb0\x0b\xcd\x80" #endif ; int main(int argc, char **argv) { char code[500]; strcpy(code, shellcode); // 将 shellcode 复制到栈上 int (*func)() = (int(*)())code; func(); // 从栈上调用 shellcode return 1; }
上述代码包括两个 shellcode 的副本,一个是 32 位的,另一个是 64 位的。当我们使用 -m32 标志编译程序时将使用 32 位版本;如果不使用此标志,则将使用 64 位版本。 使用提供的 Makefile 通过键入 make 编译代码。两个二进制文件将被创建,a32.out (32位) 和 a64.out (64位)。运行它们并描述你的观察结果。 请注意编译时使用了 execstack 选项,这允许代码从堆栈执行;如果没有此选项,则程序将失败。