Docker搭建pwn环境
2021-01-13 19:20:59 Author: mp.weixin.qq.com(查看原文) 阅读量:159 收藏

本文为看雪论坛优秀文章

看雪论坛作者ID:直木

其实以前就想搭建一个docker环境,但是那时候想用dockfile来一步到位,奈何自身水平原因,反而花了很多时间,然后就放弃了。现在我直接用Dockerfile拉一个基础的ubuntu镜像,然后在容器里一个一个安装工具。

一、docker常用命令

1.1 镜像操作

1) 拉取镜像到本地
docker pull 镜像名称[:tag]# e.gdocker pull ubuntu:16.04
2) 查看全部本地镜像
docker images
3) 删除本地的镜像
docker rmi 镜像的标识(id或者镜像名称:tag)
4) 镜像的导入导出
### 将本地的镜像导出docker save -o 导出的路径 镜像id/镜像名称:tag#e.g.docker save -o ./ubuntu.image ubuntu:16.04
### 加载本地的镜像文件docker load -i 镜像文件# e.g.docker load -i ubuntu.image
5) 修改镜像的名称
docker tag 镜像id 新镜像名称:版本(tag)

1.2 容器操作

1) 运行容器
# 简单操作docker run 镜像的标识|镜像名称[:tag]
# 常用的参数docker run -d -p 宿主机端口:容器端口 --name 容器名称 镜像的标识:镜像名称[:tag]# -d:代表后台运行容器# -p 宿主机端口:容器端口 :为了映射当前主机的端口和容器的端口# --name:容器名称;指定容器的名称
2) 查看正在运行的容器
docker ps [-qa]# -a:查看全部的容器,包括没有运行# -q:只查看容器的标识
3) 进入到容器的内部
docker exec -it 容器id bash
4) 停止和删除容器(删除容器前,需要先停止容器)
# 停止指定容器docker stop 容器id# 停止全部容器docker stop $(docker ps -qa)
# 删除指定容器docker rm 容器id# 删除全部容器docker rm $(docker ps -qa)
5) 启动容器
docker start 容器id
6) 从容器创建一个新的镜像
docker commit [OPTIONS] 容器标识 [镜像名称[:TAG]]
# OPTIONS:# -a :提交的镜像作者;# -c :使用Dockerfile指令来创建镜像;# -m :提交时的说明文字;# -p :在commit时,将容器暂停。

二、搭建步骤

2.1 自定义基础镜像

Dockerfile
FROM ubuntu:16.04RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.listRUN apt-get cleanRUN apt-get updateRUN 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
制作自定义基础镜像
docker build -t pwn:latest .

apt update 可能会失败,多试几次。

2.2 运行容器

-v:映射数据卷。目的是为了将来把文件放入容器中,当然这里需要你自己建一个空目录,然后映射到容器中。
不停止运行退出:先按 ctrl+p ,然后按 ctrl+q。

2.3 安装gdb插件

进入容器,在/opt目录下创建tools目录

2.3.1 pwndbg

pwndgb:https://github.com/pwndbg/pwndbg
官方安装过程:
git clone https://github.com/pwndbg/pwndbgcd pwndbg./setup.sh
我的安装过程:
git clone https://hub.fastgit.org/pwndbg/pwndbg.git # 高速下载油猴脚本:https://zhuanlan.zhihu.com/p/262905603cd pwndbg./setup.sh
安装成功:
期间会安装python库,可能因为源和网络的原因会安装失败,这时候有两个选择:
  1. 重试./setup.sh

  2. 哪个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
官方安装过程:
git clone git://github.com/Mipu94/peda-heap.git ~/peda-heapecho "source ~/peda-heap/peda.py" >> ~/.gdbinit
我的安装过程:
git clone https://hub.fastgit.org/Mipu94/peda-heap.gitecho "source /opt/tools/peda-heap/peda.py" >> ~/.gdbinit
图中还有dbg的影子,如果不想要的话,那么可以注释掉~/.gdbinit(这里也就是/root/.gdbinit)里关于pwndbg的那一行。

2.3.3 安装gef

我在docker里安装gef失败,暂时没找到原因,但是我在虚拟机里是安装成功的。
PS:gef我用得少,就没管了。如果有哪位大佬知道为啥,请告诉我。
 
