利用docker搭建基于debian9的路由器分析环境

一、工具链
  1. binwalk 用来解压固件包
  2. qemu 用来模拟运行mips架构的程序
  3. mips linux gcc 用来交叉编译mips架构下的可执行文件
  4. retdec 用来反编译mips架构下的可执行文件

Docker的安装和debian9镜像的拉取就不介绍的,自行百度

二、debian 9

1.更新源
# 备份原始更新源
mv /etc/apt/sources.list /etc/apt/sources.list.bak

# 替换为科大 阿里源
echo 'deb http://mirrors.ustc.edu.cn/debian/ stretch main non-free contrib
deb http://mirrors.ustc.edu.cn/debian/ stretch-updates main non-free contrib
deb http://mirrors.ustc.edu.cn/debian/ stretch-backports main non-free contrib
deb-src http://mirrors.ustc.edu.cn/debian/ stretch main non-free contrib
deb-src http://mirrors.ustc.edu.cn/debian/ stretch-updates main non-free contrib
deb-src http://mirrors.ustc.edu.cn/debian/ stretch-backports main non-free contrib
deb http://mirrors.ustc.edu.cn/debian-security/ stretch/updates main non-free contrib
deb-src http://mirrors.ustc.edu.cn/debian-security/ stretch/updates main non-free contrib
deb http://mirrors.aliyun.com/debian wheezy main contrib non-free
deb-src http://mirrors.aliyun.com/debian wheezy main contrib non-free
deb http://mirrors.aliyun.com/debian wheezy-updates main contrib non-free
deb-src http://mirrors.aliyun.com/debian wheezy-updates main contrib non-free
deb http://mirrors.aliyun.com/debian-security wheezy/updates main contrib non-free
deb-src http://mirrors.aliyun.com/debian-security wheezy/updates main contrib non-free' > /etc/apt/sources.list
2.基础环境
# python
apt-get update
apt-get install -y python2.7
apt-get install -y 
python3
#git
apt-get install -y git
#wget
apt-get install wget

三、binwalk
git clone https://github.com/ReFirmLabs/binwalk
python2.7 setup.py install
apt-get install -y python-lzma
# 完整安装需要运行deps.sh安装各种依赖
cd binwalk
./deps.sh

如果正常的话会发现sasquatch这个命令安装失败了( - -!),在用binwalk -Me xxx.bin 解包的时候squtchfs文件没法解压,这里的坑特别多。查看日志发现是在运行sudo ./build.sh时sudo命令找不到,原因debian 9 docker images缺少这个命令。解法方法是找到deps.sh文件中的

function install_sasquatch
{
    git clone https://github.com/devttys0/sasquatch
    (cd sasquatch && $SUDO ./build.sh)
    $SUDO rm -rf sasquatch
}

自己执行一遍。除了这个问题,sasquatch在kali linux中编译的时候也遇到过缺少某些头文件的错误,删除掉对应的include即可,总之编译这个库很多坑需要耐心解决。
这里提供一个dlink固件供测试

wget http://ftp.dlink.ru/pub/Router/DIR-300/Firmware/DIR-300A1_FW105b09.bin
binwalk -Me DIR-300A1_FW105b09.bin

四、qemu

安装通过如下命令

apt-get install binfmt-support
# 用户模式执行单个文件
apt-get install -y qemu-user-static
# 系统模式运行Mips系统
apt-get install -y qemu

测试需要先用binwalk解压DIR-300A1_FW105b09.bin这个固件,在当前文件夹下面会有_DIR-300A1_FW105b09.bin.extracted这个文件夹,进入squashfs-root执行

#将qemu用户模式执行文件拷贝到当前目录
cp $(which qemu-mips-static) ./
#切换根目录,执行ls
chroot . ./qemu-mips-static ./bin/ls

测试系统模式首先需要从debian-qemu-mips-images下载mips系统镜像,根据系统不一样有大端和小端的区分,因为DIR-300A1_FW105b09.bin固件是大端,因此下载mipsel文件夹里面的debian_wheezy_mips_standard.qcow2vmlinux-3.2.0-4-4kc-malta即可

随后运行

qemu-system-mips -M malta -kernel vmlinux-3.2.0-4-4kc-malta -hda debian_wheezy_mips_standard.qcow2 -append "root=/dev/sda1 console=tty0"  -nographic

即可启动qemu虚拟机,默认账号密码是root:root。
正常来说还需要搭建网桥,使qemu能和debian的宿主通信,我在虚拟机上配通了,但是在docker环境中的网络问题暂时没有解决。

五、mips linux gcc

#下载buildroot工具包
wget https://buildroot.org/downloads/buildroot-2018.02.tar.gz
#解压
tar -xvf buildroot-2018.02.tar.gz
#进入目录
cd buildroot-2018.02

# make menuconfig时需要使用到的库
apt-get install -y ncurses-dev

# make 时需要用到的库
apt-get install -y unzip 
apt-get install -y bc

# 接下来配置make, target options选择mips(大端)
# 或者mipsel(小端)接着 选择save 然后退出
make clean
make menuconfig

# 最后就是编译了
make

编译完成后可执行文件和依赖库都放在buildroot-2018.02/output下,我们可以将build删除,保留其他文件夹并配置环境变量方便调用。

测试代码

#include <stdio.h>

int main(){
    printf("hello world");
}
#编译
mipsel-linux-gcc hello.c -o hello.o -static

#在用qemu模拟运行
qemu-mipsel-static hello.o 

正常的话会输出hello world

六、retdec

# 安装所有的依赖库
apt-get install build-essential cmake git perl python3 bash bison flex autoconf automake libtool pkg-config m4 coreutils zlib1g-dev libtinfo-dev wget bc upx doxygen graphviz

# 拷贝项目 
git clone https://github.com/avast-tl/retdec
cd retdec
mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=<path>(path设置为可执行文件的路径)
make -jN (N设置为CPU的数量)
make install

测试反编译上面输出的hello.o文件

retdec-decompiler.sh hello.o

当前目录下可看到hello.o.c文件,这就是反编译后文件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值