【揭秘家用路由器0day】漏洞分析环境搭建 - binwalk

操作系统环境信息请查看第一篇文章 【揭秘家用路由器0day】Wine + IDA环境搭建

apt 安装 binwalk

binwalk 可以直接通过 apt 安装:

sudo apt install build-essential autoconf git
sudo apt install binwalk

安装固件提取组件

但是装完之后没有办法正确分析书中自带的 firmware 程序,因此重新安装一遍书中提到的固件提取组件

sudo apt-get install mtd-utils zlib1g-dev liblzma-dev gzip bzip2 tar arj lhasa p7zip p7zip-full cabextract cramfsswap 

删除了 cramfsprogsopenjdk-6-jdk,这两个找不到。

其中jdk 版本太低了,因为不确定后面的分析会不会对 JAVA 版本有要求,所以暂时先不安装 jdk,cramfsprogs 可以自己下载安装包安装

wget http://launchpadlibrarian.net/251826685/cramfsprogs_1.1-6ubuntu1_amd64.deb
sudo dpkg -i cramfsprogs_1.1-6ubuntu1_amd64.deb

删除了 squashfs-tools,后面会说明原因

安装 sasquatch

然后安装 sasquatch SquashFS 提取工具

sudo apt install liblzo2-dev
git clone https://github.com/devttys0/sasquatch.git
cd sasquatch
./build.sh

注意这个工具的安装命令和书里的不一样,因为它后面又更新了。在 build.sh 这个脚本中,会自己安装 squashfs-tools 这个工具并进行 patch,但是它安装的是 4.3 版本的,如果通过 apt 进行安装,版本是 4.5。

这里我们执行 build.sh 选择安装 4.3 版本的 squashfs-tools

执行之后提示错误:

......
patching file squashfs-tools/squashfs_fs.h
patching file squashfs-tools/unsquashfs.c
Hunk #1 succeeded at 32 with fuzz 1.
cc -g -O2  -I. -I./LZMA/lzma465/C -I./LZMA/lzmalt -I./LZMA/lzmadaptive/C/7zip/Compress/LZMA_Lib -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE -DCOMP_DEFAULT=\"gzip\" -Wall -Werror  -DGZIP_SUPPORT -DLZMA_SUPPORT -DXZ_SUPPORT -DLZO_SUPPORT -DXATTR_SUPPORT -DXATTR_DEFAULT   -c -o unsquashfs.o unsquashfs.c
unsquashfs.c: In function ‘read_super’:
unsquashfs.c:1835:5: error: this ‘if’ clause does not guard... [-Werror=misleading-indentation]
 1835 |     if(swap)
      |     ^~
unsquashfs.c:1841:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
 1841 |         read_fs_bytes(fd, SQUASHFS_START, sizeof(struct squashfs_super_block),
      |         ^~~~~~~~~~~~~
cc1: all warnings being treated as errors
make: *** [: unsquashfs.o] Error 1

从提示看应该已经 patch 完,开始进行编译了,但是由于编译器版本过高,检查比较严格,因此报错。

此时我们应该在 sasquatch 目录,执行:

cd squashfs-tools
cp Makefile Makefile.bak
vim Makefile

找到:

# CJH: Added -g, -Werror and -DSQUASHFS_TRACE

修改下面的 CFLAGS,完整内容如下:

CFLAGS ?= -g -O2
CFLAGS += $(EXTRA_CFLAGS) $(INCLUDEDIR) -D_FILE_OFFSET_BITS=64 \
        -D_LARGEFILE_SOURCE -D_GNU_SOURCE -DCOMP_DEFAULT=\"$(COMP_DEFAULT)\" \
        -Wall -fcommon #-DSQUASHFS_TRACE

修改的地方是把最后的 -Werror 修改成了 -fcommon

最后执行

make & sudo make install

安装 capstone

因为书中同时也安装了 capstone,所以这里顺便也安装了

apt 安装的版本比较低,通过源码进行安装

git clone https://github.com/capstone-engine/capstone.git
cd capstone
./make.sh nix32

错误提示:

In file included from utils.c:8:
/usr/include/stdlib.h:26:10: fatal error: bits/libc-header-start.h: No such file or directory
   26 | #include 
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
......

执行:

sudo apt install gcc-multilib
./make.sh nix32
sudo ./make.sh install

测试

使用 binwalk 分析第八章中的文件 firmware.bin

ubuntu@ubuntu-VM:~/resources/8$ binwalk -e firmware.bin

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             DLOB firmware header, boot partition: "dev=/dev/mtdblock/2"
112           0x70            LZMA compressed data, properties: 0x5D, dictionary size: 33554432 bytes, uncompressed size: 4237652 bytes
1441904       0x160070        PackImg section delimiter tag, little endian size: 2121216 bytes; big endian size: 6168576 bytes

WARNING: Symlink points outside of the extraction directory: /home/ubuntu/resources/8/_firmware.bin.extracted/squashfs-root/tmp -> /var/tmp; changing link target to /dev/null for security purposes.

......

1441936       0x160090        Squashfs filesystem, little endian, version 4.0, compression:lzma, size: 6164554 bytes, 2205 inodes, blocksize: 262144 bytes, created: 2013-06-14 07:05:15

ubuntu@ubuntu-VM:~/resources/8$ ls
a.f    exe.f1  extract.conf  filesystems-hsqs  firmware.bin             magic.file  squashfs1
exe.f  exe.f2  filesystems   firmware          _firmware.bin.extracted  squashfs
ubuntu@ubuntu-VM:~/resources/8$ cd _firmware.bin.extracted/
ubuntu@ubuntu-VM:~/resources/8/_firmware.bin.extracted$ ls
160090.squashfs  70  70.7z  squashfs-root
ubuntu@ubuntu-VM:~/resources/8/_firmware.bin.extracted$ cd squashfs-root/
ubuntu@ubuntu-VM:~/resources/8/_firmware.bin.extracted/squashfs-root$ ls
bin  dev  etc  home  htdocs  include  lib  mnt  proc  sbin  sys  tmp  usr  var  www

可以看到已经能够正常分析,其中输出了很多 WARNING,不影响结果,我用省略号代替了。

你可能感兴趣的:(路由器安全,安全,linux)