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