看过几篇教程之后我已经知道怎么备份固件了。但是现在有一个问题,我的本意是把K2P原机带的固件备份出来,用教程上的方法进行开启telnet、备份固件等操作是否会改变固件呢?下面我们来验证这个问题。

OpenWrt的文件系统

K2P使用的是OpenWrt系统,我们先来看一下K2P的Flash Layout(图片来自恩山)

图片上的 firmware 就是我要的固件,它分成 Kernel 和 rootfs 两部分。其中 rootfs 使用SquashFS和JFFS2实现了一个可读写的文件系统,但实际上SquashFS是只读的压缩包,所有的文件修改都是记录在JFFS2,也就是rootfs_data上的。

感兴趣的话可以看看:

https://wiki.openwrt.org/doc/techref/filesystems

https://blog.csdn.net/lee244868149/article/details/57076615

路由器重置的原理

我们知道所有的文件修改都是记录在 rootfs_data 上的,系统重置实际上就是将这部分数据抹掉,我们来验证一下。

我之前下了一个网友备份的固件,地址

打开 "/usr/lib/lua/luci/controller/admin/backuprestore.lua" 文件,查看第136~141行。

     elseif reset_avail and luci.http.formvalue("reset") then
luci.template.render("backuprestore", {
reset_process =
})
fork_exec("sleep 3; killall dropbear lighttpd miniupnpd; sleep 3; mtd -r erase rootfs_data")
else

mtd -r erase rootfs_data 这一句就是抹掉 rootfs_data 的数据。

通过配置文件开启telnet的原理

我们来看一下恢复配置文件是怎么实现的。

还是 "/usr/lib/lua/luci/controller/admin/backuprestore.lua" 这个文件,查看第81~136行。

     elseif luci.http.formvalue("restore") then
local fs = require("luci.fs")
luci.http.formvalue("filename")
--校验配置文件
luci.util.exec("encryconfig decrypt /tmp/backupFile_encode /tmp/backupFile")
nixio.fs.unlink("/tmp/backupFile_encode")
local fd = io.open("/tmp/backupFile", r)
local restore_error_message
if fd ~= nil then
local line = fd:read()
fd:close()
if line ~= nil then
if not checkfwversion() then
nixio.fs.unlink("/tmp/backupFile")
restore_error_fwversion = {"restore_error"}
luci.template.render("backuprestore", {
restore_error_fwversion = restore_error_fwversion
})
else
luci.util.exec("sed 1,10d /tmp/backupFile >/tmp/restore_rm_header")
luci.util.exec("tar -xzC/ -f /tmp/restore_rm_header")
nixio.fs.unlink("/tmp/restore_rm_header")
local cur_lan_mac = luci.util.exec("uci get network.lan.macaddr")
local cur_wan_mac = luci.util.exec("uci get network.wan.macaddr")
local flash_lan_mac = luci.util.exec("eth_mac r lan")
local flash_wan_mac = luci.util.exec("eth_mac r wan")
if cur_lan_mac ~= flash_lan_mac then
luci.util.exec("uci set network.lan.macaddr=%s" % flash_lan_mac)
end
if cur_wan_mac ~= flash_wan_mac then
luci.util.exec("uci set network.wan.macaddr=%s" % flash_wan_mac)
end
luci.util.exec("uci commit")
local upload = luci.http.formvalue("restore")
if upload and #upload > then
luci.template.render("backuprestore", {
restore_avail =
})
fork_exec("sleep 3; reboot")
end
end
else
restore_error_message = {"restore_error"}
nixio.fs.unlink("/tmp/backupFile")
luci.template.render("backuprestore", {
restore_error_message = restore_error_message
})
end
else
nixio.fs.unlink("/tmp/backupFile")
restore_error_message = {"restore_error"}
luci.template.render("backuprestore", {
restore_error_message = restore_error_message
})
end
elseif reset_avail and luci.http.formvalue("reset") then

第101行,tar -xzC/ -f /tmp/restore_rm_header,这条命令把配置文件解压缩到根目录下覆盖现在用的文件。

利用这个特性我们可以向系统写入我们想要的数据,比如自启动脚本。

