PWN环境搭建

环境安装总能捣鼓半天,同样的坑重复踩,干脆记录下来,方便后续回看。

VMware安装Ubuntu18虚拟机

安装包地址如下,先直接 down 下来,存在一个方便找的地方。

https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/18.04/ubuntu-18.04.6-desktop-amd64.iso

然后安装教程如下,特别要提示一下的就是,语言建议还是选英文,不要选汉语。

VMware安装Ubuntu18.04教程

安装好之后你会发现你的 Ubuntu18 窗口很小,而且不能虚拟机与主机之间不能进行复制粘贴。我在解决这俩问题的时候踩了一晚上的坑,网上很多 blog 的方法都试了遍,只能说真的水,没有一个能完美地同时解决上述两个问题的,要么就是窗口恢复了,但是不能复制粘贴,要么就是重启后能复制粘贴了,但是窗口又变小了…

解决方法是换源,当然,在此之前你肯定是得恢复你的复制粘贴功能的,不然手码 source.list 的话太难受了。先将 open-vm-tools 重新安装个。

sudo apt-get autoremove open-vm-tools
sudo apt-get install open-vm-tools
sudo apt-get install open-vm-tools-desktop
reboot

重启后再换源,我换的是清华源,真的 yyds,有不同需求的话可以看下面链接。

Ubuntu18.04更改国内镜像源(阿里云、网易、清华、中科大)_ubuntu18.04下载国内镜像

在桌面新建一个文件 source.list,然后将下面镜像内容复制到文件中。

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

