pwnable: Collision

SangharshaSangharsha
2 min read

Desc:

Daddy told me about cool MD5 hash collision today. I wanna do something like that too!

Source Code:

#include <stdio.h>
#include <string.h>
unsigned long hashcode = 0x21DD09EC;
unsigned long check_password(const char* p){
    int* ip = (int*)p;  # this here is imp
    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] )){
        setregid(getegid(), getegid());
        system("/bin/cat flag");
        return 0;
    }
    else
        printf("wrong passcode.\n");
    return 0;
}

What happen’s above is:

int* ip = (int*)p; 
# input (argv[1]) is treated as raw memory, split into 5 x 4-byte integers (total 20 bytes)

res = (int)bytes[0-3] + (int)bytes[4-7] + (int)bytes[8-11] + (int)bytes[12-15] + (int)bytes[16-19];
# we need to make this 
ip[0] + ip[1] + ip[2] + ip[3] + ip[4] == 0x21DD09EC

So what happen’s when we Split the total sum equally into 5 chunks.

>>> 0x21DD09EC // 5
113626824
>>> 113626824 * 5
568134120

But this is Not enough — 568134120 < 568134124 by 4.We need to make the last int needs a +4 bump.

Final payload should look like this in code

[113626824, 113626824, 113626824, 113626824, 113626828] 

>>> 113626824*4 + 113626828
568134124 # here verified finally

Now let’s make the payload from above

python3 -c 'import sys; sys.stdout.buffer.write(b"\xc8\xce\xc5\x06"*4 + b"\xcc\xce\xc5\x06")' | xargs -0 ./col

Flag:

0
Subscribe to my newsletter

Read articles from Sangharsha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sangharsha
Sangharsha

Aspiring developer and security enthusiast.