Firmadyne固件模拟路由器环境搭建

0x01 前言

本文介绍了在对固件进行分析的环境准备部分,主要是对Firmadyne这个工具的环境搭建,最后搭建完用Netgear的路由器固件进行测试。

安装完后测试了一下,在我测试的几个固件中(Tenda路由器,Cisco交换机、防火墙、路由器,D-Link路由器),目前只能运行大部分的路由器的固件,交换机和防火墙的固件都运行不起来。

Firmadyne是一款自动化分析嵌入式Linux系统安全的开源软件,由卡内基梅隆大学的Daming D. Chen开发完成的。它支持批量检测,整个系统包括固件的爬取、root文件系统的提取、QEMU模拟执行以及漏洞的挖掘。

实验环境及需要的工具:

  • 系统:Ubuntu14.04
    • 在Kali最新版和Ubuntu16.04LTS上都会有奇奇怪怪的问题,后来在issue中找到作者的实验环境为Ubuntu14.04
    • 下载:http://mirrors.ustc.edu.cn/ubuntu-releases/14.04/
  • 工具:
    • Firmadyne
      • 项目地址:https://github.com/firmadyne/firmadyne
      • README.md中有详细的配置和安装步骤
    • Firmware Analysis Toolkit
      • 项目地址:https://github.com/attify/firmware-analysis-toolkit
      • 该工具集包含了binwalkFirmadyne等必须的工具。这里我们只需要克隆该仓库到本地即可
    • qemu
      • 可以直接用apt-get安装,只安装一部分
      • 也可以从github的仓库编译安装所有的模块

0x02 环境配置

2.1 克隆Firmware Analysis Toolkit工具集仓库

# 1. 安装依赖
sudo apt-get install busybox-static fakeroot git dmsetup kpartx netcat-openbsd nmap python-psycopg2 python3-psycopg2 snmp uml-utilities util-linux vlan

# 2. clone 
git clone --recursive https://github.com/attify/firmware-analysis-toolkit.git

2.2 安装binwalk

# 1. 安装依赖和binwalk
cd firmware-analysis-toolkit/binwalk
sudo ./deps.sh
sudo python setup.py install

# 2. 对于 python2.x,还需要安装以下的库
sudo -H pip install git+https://github.com/ahupp/python-magic
sudo -H pip install git+https://github.com/sviehb/jefferson

测试是否安装成功:

hzy@ubuntu:~$ binwalk 

Binwalk v2.1.2-c036535
Craig Heffner, ReFirmLabs
https://github.com/ReFirmLabs/binwalk

Usage: binwalk [OPTIONS] [FILE1] [FILE2] [FILE3] ...

Disassembly Scan Options:
    -Y, --disasm                 Identify the CPU architecture of a file using the capstone disassembler

... ...


    -s, --status=<int>           Enable the status server on the specified port

hzy@ubuntu:~$ 

2.3 安装Firmadyne

     1、进入Firmadyne目录,然后打开firmadyne.config,修改 FIRMWARE_DIR的路径为当前Firmadyne目录的绝对路径

cd firmware-analysis-toolkit/firmadyne

vim firmadyne.config

# 以下为firmadyne.config中的内容
#!/bin/sh

# uncomment and specify full path to FIRMADYNE repository
FIRMWARE_DIR=/home/hzy/firmware-analysis-toolkit/firmadyne/

# specify full paths to other directories
BINARY_DIR=${FIRMWARE_DIR}/binaries/
TARBALL_DIR=${FIRMWARE_DIR}/images/
SCRATCH_DIR=${FIRMWARE_DIR}/scratch/
SCRIPT_DIR=${FIRMWARE_DIR}/scripts/

# functions to safely compute other paths

... ...
————————————————

    2、安装Firmadyne

sh ./download.sh

2.4 安装postgresql数据库

sudo apt-get install postgresql

# 用户的密码设置为:firmadyne
sudo -u postgres createuser -P firmadyne, with password firmadyne

sudo -u postgres createdb -O firmadyne firmware

# 注意这里的数据库文件是在firmadyne/目录下,也就是该命令要在根目录firmware-analysis-toolkit/目录下执行
sudo -u postgres psql -d firmware < ./firmadyne/database/schema

