PicoCTF - tic-tac
Background
後端工程師面試考什麼 - Race Condition 篇 [Day24]攻擊篇
TOCTTOU
Time of check to time of use 在檢查和使用之間影響資源狀態的攻擊
這種攻擊可能發生在共享資源中。 可能導致程式在資源處於意外狀態時執行無效操作。
Source code
:::spoiler Source code
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sys/stat.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <filename>" << std::endl;
return 1;
}
std::string filename = argv[1];
std::ifstream file(filename);
struct stat statbuf;
// Check the file's status information.
if (stat(filename.c_str(), &statbuf) == -1) {
std::cerr << "Error: Could not retrieve file information" << std::endl;
return 1;
}
// Check the file's owner.
if (statbuf.st_uid != getuid()) {
std::cerr << "Error: you don't own this file" << std::endl;
return 1;
}
// Read the contents of the file.
if (file.is_open()) {
std::string line;
while (getline(file, line)) {
std::cout << line << std::endl;
}
} else {
std::cerr << "Error: Could not open file" << std::endl;
return 1;
}
return 0;
}
:::
Recon
第一次寫這一種題目,具@ccccctw所說算是考古題了,看了123還是不知道怎麼做出來的,所以問了@ccccctw
- 可以看到source code是檢查root權限才可以讀到flag
- 用兩次soft link讓這支程式呈現race condiction的狀態(不見得每次都會所以要靠賽)
- 首先要先用一個infinity while loop創兩個soft link,然後在背景執行,第一次的soft link(
$ ln -sf test1 test
)是為了要過掉root權限的檢查,而第二次的soft link(ln -sf flag.txt test
)是用來讀flag的 具體來說是這樣: test的link會在flag.txt和test1之間切換,若我們用txtreader讀取test時,會有權限檢查,如果此時的link是test1,權限檢查就會通過,此時如果剛好test的link指向flag.txt,那我們就可以無縫的讀取到flag.txt的內容 - 接著就可以用他的txtreader讀取test,如果幸運的話就可以讀到需要root權限的flag
Exploit
1 |
|
Flag: picoCTF{ToctoU_!s_3a5y_007659c9}