花式技巧1——patchelf重链接
参考博客:https://www.freebuf.com/sectool/366854.html
https://www.cnblogs.com/9man/p/17581934.html
- 很多情况下题目附件除了给可执行程序以外还会给.so文件,.so文件有很多版本。但是.so文件里面的字串有显示其具体版本,这样我们就可以找相应的.dl文件和.so文件在本地进行重链接,这样就可以在本地模拟靶机上的环境,有利于gdb动态调试和查看内存地址
前提准备
安装glibc-all-in-one
1 2 3 4
| git clone https://github.com/matrix1001/glibc-all-in-one.git cd glibc-all-in-one chmod a+x build download extract sudo python3 update_list # 需要Python3 安装requests库 pip3 install requests
|

安装patch
sudo apt install -y patchelf chrpath
- 下载完成后再检查patch能否使用:出现以下就代表可以使用了
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
| iyheart@iyheart-virtual-machine ~/pwn_learn> patchelf -h syntax: patchelf [--set-interpreter FILENAME] [--page-size SIZE] [--print-interpreter] [--print-soname] Prints 'DT_SONAME' entry of .dynamic section. Raises an error if DT_SONAME doesn't exist [--set-soname SONAME] Sets 'DT_SONAME' entry to SONAME. [--set-rpath RPATH] [--add-rpath RPATH] [--remove-rpath] [--shrink-rpath] [--allowed-rpath-prefixes PREFIXES] With '--shrink-rpath', reject rpath entries not starting with the allowed prefix [--make-rpath-relative ROOTDIR] [--no-standard-lib-dirs] [--relative-to-file] [--print-rpath] [--force-rpath] [--add-needed LIBRARY] [--remove-needed LIBRARY] [--replace-needed LIBRARY NEW_LIBRARY] [--print-needed] [--no-default-lib] [--clear-symbol-version SYMBOL] [--output FILE] [--debug] [--version] FILENAME...
|
确定patch的库
方法一
- 在有题目所给libc.so.6的目录下直接输入
strings libc.so.6 |grep Ubuntu

1
| GNU C Library (Ubuntu GLIBC 2.35-0ubuntu3.6) stable release version 2.35.
|
方法二
- 确定偏移量,然后前往在线libc库搜索去搜索到对应的库
下载库
- 进入到glibc-all-in-one文件夹中,cat list查看对应的库**(Ubuntu GLIBC 2.35-0ubuntu3.6)**

1
| ./download 2.35-0ubuntu3.6_amd64
|

- 报错信息表示没有相应的工具解压
.zst
文件,下载相应软件,再重新 ./download 2.35-0ubuntu3.6_amd64
,没有报错,成功
1 2
| sudo apt update sudo apt install zstd
|

patch
- 找到题目所在
.ld
文件和.so.6
文件所在的位置

- 在patch之前先给
.ld
文件和 .so
文件添加可执行权限(下图仅做加权限的例子)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| # 设置新文件的ld路径 patchelf --set-interpreter 新ld路径 要patch的elf路径
patchelf --set-interpreter /home/myheart/glibc-all-in-one/libs/2.23-0ubuntu11.3_amd64/ld-linux-x 86-64.so.2 ./Palu
# 设置新的libc路径 patchelf --replace-needed libc.so.6 新libc路径 要patch的elf路径
patchelf --replace-needed libc.so.6 /home/myheart/glibc-all-in-one/libs/2.23-0ubuntu11.3_amd64/l ibc-2.23.so ./Palu
# 如果是一个多线程依赖库那么就需要添加libc库,而不是替换libc库 patchelf --add-needed 你的文件目录/libc.so.6 ./pwn patchelf --add-needed 你的目录/libpthread.so.0 ./pwn (如果提示没有libpthread.so.0的话) #该命令我还不太了解 patchelf --set-rpath 你的文件目录/ld-linux-x86-64.so.2 ./pwn patchelf --set-rpath /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 ./vuln
|

- 在重链接后使用
ldd
指令查看一下文件的链接路径是否成功

