Step0 关于为什么重新配置环境
在此之前一直使用的是Kali镜像 + 虚拟机的组合,因为kali自称是专精于渗透和测试方面,预安装了许多的有用的工具。但实际操作下来,发现预安装的工具绝大多数都对pwn方向没有太大帮助,很多工具和环境还是需要手动配置,况且无论是vim还是idle编辑器说实话都不是特别好用。
但最关键的其实是因为编译器项目要使用wsl,而在虚拟机里使用qemu需要开启虚拟化 intel vt-x/ept 或 amd-v/rvi
选项,而这个选项与wsl所需的Hyper-V相冲突。也就是说写kernel-pwn就没法同时做项目,做项目就没法写kernel-pwn,Hyper-V的开关都需要重启才能生效。
考虑到wsl + vscode由于没有图形化,效率更上一层,所以写下以下blog记录从头开始的环境配置。
Step1 vscode + wsl
略,网上攻略很多。
有一点需要注意,就是wsl和window进行文件交换时,window文件可以直接拖拽到vsCode的文件侧栏里,反之vsCode里的wsl文件无法拖动到windows中,对此可以使用cp xxx /mnt/小写盘符/xxx
将wsl文件交换到windows中,
比如将某个文件放置在桌面上
$ cp pwn /mnt/c/Users/nobady/pwn
其次一点,从windows拖拽到wsl理论的文件默认没有执行权限,记得对必要的文件使用chmod +x
,包括可执行文件以及ld文件。
Step2 新建一个用户
开启root的远程连接,直接adduser建立一个新用户。这一步单纯是为了获得一个干净的工作区,因为无论cmd还是vscode都是直接以root打开wsl的,本身就没有权限限制。
Step3 各种杂七杂八的小工具
pwntools
$ apt update
$ apt upgrade
$ apt install python3 python3-pip python3-dev git libssl-dev libffi-dev build-essential -y
$ python3 -m pip install --upgrade pip
$ pip3 install --upgrade pwntools
哥们sudo坏了,不知道是不是wsl都这样
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager,
possibly rendering your system unusable.
It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv.
Use the --root-user-action option if you know what you are doing and want to suppress this warning.
以root身份使用pip3会报如上warning,由于不打算用python做大型项目,选择忽略.
简单检查一下
root@PainTech:/home/pwn# cyclic 100
aaaabaaacaaadaaaeaaafaaagaaahaaaiaaajaaakaaalaaamaaanaaaoaaapaaaqaaaraaasaaataaauaaavaaawaaaxaaayaaa
root@PainTech:/home/pwn# checksec
usage: pwn checksec [-h] [--file [elf ...]] [elf ...]
root@PainTech:/home/pwn# python3
Python 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pwn import *
>>> exit()
glibc-all-in-one
# 示范一下如何安装使用
$ git clone https://github.com/matrix1001/glibc-all-in-one.git
$ pushd glibc-all-in-one
$ python3 update_list
[+] Common list has been save to "list"
[+] Old-release list has been save to "old_list"
$ cat list
2.23-0ubuntu11.3_amd64
2.23-0ubuntu11.3_i386
2.23-0ubuntu3_amd64
2.23-0ubuntu3_i386
2.27-3ubuntu1.5_amd64
2.27-3ubuntu1.5_i386
2.27-3ubuntu1.6_amd64
2.27-3ubuntu1.6_i386
2.27-3ubuntu1_amd64
2.27-3ubuntu1_i386
# i386是32位,amd64是64位
$ ./download 2.23-0ubuntu11.3_amd64
$ ls libs
2.23-0ubuntu11.3_amd64
patchelf
# 还是示例
$ apt-get install patchelf
$ patchelf --set-interpreter ./glibc-all-in-one/libs/2.23-0ubuntu11.3_amd64/ld-2.23.so targetfile
$ patchelf --replace-needed libc.so.6 ./glibc-all-in-one/libs/2.23-0ubuntu11.3_amd64/libc.so.6 targetfile
# 或者patchelf --replace-needed libc.so.6 ./glibc-all-in-one/libs/2.23-0ubuntu11.3_amd64/libc-2.23.so targetfile
# 去file libc.so.6你就知道为什么
需要注意一般C程序只需要更改链接器和libc,如果不是C程序就使用ldd
命令查看依赖项,然后再去网上找着配,这会是一个比较繁琐的过程。
gdb插件(这里选择pwndbg)
$ git clone https://github.com/pwndbg/pwndbg
$ pusd pwndbg
$ ./setup.sh
sudo: /etc/sudo.conf is world writable
sudo: /etc/sudo.conf is world writable
sudo: error in /etc/sudo.conf, line 0 while loading plugin "sudoers_policy"
sudo: /usr/libexec/sudo/sudoers.so must be only be writable by owner
sudo: fatal error, unable to load plugins
sudo: /etc/sudo.conf is world writable
sudo: /etc/sudo.conf is world writable
sudo: error in /etc/sudo.conf, line 0 while loading plugin "sudoers_policy"
sudo: /usr/libexec/sudo/sudoers.so must be only be writable by owner
sudo: fatal error, unable to load plugins
这里报错是因为sh脚本中有sudo
,需要修改一下脚本
(1)7-11行 和 155-159行,注释掉
(2)查找所有sudo
,然后全部删除
init完成后提示
[*] Added 'source /home/pwn/pwndbg/gdbinit.py' to ~/.gdbinit so that Pwndbg will be loaded on every launch of GDB.
提示要配置gdb的启动脚本
$ touch ~/.gdbinit ; echo 'source /home/pwn/pwndbg/gdbinit.py' > ~/.gdbinit
注意由于是root用户,所以.gdbinit文件位置有所不同
安装其他插件时,也要配置这个文件,比如
source /home/pwn/gef/gef.py
如果.gdbinit中路径错误,那么gdb
命令将打开原生gdb,好像不会有错误提示
seccomp-tools
$ apt install gcc ruby-dev
$ gem install seccomp-tools
one_gadget
$ gem install one_gadget
Step4 Qume
$ apt-get install libc6-dev
$ apt install qemu-kvm
# 时间比较长,可以考虑换源
busybox和kernel就先不考虑了,编译花费的时间过长
pip3 install --upgrade lz4 git+https://github.com/marin-m/vmlinux-to-elf
# 用来抽取vmlinux的妙妙工具