system hacking/개념, 준비물

libc 링킹(patchelf)

blackbearwow 2022. 4. 17. 15:43

워게임 문제를 풀다보면, 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

를 사용하여 주어진 libc로 링킹하자.

 

설치하는법:

home디렉토리에서(다른곳에 해도 된다) 

git clone https://github.com/NixOS/patchelf
cd patchelf
./bootstrap.sh
./configure
make
make check
sudo make install

하면 된다.

 

명령하는법:

patchelf --replace-needed liboriginal.so.1 libreplacement.so.1 my-program
#libc 바꾸기
patchelf --set-interpreter /lib/my-ld-linux.so.2 my-program
#ld 바꾸기

포너블 문제가 주어질 때 libc만 주어지고, ld는 안 주는 경우가 있다. 그 때는 ld가 해당 우분투 버전과 맞지 않아서 그런것인데, 도커를 이용해서 해당 우분투 버전의 ld를 가져와서 링킹하면 된다. 이것이 안돼서 한참 해메었다....

 

-> 여기까지 하면 실행파일은 정상적으로 작동한다.

 

하지만 gdb를 이용해 디버깅 하면, parseheap heapinfo명령어가 먹질 않는다. 

디버깅 심볼이 로딩되지 않은것인데, 로컬 디버깅 심볼과 달라서 로딩되지 않은것이다.

인터넷에서 버전에 맞는 libc6-gdb 패키지를 다운받아 gdb에 적용하면 된다.

http://old-releases.ubuntu.com/ubuntu/pool/main/g/glibc/ 또는

http://archive.ubuntu.com/ubuntu/pool/main/g/glibc/?C=M;O=D 여기서 찾으면 된다.

여기 둘다 없으면 인터넷에 검색하도록 하자.

 

gdb에 심볼 테이블을 연결한 후 힙분석을 하면 된다.

set debug-file-directory ~/Unix/dreamhack/debug/libc6-dbg_2.27-3ubuntu1.6_amd64/data/usr/lib/debug/lib/x86_64-linux-gnu

 

 

참고: https://typemiss.tistory.com/2