Yk2eR0's Blog.

MIPS/ARM动态调试环境搭建

字数统计: 1.6k阅读时长: 8 min
2021/01/22 Share

前提

先安装过qemu!!

qemu启动参数

1
2
ARM:
qemu-system-arm -M versatilepb -kernel vmlinuz-2.6.32-5-versatile -initrd initrd.img-2.6.32-5-versatile -hda debian_squeeze_armel_standard.qcow2 -append "root=/dev/sda1" -net nic -net tap,ifname=tap0,script=no,downscript=no -nographic
1
2
MIPS:
sudo qemu-system-mips -M malta -kernel vmlinux-2.6.32-5-4kc-malta -hda debian_squeeze_mips_standard.qcow2 -append "root=/dev/sda1 console=tty0" -netdev tap,id=tapnet,ifname=tap0,script=no -device rtl8139,netdev=tapnet -nographic

网络配置

1
ifconfig eth0 192.168.64.147/24 gw 192.168.64.2 bcast 192.168.64.255 up

配置gdbserver

mips/mipsel架构

0x01软件安装交叉编译链

安装交叉编译链(mips)

1
2
3
4
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

1
2
3
4
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配置网络
安装依赖

1
sudo apt-get install bridge-utils uml-utilities

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

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

1
2
3
4
5
6
7
8
9
10
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文件开始添加

1
2
3
4
5
6
7
#!/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

加权限,重启网卡

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

虚拟机里面运行

1
ifconfig eth0 192.168.0.1

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

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

静态ip配置(加深理解)移步
http://10.20.152.151/web/#/9?page_id=189

0x03启动qemu

安装qemu

1
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放进去会报错。

1
2
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

命令讲解

1
2
3
4
5
6
7
8
9
10
-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安装难卸载更难)

1
2
3
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测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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虚拟机)

1
2
3

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

调试机(主机)运行

1
2
3
4
gdb-multiarch
gef➤ set architecture mips
The target architecture is assumed to be mips
gef➤ gef-remote -q 192.168.1.20:2333

arm架构

一定要静态编译的哦

armhf:

交叉编译链都要装:

1
2
3
4
5
6
7
8
9
sudo apt-get install -y gcc-arm-linux-gnueabihf
sudo apt-get install -y libncurses-dev
sudo apt-get install -y libqt4-dev pkg-config
sudo apt-get install -y u-boot-tools
sudo apt-get install -y device-tree-compiler
sudo apt-get install g++-arm-linux-gnueabihf


sudo apt-get install binutils-dev

我的系统是ubuntu18.04阿里云源apt安装的是gdb-8.1.1
下载对应文件gdb
指定c编译器,c++编译器指定目标文件运行的平台。prefix指定安装目录(文件生成的目录)(建议在编译之前先快照,gdb用make安装难卸载更难)
在gdb 8.1.1文件夹下运行:

1
2
3
4
5
6
7
8
9
10
11
12

cd gdb-8.1

mkdir build

cd build

../configure --target=arm-linux-gnueabihf --prefix=/opt/arm-gdb

make -j8

sudo make install

在gdb/gdbserver下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

cd /gdb-8.1/gdb/gdbserver/

mkdir build

cd build


../configure --target=arm-linux-gnueabihf LDFLAGS=-static

make
CC=arm-linux-gnueabi-gcc CXX=arm-linux-gnueabi-g++ CXXFLAGS="-std=c++11"

make install
如果出现错误:
1
2
3
4
5
In file included from ../common/common-defs.h:77:0,
from ../defs.h:28,
from ../gdb.c:19:
../common/common-types.h:35:10: fatal error: bfd.h: No such file or directory
#include "bfd.h"

解决方法:sudo apt-get install binutils-dev

1
2
3
4
5
6
g++: error: version.c: No such file or directory
g++: warning: ‘-x c++’ after last input file has no effect
g++: fatal error: no input files
compilation terminated.
Makefile:1621: recipe for target 'version.o' failed
make: *** [version.o] Error 1

解决方法:apt install g++-arm-linux-gnueabi

40ee0eed9dd4bfc96b98f0df2b15025d.png
需要在linux-arm-low.c中添加文件包含

1
2
3
#ifndef  __NR_sigreturn
#include <bits/syscall.h>
#endif

然后make clean…

armel

1
2
3
4
5
6
sudo apt-get install -y gcc-arm-linux-gnueabi
sudo apt-get install -y libncurses-dev
sudo apt-get install -y libqt4-dev pkg-config
sudo apt-get install -y u-boot-tools
sudo apt-get install -y device-tree-compiler
sudo apt-get install g++-arm-linux-gnueabi
1
2
3
4
5
6
7
8
9
10
11
12
13
shell

cd gdb-8.1

mkdir build

cd build

../configure --target=arm-linux-gnueabi --prefix=/opt/arm-gdb

make -j8

sudo make install

在gdb/gdbserver下:

1
2
3
4
5
6
7
8
9
10
11
12
cd /gdb-8.1/gdb/gdbserver/

mkdir build

cd build


../configure --target=arm-linux-gnueabi

make CC=arm-linux-gnueabi-gcc CXX=arm-linux-gnueabi-g++ CXXFLAGS="-std=c++11"

make install

将gdbserver传到qemu虚拟机中

1
scp /usr/bin/gdbserver root@192.168.64.147:

在虚拟机中启动gdbserver:
``gdbserver :1234 typo’’

报错:

1
2
3
./gdbserver: /usr/lib/arm-linux-gnueabi/libstdc++.so.6: version `GLIBCXX_3.4.19' not found (required by ./gdbserver)
./gdbserver: /usr/lib/arm-linux-gnueabi/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./gdbserver)
./gdbserver: /lib/arm-linux-gnueabi/libc.so.6: version `GLIBC_2.14' not found (required by ./gdbserver)

说明没有静态编译

使用:

qemu中:

1
./gdbserver 0.0.0.0:6666 \typo

宿主机中:

1
2
3
gdb-multiarch
pwndbg> set arch arm
pwndbg> target remote 192.168.64.147:6666

然后就可以成功调试了.

原文作者:Yk2eR0

原文链接:https://www.yk2er0.fun/2021/01/22/env/

发表日期:January 22nd 2021, 5:08:42 pm

更新日期:April 5th 2021, 11:47:08 am

版权声明:非商业用允许转载

CATALOG
  1. 1. 前提
  • qemu启动参数
  • 配置gdbserver
    1. 1. mips/mipsel架构
  • arm架构
    1. 0.1. armhf:
      1. 0.1.1. 如果出现错误:
    2. 0.2. armel
  • 1. 将gdbserver传到qemu虚拟机中
    1. 1.1. 使用: