system hacking 30

validator

해당 워게임은 소스코드가 주어지지 않는다. $ checksec validator_dist [*] '/home/juho/Unix/dreamhack/systemHacking/validator/validator_dist' Arch: amd64-64-little RELRO: Partial RELRO Stack: No canary found NX: NX disabled PIE: No PIE (0x400000) RWX: Has RWX segments partial relro 외에 다른 보호기법은 없다. str변수에 0x400만큼 read한후 validate([rbp+str], 0x80)을 호출한다. validate함수로 들어간후 switch to source를 하면 맨처음 10개는 DREAMHACK!이고 나머지는 ..

ida64 free 다운로드와 설치, qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found오류

다시 다운로드하려고 하니 오류가 생겨서 글을 남긴다. https://hex-rays.com/ida-free/ IDA Freeware IDA Freeware The free binary code analysis tool to kickstart your reverse engineering experience. hex-rays.com 에서 IDA Freeware for Linux (76MB)를 다운받는다. chmod로 execute권한을 추가해 실행시켜주고 설치한다. sudo ln -s ~/idafree-7.7/ida64 /usr/bin/ 소프트링크를 /usr/bin/에 추가해서 어느 경로에서든 실행시킬 수 있게 한다. 아래 오류가 뜬다면 특정 모듈을 설치 하면 된다. apt install libxcb-xin..

sint

C언어의 Type Error에 대한 문제이다. read함수의 read (int __fd, void *__buf, size_t __nbytes)에서 __nbytes는 size_t형이다. 이때 int형 변수를 매개변수로 건내준다면, 음수인 매개변수는 size_t에서 양수로 변환되어 엄청 큰 값이 된다. #include #include #include #include void alarm_handler() { puts("TIME OUT"); exit(-1); } void initialize() { setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); signal(SIGALRM, alarm_handler); alarm(30); } void get_..

tcache_dup2

tcache_dup워게임은 tcache key가 없는 libc버전이지만 tcache_dup2워게임은 tcache key가 있는 libc버전이다. #include #include #include #include char *ptr[7]; void initialize() { setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); } void create_heap(int idx) { size_t size; if( idx >= 7 ) exit(0); printf("Size: "); scanf("%ld", &size); ptr[idx] = malloc(size); if(!ptr[idx]) exit(0); printf("Data: "); read(0, ..

tcache_dup

... 멘탈이 몇번나갔는지 모르겠다... 결국 docker와 patchelf 모두 실패했고 혹시나 하는 심정으로 코드를 조금 고쳐서 해봤더니 성공했다. // gcc -o tcache_dup tcache_dup.c -no-pie #include #include #include #include char *ptr[10]; void alarm_handler() { exit(-1); } void initialize() { setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); signal(SIGALRM, alarm_handler); alarm(60); } int create(int cnt) { int size; if(cnt > 10) { return..

docker (pwnable 문제 다양한 ubuntu버전에서 실행하기)

포너블을 풀 때 매번 libc의 버전이 다르고 내부 동작이 다르면 gdb로 분석할 때 서버 작동과 다르다. docker를 이용해서 가상 환경을 만들고 포너블 하자. docker는 vmware과는 다르다. 1. 도커를 잘 모른다면 다음 블로그를 읽어보자. docker의 이해: https://subicura.com/2017/01/19/docker-guide-for-beginners-1.html 초보를 위한 도커 안내서 - 도커란 무엇인가? 도커를 처음 접하는 시스템 관리자나 서버 개발자를 대상으로 도커 전반에 대해 얕고 넓은 지식을 담고 있습니다. 도커가 등장한 배경과 도커의 역사, 그리고 도커의 핵심 개념인 컨테이너와 이 subicura.com 2. 도커 설치 https://subicura.com/2017..

Tcache Poisoning