启动postgresql数据库,确认其正在运行。

  • 这里我在kali测试的时候,如果没有先启动数据库,就进行添加用户的命令会报错。
  • 还遇到了一个问题,明明数据库服务在运行,但是添加用户的时候一直报的错也是服务没有运行的错,这种情况下我是直接重装了一遍postgresql
  • 具体出现的错大家可以在网上搜索解决方案即可。一般还是有现成的方法的。

sudo service postgresql start

sudo service postgresql status

2.5 安装qemu

QEMU是一套由法布里斯·贝拉(Fabrice Bellard)所编写的以GPL许可证分发源码的模拟处理器,在GNU/Linux平台上使用广泛。

这里有两种安装方法:

  • 直接通过apt-get安装
sudo apt-get install qemu-system-arm qemu-system-mips qemu-system-x86 qemu-utils
  • 编译安装
git clone git://git.qemu.org/qemu.git
cd qemu
git submodule init
git submodule update --recursive
apt install libglib2.0 libglib2.0-dev
apt install autoconf automake libtool
./configure 
make
make install

在执行命令./configure以后可能会报错:

ERROR: pixman >= 0.21.8 not present.
       Please install the pixman devel package.

可以通过执行命令:apt-get install libpixman-1-dev解决

0x03 测试运行

    1、将firmware-analysis-toolkit/目录下的fat.py和reset.py移动到firmadyne/目录下:

mv fat.py ./firmadyne
mv reset.py ./firmadyne

    2、修改fat.py中的执行权限、firmadyne的路径firmadyne_path以及root密码root_pass

chmod +x fat.py

vim fat.py
# 以下是fat.py中的内容

#!/usr/bin/env python2.7

import os
import pexpect
import sys

# Put this script in the firmadyne path downloadable from
# https://github.com/firmadyne/firmadyne

#Configurations - change this according to your system
firmadyne_path = "/home/hzy/firmware-analysis-toolkit/firmadyne"
binwalk_path = "/usr/local/bin/binwalk"
root_pass = "123456"
firmadyne_pass = "firmadyne"

... ...

    3、下载要模拟的路由器固件

    要模拟的路由器为:WNAP320

    在https://www.netgear.com/support/product/WNAP320.aspx#Firmware%20Version%203.7.11.4中下载固件文件

    假设我这里将固件文件重命名为 netgear.zip,并放在/home/hzy/firmware/目录下
 

    4、测试运行

hzy@ubuntu:~/firmware-analysis-toolkit/firmadyne$ sudo ./fat.py 
[sudo] password for hzy: 

                               __           _   
                              / _|         | |  
                             | |_    __ _  | |_ 
                             |  _|  / _` | | __|
                             | |   | (_| | | |_ 
                             |_|    \__,_|  \__|                    
                    
                Welcome to the Firmware Analysis Toolkit - v0.2
    Offensive IoT Exploitation Training  - http://offensiveiotexploitation.com
                  By Attify - https://attify.com  | @attifyme
    
[?] Enter the name or absolute path of the firmware you want to analyse : /home/hzy/firmware/netgear.zip
[?] Enter the brand of the firmware : Netgear
[+] Now going to extract the firmware. Hold on..
[+] Firmware : /home/hzy/firmware/netgear.zip
[+] Brand : Netgear
[+] Database image ID : 2
[+] Identifying architecture
[+] Architecture : mipseb
[+] Storing filesystem in database
[!] Filesystem already exists
[+] Building QEMU disk image
[+] Setting up the network connection, please standby
[+] Network interfaces : [('brtrunk', '192.168.0.100')]
[+] Running the firmware finally
[+] command line : sudo /home/hzy/firmware-analysis-toolkit/firmadyne/scratch/2/run.sh
[*] Press ENTER to run the firmware...

[+] Network interfaces : [('brtrunk', '192.168.0.100')]可以看到,启动了一个服务,可以通过192.168.0.100访问

接下里按Enter键运行该固件,等运行完了以后就可以从浏览器访问了:

在这里插入图片描述

0x04 总结

主要还是参考了参考资料1,但是中间踩了很多坑,可以查阅参考资料3。

这个firmadyne还有其他的用法,可以参考下参考资料2。

0x05 参考资料

  1. 物联网设备的固件模拟环境搭建
  2. 嵌入式Linux固件模拟与安全分析系统Firmadyne交流
  3. Error:Traceback.
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值