How to Root an Android Emulator? - android

I know there are quite a few question like this around but none of them really seems to work for me.
I am writing a program that automatically updates and installs itself, however to install the update it requires user confirmation but I do not want this as the device I will deploy on will never be physically used by anyone. So to try work around this I want to root the device and use a runtime command in the code to get it to just install.
To test this out I want to try it on the (AVD) emulator first however it (obviously) needs to be rooted. So how would i go about this?
I have tried commands in the adb shell (and out of it with the adb shell) like:
mount -o rw,remount -t yaffs2 /dev/block/mtdblock03 /system
push su /system/xbin/su
chmod 06755 /system
chmod 06755 /system/xbin/su
and a few others I can't find again at this moment but they will always give me errors such as cannot stat 'su' and stuff like that (will edit if i can find the commands and recreate again) or they just don't say anything and it doesn't appear to work.
If it helps i'm using xamarin in visual studio.

When working with android emulators, just do:
adb.exe root - restart adbd with root permissions
adb.exe unroot - restart adbd without root permissions
You can find adb.exe at: android_sdk\platform-tools\
You can confirm if the device is rooted by doing:
adb.exe shell
Inside the shell type: whoami. Then type exit to exit the shell
Example
P:\Android\sdk\platform-tools>adb.exe shell
generic_x86:/ $ whoami
shell
generic_x86:/ $ exit
P:\Android\sdk\platform-tools>adb.exe root
P:\Android\sdk\platform-tools>adb.exe shell
generic_x86:/ # whoami
root
generic_x86:/ # exit
P:\Android\sdk\platform-tools>adb.exe unroot
P:\Android\sdk\platform-tools>adb.exe shell
generic_x86:/ $ whoami
shell
generic_x86:/ $ exit

You can use https://www.genymotion.com/. All devices created are rooted by default. For example start a device then type adb shell command. It will open a rooted shell.

Related

- ddms] transfer error: open failed:Permission denied

I get a trouble when learning android Data-Persistence.
I changed the daenter image description hereta/data/com.flexible.filepersistencetest/files/data permission to 777. But I still can't pull it to my PC.
when I try to delete Nexus_5_API_24.avd 's permission of Read-Only, the Read-Only will be back.
It really exaust me.
Any advice is ok, thank you very much!
change file permissions.
C:\Android\sdk\platform-tools> adb shell
generic_x86:/ $ su
generic_x86_64:/ # chmod -R 777 /data
generic_x86_64:/ # exit
run the below commands under the terminal in Android studio integration.
C:\Android\sdk\platform-tools> adb shell
generic_x86:/ $
generic_x86:/ $ exit
$ adb root
restarting adbd as root
$ adb shell
generic_x86:/ # exit
open cmd windows.
C:\Android\sdk\platform-tools> adb pull /data/data/xxxx C:\Android\sdk\platform-tools
or use "pull" button

AOSP on Nexus 5X: How to get root

I am working on building AOSP for nexus 5X. code builds and installs and runs,
but when trying to access adb shell, I couldn't get to su.
I checked and /system/xbin/su is now in su_exec tag, so I modified file_contexts to put su in system tag
shell#bullhead:/ $ which su
/system/xbin/su
shell#bullhead:/ $ ls -Z /system/xbin/su
-rwsr-x--- root shell u:object_r:system_file:s0 su
But still:
shell#bullhead:/ $ su
su: setgid failed: Operation not permitted
I also tried disabling selinux but:
shell#bullhead:/ $ setenforce 0
setenforce: Couldn't set enforcing status to '0': Permission denied
I am out of ideas now. Please help.
adb root with userdebug build doesn't work for me.
to get root, I had to make an eng bui. on this build, adb always lands in root shell.
Thanks #Chris for answer.

Cannot adb pull database even after chmod 777 on my device

