Mac下的pwn环境搭建。

Mac (ARM)
└── Docker (x86_64 userspace)
└── Ubuntu (16/20/22)
└── gdb + pwndbg
└── tmux 多窗口调试

利用Docker 卷挂载,把Mac本地的文件夹(/dapwn)映射到容器(/ctf)里,通过tmux将终端切分成多个窗口,并在容器内直接运行原生的 Linux。避开arm指令集不一致等问题。

0x00 ubuntu16.04

docker run -d \
--name pwn1604 \
--platform linux/amd64 \
--privileged \
--cap-add=SYS_PTRACE \
-v ~/dapwn:/ctf \
-it ubuntu:16.04 /bin/bash

进入镜像安装,工具都安装在了/root下:

# 一键把软件源替换为国内清华大学的 Ubuntu 历史归档源
sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list

#更新并安装语言包环境
apt-get update && apt-get install -y locales && locale-gen en_US.UTF-8

#将全局 UTF-8 编码写入环境变量
echo "export LANG=en_US.UTF-8" >> ~/.bashrc
echo "export LANGUAGE=en_US:en" >> ~/.bashrc
echo "export LC_ALL=en_US.UTF-8" >> ~/.bashrc

#上述设置生效
source ~/.bashrc

# 更新并安装基础包
apt update && apt-get install -y git gdb tmux wget curl build-essential python3-dev python3-pip python3-setuptools libc6-dbg

# 锁定 pip3 版本以兼容 Python 3.5
pip3 install --upgrade pip==20.3.4

# 安装 Pwntools
pip3 install pwntools

# 安装 Pwndbg (建议安装2020.07.23或更早之前版本)
https://github.com/pwndbg/pwndbg

# 补齐 Pwndbg 运行所需的全部 Python 依赖包
pip3 install -r /root/pwndbg-2020.07.23/requirements.txt

# pwndbg 从 2021 年初的代码开始,全面引入了 f-string(形如 f"{var}")等新语法,这些语法至少需要 Python 3.6+ 才能运行
cd pwndbg && ./setup.sh

#挂载gdb
echo "source /root/pwndbg-2020.07.23/gdbinit.py" > ~/.gdbinit

#测试
gdb /bin/ls

#安装peda
git clone https://github.com/longld/peda.git ~/peda
echo "source /root/peda/peda.py" >> ~/.gdbinit

0x01 ubuntu20.04

docker run -d \
--name pwn2004 \
--platform linux/amd64 \
--privileged \
--cap-add=SYS_PTRACE \
-v ~/dapwn:/ctf \
-it ubuntu:20.04 /bin/bash

更换源:

sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list

安装基础工具:

dpkg --add-architecture i386
apt-get update && apt-get install -y \
git gdb tmux wget curl build-essential \
python3-dev python3-pip python3-setuptools \
libc6:i386 libc6-dbg:i386 libc6-dbg \
libffi-dev libssl-dev patchelf

更新pip并换源:

pip3 install --upgrade pip
pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/

安装pwntools:

pip3 install pwntools

安装pwndbg和peda,我安装了2023.07.17,新版会因为Poetryuv引发各种错误。

git clone https://github.com/pwndbg/pwndbg
git checkout 2023.07.17
./setup.sh

#安装peda
git clone https://github.com/longld/peda.git ~/peda
echo "source /root/peda/peda.py" >> ~/.gdbinit

0x02 ubuntu22.04

docker run -d \
--name pwn2204 \
--platform linux/amd64 \
--privileged \
--cap-add=SYS_PTRACE \
-v ~/dapwn:/ctf \
-it ubuntu:22.04 /bin/bash

步骤同上 pwndbg可以安装最新版。

0x03 tmux

分屏操作:

左右分屏:Ctrl + b ➡️ %

上下分屏:Ctrl + b ➡️ "

关闭当前分屏:Ctrl + b ➡️ x (再按 y 确认) 或在当前分屏输入 exit / Ctrl + d

光标切换与大小调整:

切换到邻近分屏:Ctrl + b ➡️ 方向键 ↑ ↓ ← → (或鼠标直接点击)

临时最大化/恢复当前分屏:Ctrl + b ➡️ z

鼠标点击切换分屏,拖动调整大小:

# 1. 允许用鼠标点击切换分屏、拖动边界调整大小
echo "set -g mouse on" >> ~/.tmux.conf

# 2. 让配置立刻生效
tmux source-file ~/.tmux.conf