电脑通过USB连接树莓派Zero(Bad USB)

Linux
Article Directory
  1. 1. 实现过程

This article was last updated on <span id="expire-date"></span> days ago, the information described in the article may be outdated.

最近正在不断的修改代码、训练模型,每次训练都至少需要40分钟左右的时间,实在是无聊。所以又拿出了我的树莓派Zero 2W,看看所谓的BadUSB怎么搞。

其实大概一两年前我就搞过,但是在我的Arch系统上完全复现不出网上的那些教程所谓的最后效果。试过了包括但不限于来自CSDN,StackOverflow,UbuntuAsk等各大论坛上的教程,各种配置都试过了,但是效果就是不好(至少在我的Arch系统上不好)。真是可恶啊,小小的一个事情竟然没有一个比较靠谱的实现教程。

经过参考著名的USB攻击平台项目 P4wnP1 的设置,我截取了其中的部分代码进行了设置,最终完成了电脑通过USB连接树莓派Zero这个目标。

实现过程

树莓派Zero可以通过模拟USB设备来连接到电脑上,包括模拟 USB 网络适配器USB 大容量存储设备 等。P4wnP1 项目集成了很多的设置,使得树莓派Zero可以通过模拟这些设备连接到电脑上并对其发起攻击。我从其中截取了部分设置来使树莓派模拟成 USB 网络适配器 并为电脑自动分配IP地址来进行连接。这样在电脑将其识别成 USB 网络适配器 并连接、获取到分配的IP之后,我们就可以通过电脑来连接树莓派Zero了(反过来,树莓派Zero也可以连接电脑,因为都在一个子网下)。

首先需要编辑以下文件:

  • /boot/config.txt,在最后一行加入dtoverlay=dwc2
  • /boot/cmdline.txt:在rootwait后面加上modules-load=dwc2,g_ether(注意要与rootwait之间相隔一个空格)
  • /etc/modules:在最后一行加入libcomposite
  • /etc/network/interfaces.d/usb0.conf:创建文件并加入iface usb0 inet manual
  • /etc/network/interfaces.d/usb1.conf:创建文件并加入iface usb1 inet manual

然后需要安装依赖库dnsmasq,我们需要使用它来充当DHCP服务器。

1
sudo apt install dnsmasq

关闭不需要的自启动

1
2
sudo systemctl disable dnsmasq.service
sudo systemctl disable avahi-daemon # 这个我还不确定如果自启动的话会对后面的运行有什么影响

然后设置的脚本如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash

GADGETS_DIR="rpi_zero_usb_gadget"
USB_VID="0x1d6b"
USB_PID="0x0137"
USB0_IP="172.16.0.1"
USB0_MASK="255.255.255.252"
USB0_DHCP_RANGE="172.16.0.2,172.16.0.2"

# init usb gadget
cd "/sys/kernel/config/usb_gadget"
mkdir "$GADGETS_DIR"
cd "$GADGETS_DIR"

# configure gadget
echo "$USB_VID" > "idVendor"
echo "$USB_PID" > "idProduct"
# device version
echo 0x0100 > "bcdDevice"
# usb 2.0
echo 0x0200 > "bcdUSB"
# composite class
echo 0xEF > "bDeviceClass"
# subclass
echo 0x02 > "bDeviceSubClass"
# proto
echo 0x01 > "bDeviceProtocol"
# device language
mkdir -p "strings/0x409"
# serial
echo "deadbeefdeadbeef" > "strings/0x409/serialnumber"
# manufacturer
echo "Syize" > "strings/0x409/manufacturer"
# product
echo "Bad USB by Syize" > "strings/0x409/product"
# create instance configuration
mkdir -p "configs/c.1/strings/0x409"
echo "Config 1: RNDIS network" > "configs/c.1/strings/0x409/configuration"
echo 250 > "configs/c.1/MaxPower"
echo 0x80 > "configs/c.1/bmAttributes"
# RNDIS configuration
mkdir -p "functions/rndis.usb0"
# set up mac address of remote device
echo "42:63:65:13:34:56" > "functions/rndis.usb0/host_addr"
# set up local mac address
echo "42:63:65:66:43:21" > "functions/rndis.usb0/dev_addr"
# CDC ECM configuration
mkdir -p "functions/ecm.usb1"
# set up mac address of remote device
echo "42:63:65:12:34:56" > "functions/ecm.usb1/host_addr"
# set up local mac address
echo "42:63:65:65:43:21" > "functions/ecm.usb1/dev_addr"
# add OS specific device descriptors to force Windows to load RNDIS drivers
mkdir -p "os_desc"
echo 1 > "os_desc/use"
echo 0xbc > "os_desc/b_vendor_code"
echo MSFT100 > "os_desc/qw_sign"
mkdir -p "functions/rndis.usb0/os_desc/interface.rndis"
echo RNDIS > "functions/rndis.usb0/os_desc/interface.rndis/compatible_id"
echo 5162001 > "functions/rndis.usb0/os_desc/interface.rndis/sub_compatible_id"
# bind function instances to respective configuration
ln -s "functions/rndis.usb0 configs/c.1/"
ln -s "functions/ecm.usb1 configs/c.1/"
# add config 1 to OS descriptors
ln -s "configs/c.1/" "os_desc"
# check for first available UDC driver
UDC_DRIVER=$(ls "/sys/class/udc" | cut -f1 | head -n 1)
# bind USB gadget to this UDC driver
echo $UDC_DRIVER > "UDC"

# wait
sleep 0.2

# set ip and mask for usb0
ifconfig usb0 $USB0_IP netmask $USB0_MASK
# create DHCP configuration
cat <<- EOF > /tmp/dnsmasq_usb_eth.conf
bind-interfaces
port=0
interface=usb0
listen-address=$USB0_IP
dhcp-range=$USB0_DHCP_RANGE,$USB0_MASK,5m
dhcp-option=3
dhcp-option=6
dhcp-leasefile=/tmp/dnsmasq.leases
dhcp-authoritative
log-dhcp
EOF

dnsmasq -C /tmp/dnsmasq_usb_eth.conf

其中CDC ECMP4wnP1 的代码注释说是用于 Linux 系统的,对应的 RNDIS 是用于 Windows 系统的,具体不明白是什么含义,实测使用usb0这个网络设备就可以。

这个启动脚本需要在树莓派开机时运行,因此需要设置systemd自启动服务,服务的文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=Bad USB start service
After=local-fs.target
DefaultDependencies=no
Before=sysinit.target

[Service]
Type=forking
RemainAfterExit=yes
ExecStart=/bin/bash /etc/bad-usb/init.sh # 启动脚本的路径
StandardOutput=journal+console
StandardError=journal+console

[Install]
WantedBy=multi-user.target

保存为/etc/systemd/system/bad-usb.service,并运行以下命令设置自启动

1
sudo systemctl enable bad-usb.service

然后就是重启树莓派Zero,等待电脑连接到树莓派Zero上。

由于我们设定了dnsmasq可分配IP固定为172.16.0.2,因此电脑必定会被分配到这个地址。树莓派Zero所在的IP为172.16.0.1,使用该地址即可连接到树莓派。

image-20231116200218574

树莓派访问一下电脑

image-20231116200442833

Author: Syize

Permalink: https://blog.syize.cn/2023/11/16/rpi-zero-bad-usb/

本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Syizeのblog

Comments