I am unable to pull a the database from the device even after changing the permission. I have a rooted phone.
It used to work. I could pull before. For some unknown reason now I cannot.
The error I receive is
remote object '/data/data/com.thuptencho.transitbus/databases/ttc.db' does not exist
Does anybody know why this is happening?
Below is what I did in command window.
C:\users\thupten>adb shell
shell#android:/ $ su
su
root#android:/ # cd /data/data/com.thuptencho.transitbus/databases/
cd /data/data/com.thuptencho.transitbus/databases/
root#android:/data/data/com.thuptencho.transitbus/databases # ls
ls
ttc.db
ttc.db-journal
webview.db
webview.db-journal
webviewCookiesChromium.db
webviewCookiesChromiumPrivate.db
root#android:/data/data/com.thuptencho.transitbus/databases # chmod 755 ttc.db
5 ttc.db <
root#android:/data/data/com.thuptencho.transitbus/databases # chmod 777 ttc.db
7 ttc.db <
root#android:/data/data/com.thuptencho.transitbus/databases # exit
exit
shell#android:/ $ exit
exit
C:\users\thupten>adb pull /data/data/com.thuptencho.transitbus/databases/ttc.db
remote object '/data/data/com.thuptencho.transitbus/databases/ttc.db' does not exist
I using these commands to get data from /data/data folders, no changing permission required
adb kill-server
adb root
I figured it out.
I had to chmod the databases folder as well and then the file.
The problem is that you need permission not just to the file, but also to its parent directories.
(That permission should not be 777 though!)
Rather than trying to change the permission, what you probably want to do is get adb running as root if that is supported, (ie, if you have an engineering build, rather than an aftermarket "rooting" of a secured device) or else use your root access (or the app itself, or the stock run-as command if you have a debug apk) to copy the file of interest somewhere accessible and then adb pull the copy.
My preferred solution was:
Install Chainfire's adbd insecure app
From within the adbd insecure app, select "Enable Insecure adbd"
adb pull /data/data/com.package.name/databases/database.db
Caution - adb insecure means adb is running as root on your device.
for i in `adb shell ls /data/ -1`;do adb pull /data/$i data; done

Attempting to gain r/w access to android /system

Working on a root script for the Nexus 4 with the latest stock rom .img for google (occam) and I have the following code snippet:
./adb wait-for-device
echo "remounting system"
./adb shell "mount -o remount,rw /system"
./adb push su /system/bin/
echo "pushing super user"
./adb push Superuser.apk /system/app/
echo "pushing busybox"
./adb push busybox /system/xbin/
./adb shell "chmod 06755 /system/bin/su"
./adb shell "chmod 0644 /system/app/Superuser.apk"
./adb shell "chmod 04755 /system/xbin/busybox"
./adb shell "cd /system/xbin"
./adb shell "busybox --install /system/xbin/"
I keep getting the error
mount: Operation not permitted
failed to copy 'su' to '/system/bin//su': Read-only file system
pushing super user
failed to copy 'Superuser.apk' to '/system/app//Superuser.apk': Read-only file system
pushing busybox
failed to copy 'busybox' to '/system/xbin//busybox': Read-only file system
Unable to chmod /system/bin/su: No such file or directory
Unable to chmod /system/app/Superuser.apk: No such file or directory
Unable to chmod /system/xbin/busybox: No such file or directory
/system/bin/sh: busybox: not found
I've tried using multiple methods of obtaining r/w access, but nothing seems to be working. I have to automate this process due to the fact that other people will use the script so it needs to be automation friendly, but I just can't figure this out.
I've also tried the
#su
#mount
#mount | grep system
followed by inputting the partition with the system mount and changing it to r/w access, but that also hasn't worked.
Really frustrated at this point. Can anyone help?
It gives the error because you aren't root. The system partition is mounted read-only. You can try to push the binary to /data/local/tmp. Then you can make su executable and eventual run it. But it doesn't mean you can have root. To become root you need to push an exploit like psneuter to /data/local/tmp and run it. It crashes the shell and reopen a new one with root rights. Then you can remount the system-partition read-write and install su.
Try the commands below
adb shell "su -c mount -o remount,rw /system"
adb shell "su -c chmod 06755 /system/bin/su"
and so on.

How to execute adb commands programmatically in android

I manually rooted my emulator using following commands
adb shell mount -o rw,remount -t yaffs2 /dev/block/mtdblock03 /system
adb push su /system/xbin/su
adb shell chmod 06755 /system
adb shell chmod 06755 /system/xbin/su
adb install superuser.apk
It is successfully rooted and working su command but while closing emulator and again opening emulator then su command is not working. To overcome this one I want to create a sample app for execute above commands programmaticaly using
Runtime.getRuntime().exec();
Please can one help me how to execute above commands programmatically.
The emulator is rooted out of the box in that the ADB shell runs as root.
What you've done is not "rooted" it, but rather used that pre-existing root shell to install a common hack which will then let application programs run helper tasks as root.
But you cannot do this from an application on the device which does not already have the ability to launch a helper as root - so you are stuck with a circular dependency or chicken-and-egg problem of needing root in order to install a root helper.
You will not be able to do this on the device or even emulator without finding a security hole (or pre-existing intentional privilege escalation mechanism) to exploit.
However:
you could read up on how to make the emulator's state persistent, and do it manually only once
you could issue the commands from a script running on the develpment machine, rather than on the device/emulator

Categories

Resources