pwnable.kr collision

Pwnable.kr的第二道题目collision的writeup

首先连接题目,可以看到当前目录下有三个文件:

1

查看col.c文件,可以看到col的程序逻辑:

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
#include <stdio.h>
#include <string.h>
unsigned long hashcode = 0x21DD09EC;
unsigned long check_password(const char* p){
int* ip = (int*)p;
int i;
int res=0;
for(i=0; i<5; i++){
res += ip[i];
}
return res;
}

int main(int argc, char* argv[]){
if(argc<2){
printf("usage : %s [passcode]\n", argv[0]);
return 0;
}
if(strlen(argv[1]) != 20){
printf("passcode length should be 20 bytes\n");
return 0;
}
if(hashcode == check_password( argv[1] )){
system("/bin/cat flag");
return 0;
}
else
printf("wrong passcode.\n");
return 0;
}

可以看到,程序将第一个参数经过变换与hashcode作比较,若相同则获得flag,并且参数必须为20个字节。在check_password()中,函数将输入由字符串转换为整型,因为char是1个字节,int为4个字节,因此原本20个字节的字符串就转换成了地址连续的5个整型变量,经过for循环将这5个变量进行累加,要求最终得到的值与0x21DD09EC相同。

经过尝试,发现当前目录下无法执行脚本程序,但发现服务器安装了python,因此可以直接在命令行里写攻击脚本,只需构造5个和为0x21DD09EC的整型数即可,最终得到flag:

2

这里使用了python的subprocess模块中的call方法来运行一个外部程序(./col)。