Firmadyne学习笔记


项目地址:https://github.com/firmadyne/firmadyne

1. 简介及安装

FIRMADYNE is an automated and scalable system for performing emulation and dynamic analysis of Linux-based embedded firmware.

依赖qemu和binwalk,qemu的安装我放在下面的脚本里,binwalk可参考binwalk笔记

先把源码down下来,解压备用(/database/schema)。

安装Firmadyne依赖:

sudo apt-get install busybox-static fakeroot git dmsetup kpartx netcat-openbsd nmap python-psycopg2 python3-psycopg2 snmp uml-utilities util-linux vlan -y
# qemu 以mips为例
sudo apt-get install qemu-system-arm qemu-system-mips qemu-system-x86 qemu-utils -y
# database
sudo apt-get install postgresql -y
sudo -u postgres createuser -P firmadyne  # with password firmadyne
sudo -u postgres createdb -O firmadyne firmware
sudo -u postgres psql -d firmware < ./firmadyne/database/schema	# 创建table

# python依赖库
pip install python-magic

Firmadyne模拟工具包括以下部分:

  • 修改后的内核,可用于插桩, 有mips和arm;
  • libnvram.so,模拟固件启动时加载nvram配置信息的行为;
  • 固件提取器,原仓库已经去掉,有需要可以下个副本;
  • 用于调试的console
  • 用于下载固件的爬虫

执行源码目录下的download.sh安装以上二进制文件到binaries目录,考虑到网速问题,我是从gitee手动下载的。

设置firmadyne.config中的FIRMWARE_DIR为项目绝对路径。

scripts目录下的脚本,看文件名就知道作用。大部分会将结果保存到数据库中。

2. Demo

# 下载固件
wget http://www.downloads.netgear.com/files/GDC/WNAP320/WNAP320%20Firmware%20Version%202.0.3.zip -O netgear.zip


# 提取固件
sudo python3 ./sources/extractor/extractor.py -b Netgear -sql 127.0.0.1 -np -nk "netgear.zip" images/

解压后的文件系统存在/images/1.tar.gz压缩包里,1是本次工程的id,可以对它进行以下操作:

$ ./scripts/getArch.sh ./images/1.tar.gz
./bin/busybox: mipseb

# 存入数据库,table结构可参考/database/schema,或者github README
# 1是PRIMARY KEY
$ ./scripts/tar2db.py -i 1 -f ./images/1.tar.gz

为qemu system创建镜像,并分析:

# Create the QEMU disk image at /scratch
$ sudo ./scripts/makeImage.sh 1
...
$ ll scratch/1/
total 22232
drwxrwxrwx 3 root root     4096 416 11:11 ./
drwxr-xr-x 3 root root     4096 416 11:11 ../
drwxr-xr-x 2 root root     4096 416 11:11 image/
-rw-rw-rw- 1 root root 33554432 416 11:11 image.raw

# 自动判断项目1的网络结构
# 日志位于./scratch/1/qemu.initial.serial.log
# 生成run.sh用于运行qemu system
$ ./scripts/inferNetwork.sh 1
Querying database for architecture... Password for user firmadyne:
mipseb
Running firmware 1: terminating after 60 secs...
qemu-system-mips: terminating on signal 2 from pid 15577 (timeout)
Inferring network...
Interfaces: [('brtrunk', '192.168.0.100')]
Done!
# 运行qemu-system,脚本会自动桥接, 可以研究一下
# 登录账户root/password
$ ./scratch/1/run.sh

可以相互ping一下确认网络正常。

$ ifconfig
...
tap1_0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.99  netmask 255.255.255.0  broadcast 0.0.0.0
        inet6 fe80::5890:42ff:feab:4852  prefixlen 64  scopeid 0x20<link>
        ether 5a:90:42:ab:48:52  txqueuelen 1000  (Ethernet)
        RX packets 10  bytes 1154 (1.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 44  bytes 6889 (6.8 KB)
        TX errors 0  dropped 0 overruns 0  car

后续的所有信息都会保存在 ./scratch/1/qemu.final.serial.log

$ ping 192.168.0.100
PING 192.168.0.100 (192.168.0.100) 56(84) bytes of data.
64 bytes from 192.168.0.100: icmp_seq=1 ttl=64 time=0.583 ms
...

其他操作:

# 文件系统`scratch/1/image`的挂载与卸载:
./scripts/mount.sh 1
./scripts/umount.sh 1

# 调用snmpwalk(需要了解下snmp协议):
# 在日常监控中,经常会用到snmp服务, snmpwalk工具可以用来查询指定OID(SNMP协议中的对象标识)入口的所有OID树信息
$ ./analyses/snmpwalk.sh 192.168.0.100
Dumped to snmp.public.txt and snmp.private.txt!

# 获取可访问的web路径
# 可调试下源码学习一下
$ ./analyses/webAccess.py 1 192.168.0.100 log.txt

# 利用,需要安装msf
$ mkdir exploits; ./analyses/runExploits.py -t 192.168.0.100 -o exploits/exploit -e x

# nmap扫描
$ sudo nmap -O -sV 192.168.0.100
[sudo] password for starr:

Starting Nmap 7.60 ( https://nmap.org ) at 2022-04-16 11:49 CST
Nmap scan report for 192.168.0.100 (192.168.0.100)
Host is up (0.0085s latency).
Not shown: 997 closed ports
PORT    STATE SERVICE  VERSION
22/tcp  open  ssh      Dropbear sshd 0.51 (protocol 2.0)
80/tcp  open  http     lighttpd 1.4.18
443/tcp open  ssl/http lighttpd 1.4.18
...

访问一下http://192.168.0.100/index.php,可以正常访问。

分析完后,ctrl a+x关闭qemu。

3. 调试

Readme提供了3种方法:

  1. Full-system QEMU emulation: gdb server+gdb-multiarch/ida remote gdb debugger, 或ida的linux_server+remote linux debugger
  2. Full-system QEMU emulation, pass the -s -S parameters to QEMU and connect to the stub using target remote localhost:1234 from gdb-multiarch. However, the debugger won’t automatically know where kernel and userspace is in memory, so you may need to manually do add-symbol-file in gdb and break around try_to_run_init_process() in the kernel.
  3. User-mode QEMU emulation, 可参考另一篇笔记

第一种,把交叉编译的gdbserver考进qemu system,宿主机用gdb-multiarch调试。可以用python3 -m http.server配合wget拷进去。因为qemu是桥接网络,所以也能用windows ida远程调试。

第二种方法是使用qemu system的两个参数:

-S  Do not start CPU at startup (you must type 'c' in the monitor).
-s  Shorthand for -gdb tcp::1234, i.e. open a gdbserver on TCP port 1234.

但我试了一下,直接卡在启动流程了,按c也没用。

第三种方法,用Qemu user的话,需要运行多个固件加载运行后的程序,也很复杂。比如如果只是启动lighttpd的话,一访问php文件就会下载php文件,而不是返回网页。

4. 参考资料

firmadyne 详解 - So who are you (kms.app)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值