How to acquire root access using shell script in Java code? - java

I can get root access to phone using 'adb root' in my PC then change a file content under /data folder. Now I want to do it in a apk using Java code.
I tried blow code:
Runtime.getRuntime().exec(new String[]{"sh", "/sdcard/my.sh"});
This function works well when shell command do not need a root permission.
Now I change my.sh into
su root
chmod 777 /data/filetochange.xml
rm -r /data/filetochange.xml
But it didn't work. Can I do this in Java code?

I think your app need have root permission. You can try to add a line 'android:sharedUserId="android.uid.system"' to AndroidManifest.xml, which property can get system permission. But this is not enough, you need to do other steps and you can google it, there are many detailed tutorials online.

Related

error=13, permission denied while running a .sh file through automation in eclipse

I am Creating a .sh file newly every time while running automation and while executing that file i am getting permission error always, for time being i am adding permission by running the below command in terminal
chmod u+x "filelocation"
Can someone help me to handle this issue permanently.
As you are always creating the new file, permissions must also be reassigned.
You can run chmod u+x "filelocation" from your code where you created the file to give permissions to the file.
You can reference https://www.mkyong.com/java/how-to-execute-shell-command-from-java/ on how to run shell commands from java using ProcessBuilder.

studio.sh open permission denied for android studio in ubuntu