然后执行命令(记得将下面 ??? 处改成自己的用户名。

sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
cd /etc/apt
sudo cp ../../home/???/Desktop/sources.list ./

换源后更新一下。

sudo apt-get update
sudo apt-get upgrade

设置一下你的 root 密码。

sudo passwd root

Ubuntu18虚拟机-PWN环境搭建

可以和 PWN环境搭建(萌新入门) - 鷺雨のBlog 一起食用。

安装 vim 编辑器。

sudo apt-get install vim

安装 python2 及其所有依赖项安装 pip,我比较习惯用 python2,用得顺手,用来做 pwn 题也完全够用了。

sudo apt install python
sudo apt install python-pip

安装 curl。

sudo apt install curl

装 pwntools 之类的。

sudo apt-get install python-dev libssl-dev libffi-dev build-essential
pip install --upgrade pwntools
pip install --upgrade pwn
pip install --upgrade zio
pip install pathlib2

测试下 pwntools 有无被正确安装,我的是这样测试的。

ve1kcon@ubuntu18:~$ python
Python 2.7.17 (default, Mar  8 2023, 18:40:28) 
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pwn
>>> pwn.asm('xor eax,eax')
'1\xc0'
>>> quit()

安装 git。

sudo apt install git

安装 pwndbg。

pwndbg 是调试程序时会经常用到的一个插件,除此之外还有 gef, peda 等插件,插件的安装使用方法参考 gdb调试入门指北 里的“GDB安装及其插件控制”

git clone https://github.com/pwndbg/pwndbg
cd pwndbg
sudo ./setup.sh

要是在执行 setup.sh 时报错了的话,试下先执行 git init,然后再跑一遍 setup.sh,还是不行的话重新搞个旧版的 pwndbg 文件…新版感觉有点bug。

安装 LibcSearcher。

git clone https://github.com/lieanu/LibcSearcher.git
cd LibcSearcher
rm -rf libc-database
git clone https://github.com/niklasb/libc-database.git
cd libc-database
./get ubuntu
python setup.py develop

在执行 ./get ubuntu 前可能的报错如下。

Will download or update for:
Requirements for download or update 'ubuntu' are not met. Please, refer to README.md for installation instructions

大概率是缺了 zstd,安装一下即可。

apt install zstd

WSL2安装Ubuntu

WSL2

在此基础上还可以整一台 wsl 的虚拟机,可以与本地的 vscode 联动,写代码很方便。

可以先去装一个 Windows Terminal,安装这个很方便,也能进行相应美化,哈哈。

image-20240409151732547

可以先通过下列文章了解 wsl,开启相应服务,并安装 wsl。可一同食用。

WSL 安装与使用|极客笔记 (deepinout.com)

超详细的wsl2下配置Ubuntu教程 - 知乎 (zhihu.com)

安装Ubuntu

如何就可以开始安装各版本 Ubuntu 了,查看所有可用的发行版。

wsl --list --online

安装特定版本的 Ubuntu。

wsl --install Ubuntu-22.04

进去之后设置用户名和密码即可。

WSL相关命令

列出已安装的 WSL 发行版的自定义名称、状态和版本号。

wsl -l -v

在 Windows 环境中使用 Linux 命令行工具和应用程序。

wsl -d Ubuntu-20.04

关闭 ubuntu。

wsl -t ubuntu

卸载子系统。

  1. 查看当前环境安装的wsl。

    wsl --list
    
  2. 注销(卸载)当前安装的 Linux 的 Windows 子系统(名称要与list获取的一致)。

    wsl --unregister Ubuntu-20.04
    

切换到第x个选项卡:ctrl + alt + x

切换到上一个选项卡:ctrl + shift + tab

切换到上一个选项卡:ctrl + tab

水平拆分窗格:alt + shift + -

垂直拆分窗格:alt + shift + plus

关闭窗格:ctrl + shift + w

打开设置界面:ctrl + alt + ,

Windows Terminal简单美化

点击设置左下角 “打开JSON文件”,在 profils 参数中修改,defaults 对所有终端进行设置,list 对指定终端进行设置。

        "defaults": 
        {
            "background": "#0C0C0C",
            "backgroundImage": "图片路径",
            "backgroundImageOpacity": 0.52		//调透明度0-1 值越小越透明
        }

后面可以点进相应终端,在 “其他设置 - 外观” 进行微调。

WSL相关报错

  1. 报错一。

    现象:

    > wsl --list --online
    无法从“https://raw.githubusercontent.com/microsoft/WSL/master/distributions/DistributionInfo.json”中提取列表分发。无法解析服务器的名称或地址
    Error code: Wsl/WININET_E_NAME_NOT_RESOLVED
    

    解决方法:

    Website Overview: Daily Visitors, Safety Status, Traffic Rank, Competitor Analysis (ipaddress.com) 的搜索框里输入 raw.githubusercontent.com,能查出几个 IP 地址,随便选一个。记事本打开 C:\Windows\System32\drivers\etc\hosts,添加一条 DNS 查询(使用自己查询到的 IP 来替换)。

    185.199.108.133 raw.githubusercontent.com
    
  2. 报错二。

    现象:使用 wsl 打开 ubuntu 系统时无任何回显,使用 wsl 相关指令时也没有反应。

    解决方法:

    把任务管理器里跟 wsl 有关的任务全部结束掉,然后再重新打开……

  3. 报错三。

    现象:

    资源不足无法完成请求的服务。

    解决方法:

    wls2空间释放]资源不足无法完成请求的服务。 Press any key to continue...process exited with code 4294967295 (0xffffffff)_CPT1024的博客-CSDN博客

    随着使用时间的延长,WSL2占用的硬盘空间会越来越多,这个时候就需要对其文件进行压缩。

    管理员的运行 Windows PowerShell,everything 搜一下要压缩的机子的 ext4.vhdx 的路径。

    > wsl --shutdown
    
    > diskpart
    DISKPART> select vdisk file="C:\Users\维\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx"
    DISKPART> attach vdisk readonly
    DISKPART> compact vdisk
    DISKPART> detach vdisk
    DISKPART> exit
    

    然后去提高内存上限,修改 C:\Users\username\.wslconfig 文件。

    [wsl2]
    memory=32GB
    
  4. 报错四。

    现象:

    出现错误 2147942402 (0x80070002) (启动“ubuntu2204.exe”时)]。

    解决方法:

    添加 C:\Users\username\AppData\Local\Microsoft\WindowsApps 到用户环境变量。

WSL2配置Ubuntu22

PWN相关

首先换源,更换清华源。

$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
$ sudo vim /etc/apt/sources.list

替换文件内容为以下内容。

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

更新源。

$ sudo apt update

python3 安装 pwntools。

$ sudo apt-get update
$ sudo apt-get install python3 python3-pip python3-dev git libssl-dev libffi-dev build-essential
$ sudo python3 -m pip install --upgrade pip
$ sudo python3 -m pip install --upgrade pwntools

checksec 发现报错如下。

$ checksec ./pwn
Traceback (most recent call last):
  File "/home/ve1kcon/.local/bin/checksec", line 8, in <module>
    sys.exit(main())
  File "/home/ve1kcon/.local/lib/python3.10/site-packages/pwnlib/commandline/common.py", line 29, in main
    import pwnlib.commandline.main
  File "/home/ve1kcon/.local/lib/python3.10/site-packages/pwnlib/commandline/main.py", line 5, in <module>
    from pwnlib.commandline import asm
  File "/home/ve1kcon/.local/lib/python3.10/site-packages/pwnlib/commandline/asm.py", line 11, in <module>
    from pwn import *
  File "/home/ve1kcon/.local/lib/python3.10/site-packages/pwn/__init__.py", line 4, in <module>
    from pwn.toplevel import *
  File "/home/ve1kcon/.local/lib/python3.10/site-packages/pwn/toplevel.py", line 23, in <module>
    from pwnlib import *
  File "/home/ve1kcon/.local/lib/python3.10/site-packages/pwnlib/dynelf.py", line 57, in <module>
    from pwnlib import elf
  File "/home/ve1kcon/.local/lib/python3.10/site-packages/pwnlib/elf/__init__.py", line 9, in <module>
    from pwnlib.elf.corefile import Core
  File "/home/ve1kcon/.local/lib/python3.10/site-packages/pwnlib/elf/corefile.py", line 79, in <module>
    from elftools.common.py3compat import bytes2str
ModuleNotFoundError: No module named 'elftools.common.py3compat'

原因:

高版本弃用 py3compat 模块。

解决方法:

参考 install · Issue #2262 · Gallopsled/pwntools · GitHub

修改对应文件第79行,原本为:from elftools.common.py3compat import bytes2str

$ sudo vim /home/ve1kcon/.local/lib/python3.10/site-packages/pwnlib/elf/corefile.py

修改结果如下。

from elftools.common.utils import bytes2str

安装 python2。

$ sudo apt-add-repository universe
$ sudo apt update
$ sudo apt install python2-minimal
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1
$ sudo apt install python-pip

安装 curl。

$ sudo apt install curl

给 python2 装 pwntools 之类的。

$ sudo apt-get install libssl-dev libffi-dev build-essential
$ sudo pip2 install pathlib2
$ python -m pip install --upgrade pwntools
$ sudo pip2 install -U pwntools==4.8.0b0

测试下 pwntools 有无被正确安装,我的是这样的。

