增加新设备

开始之前先去看新增加设备的commit,了解文件的变化。大多数很容易理解。

教学开始:

grep -lri mt300a target/

以下是增加MT300A设备的最简列表:

target/linux/ramips/base-files/etc/board.d/01_leds
target/linux/ramips/base-files/etc/board.d/02_network
target/linux/ramips/base-files/lib/upgrade/platform.sh
target/linux/ramips/base-files/lib/ramips.sh
target/linux/ramips/dts/GL-MT300A.dts
target/linux/ramips/image/mt7620.mk

以 "add support for" 过滤查看新增设备 也可以用网站中的 diff 来看,后者只显示代码增减。

以下是他们的位置路径(arch_name是架构名称):

该文件夹内的文件和子文件夹将被整合到固件的 /etc 文件夹:

  • …board.d/ 定义设备特定的默认硬件设置的脚本,如 LED 灯和网络接口。
  • …hotplug.d/ 定义设备热插拔时自动执行特定操作的脚本。
  • …init.d/ 定义设备启动时自动执行特定操作的脚本。
  • …uci-defaults/ 定义使用uci更改特定配置的文件。例如使用uci语法更改插件的配置。
  • …diag.sh 定义设备LED的错误代码。

注意:部分功能DTS中已实现

该文件夹内的文件和子文件夹将被整合到固件的 /lib 文件夹:

  • …<arch_name>.sh 将可阅读设备名称转换为脚本使用的设备名称的脚本。
  • …preinit/ <arch_name> 通用系统初始化脚本。
  • …upgrade/ <arch_name> 通用固件升级脚本。

该文件夹内的文件和子文件夹将被整合到固件的 /sbin 文件夹,通常是 <arch_name> 通用的sbin 脚本和工具。

DTS:设备树源文件

部分架构的DTS目录隐藏的很深。ARM 设备通常在:files-X.yy/arch/arm/boot/dts/

如果上游Linux中已存在的DTS或DTSI,OpenWrt通常不设置。设备配置完成后运行: make target/linux/{clean,prepare} 会自动配置好源代码,生成文件的目录在:build_dir

配置生成特定架构设备的可刷写固件

特定设备的配置项

menuconfig 下的架构核心模块配置文件

以上文件编辑后,你需要使用 touch 命令更新下 makefile。

touch target/linux/*/Makefile

patches-* 子目录包含了应用于该目标平台的所有内核补丁。
所有 patches 都应该遵循 'NNN-lowercase_shortname.patch' 格式和以下规范进行分类

0xx - 移植上游代码
1xx - 等待上游合并的代码
2xx - 内核的构建 / 配置 / 头文件 patches
3xx - 特定架构 patches
4xx - mtd 相关 patches(子系统和驱动)
5xx - 文件系统相关 patches
6xx - 通用网络 patches
7xx - network / phy 驱动 patches
8xx - 其他驱动
9xx - 未分类的其他 patches

所有 patches 必须有以下内容,确保可以被上游采纳:

  1. 必须包含 规范的主题
  2. 必须包含 规范的commit说明 解释改变了什么
  3. 必须包含 有效的 Signed-off-by 行

通过使用 ramdisk 镜像可以不刷写固件到闪存进行测试。

使用 make menuconfig 进入 Target Images 然后选择 ramdisk 选项。

这会创建包含内核(kernel)和初始内存文件系统(initramfs)的镜像, 固件名称中带有initramfs。生成的镜像可以通过设备引导加载程序(bootloader)的 tftp 功能加载到设备中,应该能直接启动并进入命令提示符,而且整个过程完全不依赖闪存或文件系统。

获取shell访问权限可以收集移植OpenWrt所需要的相关数据。大多数设备并不支持通过telnet或ssh直接获取shell访问权限。

利用未过滤输入获取

有些路由器提供了 Ping 测试或 NTP 服务器配置功能,但可能并没有对用户的输入进行严格的过滤。你可以尝试一些shell命令,走运的话可以成功。你可能需要懂点 javascript 知识来绕过或禁用前端的输入验证。

启动 telnetd
$( /bin/busybox telnetd -l/bin/sh -p23 & )
如果只能telnet,可以通过HTTP获取密码哈希或使用 ''sed'' 更改默认密码
$( cp /etc/shadow /www )
$( cp /etc/passwd /www )

然后尝试下载到电脑上破解密码哈希。

降级到旧版本固件

一些路由器在启动阶段会尝试从特定的IP下载固件 (例如 TP-Link Archer C2 AC750)。利用这个特点可以把路由器降级到旧版本固件。

通过串口降级

串口可以进入u-boot的控制台,进行刷入或加载其他固件的操作,通常需要手工引出串口焊脚。 参阅:通过串口刷机

HTTP Server 漏洞

一些路由器可能运行过时或不安全的 HTTP 服务器,易遭受 缓冲区溢出或其他攻击。

Netgear

使用 netgear-telnetenable 许多 Netgear 设备可以被开启 Telnet 访问权限。参阅 GitHub: insanid/NetgearTelnetEnable

如果上述方法无法使用,可以尝试从厂商发布的源代码构建一个固件镜像,在其中开启 telnetd 服务,然后刷入设备。参阅: nmrpflash 可以对许多 Netgear 设备进行刷机。对于支持该工具的设备,实际上可以做到“永不变砖”(即使系统损坏也能恢复)。

WikiDevi 能找到许多信息(国内无法访问),如 FCC ID 在寻找文档、数据手册以及内部照片时非常有用(可以在不必拆开设备外壳的情况下,分辨出使用的具体芯片型号)。

通常可以使用以下命令:

dmesg                          # log buffer might be to small, see note 1.
cat /proc/cmdline
cat /proc/cpuinfo
cat /proc/meminfo
cat /proc/devices
ls /sys/devices/platform
cat /proc/mtd
cat /sys/class/mtd/mtd*/offset # Linux 4.1 and newer, see note 2.
ifconfig -a
ls /sys/class/net
brctl show
cat /sys/kernel/debug/gpio     # GPIO information

