一些黑客教程中常见的在main中执行shellcode的代码:
char shellcode[] =" your shellcode"; //replace your shellcode here
int main() {
int *ret; //define a variable on the stack
ret = (int *)&ret+2; //let ret point to place that the place of ret(&ret)add offset(2,return address on there)
(*ret) = (int)shellcode; //change the saved return value to the address of the shellcode, so it executes.
}
注释简略介绍了代码的含义。 代码表面没有做什么工作,但是通过其对栈的影响,尤其是覆盖main函数返回地址,影响了正常的返回操作,
使运行时库无法接管控制权,而把控制权转到我们改写的shellcode上去。
通过下图能更好的理解代码的运行