-
-
[原创]Docker搭建pwn环境
-
2020-12-23 03:11
20824
-
Docker搭建pwn环境
最近有点忙,没什么时间和心思学堆了,就先利用零碎时间搭建了一个docker上的pwn环境。其实以前就想搭建一个docker环境,但是那时候想用dockfile来一步到位,奈何自身水平原因,反而花了很多时间,然后就放弃了。现在我直接用Dockerfile拉一个基础的ubuntu镜像,然后在容器里一个一个安装工具。
1. docker常用命令
1.1 镜像操作
1) 拉取镜像到本地
1 2 3 | docker pull 镜像名称[:tag]
docker pull ubuntu: 16.04
|
2) 查看全部本地镜像
3) 删除本地的镜像
1 | docker rmi 镜像的标识( id 或者镜像名称:tag)
|
4) 镜像的导入导出
1 2 3 4 5 6 7 8 9 | docker save - o 导出的路径 镜像 id / 镜像名称:tag
docker save - o . / ubuntu.image ubuntu: 16.04
docker load - i 镜像文件
docker load - i ubuntu.image
|
5) 修改镜像的名称
1 | docker tag 镜像 id 新镜像名称:版本(tag)
|
1.2 容器操作
1) 运行容器
1 2 3 4 5 6 7 8 | docker run 镜像的标识|镜像名称[:tag]
docker run - d - p 宿主机端口:容器端口 - - name 容器名称 镜像的标识:镜像名称[:tag]
|
2) 查看正在运行的容器
3) 进入到容器的内部
1 | docker exec - it 容器 id bash
|
4) 停止和删除容器(删除容器前,需要先停止容器)
1 2 3 4 5 6 7 8 9 | docker stop 容器 id
docker stop $(docker ps - qa)
docker rm 容器 id
docker rm $(docker ps - qa)
|
5) 启动容器
6) 从容器创建一个新的镜像
1 2 3 4 5 6 7 | docker commit [OPTIONS] 容器标识 [镜像名称[:TAG]]
|
2. 搭建步骤
2.1 自定义基础镜像
1 2 3 4 5 6 7 8 9 10 11 12 | FROM ubuntu: 16.04
RUN sed - i s@ / archive.ubuntu.com / @ / mirrors.aliyun.com / @g / etc / apt / sources. list
RUN apt - get clean
RUN apt - get update
RUN apt - get install \
python python3 \
python - pip python3 - pip \
python - dev libffi - dev libssl - dev \
vim \
lib32ncurses5 lib32z1 \
ruby gem git \
- y
|
- lib32ncurses5 和 lib32z1 是32位库,是为了能够运行32位程序
- ruby和gem是为了后面安装one_gadget
1 | docker build - t pwn:latest .
|
apt update 可能会失败,多试几次。
2.2 运行容器
1 | docker run - it - - name pwn - v xxx / challenges: / opt / challenges pwn:latest
|
- -v:映射数据卷。目的是为了将来把文件放入容器中,当然这里需要你自己建一个空目录,然后映射到容器中。
不停止运行退出:先按 ctrl+p
,然后按 ctrl+q
。
2.3 安装gdb插件
2.3.1 pwndbg
pwndgb: https://github.com/pwndbg/pwndbg
1 2 3 | git clone https: / / github.com / pwndbg / pwndbg
cd pwndbg
. / setup.sh
|
1 2 3 | git clone https: / / hub.fastgit.org / pwndbg / pwndbg.git
cd pwndbg
. / setup.sh
|
期间会安装python库,可能因为源和网络的原因会安装失败,这时候有两个选择:
- 重试./setup.sh
- 哪个python库因为网络原因安装失败,那就自己指定源,先用pip3安装它。
2.3.2 安装peda-heap
这里我就不安装peda了,因为peda-heap是在peda的基础上添加了一些调试堆的命令。
而且peda的安装过程也和peda-heap基本一样。
peda: https://github.com/longld/peda
peda-heap:https://github.com/Mipu94/peda-heap
1 2 | git clone git: / / github.com / Mipu94 / peda - heap.git ~ / peda - heap
echo "source ~/peda-heap/peda.py" >> ~ / .gdbinit
|
1 2 | git clone https: / / hub.fastgit.org / Mipu94 / peda - heap.git
echo "source /opt/tools/peda-heap/peda.py" >> ~ / .gdbinit
|
图中还有dbg的影子,如果不想要的话,那么可以注释掉~/.gdbinit(这里也就是/root/.gdbinit)里关于pwndbg的那一行。
2.3.3 安装gef
我在docker里安装gef失败,暂时没找到原因,但是我在虚拟机里是安装成功的。
PS:gef我用得少,就没管了。如果有哪位大佬知道为啥,请告诉我。
1) 安装wget
2) 安装gef
1 2 3 4 5 6 | wget - q - O - https: / / github.com / hugsy / gef / raw / master / gef.sh | sh
wget - q - O ~ / .gdbinit - gef.py https: / / github.com / hugsy / gef / raw / master / gef.py
echo source ~ / .gdbinit - gef.py >> ~ / .gdbinit
|
2.3.4 安装pwngdb
我安装 pwngdb 主要也是为了调试堆。
1 2 | git clone https: / / github.com / scwuaptx / Pwngdb.git
cp Pwngdb / .gdbinit ~ /
|
安装完成后,修改~/.gdbinit里面的内容
2.3.5 命令切换
1 2 3 4 5 6 7 8 | pwndbg:
echo "source /opt/tools/pwndbg/gdbinit.py" > ~ / .gdbinit
peda - heap:
echo "source /opt/tools/peda-heap/peda.py" > ~ / .gdbinit
gef:
echo "source ~/.gdbinit-gef.py" > ~ / .gdbinit
|
2.4 安装LibcSearcher
1 2 3 4 | git clone https: / / gitclone.com / github.com / lieanu / LibcSearcher.git
cd LibcSearcher
python setup.py develop
|
2.5 安装pwntools
1 2 3 4 | apt - get update
apt - get install python3 python3 - pip python3 - dev git libssl - dev libffi - dev build - essential
python3 - m pip install - - upgrade pip
python3 - m pip install - - upgrade pwntools
|
- 我的安装(依赖我在dockerfile安装了,pwntools习惯用python2)
1 2 3 | python - m pip install - - upgrade pip
python - m pip install - - upgrade pwntools - i https: / / pypi.douban.com / simple /
|
pwntools:https://github.com/Gallopsled/pwntools
但是shellcraft使用报错:语法错误。初步猜测问题出在Pygments这个库,可能版本过高或者过低?
pip list 查看到Pygments的版本是2.7.3,然后故意写错安装命令查看Pygments有哪些版本,发现提供的最高是2.5.2,安装2.5.2版本后发现使用shellcraft成功。
2.6 安装ROPgadget
1 2 3 4 5 6 7 | git clone https: / / github.com / JonathanSalwan / ROPgadget.git
git clone https: / / gitclone.com / github.com / JonathanSalwan / ROPgadget.git
cd ROPgadget
python setup.py install
|
2.7 安装one_gadget
1 2 3 4 5 6 7 8 | gem sources - - remove https: / / rubygems.org /
gem sources - a http: / / gems.ruby - china.com /
gem install one_gadget
|
2.8 安装glibc-all-in-one
1 2 3 4 5 6 7 8 9 10 11 12 | git clone https: / / gitclone.com / github.com / matrix1001 / glibc - all - in - one.git
cd glibc - all - in - one
. / update_list
cat list
. / download glibc
ps:查看当前glibc版本号方法
/ lib / x86_64 - linux - gnu / libc.so. 6
|
2.9 安装patchelf
1 2 3 4 5 6 7 8 9 10 11 12 | git clone https: / / gitclone.com / github.com / NixOS / patchelf.git
cd patchelf
. / bootstrap.sh
. / configure
make
make install
make check
|
1 2 | apt - get install autoconf automake libtool
apt - get install libffi - dev
|
2.10 给程序换glibc
2.10.1 生成所需的符号链接
1 2 3 4 5 | cd / lib64
ln - s / opt / tools / glibc - all - in - one / libs / 2.27 - 3ubuntu1 . 4_amd64 / ld - 2.27 .so . / 27_14 - linux.so. 2
|
2.10.2 更改elf文件的ld和libc
1 2 | patchelf - - set - interpreter / lib64 / 27_14 - linux.so. 2 ~ / hello
|
1 2 | patchelf - - replace - needed libc.so. 6 / opt / tools / glibc - all - in - one / libs / 2.27 - 3ubuntu1 . 4_amd64 / libc - 2.27 .so ~ / hello
|
2.11 从容器创建新镜像
1 2 | docker commit - a "lzx" pwn:latest
|
最后打包:
1 | docker save pwn:latest - o pwn.tar
|
需要的同学可以去百度云自取:链接: https://pan.baidu.com/s/1HpyYVtwBrR-7ty1BMMa1eg 密码: uh6s
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课