collision

提示md5哈希碰撞(並無用)html

照舊ssh ls -alpython

和上一關差很少,一個可執行文件,一個源碼,一個沒法讀取的flag文件數組

查看源碼ssh

  
#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;
    }
    //參數長度必須爲20
    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;
}

 

 
 
 

 分析源碼:post

將長度爲20byte的char型數組轉換爲int型數組.獲得五個整型this

(char型爲1Byte,int型爲4Byte,並且數據是從右向左存儲,因此每四個char轉換成一個int)spa

最終要求五個整型數字之和等於0x21DD09ECcode

解決方法:orm

 理論上有無數種組合,方便起見這裏咱們設置四組都是相同的’0x02020202’,最後一組算出來是:htm

>>> hex(0x21dd09ec-0x02020202*4)
'0x19d501e4'

因此反序輸入應該爲0xe4 0x01 0xd5 0x19

因爲有非可見字符串,咱們沒法手動從終端輸入,利用python進行輸入:

python -c 'print 4*"\x02\x02\x02\x02"+"\xe4\x01\xd5\x19"' | xargs ./col  

操做:

 

flag: daddy! I just managed to create a hash collision

完畢

附:

 char型數組轉換爲int型數組:

例如咱們構造了char型數組abcdefgh,即傳入了四個char:0xab、0xcd、0xef、0xgh,轉換成int後對應的數值變成0xghefcdab,(小端存儲)

而後一共構造了5組這樣的0xghefcdab進行求和最後的結果爲0x21DD09EC

 

python -c (command):

Execute the Python code in command. command can be one or more statements separated by newlines, with significant leading whitespace as in normal module code.

If this option is given, the first element of sys.argv will be "-c" and the current directory will be added to the start of sys.path (allowing modules in that directory to be imported as top level modules).

xargs命令:

xargs是給命令傳遞參數的一個過濾器.一般狀況下,xargs從管道或者stdin中讀取數據.

xargs的默認命令是echo,這意味着數據將會包含換行和空白,不過經過xargs的處理,換行和空白將被空格取代。

相關文章
相關標籤/搜索