Getting 'root" permission for Android App
Asked Answered
G

2

9

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 :)

Gonfanon answered 6/2, 2014 at 7:16 Comment(12)
There is a big difference between an app with root permissions and a system app. Which one do you need?Anacreontic
Hm. re-reading your question, it seams as you're trying to program your own "supersu"-alternative? If so: you cannot do that in Java. You'll need C to program and compile an executable that can run natively on Android's kernel. That one can of course plug into any Java APK that handles user-interaction, but you still ned the su-executable to call your java app.Psychopath
@Simaon I m basically looking to alter the Android kernel from my app. And I have no idea how to go about it. Pls help me out here. Whats the right way to get started for this purpose?Gonfanon
@Johannes I m basically looking to alter the Android kernel from my app. And I have no idea how to go about it. Pls help me out here. Whats the right way to get started for this purpose?Gonfanon
Calling shell commands using su is ther right way to get there. You just have to figure out what commands to call - and what commands are avialable. First thing to do: learn how to use the linux shell. Android features a bourne-shell, so you can use bourne-shell specific syntax.Psychopath
@JohannesH. so can we execute shell commands using su from android app ?Gonfanon
Yes. see my answer on how to do that. You may also take a look at the great code linked by @NirHartmannPsychopath
@JohannesH. okay so what is wrong with the code that I have posted in my question above? It was not working why is it?Gonfanon
It shoudl work, just do noting - su without any other command to execute won't do much. Also note that the phone has to be rooted of course, otherwise the su binary isn't available.Psychopath
@JohannesH. so if I want to test the app in another device that should also be rooted right?Gonfanon
and also is it possible to test it out in emulator?Gonfanon
EVERY device that shoudl run suhas to HAVE suofr course, so yes. I don't know about the emulator, I guess it doesn't feature a su binary unless you inject one. Haven't done anything that required su myself though, so I really don't know.Psychopath
P
5

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.

Psychopath answered 6/2, 2014 at 7:27 Comment(0)
B
6

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"

Biotechnology answered 6/2, 2014 at 7:28 Comment(0)
P
5

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.

Psychopath answered 6/2, 2014 at 7:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.