fd
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char buf[32];
int main(int argc, char* argv[], char* envp[]){
if(argc<2){
printf("pass argv[1] a number\n");
return 0;
}
int fd = atoi( argv[1] ) - 0x1234;
int len = 0;
len = read(fd, buf, 32);
if(!strcmp("LETMEWIN\n", buf)){
printf("good job :)\n");
system("/bin/cat flag");
exit(0);
}
printf("learn about Linux file IO\n");
return 0;
}
- 若要取得flag,則
if(!strcmp("LETMEWIN\n", buf)){
必須成立,也就是LETMEWIN\n
==buf
- 如何讓
buf
等於LETMEWIN\n
?
- 利用
read
函數從fd
讀取32 bytes至buf
read()
定義為:read函數從指定打開的文件fd中讀取指定大小byte到從buf開始的緩衝
- 這個
fd
是file descriptor
- 0代表stdin
- 1代表stdout
- 2代表stderr
- 因此當
fd
為0時,read()
會從stdin,也就是輸入的參數讀取32 bytes至buf
- 所以輸入的參數就是
LETMEWIN\n
- 接著就讓
fd
為0
int fd = atoi( argv[1] ) - 0x1234;
- 輸入參數argv[1]轉為整數再減去
0x1234
,也就是十進制4660,因此要輸入4660讓fd
等於零
fd@ubuntu:~$ ./fd 4660
LETMEWIN
good job :)
mommy! I think I know what a file descriptor is!!