zionhigt / android-python27

Automatically exported from code.google.com/p/android-python27
0 stars 0 forks source link

How to run a python script with root privileges using PythonAPK? #32

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I was able to run a python script that does not need root privileges in my 
android phone, which is rooted, using PythonAPK. The problem is when I want to 
run a script that for some reason need to be run by root user. It returns this 
exception "Error code: 1 Message operation not permitted".
Is there a way to call my script from java code with root permissions 
(something like su)? 

Original issue reported on code.google.com by georgelo...@gmail.com on 18 Sep 2013 at 5:17

GoogleCodeExporter commented 8 years ago
I'm seeing two potential ways:

1. using "Runtime.getRuntime().exec...", clean way is to use it through this 
lib: 
https://github.com/Chainfire/libsuperuser/tree/master/libsuperuser/src/eu/chainf
ire/libsuperuser

2. adding in com_googlecode_android_scripting_Exec.cpp 
(http://code.google.com/p/android-scripting/source/browse/android/ScriptingLayer
ForAndroid/jni/com_googlecode_android_scripting_Exec.cpp?r=28fd0c99780e713524ff0
8b0a362a56eaf803429) ability to launch process as root (setuid setgid...)

Original comment by anthony....@gmail.com on 18 Sep 2013 at 5:45

GoogleCodeExporter commented 8 years ago
Thank you very much for your reply!! I tried the second solution by changing 
the cpp file and recreating the .so but again the same problem. I think I do 
not put setuid(0) and setgid(0) in the suitable position. Can you please give 
me a clarification? It would help me a lot. 

Original comment by georgelo...@gmail.com on 19 Sep 2013 at 12:49

GoogleCodeExporter commented 8 years ago
I would try first of a simple example and printf output, smth like:

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
    printf("euid=%4d uid=%4d egid=%4d gid=%4d\n", geteuid(), getuid(), getegid(), getgid() );

    printf("%i \n",seteuid(0));
    perror("seteuid");
    printf("%i \n",setegid(0));
    perror("setegid");

    printf("euid=%4d uid=%4d egid=%4d gid=%4d\n", geteuid(), getuid(), getegid(), getgid() );

    return 0;
}

Also some permission change might be needed in 
http://code.google.com/p/android-python27/source/browse/apk/src/com/android/pyth
on27/ScriptActivity.java , 6755 instead of 0755 in the line:                    

FileUtils.chmod(new File(this.getFilesDir().getAbsolutePath()+ 
"/python/bin/python" ), 0755);

Same in 
http://code.google.com/p/android-python27/source/browse/apk/src/com/android/pyth
on27/support/Utils.java

Original comment by anthony....@gmail.com on 19 Sep 2013 at 1:20

GoogleCodeExporter commented 8 years ago
Any updates ? Did you manage to get it working ?

Original comment by anthony....@gmail.com on 28 Sep 2013 at 7:28

GoogleCodeExporter commented 8 years ago
I didn't manage to get it working in this way. I used the project just to 
install python27 to android and then I managed to run the script with root 
privileges with terminal commands. Thank you!

Original comment by georgelo...@gmail.com on 1 Oct 2013 at 10:10