在学习编写shellcode的过程中书本上的一个例子,代码如下
char shellcode[] = "\xbb\x00\x00\x00\x00"\
"\xb8\xfc\x00\x00\x00"\
"\xcd\x80";
int main()
{
int *ret;
ret = (int* )&ret + (int)2;
(*ret) = (int)shellcode;
}
但是编译执行之后,出现了段错误
在网上百度了一下找到问题的原因http://bbs.chinaunix.net/thread-3712426-1-1.html,因为shellcode保存在内存的.data数据段是不能被执行的(可能比较的低版本的linux能够执行,我也不清楚,如果知道的朋友可以告诉我一下,评论即可),将shellcode定义为const或者static const,将shellcode放在.rodata数据段,编译之后就可以执行了。代码如下
const char shellcode[] = "\xbb\x00\x00\x00\x00"\
"\xb8\xfc\x00\x00\x00"\
"\xcd\x80";
int main()
{
int *ret;
ret = (int* )&ret + (int)2;
(*ret) = (int)shellcode;
}
附存储器内存分布图: