在分析一些嵌入式固件的时候,我们往往需要模拟一个跨平台的运行环境,只有这样才能解决嵌入式固件模拟运行的基本条件

设:

  • 目前希望在x86平台上模拟运行完整的mips系统环境

解:

  1. 首先准备一个 mips架构的 linux系统,这里选择使用 debian的mips分支
  2. 使用qemu在x86平台上模拟运行 debian.mips
  3. 在模拟出来的 mips系统中运行 目标程序

模拟完整的 debian.misp

这里主要介绍下 qemu配置mips虚拟机的原理,核心是让 mips虚拟机运行在宿主机的 同级网络

  1. 首先 将宿主机的 外网网卡配置为 桥接网卡
  2. 让 qemu 开出来的虚拟机 直接绑定到 桥接的网卡
  3. 基于 ubuntu 18 x64 server
  4. 本实验环境中 qemu自动将 新创建的tap绑定到 虚拟交换机上
  5. 考虑到兼容性问题,尽可能用 命令行来实现所有配置:happy:
  6. 基于ubuntu18 lts
  7. debian提供的交叉虚拟机

0x1 创建桥接网卡

安装 桥接 需要的软件包

apt-get install bridge-utils uml-utilities

桥接网卡:将网卡 eth0 桥接到 虚拟交换机 br0

str_br_name="br0"
str_nic="eth0"

#创建 虚拟交换机 
brctl addbr $str_br_name     

#将 网卡 str_nic 绑定到虚拟交换机 str_br_name (可能让网络断掉,最好在本机操作,不要通过网络)     
brctl addif $str_br_name $str_nic

#将 str_br_name 设置为启用STP协议
brctl stp $str_br_name on         
 
#将 str_nic 的IP设置为 0     
ifconfig $str_nic 0 
 
#将 恢复宿主机的 网络链接      
dhclient $str_br_name              

脚本运行的时间可能会长达数十秒,执行完成后应该可以恢复网络连接

0x2 启动虚拟机

  • 这里直接采用debian 封装的镜像来 测试 配置是否完善
  • 这个镜像十分干净,只能作为测试系统使用
  • 下一章中会介绍如何 手动 配置最新的镜像

0x2.1 下载镜像

  • 首先下载debian提供的镜像
  • 账号密码都是root
# 总链接,网页里有详细的说明(账号密码,官方的说明)
https://people.debian.org/~aurel32/qemu/mips/

# 内核
wget https://people.debian.org/~aurel32/qemu/mips/vmlinux-3.2.0-4-4kc-malta

# debian 9 磁盘镜像
wget https://people.debian.org/~aurel32/qemu/mips/debian_wheezy_mips_standard.qcow2

0x2.2 启动虚拟机

创建的虚拟机会自动连接到之前创建的虚拟交换机 br0

qemu-system-mips -no-reboot \
-M malta \
-m 256 \
-kernel vmlinux-3.2.0-4-4kc-malta \
-hda debian_wheezy_mips_standard.qcow2 \
-append "root=/dev/sda1 console=tty0" \
-net nic \
-net tap \
-nographic

参数说明

-m 设置内存

-net nic 自动在 在宿主机上为每个虚拟机 都创建一个 网卡

-net tap 自动创建的名卡名称 前缀

启动后会直接显示控制台

image-20200303140913238.png

0x3 修复源列表

debian提供的镜像是基于debian7的,镜像已经无法使用apt-get来安装软件了,需要自己修复源列表

echo "deb http://archive.debian.org/debian/ wheezy main contrib non-free" >/etc/apt/sources.list
Last modification:February 5, 2021
如果觉得我的文章对你有用,请随意赞赏