collision
- 看起來要20bytes
col@ubuntu:~$ ./col 1234 passcode length should be 20 bytes
- 20bytes了,但密碼錯誤
col@ubuntu:~$ ./col AAAABBBBCCCCDDDDEEEE wrong passcode.
Source Code
#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; }
分析原始碼
- 取得flag成立條件:要如何輸入20 bytes,又可以等於
0x21DD09EC
if(hashcode == check_password( argv[1] )){ system("/bin/cat flag"); return 0; }
- 可以利用此種方式輸入20 bytes:
- ./col `python -c "print '\x01' * 20"`
- 檢查password:將使用者輸入參數拆成5組整數(4 bytes)做相加
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; }
- 自訂整數相加等於
0x21DD09EC
即可- 前16 bytes四組參數分別都設為
\x01\x01\x01\x01
- 後4 bytes計算方式為:
0x21DD09EC - 0x01010101 × 4
=0x1DD905E8
- 前16 bytes四組參數分別都設為
- 最後輸入參數為:
- ./col `python -c "print '\x01' * 16 + '\xE8\x05\xD9\x1D'"`