Understanding command through adb shell and through code - Android - android

I am trying to give BATTERSTATS permission to an app. When I run the command from a PC - it works just fine:
adb shell pm grant com.example.sample.myapplication android.permission.BATTERY_STATS
But the same pm grant command does not work when run from Android app:
java.lang.Process process = Runtime.getRuntime().exec("pm grant com.example.sample.myapplication android.permission.BATTERY_STATS");
Does it require root permission to give this permission?
If it is so, why it does not require the device to be rooted to run it via adb shell?
I am new to Android, please explain a bit more clearly what is happening inside and how to proceed.

The command you run with adb shell gets executed with shell(UID=2000) user privileges. The command you run from your java code gets executed with your app's UID privileges. Thus the difference.

Related

Is there an ADB command to change the advanced settings of an installed application?

I have been trying to figure out a command using ADB command line to change the advanced settings of an application I am installing through ADB.
Once installed I wish to change the following settings to 'allow' by using an ADB command:
Is this possible?
I thought it might be: found here
adb shell pm grant com.mycompany.mypackage INSTALL_NON_MARKET_APPS 1
The command for the Install unknown apps would be:
adb shell pm grant com.company.testapp android.permission.REQUEST_INSTALL_PACKAGES
Which will fail, because android.permission.REQUEST_INSTALL_PACKAGES is not a changeable permission type:
Operation not allowed: java.lang.SecurityException: Permission android.permission.REQUEST_INSTALL_PACKAGES is not a changeable permission type
Maybe there is a way around this, that I don't know of.
You can try:
appops set com.mycompany.mypackage REQUEST_INSTALL_PACKAGES allow
It works for my device with Android 10.

run shell command programmatically (not rooted) [duplicate]

I am trying to give BATTERSTATS permission to an app. When I run the command from a PC - it works just fine:
adb shell pm grant com.example.sample.myapplication android.permission.BATTERY_STATS
But the same pm grant command does not work when run from Android app:
java.lang.Process process = Runtime.getRuntime().exec("pm grant com.example.sample.myapplication android.permission.BATTERY_STATS");
Does it require root permission to give this permission?
If it is so, why it does not require the device to be rooted to run it via adb shell?
I am new to Android, please explain a bit more clearly what is happening inside and how to proceed.
The command you run with adb shell gets executed with shell(UID=2000) user privileges. The command you run from your java code gets executed with your app's UID privileges. Thus the difference.

Getting 'root" permission for Android App

I would like to know how can we get root permission from android app? Are there any app out there in android market?
I tried out the below line of code to list out files but nothing happened
Process process = Runtime.getRuntime().exec(new String[] { "su", "-", "root"});
I tried to give TEST_FACTORY permission in my manifest file but I got an error "permitted to system app"
How can I make my app system app?
I want help to get started with these stuff (make app if possible to get root permission) any help on this is very much appreciated. Thanks in advance :)
Theres a good answer here - ANDROID: How to gain root access in an Android application?
"As far as I know, you can only run command-line commands using root privileges. You can use this generic class I made that wraps the root access in your code: http://muzikant-android.blogspot.com/2011/02/how-to-get-root-access-and-execute.html"
First: note that you can only execute shell commands using su (= you can only use shell commands as root, not java code).
Second: Not sure if this applies to all su apps out there, but this is the help message of su on my phone:
Usage: su [options] [--] [-] [LOGIN] [--] [args...]
Options:
--daemon start the su daemon agent
-c, --command COMMAND pass COMMAND to the invoked shell
-h, --help display this help message and exit
-, -l, --login pretend the shell to be a login shell
-m, -p,
--preserve-environment do not change environment variables
-s, --shell SHELL use SHELL instead of the default /system/bin/sh
-u display the multiuser mode and exit
-v, --version display version number and exit
-V display version code and exit,
this is used almost exclusively by Superuser.apk
This means: you have to run su -c something (or su -c something - root, but rootis the default anyway). essentially this is equal to su on most Linux systems, except the daemon-thing, as there is no daemon ahndling su calls on regular linux systems.
If other su commands behave differently (which is possible), it's more secure to open a stream to a shell, execute su, evaluate it's return code, then proceed to execute other commands, finally execute exit.

Is it possible to run a shell command as root with only the su binary and no SuperSU or Superuser apk installed?

I have an Android phone that has only su binary installed and it works, meaning I can adb shell into the phone and run an 'su' command and I will be root.
When I try to run a command via code, it doesn't seem to work no matter which way I try to run it. I've tried many different variants of the following command.
Runtime.getRuntime().exec("su -c ps");
When I run this command on another rooted phone with a Superuser.apk or SuperSU.apk app installed, I get a dialog asking if I want to allow it to run with root permissions. When the apks are not there, it never asks and the command never works.
I've tried installing the apks on the first phone but they don't seem to do anything. So, as the original question asks --> Is there any way to run the elevated command from within the app without the SU apps installed?
It might be because you need to pass the commands to su as parameters like this:
su -c 'ls -l'
Or you might need to specify the full path to su, but I don't see why it wouldn't work the way you have it:
Runtime.exec("/system/bin/su -c ps")Or maybeRuntime.exec("/system/bin/su -c \'ps\'")
Try checking the output of this command too: System.getenv("PATH")
Another variant could be Runtime.exec("su -c \'ls -s \'")
Make sure you don't forget to escape the single quotes as they are part of the actual String.
Thats the way that I've found works most consistently, and it has also worked on devices that don't have Superuser or SuperSU installed, as those apps only listen for the Broadcast that is sent out when an application tries to run a command as root. #Boardy SuperSU and Superuser intercept the broadcast and so act as a middle man between the app and root privileges, but its not necessary for a rooted device. It IS necessary if you want to have more control over the applications that are running commands as root, but even then it still only limits you to deciding which applications, not which commands, are given root privileges.
Also, you might wanna take a look at RootTools and more specifically, the RootTools.isAccessGiven() command, which requests root privileges for your app.
Source: Launch a script as root through ADB
Not all versions of an 'su' for Android will accept a command to execute from the command line parameters.
Where they do not, you will need to let 'su' launch a privileged shell, obtain its input file descriptor, and pipe command(s) into that. This has been covered numerous times here on Stackoverflow.
I believe you would need to have the SU apps installed as they are what provide the user the question as to whether the app should be allowed to run as root or not.
You should be able to do this.
try :
adb root shell ls -l

Executing a Root access based commands on Android application

I basically want to start a process which resides in /system/bin/... from the Application java command.
I tried all types of Runtime.process.exec() options
Tried the su and ouputStream combination as well, but nothing is able to start the application.
I am using the code in device and emulators, no luck in both.
I am able to run the same commands from ADB Shell successfully (as it has root permissions).
The device is rooted, I am able to see the # when I use adb to debug my device and it also can go into su via adb.
based on my searching I found that (after doing ps from adb shell)
I am able to run any command with lrwz--x--x permission such as (ls->toolbox, ps->toolbox) from the application layer but I am not able to execute any other commands which are not linked to the toolbox.
This doesn't execute ls:
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os=new DataOutputStream(p.getOutputStream());
os.writeBytes("ls \n");
os.flush();
But this does execute ls:
Process p = Runtime.getRuntime().exec("ls");
I would really appreciate if I can get any help on this here! I am posting this after doing lots of research.
Rather than sending ls \n to the su command's standard input, try running:
su -c ls
The -c option to su(1) asks it to run a command with elevated privileges rather than starting an interactive shell.
Try creating a shell script file(eg: init.sh) and write following commands in it:
su
reboot
Now try executing this shell script by
Runtime.getRuntime().exec("sh init.sh");
If this restarted your droid then you can write your required code in init.sh file & run it.

Categories

Resources