/etc/rc.local

在 "/etc/rc.local" 脚本中加入命令开启telnet服务是一个不错的选择。

修改后的文件如下:

 # Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing. case `cat /proc/cpuinfo | grep MT76` in
**)
CONFIG_RALINK_MT7621=y
;;
**)
CONFIG_ARCH_MT7623=y
;;
esac
if [ "$CONFIG_RALINK_MT7621" = "y" ]; then
echo > /proc/sys/vm/min_free_kbytes
#echo > /proc/sys/vm/overcommit_memory
#echo > /proc/sys/vm/overcommit_ratio
fi
block mount # add nat rule manually smp.sh wifi
hwnat-enable.sh
brctl addif br-lan ra0
brctl addif br-lan rax0
/usr/sbin/telnetd -l /bin/login.sh
exit

第25行 /usr/sbin/telnetd -l /bin/login.sh 就是开启服务的命令。注意,在这个命令中要写完整路径名。

打包配置文件

生成配置文件的代码在 "/sbin/sysupgrade" 脚本里,第131~174行。

 do_save_conffiles() {
local conf_tar="${1:-$CONF_TAR}" if [ -z "$CONF_BACKUP" ]; then
platform_config_prepare
fi [ -z "$(rootfs_type)" ] && {
echo "Cannot save config while running from ramdisk."
ask_bool "Abort" && exit
return
}
run_hooks "$CONFFILES" $sysupgrade_init_conffiles
ask_bool "Edit config file list" && vi "$CONFFILES" # v "Saving config files..."
[ "$VERBOSE" -gt ] && TAR_V="v" || TAR_V=""
tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" if [ -n "$CONF_BACKUP" ]; then
#fix project name must be first line
#phic_fac -g product > /tmp/backup_add_header
#fix project hardware info must be second line
#phic_fac -g hw_ver >> /tmp/backup_add_header
#fix project software version must be third line
#phic_fac -g fw_ver >> /tmp/backup_add_header
#we leave here blank lines,so we have total lines include above lines at the top of /tmp/backup_2
echo "product=`uci get system.system.hostname`" >> /tmp/backupFile
echo "hw_ver=`uci get system.system.hw_ver`" >> /tmp/backupFile
echo "fw_ver=`uci get system.system.fw_ver`" >> /tmp/backupFile
for i in
do
echo "" >> /tmp/backupFile
done
cat $conf_tar >> /tmp/backupFile
encryconfig encrypt /tmp/backupFile /tmp/backupFile_encode
#cat /tmp/backup_add_header_encry >/dev/null #rm -f "/tmp/backup_add_header"
#rm -f "/tmp/backup_add_header_encry"
rm -f "$conf_tar"
fi
rm -f "$CONFFILES"
}

配置文件由文件头和一个tar包组成。文件头是10行文本,这个可以在备份出的文件中截取。在tar包里放我们想写入的文件,与文件头合并成在一起。最后用openssl加密就可以了。