I downloaded an Android Studio for linux and then tried to run the studio.sh file inside the 'bin' directory as per the instruction said. The terminal showed an error saying something like this:
bash: ./studio.sh: permission denied
The whole Studio bundle was in .tdz format and I extracted the files before accessing via the terminal. What is the main cause for such error?
Thank you so much!
Got the same problem.
But solved it by following steps:-
Right click on studio.sh and select Properties.
There go to Permissions.
Check "Allow Executing file as program"
And you are done.
Type
sudo ./studio.sh
This should launch android studio with admin privileges. You will need to type your password, assuming you have admin privileges. If you do not have them, you need to contact whoever manages the computer.
When it launches for the first time, it will ask yo to add a shortcut so it is reachable from the GUI app menu. After that, launching android studio should not require sudo permissions.
https://askubuntu.com/questions/421389/where-to-unpack-the-android-studio-file has good recommendations of where to extract android studio to.
You've probably extracted it as root. Change the owner of the file to you with chown command and extract it again, or change the owner of the whole extracted content to you. Example (sudo may be required):
sudo chown yourUserName studio.sh
The other way to do it: change privileges with chmod
sudo chmod a+x studio.sh
Beware, the above one gives every registered user privileges to execute studio script.
--make this--
1°
sudo chown root:YOURUSERHERE /dev/kvm
2°
sudo chmod -R 770 /dev/kvm
so you will be allowed to install and run android studio
Hope this helps :)
I have the same problem, then i used
sudo sh studio.sh
then it`s worked.

Android: Permission denied for /data/local/tmp/*

I am trying to run a shell script through an android app. The script has a command which just runs a jar on the device. When I run this command directly on the shell using adb, everything works fine. But when I run it through the script using the android app, I get a permission denied exception (open failed: EACCES (Permission denied)) for the files created in /data/local/tmp folder. Can anyone guide in how to resolve this issue?
This is what my manifest looks like
<manifest ….>
<uses-sdk …>
<uses-permission… .>
…
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
…
<application …>
…
</application>
</manifest>
Thanks.
Update 8 years later
On modern versions of Android, there is no way to run downloaded or generated code (a.k.a. W^X violation). We can only embed prebuilt binaries in the APK (or bundle) (using jniLibs and other settings in gradle.build) and make sure it is extracted to the native file system (explicitly set android:extractNativeLibs = "true"). Then you can run the binary from getContext().getApplicationInfo().nativeLibraryDir.
If I understand the scenario correctly, you create the script on the fly, and use /data/local/tmp as an easy location that is both publicly writable and executable. Once, this was possible. But on recent versions of Android, security has been tightened.
Your app can execute files under /data/data/${your.package}. You can use getContext().getFilesDir() to reliably obtain the full path. Note that you still need to use chmod 500 to ensure that the file has executable permission.
If you have some fixed executables (binaries or scripts) that must be installed with your app, there is a clever trick to let the system package installer take care of all that for you: make sure the file has a name "libsomething.so" and put it in /libs/armeabi directory under the Eclipse project root. After installation, the files will be present in getContext().getApplicationInfo().nativeLibraryDir directory with executable permissions set.
PS You don't need the WRITE_EXTERNAL_STORAGE permission for this to work (maybe you need it for other things your app does).
PPS You can run a shell script from anywhere, including /sdcard, and you don't need executable permission for the script. Instead, use sh -c source full.path.to.script.
You can use the adb interface to copy and/or push files to the /data/local/tmp folder but if you want to use/see them in the terminal app you will need to (in adb interface) first
cd /data/local/tmp
then make a folder inside the folder. Example
mkdir folder
next change the permissions
chmod - R 777 folder
Now you have a folder you can read and write to.
A few things that I would like to know is how to make the system think that the su binary is in the /system/bin folder (without copying) because I can only get tmp... root access because even with root access, I can not remount the system directory as rw because the zte-paragon has its system partition formatted as a read-only file-system
I think the reason is that your Andorid is not been rooted.
the "/data" have the root Permission.
So root your phone first, and give your app the permission.
you can try command:"su" int the shell change to root.
Make sure the /data/local/tmp location is world-executable on your device:
adb shell ls -ld /data/ /data/local/ /data/local/tmp/
drwxrwx--x system system XXXX-XX-XX XX:XX
drwxr-x--x root root XXXX-XX-XX XX:XX
drwxrwx--x shell shell XXXX-XX-XX XX:XX
# ^ all x here == OK!
If it's not, there's nothing you can do, and you need to use the standard storage (internal /data/data/{package} or external /sdcard/).
Make sure you made the file world-readable.
adb shell chmod o+r /data/local/tmp/myfile.txt
You don't need any permissions.

How to R/W file in android system directory (Internal Storage)

I want to create the file in android system folder.
I have also tried this "mount -o rw,remount -t yaffs2
/dev/block/mtdblock3 /system" using Runtime.getRuntime().exec() but
it's not working.
if you have example please let me know.
Thanks
Android applications don't run as root on an unmodified device so they can't write to the /system folder or remount file systems.
Why do you want to write to /system? What's wrong the the storage directory for you app?
This may be possible on a "rooted" Android device but this rules out the majority of users.
//if its showing as $ you need to root your phone
padmakumar#padmakumar-desktop:~$ adb shell
$
//if its showing as # you can copy files
padmakumar#padmakumar-desktop:~$ adb shell
#
//let do this step
padmakumar#padmakumar-desktop:~$ adb root
padmakumar#padmakumar-desktop:~$ adb remount
//push a file from you system to your phone
adb push ./Desktop/my_lib.so /system/lib

Android SU permissions: How to use them?

There is a situation here, I'm developing an Android application, using Java. I'm pretty familiar with all this stuff, but now it's the first time when I need to use SU permissions. I just need to replace (actually, rename) the file in system/app directory, but it looks like I'm not able to perform it in a usual way (renameTo method in File class), it just returns me FALSE, which means that there was some error in operation.
So can anybody tell me how to use SU? My test phone is fully rooted with SU 3.0.3.2, any app that require SU works perfectly fine.
Shall I use the same method but with some additions in manifest? Shall I use busybox in some way?
I already googled for this, and I can't find any useful information. And also, there is no documentation on official Android Superuser website.
Thanks in advance!
You'll probably also need to remount the filesystem as RW since /system is read-only. So you might need to call SU with a similar command below:
mount -o r,remount -t yaffs2 /dev/block/mtdblock3 /system
To execute the command, you can try two ways (I've notice in android sometimes one works and the other won't )
Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "mount -o r,remount -t yaffs2 /dev/block/mtdblock3 /system"});
Or you can do
Process p = Runtime.getRuntime().exec("su");
p.getOutputStream().write("mount -o r,remount -t yaffs2 /dev/block/mtdblock3 /system".getBytes());
p.getOutputStream().write(<my next command>);

Categories

Resources