物聯網設備固件分析: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

... ...

  1. 安裝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

  1. 修改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"

... ...

  1. 下載要模擬的路由器固件
  • 要模擬的路由器爲:WNAP320

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

  • 假設我這裏將固件文件重命名爲 netgear.zip,並放在/home/hzy/firmware/目錄下

  1. 測試運行

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。這篇文章就點到爲止啦。

爲了裝這個分析環境,前前後後折騰了三個虛擬機。裝軟件和配環境是我的一生之敵!!(ಥ_ಥ)

By:hzy

0x05 參考資料

  1. 物聯網設備的固件模擬環境搭建
  2. 嵌入式Linux固件模擬與安全分析系統Firmadyne交流
  3. Error:Traceback.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章