记录-外挂recovery的制作(魅蓝note)

      安卓的开源使其具有很强的可定制性,对于用户来说很具有可玩性。玩机一般来说就是解锁BootLoader刷入第三方recovery,利用第三方recovery刷第三方ROM,刷supersu获取root权限,当然少不了鼎鼎大名的xposed框架……

      但是很多厂商是锁定BootLoader且不提供官方解锁的,比如魅族、锤子、360……热门机型还好,或许有大神关注,破解BootLoader,从而刷入第三方recovery。如果使用了小众机型,或是官方对漏洞控制的很好,无法破解BootLoader,进而无法刷入第三方recovery呢?所以就出现了所谓的外挂recovery。

      之所以称为外挂recovery,是因为我们并没有把一个recovery镜像(.img)刷入手机的recovery分区,而是利用安卓开机流程中会执行特定路径下的shell脚本,来执行自定义shell代码启动recovery。比如安卓4.4.4时的system/etc/,再如安卓5.0之后的system/su.d/(发现这一点其实是当时找关闭selinux方法时看的一些帖子)。

      我手里用的是魅族的魅蓝note,因为一次系统升级时摔了手机一下,变砖了,无法进入recovery,只能进fastboot模式,可惜bootloader是锁定的。因为急需手机的数据,邮寄太慢,打车往返100多公里去售后刷机,刷机的人特多,从早上等到下午4点多,午饭都没吃,最后手机数据全清了。如果bootloader可以解锁,直接flash boot和recovery就不用清除手机数据。为此特别气愤,为什么官方不提供BootLoader解锁,甚至线刷包都刷不进去,需要加密狗。为了所谓的安全?后来在淘宝发现,解屏幕锁15块钱,线刷救砖30块钱左右,全部可远程操作,甚至很多手机维修点也提供服务……

      所以,搞起了外挂recovery,现在把要点记录一下。

  1. 首先手机要能 root,最好是supersu授权;
  2. 编译机型的recovery,也可以解包同配置其他手机的recovery试试;
  3. 解包recovery得到ramdisk里的etc,res,sbin,twrps四个文件夹;
  4. 利用shell脚本或apk程序把这些目录拷贝到手机system分区,把启动recovery的shell脚本拷贝到system/su.d/;
  5. 开机时判断、拷贝etc,res,sbin,twrps到根目录并启动recovery

下面是我为魅蓝note做的一个外挂recovery,方法适用于安卓5.0以上版本已root的手机。首先编译了魅蓝note的twrp,再利用apk安装,apk提供“安装”,“重启”两个button,“安装”主要是文件拷贝,apk里assets目录下是recovery目录,recovery目录下有busybox、install-recovery.sh、recovery.img(实为zip),recovery.img里有etc,res,sbin(空目录),twrps四个目录,sbin目录打包成sbin.tar.gz(直接拷贝sbin目录可能因权限问题导致某些文件拷贝失败),三个shell脚本,“重启”执行重启手机并启动recovery,主要用到三个shell脚本。

  1. install-recovery.sh

#!/system/bin/sh

# recoverydir,apk程序会把assets/目录下的文件拷贝到recoverydir

# targetdir,这个脚本会把etc,res,sbin,twrps拷贝到targetdir

# busybox,busybox的路径

recoverydir=/data/data/com.meizu.recoveryinstaller/files/recovery

targetdir=/system/etc/recovery

busybox=$recoverydir/busybox

mount -o rw,remount /system

chmod 777 $busybox

if [ ! -f /system/xbin/busybox ];then

  $busybox cp $recoverydir/busybox /system/xbin/busybox

  chown 0.0 /system/xbin/busybox

  chmod 777 /system/xbin/busybox

fi

if [ ! -e /system/xbin/unzip ]; then

  chmod 777 /system/xbin/busybox

  /system/xbin/busybox --install -s /system/xbin

fi

if [ -d $targetdir ];then

  rm -rf $targetdir

fi

mkdir -p $targetdir

#解压recovery.img(实为zip)

unzip $recoverydir/recovery.img -d $targetdir

chmod -R 777 $targetdir

#解压sbin.tar.gz

tar xvzpf $targetdir/sbin.tar.gz -C $targetdir/sbin

rm -rf $targetdir/sbin.tar.gz

if [ ! -d /system/su.d ]; then

  mkdir /system/su.d

  chmod 777 /system/su.d

fi

cat /system/etc/recovery/start-recovery.sh > /system/su.d/start-recovery.sh

chmod 777 /system/su.d/start-recovery.sh

mount -o ro,remount /system

 

  2.reboot-recovery.sh

#!/system/bin/sh

if [ -d /cache/recovery ]; then

    rm -rf /cache/recovery

  mkdir /cache/recovery

  chmod 755 /cache/recovery

else

mkdir /cache/recovery

fi

touch /cache/recovery/command

chmod 755 /cache/recovery/command

reboot

 

  3.start-recovery.sh

#!/system/bin/sh

#挂载可读写

mountr() {

mount -o remount,rw /

mount -o remount,rw /system

mount -o remount,rw /custom

mount -o remount,rw /data

mount -o remount,rw /cache

}

mountr

stop

kill -9 $!

#只有执行过reboot-recovery.sh之后-e /cache/recovery/command才为真

if [ -e /cache/recovery/command ];then

  mountr

  rm -rf /cache/keycache

  rm -rf /cache/recovery/command

  chmod -R 777 /system/etc/recovery

#拷贝etc,res,sbin,twrps到根目录

  cp -rf /system/etc/recovery/etc  /

  cp -rf /system/etc/recovery/sbin  /

  cp -rf /system/etc/recovery/res  /

  cp -rf /system/etc/recovery/twres  /

  chmod -R 777 /sbin

  chmod -R 777 /twres

  chmod -R 777 /res

  chmod -R 777 /etc

mkdir /tmp

chmod -R 777 /tmp

setenforce 0

mountr

#绑定/data/media/0 /sdcard

rm -rf /sdcard

mkdir /sdcard

mount -o remount,rw /sdcard

mount --bind /data/media/0 /sdcard

runcon u:r:recovery:s0

busybox killall cploadserver

/sbin/recovery

else

  start

fi

 

posted on 2017-06-09 12:55  iJessie  阅读(2606)  评论(0编辑  收藏  举报