注意 1: 通过 demsg 获取的日志因为日志缓冲区小而不完整。如果使用厂商发布的源代码来编译一个原厂固件(stock image),可以在配置内核(kernel config)时,把缓冲区的大小设置大一些。

注意 2: http://lxr.free-electrons.com/source/Documentation/ABI/testing/sysfs-class-mtd

gpiodump:MT7620 芯片的 GPIOMODE 寄存器转储工具(RAMIPS)--- 获取设置 LED 信息的工具

因为空间有限,常见的文件传输工具如rsync/curl/ssh/scp/ftp/http/tftp不可用,不过busybox中有被精简过的工具。

假设路由器的 IP 地址:192.168.0.123,需要传输的文件位于:/tmp/important-data.txt

使用 SCP 下载

设备内置的 SSH 服务器可能具备 SCP 传输功能,但没有 SFTP 服务器。仅支持传统的 SCP 协议(使用时必须加上 -O 选项),支持 SCPv2 协议。

Receiver
scp -O <source> <dest>

举例:

 scp -O root@192.168.1.1:/tmp/important-data.txt ~/

HTTP by ''httpd'' and ''busybox mount''

If the web interface are served from /www.

Sender
mount -o bind /tmp /www
Receiver
wget http://192.168.0.123/important-data.txt

FTP by ''busybox ftpput''

Receiver

Setup an FTP server. Add an anonymous account with write permission

python -m pyftpdlib -w -p 21
Sender
busybox ftpput 192.168.0.123 important-data.txt /tmp/important-data.txt 

netcat by ''busybox nc''

Receiver
busybox nc -l -p 12345 > important-data.txt 
Sender
cat /tmp/important-data.txt | busybox nc 192.168.0.123:12345 

TFTP by ''busybox tftp''

Receiver

Setup a tftp server

Sender
busybox tftp -p -l /tmp/important-data.txt -r important-data.txt 192.168.0.123

使用 Curl 上传

curl如果完整,还可以使用身份验证、ftp/tftp等功能。 摘自 curl 手册页:

It supports these protocols: DICT, FILE, FTP, FTPS, GOPHER, GOPHERS,
       HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET,  TFTP,  WS
       and WSS. The command is designed to work without user interaction.
            
       curl  offers  a busload of useful tricks like proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies,
       file transfer resume and more. As you will see below, the number of features will make your head spin.
       -T, --upload-file <file>
              This transfers the specified local file to the remote URL. If there is no file part in the specified URL, curl will  append
              the  local  file  name.  NOTE that you must use a trailing / on the last directory to really prove to Curl that there is no
              file name or curl will think that your last directory name is the remote file name to use. That will most likely cause  the
              upload operation to fail. If this is used on an HTTP(S) server, the PUT command will be used.

              Use  the  file name "-" (a single dash) to use stdin instead of a given file.  Alternately, the file name "." (a single pe‐
              riod) may be specified instead of "-" to use stdin in non-blocking mode to allow reading server output while stdin is being
              uploaded.

              You can specify one -T, --upload-file for each URL on the command line. Each -T, --upload-file + URL pair specifies what to
              upload and to where. curl also supports "globbing" of the -T, --upload-file argument, meaning that you can upload  multiple
              files to a single URL by using the same URL globbing style supported in the URL.

              When  uploading  to  an SMTP server: the uploaded data is assumed to be RFC 5322 formatted. It has to feature the necessary
              set of headers and mail body formatted correctly by the user as curl will not transcode nor encode it further in any way.

              -T, --upload-file can be used several times in a command line

              Examples:
               curl -T file https://example.com
               curl -T "img[1-1000].png" ftp://ftp.example.com/
               curl --upload-file "{file1,file2}" https://example.com

              See also -G, --get and -I, --head.

从命令行复制

如果以上工具和命令都不能用,可以从telnet命令行复制,但对二进制文件无效。

base64 是绕过限制的多数选择,但是大多数路由器没有这个命令。可以先在路由器上通过管道将二进制数据传递给 busybox hexdump,把它转换成屏幕显示的十六进制格式:

hexdump -v -e '/1 "%02x"'

在电脑上使用以下命令转换:

xxd -r -p
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
  • Last modified: 2026/05/03 06:36
  • by starsfall