1) 安装wget
apt install wget
2) 安装gef
#下载 `gef.sh` 并执行wget -q -O- https://github.com/hugsy/gef/raw/master/gef.sh | sh
# 下载 `gef.py`, 并将其 `source` 写入 `.gdbinit`wget -q -O ~/.gdbinit-gef.py https://github.com/hugsy/gef/raw/master/gef.pyecho source ~/.gdbinit-gef.py >> ~/.gdbinit

2.3.4 安装pwngdb

我安装 pwngdb 主要也是为了调试堆。
git clone https://github.com/scwuaptx/Pwngdb.gitcp Pwngdb/.gdbinit ~/
安装完成后,修改~/.gdbinit里面的内容

2.3.5 命令切换

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

# git clone https://github.com/lieanu/LibcSearcher.git 官方git clone https://gitclone.com/github.com/lieanu/LibcSearcher.git # 加速cd LibcSearcherpython setup.py develop

2.5 安装pwntools

官方安装:
apt-get updateapt-get install python3 python3-pip python3-dev git libssl-dev libffi-dev build-essentialpython3 -m pip install --upgrade pippython3 -m pip install --upgrade pwntools
我的安装(依赖我在dockerfile安装了,pwntools习惯用python2)
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

# 官方git clone https://github.com/JonathanSalwan/ROPgadget.git# 高速git clone https://gitclone.com/github.com/JonathanSalwan/ROPgadget.git
cd ROPgadgetpython setup.py install

2.7 安装one_gadget

# 移除https://rubygems.org源gem sources --remove https://rubygems.org/
# 增加http://gems.ruby-china.org/源gem sources -a http://gems.ruby-china.com/
# 安装gem install one_gadget

2.8 安装glibc-all-in-one

# 官方# git clone https://github.com/matrix1001/glibc-all-in-one.git# 加速git clone https://gitclone.com/github.com/matrix1001/glibc-all-in-one.gitcd glibc-all-in-one
./update_list #更新最新版本的glibccat list #查看可下载的glibc./download glibc #glibc为你想要下载glibc的名字
ps:查看当前glibc版本号方法/lib/x86_64-linux-gnu/libc.so.6

2.9 安装patchelf

# 官方# git clone https://github.com/NixOS/patchelf.git
# 加速git clone https://gitclone.com/github.com/NixOS/patchelf.gitcd patchelf
./bootstrap.sh # 这里报错了,解决方法看下面./configuremakemake installmake check
报错:
解决:
apt-get install autoconf automake libtoolapt-get install libffi-dev
安装完成

2.10 给程序换glibc

2.10.1 生成所需的符号链接

cd /lib64 #进入64位的目录 glibc 32位就 cd /lib
# 在root态下链接ln -s /opt/tools/glibc-all-in-one/libs/2.27-3ubuntu1.4_amd64/ld-2.27.so ./27_14-linux.so.2 #27代表glibc版本,14代表ubuntu后面的数字(单纯为了好记)

2.10.2 更改elf文件的ld和libc

patchelf --set-interpreter /lib64/27_14-linux.so.2 ~/hello# 第一个参数是ld.so的目录 , hello 是二进制文件
patchelf --replace-needed libc.so.6 /opt/tools/glibc-all-in-one/libs/2.27-3ubuntu1.4_amd64/libc-2.27.so ~/hello#libc.so.6为需要替换的libc路径 第二个参数是需要加载的glibc的目录 , hello 是二进制文件
ldd ~/hello# 查看是否更换成功

2.11 从容器创建新镜像

# 创建新镜像docker commit -a "lzx" pwn:latest

最后打包:
docker save pwn:latest -o pwn.tar
 
需要的同学可以去百度云自取:
链接: https://pan.baidu.com/s/1HpyYVtwBrR-7ty1BMMa1eg 
密码: uh6s

- End -

看雪ID:直木

https://bbs.pediy.com/user-home-830671.htm

  *本文由看雪论坛 直木 原创,转载请注明来自看雪社区。

# 往期推荐

公众号ID:ikanxue
官方微博:看雪安全
商务合作:[email protected]

球分享

球点赞

球在看

点击“阅读原文”,了解更多!


文章来源: http://mp.weixin.qq.com/s?__biz=MjM5NTc2MDYxMw==&mid=2458379538&idx=2&sn=78a14562eeecd18be32f164060654fa8&chksm=b180d59886f75c8efabe4220fb918464662bb05c1c8e1592e5a4551ffd99887077d6774dcdef#rd
如有侵权请联系:admin#unsafe.sh