1. 문제를 보기 전에 원리를 살펴보자. 드림핵 Exploit Tech: Tcache Poisoning을 보면 같은 청크를 두번 해제한 후 재할당하면 그 청크는 할당된 청크이면서 헤제된 청크이므로 해당 청크의 fd(tcache의 next), bk(tcache의 key)를 조작할 수 있다는 것이다. 그렇다면, 이것을 조작할 수 있다면 왜 임의의 주소에 청크를 할당할 수 있다는 것일까? 드림핵에서는 자세히 설명하지 않아서 이해되지 않았다. tcache로 예를 들어보자. tcache는 next부분이 0이어야 tcachebins list에 해제된 청크가 하나도 없다는 뜻이다(아마도). next부분이 0이면 리스트의 끝으로 본다. 반면에 next부분을 조작하여 임의의 주소를 넣으면 해당 주소+0x10주소의 값(x..

Double Free Bug

free()로 메모리를 해제하면 ptmalloc2는 해제된 메모리를 tcache나 bins에 추가하여 관리한다. tcache나 bins같은 free list에 같은 메모리가 중복되어 추가된다면, 청크가 duplicated되었다고 말한다. duplicated free list를 이용하면 임의 주소에 청크를 할당할 수 있다. double free 보호기법을 우회하여 duplicated free list를 만들 수 있다면, free list의 청크의 fd, bk값을 조정할 수 있다. freed chunk에서의 fd, bk는 allocated chunk의 data이니 해당 값을 쓰기가 가능하다면 fd, bk조정이 가능하다. 1. double free 보호기법 우회 tcache 구조체는 일반 청크의 fd에 해당하..

uaf_overwrite

이 문제는 답을 알려준 문제이다. 하지만 모르는 내용이 많이 나와 정리해보자. // Name: uaf_overwrite.c // Compile: gcc -o uaf_overwrite uaf_overwrite.c #include #include #include #include struct Human { char name[16]; int weight; long age; }; //size: 0x20 struct Robot { char name[16]; int weight; void (*fptr)(); }; struct Human *human; struct Robot *robot; char *custom[10]; int c_idx; void print_name() { printf("Name: %s\n", rob..

libc 링킹(patchelf)

워게임 문제를 풀다보면, libc가 자신의 pc와 다르다면 로컬 환경에서 문제의 libc버전을 링킹해주어야지 제대로 문제를 풀 수 있다. https://github.com/NixOS/patchelf GitHub - NixOS/patchelf: A small utility to modify the dynamic linker and RPATH of ELF executables A small utility to modify the dynamic linker and RPATH of ELF executables - GitHub - NixOS/patchelf: A small utility to modify the dynamic linker and RPATH of ELF executables github.com 를 ..

heap

dreamhack에서 use after free내용을 보다가 이해가 되지 않아서 정리를 하고 넘어가자. 먼저 chunk에 대해서 알아보자. 참고: https://rninche01.tistory.com/entry/heap3-glibc-malloc2-feat-chunk?category=838537 32bit system: 8bytes의 배수로 할당 64bit system: 16bytes의 배수로 할당 example debugging gdb로 example을 디버깅해보자. malloc을 두번 한 후 heap을 보자. 1바이트, 2바이트를 요구했지만 0x20(32byte)씩 할당되었다. 그리고, 왜 malloc은 하나 했지만 청크는 3개가 생기는지 보자. 현재 malloc을 한번했는데, chunk는 3개가 있다..

basic_exploitation_003

이번에도 fsb을 이용한 문제이다. Arch: i386-32-little RELRO: Partial RELRO Stack: No canary found NX: NX enabled PIE: No PIE (0x8048000) 카나리도 없고 pie도 없다. 취약점은 sprintf에서 발생한다. sprintf_s로 크기를 지정하면 취약점이 없어지겠지만, sprintf는 크기를 검사하지 않고 heap_buf에 해당하는 포멧을 stack_buf에 저장한다. 여기서 오버플로우가 발생하는 것이다. 스택카나리도 없으니 return주소를 덮어쓸 수 있다. ida로 stack_buf의 주소가 ebp-0x98이라는 것을 보고, 포멧의 width를 몇으로 지정할지 정한다. 0x98*4+4(pop rbp)뒤에 return add..

basic_exploitation_002

format string bug를 이용한 exploit이다. Arch: i386-32-little RELRO: Partial RELRO Stack: No canary found NX: NX enabled PIE: No PIE (0x8048000) no pie이어서 get_shell함수의 주소를 안다면, rip를 get_shell의 주소로 설정되게 하면 된다. no canary found이고 read가 buf의 크기보다 크게 읽는다면, return address overwrite가 가능하다. 하지만 이 문제에서는 딱 buf의 크기만큼만 읽는다. 또한 buf보다 큰 값을 읽는다 하더라도 exit(0)때문에 return 주소로 가지 않고 프로그램이 종료된다. RELRO(realocation read only)..