col_WriteUp(panable.kr_col)
题目传送门:http://pwnable.kr/play.php
ssh连接目标服务器得到三个文件,查看col.c内容如下:
#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;
}
这道题的考点是hash碰撞,但是这个hash并非真的hash函数加密的hash,而是出题人指定的hash值: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;
}
此函数将用户输入的20个字节的数据作为5个字符串的值相加,最后在if(hashcode == check_password( argv[1] ))
中与hash值对比,如果值相等则执行system(“/bin/cat flag”)输出flag,因此我们需要将输入的5个数相加的值为0x21DD09EC,因此可以将输入的20个字节的前16个字节输入为“\x00”,这里有必要提一下
col@ubuntu:~$ file col
col: setuid ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.24, BuildID[sha1]=05a10e253161f02d8e6553d95018bc82c7b531fe, not stripped
本程序是x86程序,在内存中采用小端序,输入的顺序和在内存中存储的顺序是相反的,例如我们想存储\x00\x01则我们要输入\x01\x00,而没四个字节为一组,因此我们要想这5个数相加的值为0x21DD09EC我们可以输入4个”\x00\x00\x00\x00”+”\xEC\x09\xDD\21”,但是由于“0x09”有特殊含义,是一个坏字符(badchar),因此需要避开这个数字,我们可以构造4个”\x01\x01\x01\x01”和一个”\xE8\x05\xD9\x1D”
0x1DD905E8=0x21DD09EC-0x01010101-0x01010110-0x01010101-0x01010101
运行程序输入./col `python -c “print ‘\x01’ * 16 + ‘\xE8\x05\xD9\x1D’”`即可获取flag:
col@ubuntu:~$ ./col `python -c "print '\x01' * 16 + '\xE8\x05\xD9\x1D'"`
daddy! I just managed to create a hash collision :)