pwnable.kr-otp,一次性密码验证(one time password)漏洞分析。

题目分析

同样来看看题目描述:

I made a skeleton interface for one time password authentication system. I guess there are no mistakes. could you take a look at it?

hint : not a race condition. do not bruteforce.

ssh otp@pwnable.kr -p2222 (pw:guest)

题目告诉我们了,这题目用不到爆破和条件竞争的漏洞,是一个一次性密码验证系统,登录系统,下载源码和二进制文件。

源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

int main(int argc, char* argv[]){
char fname[128];
unsigned long long otp[2];

if(argc!=2){
printf("usage : ./otp [passcode]\n");
return 0;
}

int fd = open("/dev/urandom", O_RDONLY);
if(fd==-1) exit(-1);

if(read(fd, otp, 16)!=16) exit(-1);
close(fd);

sprintf(fname, "/tmp/%llu", otp[0]);
FILE* fp = fopen(fname, "w");
if(fp==NULL){ exit(-1); }
fwrite(&otp[1], 8, 1, fp);
fclose(fp);

printf("OTP generated.\n");

unsigned long long passcode=0;
FILE* fp2 = fopen(fname, "r");
if(fp2==NULL){ exit(-1); }
fread(&passcode, 8, 1, fp2);
fclose(fp2);

if(strtoul(argv[1], 0, 16) == passcode){
printf("Congratz!\n");
system("/bin/cat flag");
}
else{
printf("OTP mismatch\n");
}

unlink(fname);
return 0;
}

自己尝试GDB去调试了一下,确实就是 urandom 里面读取 8 个字节然后和命令行传参进去比较,我们甚至没有权限等它生成完 password 文件然后去 /tmp 目录下面 cat 来更改输入。

后面也是参考了别人的 wp,发现 Linux 居然有一个命令可以限制 SHELL 创建的文件大小,使用 ulimit -f 0 来限制文件创建的大小只能小于等于 0,由于这个限制无法向文件写入内容,因此读出来的就是 0 了。

题解

先在 shell 当中执行命令 ulimit -f 0,然后直接运行 ./otp 0 即可,但是 SHELL 无法直接执行,只能用 python 导入 os 之后再运行 ./otp 0

flag:Darn... I always forget to check the return value of fclose() :(