代码
#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;
}
分析
分析题目,即输入20个字符,然后4个字符作为一组,每组当成是一个整数,然后计算这5个整数的和。
如果直接输入字符的话,比较困难。这里可以在执行二进制代码的时候调用python,将python的输出作为输入。
想象成两个数,sum为a和b的和。
sum - 4a + 4a = sum;
(sum - 4a) + 4a = sum;
把sum -4a看成一个整数,4a看成4个整数。
因为采用小端方式,所以拼接字符串的时候要进行调整
>>> hex(0x21dd09ec - 4*0x05616161)
'0xc578468'
print '\x68\x84\x57\x0c' + 4*'\x61\x61\x61\x05'
调用
adog@E531:~$ ./c $(python -c "print '\x68\x84\x57\x0c' + 4*'\x61\x61\x61\x05'")
success
adog@E531:~$