转adb Shell root 权限
永久root帶文件
因?yàn)殚_(kāi)發(fā)需要,我經(jīng)常會(huì)用到adb這個(gè)工具(Android Debug Bridge),我們都知道adb shell默認(rèn)是沒(méi)有root權(quán)限的,修改系統(tǒng)文件就很不方便了,adb push一個(gè)文件就提示Permission Denied。刪除system下的文件也沒(méi)有權(quán)限。其實(shí)有兩種方法可以獲取adb shell的root權(quán)限,這兩種方法的前提都是手機(jī)已經(jīng)root。 1、用su可以提權(quán),直接執(zhí)行su就會(huì)看到用戶命令提示符由”$”變成了”#”,如果手機(jī)沒(méi)有root,會(huì)提示su: Permission Denied。這個(gè)文件不是每個(gè)手機(jī)都有的,可以百度。 解壓后把su放在adb同一目錄下,執(zhí)行:
adb push su /system/bin/adb shell chmod4755/system/bin/su
如果提示Read-only filesystem,那么就要重新掛載一下/system,把只讀掛載成可讀寫(xiě),只有手機(jī)root了才能運(yùn)行:
mount -o remount,rw/dev/block/mtdblock0/system /
再運(yùn)行su就能讓adb shell獲取root權(quán)限了。 2、可以修改根目錄下的default.prop提權(quán): 根目錄默認(rèn)是不允許修改的,執(zhí)行
mount -o remount,rw rootfs/
用vi打開(kāi)default.prop,找到ro.secure,修改為ro.secure=0,保存后重啟,再adb shell一下,就會(huì)有root權(quán)限了。 方法:
修改./default.prop
把ro.secure設(shè)為0,persist.service.adb.enable設(shè)為1,adbd進(jìn)程就會(huì)以root用戶的身份啟動(dòng)。
其實(shí)兩篇文章大體效果不同,這個(gè)是完全破除限制,下文只是部分 至于文中所提到的su文件,是指被修改過(guò)的,無(wú)任何驗(yàn)證的,這樣安全性大大降低,推薦完整root前,先備份原su文件。
?
原理:
可以看一下Android系統(tǒng)根目錄下的/init.rc的片段:
... ...
# adbd is controlled by the persist.service.adb.enable system property
service adbd /sbin/adbd
??? disabled
# adbd on at boot in emulator
on property:ro.kernel.qemu=1
??? start adbd
on property:persist.service.adb.enable=1
??? start adbd
on property:persist.service.adb.enable=0
??? stop adbd
... ...
這里定義了一個(gè)觸發(fā)器,只要persist.service.adb.enable值被置為1,就會(huì)啟動(dòng)/sbin/adbd。
?
在build目錄下搜索一下,發(fā)現(xiàn)了main.mk中有這樣的代碼片段
## user/userdebug ##
?
user_variant := $(filter userdebug user,$(TARGET_BUILD_VARIANT))
enable_target_debugging := true
ifneq (,$(user_variant))
? # Target is secure in user builds.
? ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=1
?
? tags_to_install := user
? ifeq ($(user_variant),userdebug)
??? # Pick up some extra useful tools
??? tags_to_install += debug
? else
??? # Disable debugging in plain user builds.
??? enable_target_debugging :=
? endif
?
? # TODO: Always set WITH_DEXPREOPT (for user builds) once it works on OSX.
? # Also, remove the corresponding block in config/product_config.make.
? ifeq ($(HOST_OS)-$(WITH_DEXPREOPT_buildbot),linux-true)
??? WITH_DEXPREOPT := true
? endif
?
? # Disallow mock locations by default for user builds
? ADDITIONAL_DEFAULT_PROPERTIES += ro.allow.mock.location=0
?
else # !user_variant
? # Turn on checkjni for non-user builds.
? ADDITIONAL_BUILD_PROPERTIES += ro.kernel.android.checkjni=1
? # Set device insecure for non-user builds.
? ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=0
? # Allow mock locations by default for non user builds
? ADDITIONAL_DEFAULT_PROPERTIES += ro.allow.mock.location=1
endif # !user_variant
?
ifeq (true,$(strip $(enable_target_debugging)))
? # Target is more debuggable and adbd is on by default
? ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1 persist.service.adb.enable=1
? # Include the debugging/testing OTA keys in this build.
? INCLUDE_TEST_OTA_KEYS := true
else # !enable_target_debugging
? # Target is less debuggable and adbd is off by default
? ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=0 persist.service.adb.enable=0
endif # !enable_target_debugging
這段代碼我大致解釋一下:
主要通過(guò)判斷當(dāng)前的編譯模式來(lái)給幾個(gè)屬性賦予不同的值,然后把屬性存儲(chǔ)在ADDITIONAL_DEFAULT_PROPERTIES這個(gè)變量中,這個(gè)變量在后面是要寫(xiě)到根目錄下的/default.prop中去,在系統(tǒng)啟動(dòng)時(shí)被屬性服務(wù)加載的。也就是說(shuō)我們?cè)?default.prop中看到的幾個(gè)屬性的值是在這里設(shè)置的。
只看兩個(gè)屬性ro.secure,persist.service.adb.enable。當(dāng)前是user模式的話,編譯系統(tǒng)會(huì)把ro.secure置為1,把persist.service.adb.enable置為0.也就是說(shuō),用user模式編譯出來(lái)的系統(tǒng)運(yùn)行在安全模式下,adbd默認(rèn)關(guān)閉。即使通過(guò)設(shè)置屬性的方式打開(kāi),adbd進(jìn)程的用戶也是shell,不具有root權(quán)限。這樣,普通用戶或者開(kāi)發(fā)者拿到一個(gè)機(jī)器后,通過(guò)PC運(yùn)行adb shell時(shí),是以shell用戶登錄機(jī)器的。
好了,現(xiàn)在把ro.secure置為0,再重新編譯,只要設(shè)置屬性persist.service.adb.enable的值為1,adbd進(jìn)程就會(huì)以root用戶的身份啟動(dòng)。
轉(zhuǎn)載于:https://www.cnblogs.com/blues_/p/3582097.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的转adb Shell root 权限的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 简单的过滤器
- 下一篇: 【转】awk 里的substr函数用法举