-
pwnable.kr 2번 collision정보보안/포너블 2017. 12. 26. 23:03
pwnable.kr 2번 문제 collision
MD5 hash collision? 얼른 구글링해 보니 해시 충돌에 대한 문제인 것 같았다.
해시 충돌은 해시 함수가 서로 다른 두 개의 입력값에 대해 동일한 출력값을 내는 상황을 말한다.
해시 함수(hash function)은 임의의 길이의 데이터를 고정된 길이의 데이터로 매핑하는 함수를 말한다고 한다.
예를 들어 md5 encryption 사이트에서 어떤 길이의 data를 hashing하든지 32자리의 해시가 나오므로 md5는 해시 함수라고 할 수 있지만,
Base64 인코딩은 어떤 길이의 data를 넣는다고 해서 항상 일정한 길이의 출력이 나오지 않으므로 해시 함수라고 할 수 없다.
그러니까 아빠가 MD5 해시 충돌에 대해 알려주니까 그걸 보고 비슷하게 따라해 보고 싶다는 귀여운 애벌레 그림이 있는 문제다.
음?12345678910111213141516171819root@goorm:/workspace/JunhoYeo# ssh col@pwnable.kr -p2222col@pwnable.kr's password:____ __ __ ____ ____ ____ _ ___ __ _ ____| \| |__| || \ / || \ | | / _] | |/ ]| \| o ) | | || _ || o || o )| | / [_ | ' / | D )| _/| | | || | || || || |___ | _] | \ | /| | | ` ' || | || _ || O || || [_ __ | \| \| | \ / | | || | || || || || || . || . \|__| \_/\_/ |__|__||__|__||_____||_____||_____||__||__|\_||__|\_|- Site admin : daehee87.kr@gmail.com- IRC : irc.netgarage.org:6667 / #pwnable.kr- Simply type "irssi" command to join IRC now- files under /tmp can be erased anytime. make your directory under /tmp- to use peda, issue `source /usr/share/peda/peda.py` in gdb terminalLast login: Tue Dec 26 04:11:28 2017 from 118.144.139.210col@ubuntu:~$ lscol col.c flagcol@ubuntu:~$ cat flagcat: flag: Permission deniedcs 문제에서 주어진 주소로 ssh 연결을 했다. 이번에도 goorm.io를 이용했다.
순간 얼마전 goorm.io의 Facebook에서 어떤 사람들이 goorm.io의 Terminal에서 암호화폐 채굴을 했다는 글을 본 것이 생각난다.제일 먼저 ls 명령으로 현재 디렉토리에 있는 파일을 확인했다.
col, col.c, flag 세 파일이 있다. 당연히 안 될 걸 알면서도 cat flag를 시도했고, 굳이 결과까지 따로 밝히지 않아도 될 것 같다.
123456789101112131415161718192021222324252627282930col@ubuntu:~$ cat 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;}elseprintf("wrong passcode.\n");return 0;}cs 이제 col.c 파일을 확인해 보았다. C언어로 작성된 프로그램 col의 소스코드가 있다.
먼저 argument의 존재 여부와 argument의 길이가 20byte가 맞는지를 확인한다.
다음으로 hashcode와 argument를 해싱한 값을 비교하여 두 값이 일치하면 Flag를 출력한다.
해시 함수인 check_password에서는 문자열(=argument)을 4바이트씩 잘라서(4바이트 자료형인 int형으로 형변환) 더한 값을 반환한다.
그러니까 argument를 해싱한 값이 hashcode가 되도록 하면 된다.
파이썬 프롬프트도 열리더라.. 파이썬으로 필요한 연산을 쉽게 하고 결과까지 hex로 나오게 할 수 있다.1234567891011121314col@ubuntu:~$ pythonPython 2.7.12 (default, Jul 1 2016, 15:12:24)[GCC 5.4.0 20160609] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> hashcode = 0x21DD09EC #문제에서 주어진 hashcode 값>>> hex(hashcode/5) #hashcode를 5개로 나눈 값(4바이트씩 해서 5개로 끊으니까)'0x6c5cec8'>>> hex(hashcode%5) #hashcode는 5로 나누어떨어지지 않으니 그 나머지=4'0x4'>>> hex(hashcode/5+hashcode%5) #hashcode/5+4=hashcode/5+hashcode%5'0x6c5cecc'>>> hex(hashcode/5+hashcode%5+hashcode/5*4) #hashcode는 (hashcode/5)*4 + hashcode/5+4로 표현가능'0x21dd09ec'>>> exit()cs 파이썬에서 hex data 출력은 hex() 함수로 한다는 사실도 오늘 구글링하면서 처음 알게 된 사아실~
hashcode=(hasecode/5)*4+(hashcode/5+hashcode%5)=0x6c5cec8*4+0x6c5cecc
따라서 hashcode는 0x6c5cec8*4+0x6c5cecc로 표현가능하다는 것을 알아냈다!! 꺄르륵
12col@ubuntu:~$ ./col `python -c 'print "\xC8\xCE\xC5\x06"*4 + "\xCC\xCE\xC5\x06"'`daddy! I just managed to create a hash collision :)cs 파이썬 스크립트로 리틀 엔디언(Little-endian) 방식으로 인수 전달을 해주면 Flag가 똷!
Flag는 daddy! I just managed to create a hash collision :)이다.
이번엔 플래그 인증하는 사진을 잘~ 캡쳐해서 가져왔습니당
'정보보안 > 포너블' 카테고리의 다른 글
해커스쿨 FTZ level5 (0) 2017.12.28 pwnable.kr 3번 bof (0) 2017.12.27 pwnable.kr 1번 fd (0) 2017.12.26 해커스쿨 FTZ level4 (0) 2017.12.26 해커스쿨 FTZ level3 (0) 2017.12.26