$ python
Python 2.7.18 (default, Jul  1 2022, 10:30:50)
[GCC 11.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pwn
>>> pwn.asm('xor eax,eax')
'1\xc0'
>>> quit()

安装 pwndbg。

pwndbg 是调试程序时会经常用到的一个插件,除此之外还有 gef, peda 等插件

cd ~
git clone https://github.com/pwndbg/pwndbg.git
cd ~/pwndbg
sudo ./setup.sh
 
cd ~
git clone https://github.com/scwuaptx/Pwngdb.git

然后编辑配置文件。

vim ~/.gdbinit

下下列文本复制到配置文件中。

source ~/peda/peda.py
source ~/pwndbg/gdbinit.py
source ~/Pwngdb/pwngdb.py
source ~/Pwngdb/angelheap/gdbinit.py

set history save off

define hook-run
python
import angelheap
angelheap.init_angelheap()
end

安装其他插件。peda, gef...

cd ~
git clone https://github.com/yichen115/GDB-Plugins
cd GDB-Plugins/pwndbg
sudo git init
sudo ./setup.sh

插件切换。

# pwndbg
$ cp ~/Pwngdb/.gdbinit ~/
# gef
$ echo "source ~/GDB-Plugins/gef/gef.py" > ~/.gdbinit
# peda
$ echo "source ~/GDB-Plugins/peda/peda.py" > ~/.gdbinit

使用 peda 时发现会出现一大堆的警告,强迫症的看了肯定会很不舒服,解决方法就是把相应行的 "is" 换成 "==" 即可。

peda 关闭 peda-session 文件生成。

vim ~/GDB-Plugins/peda/lib/config.py

安装 one_gadget。

$ sudo apt -y install ruby
$ sudo gem install one_gadget

安装 seccomp-tools,用来查沙箱的。

$ sudo apt install gcc ruby-dev
$ sudo gem install seccomp-tools

安装 glibc_all_in_one。

$ cd ~
$ sudo git clone https://github.com/matrix1001/glibc-$ all-in-one.git
$ cd glibc-all-in-one/
$ sudo python update_list
$ sudo pip2 install requests
$ sudo python update_list

安装 zstd,否则有些 libc 解压不过来。

下这个项目 https://github.com/kunpengcompute/zstd,然后进入文件夹执行。

$ sudo make
$ sudo make install

安装 patchelf,然后能正确查看到 patchelf 版本说明安装成功。

$ cd ~
$ git clone https://github.com/NixOS/patchelf.git
$ cd patchelf
$ sudo apt-get install autoconf automake libtool
$ sh bootstrap.sh
$ ./configure
$ sudo make
$ sudo make install
$ patchelf --version

安装 libcsearcher。

$ cd ~
$ git clone https://github.com/lieanu/LibcSearcher.git
$ cd LibcSearcher
$ sudo rm -rf libc-database
$ git clone https://github.com/niklasb/libc-database.git
$ cd libc-database
$ sudo ./get ubuntu
$ cd ..
$ sudo python setup.py develop

tmux

tmux 是一个挺好用、功能很丰富的终端复用器,安装命令如下。

$ sudo apt-get install tmux

按住 shift 就能像正常终端那样右键复制粘贴,滚轮滚上去之后双击两下鼠标左键又能重新回到最下方的命令行进行输入。

如果要搭配 pwntools 的 gdb.attach 来使用,需要在 exp 开头加一句即可,然后进入 tmux 来跑脚本。

context.terminal = ["tmux", "splitw", "-h"]

快捷键使用。

呼唤键 +

↑/↓/←/→:焦点聚集到上/下/左/右边的窗格

%:分成左右两个窗格

":分成上下两个窗格

x:删除当前窗格

{:左移当前窗格

}:右移当前窗格

z:全屏显示当前窗格,再按一次恢复

t:在当前窗格显示时间

q:显示窗格编号

说一下我遇到的相关问题,并给出解决方案。

tmux 的默认前缀快捷键是 Ctrl+b,且无法使用滚轮查看前面的内容,对我来说用得有点不习惯,我想把它改成 Alt+a,操作如下,先退出 tmux。

$ sudo vim /etc/tmux.conf

直接添加如下配置。

set -g mouse on

set -g prefix M-a
unbind C-b
bind M-a send-prefix

修改完后,使之生效。

$ tmux source-file /etc/tmux.conf

要对状态栏进行美化的话,也是修改 tmux.conf,我的配置如下。

# 右下角类似效果:21:58:48 12-12
set -g status-right "%H:%M:%S %d-%b"

# 设置整个状态栏背景颜色 bg(背景色) fg(前景色)
set -g status-style "bg=#202529"

# 设置状态栏
set -g status-left "Hi,ve1."

# 会话计数:从 1 开始(Setting base-index assures newly created windows start at 1 and count upwards)
set -g base-index 1
# 窗口计数:从 1 开始编号,而不是从 0 开始
set -g pane-base-index 1

set -g status-interval 1    # 状态栏刷新时间(右下角秒针会跳动)
set -g status-justify left  # 状态栏窗口列表(window list)左对齐

set -g visual-activity on # 启用活动警告
set -wg monitor-activity on # 非当前窗口有内容更新时在状态栏通知
set -g message-style "bg=#202529, fg=#91A8BA" # 指定消息通知的前景、后景色

set -wg window-status-current-format " #I:#W#F " # 状态栏当前窗口名称格式(#I:序号,#w:窗口名 称,#F:间隔符)
set -wg window-status-current-style "fg=#d7fcaf,bg=#60875f" # 状态栏当前窗口名称的样式
set -wg window-status-separator "" # 状态栏窗口名称之间的间隔

set -g mouse on

set -g prefix M-a
unbind C-b
bind M-a send-prefix

分享链接。

[分享] pwndbg+tmux真乃天作之合-Pwn-看雪-安全社区|安全招聘|kanxue.com

terminator

也是终端复用器,相比起 tmux,这个也没那么推荐吧,自行选择。

安装terminator。

sudo apt-get install terminator

安装 dconf-tools 来设置快捷键(Ctrl+Alt+T)默认打开的终端为 terminator。

sudo apt-get install dconf-tools

打开 dconf-tools。

dconf-editor

路径。

org > gnome > desktop > terminal
org > gnome > desktop > applications > terminal

修改配置。

exec x-terminal-emulator
exec-arg -e

快捷键。

Ctrl+Shift+E    垂直分割窗口
Ctrl+Shift+O    水平分割窗口
F11         	全屏
Ctrl+Shift+C    复制
Ctrl+Shift+V    粘贴
Ctrl+Shift+N    或者 Ctrl+Tab 在分割的各窗口之间切换
Ctrl+Shift+X    将分割的某一个窗口放大至全屏使用
Ctrl+Shift+Z    从放大至全屏的某一窗口回到多窗格界面

美化。

terminator 背景板处右键 --> Preferences (首选项) --> Profiles --> Add,随便添加一个配置,添加完后删掉就行,这样应该就会有 ~/.config/terminator/config 这个文件了。

然后输命令。

cd ~/.config/terminator/ 
sudo gedit config

配置文件内容如下。

[global_config]
  enabled_plugins = CustomCommandsMenu, LaunchpadCodeURLHandler, APTURLHandler, LaunchpadBugURLHandler
  handle_size = -3
  inactive_color_offset = 1.0
  suppress_multiple_term_dialog = True
  title_transmit_bg_color = "#3e3838"
  title_transmit_fg_color = "#000000"
[keybindings]
[layouts]
  [[default]]
    [[[child1]]]
      parent = window0
      profile = default
      type = Terminal
    [[[window0]]]
      parent = ""
      size = 925, 570
      type = Window
[plugins]
[profiles]
  [[default]]
    background_color = "#1e1e1e"
    background_darkness = 0.8
    background_image = None
    background_type = transparent
    cursor_color = "#e8e8e8"
    cursor_shape = ibeam
    font = Ubuntu Mono 14
    foreground_color = "#e8e8e8"
    palette = "#292424:#5a8e1c:#2d5f5f:#cdcd00:#1e90ff:#cd00cd:#00cdcd:#d6d9d4:#4c4c4c:#868e09:#00ff00:#ffff00:#4682b4:#ff00ff:#00ffff:#ffffff"
    scroll_background = False
    scrollback_lines = 3000
    show_titlebar = False
    use_system_font = False

“打快照”

最后的最后,建议备份下你配置好的这个系统,这样如果机子被你玩坏后可以进行回滚,当然也适合迁移,免得以后要重复配置。

在 cmd 终端里,终止正在运行的wsl。

> wsl --shutdown

首先确定发行版。

> wsl -l -v

导出所选发行版的备份,最后填备份文件的保存路径及文件名称,如 D:\linux\wsl2_ubuntu.tar

> wsl --export Ubuntu-22.04 /dir/wsl2_ubuntu22.tar

假如是对机子进行迁移的话,在导出完成之后,多加一下几步,先将原有的分发进行卸载。

> wsl --unregister Ubuntu

还原备份,先填解压出的文件夹位置,再填备份压缩包的位置。

> wsl --import Ubuntu-22.04 /dir/wsl2_ubuntu22/ /dir/wsl2_ubuntu22.tar --version 2
posted @ 2024-04-09 16:10  ve1kcon  阅读(129)  评论(0编辑  收藏  举报