搭建iot动态调试环境

0x01软件安装交叉编译链

安装交叉编译链(mips)

sudo apt-get install linux-libc-dev-mips-cross 
sudo apt-get install libc6-mips-cross libc6-dev-mips-cross 
sudo apt-get install binutils-mips-linux-gnu gcc-mips-linux-gnu 
sudo apt-get install g++-mips-linux-gnu

mipsel

sudo apt-get install linux-libc-dev-mipsel-cross
sudo apt-get install libc6-mipsel-cross libc6-dev-mipsel-cross
sudo apt-get install binutils-mipsel-linux-gnu gcc-mipsel-linux-gnu
sudo apt-get install g++-mipsel-linux-gnu

0x02配置网络
安装依赖

sudo apt-get install bridge-utils uml-utilities

/etc/network/interfaces文件里面写入

推荐用ubuntu16.04 如果网卡名不是eth0换成对应网卡名

auto lo
iface lo inet loopback
 
auto eth0 
iface eth0 inet dhcp 
 
#auto br0 
iface br0 inet dhcp 
  bridge_ports eth0 
  bridge_maxwait 0

在/etc/qemu-ifup文件开始添加

#!/bin/sh
echo "Executing /etc/qemu-ifup"
echo "Bringing $1 for bridged mode..."
sudo /sbin/ifconfig $1 0.0.0.0 promisc up
echo "Adding $1 to br0..."
sudo /sbin/brctl addif br0 $1
sleep 3

加权限,重启网卡

sudo chmod a+x /etc/qemu-ifup
sudo /etc/init.d/networking restart
ifdown eth0
ifup eth1

注:网桥相当于一个交换机,虚拟网桥会和本机eth0绑定,其作用:
1、qemu虚拟机和主机之间通信
2、虚拟网桥ip就作为本机ip(外部访问本机要用虚拟网桥ip比如ssh)

关于网络的详细配置的补充后面将会有
——————带填充链接

虚拟机里面运行

ifconfig eth0 192.168.0.1

(后面跟的ip需要跟主机ip在同一网段)

0x03启动qemu

安装qemu

apt install qemu-system-mips 

下载内核和磁盘镜像文件
https://people.debian.org/~aurel32/qemu
说明:mipsel表示表示32位小端(也是LSB),mips表示32位大端(也是MSB)
下载的内核文件为 vmlinux-3.2.0-4-4kc-malta,磁盘镜像文件为debian_squeeze_mipsel_standard.qcow2。
内核需要下载比较新的版本,不然后面gdbserver放进去会报错。

qemu-system-mips -M malta -kernel vmlinux-3.2.0-4-4kc-malta -hda debian_squeeze_mips_standard.qcow2 -nographic -append "root=/dev/sda1 rw console=tty0 init=/linuxrc ignore_loglevel" -net nic,vlan=0 -net tap,vlan
=0,ifname=tap0 -redir tcp:2333::2333 -redir tcp:8080::80

命令讲解

-kernel: 指定kernel文件。
-dtb: 指定dtb(Device Tree Blob)文件,假如kernel包含dtb。
-cpu: 指定cpu类型。(qemu-system-arm -M highbank -cpu help查看可选项,(这个可能版本有bug, 必须指定-M才能查看))
-m: 指定内存大小。
-M: 制定机器类型。 (qemu-system-arm -machine help查看可选项)
-serial: 重定向串口。-append: 为kernel指定启动参数。
-hda: 指定磁盘镜像。
-net nic \ #使用默认NAT方式连接网络 
-net user,hostfwd=tcp::5022-:22 \# 为 ssh 预留,将模拟器的22端口转发到电脑5022端口 
-net user,hostfwd=tcp::2333-:2333 \ # 为 gdbserver 预留,用于远程调试

使用scp来实现qemu虚拟机和本地共享文件
0x04编译gdbserver,配置gdb
我的系统是ubuntu16.04阿里云源apt安装的是gdb-7.11.1
下载对应文件https://ftp.gnu.org/gnu/gdb/
指定c编译器,c++编译器
指定目标文件运行的平台。prefix指定安装目录(文件生成的目录)
(建议在编译之前先快照,gdb用make安装难卸载更难)

CC="mips-linux-gnu-gcc" CXX="mips-linux-gnu-g++" ./configure --target=mips-linux-gnu --host="mips-linux-gnu" --prefix="/root/tgdb" LDFLAGS="-static"
make 
make install

编译好的gdbserver 下载地址
https://download.csdn.net/download/qq_38204481/12315189
0x05测试

#include<stdio.h>

int vul(char* src)
{
    char output[20]={0};
    strcpy(output,src);
    printf("%s\n",output);
    return 0;
}
 
int main(int argc,char *argv[])
{
    if(argc<2){
        printf("need more argument\n");
        return 1;
    }
    vul(argv[1]);
    return 0;
}

mipsel-linux-gnu-gcc -g hello.c -o hello_mipsel -static

被调试客户机启动调试(qemu虚拟机)


gdbserver.mipsbe attach 0.0.0.0:12345 pid
或者 gdbserver.mipsbe 0.0.0.0:6666 /test

调试机(主机)运行

gdb-multiarch
gef➤ set architecture mips
The target architecture is assumed to be mips
gef➤ gef-remote -q 192.168.1.20:2333
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章