斐讯K2P通过配置文件开启telnet的原理分析的更多相关文章

  1. 斐讯K2P配置文件破解笔记

    手上有一个斐讯K2P路由器,刷机前我想把原机带的固件备份出来.搜到恩山A大开启telnet.固件备份的教程,里面提到了配置文件破解的方法,心血来潮试了一下,发现算出的密码不能解密,一直报"b ...

  2. 斐讯k2p 月光银 硬件版本A2-软件版本22.7.8.5 刷官改系统

    Mark https://huabuyu.net/斐讯k2p%20月光银%20硬件版本A2-软件版本22.7.8.5%20刷官改系统.html 详细资源推荐:恩山论坛 https://www.righ ...

  3. 斐讯K2 V22.X.X.X 新版固件 刷机教程 (开telnet,安装SSH,adbyby,刷breed,华硕Padavan)

    源:http://www.right.com.cn/forum/thread-191833-1-1.html 属于我的上一个帖子的升级版,基本属于无脑操作,点击恢复就可以自动刷好breed,浏览上传新 ...

  4. [无线路由] “免费”斐讯K2路由器刷OpenWRT(实战MWAN多宽带网速叠加)

    (阿财首发于什么值得买)斐讯K2可以算是一个非常另类的跨界数码产品,其产品完全的醉翁之意不在酒.最多值99元的 MT7260硬件架构和用料,售价399元,金额激活K码后自动转入合作理财P2P平台,等待 ...

  5. 斐讯K2刷不死breed与第三方固件教程

    本文主要就是简单的斐讯 K2 刷机教程,方便大家了解一下 K2 怎样刷固件.斐讯 K2 是一款 1200M AC 双频无线路由器,支持 5G 和 2.4G WiFi 信号,虽然缺少 USB 且只有百兆 ...

  6. 斐讯 FIR151M 频繁掉线(OpenWRT解决方案)

    0. 现象与前言 在使用斐讯 FIR151M 路由器连接网络时,传输数据时频繁掉线. 官方固件刷了两个版本,问题未解决. 建议高级用户看本教程,要做好不能使用 Web 管理界面的心理准备. 1. 准备 ...

  7. 斐讯N1折腾记

    斐讯N1折腾记:运行 Linux 及优化  2018-06-23  37条评论  4,445次阅读  11人点赞 最后更新时间:2019年03月10日 咳咳咳,上篇教程教大家给斐讯 N1 降级并且刷了 ...

  8. 斐讯自动下单抢购V1.3.4【自动验证码识别】

    20180530 更新 V1.3.41.增加有货下单:替代定时下单 20180519 更新 V1.3.31.增加订单满减优惠:支付宝每单立减5元2.修改商城域名及下单速度 功能介绍1.斐讯商城抢购专用 ...

  9. 给斐讯K1刷机并拨号e信(湖北地区测试无问题)

    ◆购买斐讯k1路由器 路由器在天猫京东斐讯旗舰店都有售卖,我买的价格是159,不过有一张铃铛卡,一个月之后返还160元,相当于0元购 ◆路由器刷不死Breed 1.路由与电脑有线连接好,输入192.1 ...

随机推荐

  1. Mask_RCNN训练自己的模型(练习)

  2. toFixed

    1,toFixed要使用与Number数据类型 2.parseInt将字符串转换为整形 3.parseFloat将字符串转换为浮点型

  3. sql分组获取第一条记录(sql+oracle)

    sql版本 select * from (select t.CloseDate,t.ExpiryDate,t.DataTypeLookupID,ROW_NUMBER() over(partition ...

  4. CentOS 7.2配置Apache服务httpd(上)

    http://www.jb51.net/article/97434.htm 二.安装Apache httpd 安装httpd以配置Web服务器, HTTP使用80 / TCP ? 1 2 3 4 5 ...

  5. Ubuntu16 apt-get更换为阿里源

    1.备份系统自带源 mv /etc/apt/sources.list /etc/apt/sources.list.bak 2.修改/etc/apt/sources.list文件 vim /etc/ap ...

  6. Java Decompiler(Java反编译工具)

    参考:http://blog.csdn.net/yulei_qq/article/details/24175547 Java Decompiler可以对整个jar包进行反编译,也可以将其集成到ecli ...

  7. ping别的电脑出错

    原因ifconfig 电脑1:172.31.45.101 电脑2:172.31.188.232 http://ask.csdn.net/questions/178358 如何防止别人ping自己的电脑 ...

  8. 多线程的那点儿事(之windows锁)

    在windows系统中,系统本身为我们提供了很多锁.通过这些锁的使用,一方面可以加强我们对锁的认识,另外一方面可以提高代码的性能和健壮性.常用的锁以下四种:临界区,互斥量,信号量,event. (1) ...

  9. OSI结构和TCP/IP模型

    TCP/IP层次模型共分为五层:应用层HTTP.传输层TCP.网络层IP.数据链路层Data-link.物理层physical. 应用层—应用层是所有用户所面向的应用程序的统称.ICP/IP协议族在这 ...

  10. java反射的补充:桥接方法以及Spring中一些工具类

    在上一篇博文中:http://www.cnblogs.com/guangshan/p/4660564.html 源码中有些地方用到了 this.bridgedMethod = BridgeMethod ...