2014-10-31 121 views
1

在深入研究之前,我正在对shellcode进行试验,所以我遇到了shellcoders手册中的一个例子。示例如下:运行shellcode时出现分段错误

char shellcode[] = "\xeb\x1a\x5e\x31\xc0\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x4 
\x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1\xff\xff\xff\x2f\x62\x69 
\x6e\x2f\x73\x68"; 

int main() { 

int *ret; 
ret = (int *)&ret + 2; 
(*ret) = (int)shellcode; 
} 

shellcode应该产生一个shell。但是,我收到了分段错误错误。 我编译程序使用gcc编译器-fno-stack-protector-z execstack选项。我参加了一个快速浏览一下readelf命令,很明显的是,栈是可执行

GNU_STACK  0x000000 0x00000000 0x00000000 0x00000 0x00000 RWE 0x4 
+0

反引号用于引用代码,而不是增加重点。 – 2014-10-31 09:32:41

回答

2

ret是一个指针,当你把它声明它没有指向任何内存位置。 后来你正试图通过将2与指针所指向的位置分配一定的参考价值的。(这是互相矛盾的陈述)

ret = (int *)&ret + 2;/* Which is wrong */ 
+0

是的。现在清楚了。这就是为什么你总是需要别人来阅读你写的东西,因为他只会看到你每次重新阅读代码时都会忽视的错误。 – 2014-10-31 09:31:03

+0

我低估了这个答案。问题的代码是用'shellcode'的地址覆盖返回地址;这不是偶然的未定义行为。 (显然它不应该是便携式或符合标准的C) – immibis 2014-10-31 09:45:17

+0

@immibis你想回答这个问题吗? – 2014-10-31 09:57:04

0

我编译使用“GCC filename.cpp”命令下面的代码。钛的编译没有错误,我希望这会帮助你解决你的疑问。

#include<stdio.h> 

char shellcode[] = "\xeb\x1a\x5e\x31\xc0\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x4\x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68"; 

int main() { 

int *ret; 
ret = (int *) ret + 2; //I don't know why you had written this 
ret = (int *)shellcode; 

}