#Pwnable.kr #memcpy #Toddler
memcpy 문제이다.
해당 코드를 컴파일하여 실행해보면, 위 처럼 heap 에 할당할 크기를 입력받고 자체적으로 개발한 memcpy 함수 두 개를 실행한다.
참고로, 해당 memcpy 함수는 slow_memcpy 와 fast_memcpy 이다.
아무튼 저렇게 입력 값을 주면 계속해서 slow_memcpy 와 fast_memcpy 를 실행하다가 128 값에서 프로그램이 Segmemtation fault 를 내뿜으면서 종료된다.
디버깅으로 알아보면, 위 부분에서 메모리에 데이터를 제대로 전달하지 못하고 꺼진다.
movntps 어셈블리 명령어를 알아보면
Description
Moves the packed single-precision floating-point values in the source operand (second operand) to the destination operand (first operand) using a non-temporal hint to prevent caching of the data during the write to memory. (생략) The memory operand must be aligned on a 16-byte (128-bit version), 32-byte (VEX.256 encoded version) or 64-byte (EVEX.512 encoded version) boundary otherwise a general-protection exception (#GP) will be generated.
memory operand 가 되는 메모리가 16 바이트, 32 바이트, 64 바이트 단위의 주소로 정렬되어있지않으면, 해당 명령어 실행시 genral-protection exception 이 실행되어 프로그램이 꺼진다는 것이다.
다시 디버깅으로 넘어와서 해당 movntps 명령어 실행직전을 보면 0x804c4a8 를 시작주소로 128 바이트짜리 heap 이 할당된 것을 볼 수 있다.
heap chunk 라고 하는 것은 malloc 시에 인자로 준 크기만큼 바로 할당되는 것이 아니다.
이전에 마지막으로 할당 된 heap chunk 로부터 8 bytes 떨어진 곳에 원하는 데이터가 들어가는 영역이 할당된다.
그러면 8 bytes 안에는 무엇이 들어가느냐? 4 바이트는 이전 chunk 의 크기가 들어가고, 4 바이트는 현재 chunk 크기가 들어가게 된다.
결론적으로 문제에서 원하는 16 바이트 단위 주소를 맞추려면 이 부분도 고려가 되어야한다는 것!
이것들을 고려하면 아래와 같은 payload 로 flag 를 읽어낼 수 있다.
from pwn import *
s=remote("pwnable.kr",9022)
s.sendline("8")
s.sendline("16")
s.sendline("32")
s.sendline("72") # bigger than 64, xmm register is used.
s.sendline("136")
s.sendline("264")
s.sendline("520")
s.sendline("1032")
s.sendline("2056")
s.sendline("4104")
s.interactive()
'<Wargame & CTF> > Pwnable.kr' 카테고리의 다른 글
[Pwnable.kr] blukat (0) | 2021.05.30 |
---|---|
[Pwnable.kr] asm (0) | 2021.05.29 |
[Pwnable.kr] Unlink (0) | 2020.09.22 |
[Pwnable.kr]shellshock (0) | 2019.05.11 |
[Pwnable.kr]input (0) | 2019.05.11 |