This level requires you to read the token file, but the code restricts the files that can be read. Find a way to bypass it :)
To do this level, log in as the level04 account with the password level04. Files for this level can be found in /home/flag04.
看起來是要讀取一個讀取不到的檔案呢…
登入進 /home/flag04
之後,裡面有 2 個檔案,分別是「有 setUID 的 flag04
」跟「只有 owner 有讀寫權限的 token
」
其中 flag04
的原始碼題目就有給了:
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>
int main(int argc, char **argv, char **envp)
{
char buf[1024];
int fd, rc;
// 如果指令只有一個參數,印出錯誤訊息並結束
if(argc == 1) {
printf("%s [file to read]\n", argv[0]);
exit(EXIT_FAILURE);
}
// 如果第一個參數包含 "token" 的話,印出錯誤訊息並結束
if(strstr(argv[1], "token") != NULL) {
printf("You may not access '%s'\n", argv[1]);
exit(EXIT_FAILURE);
}
// 以 read only 開啟以參數為名的檔案
// 丟給 file descriptor "fd"
fd = open(argv[1], O_RDONLY);
// 如果 fd 不存在(沒有成功讀取),印出錯誤訊息並結束
if(fd == -1) {
err(EXIT_FAILURE, "Unable to open %s", argv[1]);
}
// 讀取 fd 所指向的 file,並存入 buf 中
// 回傳讀取的位元數給"rc"
//(錯誤時回傳 -1)
rc = read(fd, buf, sizeof(buf));
// 如果 read 出錯則印出錯誤訊息並結束
if(rc == -1) {
err(EXIT_FAILURE, "Unable to read fd %d", fd);
}
// 將 buf 中的 rc 個位元寫入標準輸出 (fd = 1)
write(1, buf, rc);
}
關於 file descriptor 的介紹:這裡(中文的不要怕)
看起來只要參數有包含 token
的話就會被擋掉,方向應該是「試著讀取不以 token 為名的檔案來存取 token 的資料」…
這聽起來很像 Symbolic link ?
Symbolic Link 類似 Windows 的捷徑,但是一般的文件操作都可以透過 symbolic link 去影響到連結的檔案 (當然你要有連結的檔案的權限)
這裡因為 flag04
已經有 setUID ,意味著我們什麼都能拿
在 /tmp
建立一個 link
ln -s /home/flag04/token /tmp/TOKEN
然後用 flag04
跑做出來的 link
得到了一串看起來像是16進位的不知名東東 … (゚∀゚)?
因為 grep
跟 find
都沒有找出來只是什麼東東、轉成 ascii 也都是亂碼,所以就直接丟去 google 搜
結果是 flag04
的使用者密碼阿 ((゚д゚))
以後要學起來(?
完成啦 d(`・∀・)b