[Q] SU Command from hook - Xposed General

Im trying to run a su command from within a hook,
Code:
Process p;
try {
// Preform su to get root privledges
p = Runtime.getRuntime().exec("su");
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() != 255) {
toastMessage("root");
return true;
} else {
toastMessage("not root");
}
} catch (InterruptedException e) {
toastMessage("not root");
}
} catch (IOException e) {
toastMessage("not root");
}
But even though I grant the shell su access I still get error:
tmp-mksh: <stdin>[1]: can't create /system/sd/temporary.txt Read-only file system
Any ideas?

mount system as rw?

Ty, I'll give it a shot. Could be Selinux related too but no errors other than in su log. Might need to post this to root section. Not really a problem with the hook I don't think.

Related

[Q] Dev - How to launch an app from another one

Hi there,
I'm working on my LeoParts and integrated ROM Updater in my ROM.
Now I want my LeoParts to launch ROM Updater on a PreferenceScreen.
I tried two things:
1. With the XML
Code:
<PreferenceScreen
android:key="rom_update"
android:title="@string/updates_title"
android:summary="@string/updates_summary">
<intent android:action="android.intent.action.MAIN"
android:targetPackage="org.elegosproject.romupdater"
android:targetClass="org.elegosproject.romupdater.VersionsList"/>
</PreferenceScreen>
2. With the Java code
Code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("org.elegosproject.romupdater", "org.elegosproject.romupdater.VersionsList");
startActivity(intent);
3. From the shell
Code:
am start -a android.intent.action.MAIN -n org.elegosproject.romupdater/.VersionsList
This last one works, but I would like something less hacky...
Here is the error I get from a logcat:
Code:
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cmp=org.elegosproject.romupdater/.VersionsList } from ProcessRecord{407e1ed8 775:com.leonnib4.leo_parts/10044} (pid=775, uid=10044) requires null
I know this as something to do with permissions, most likely in the AndroidManifest.xml, but I can't get it to work.
Please help me out
Thx
I got arround by doing:
Code:
Intent intent = getPackageManager().getLaunchIntentForPackage("org.elegosproject.romupdater");
if (intent != null) {
toast.show();
String commands[] = {
"am start -a android.intent.action.MAIN -n org.elegosproject.romupdater/.VersionsList"
};
// Execute in a shell
} else {
// Toast notif "ROM Updater is missing"
}
But that is still hacky and I'm still looking for a better way to do this...
Thx

Work with files in hook

Hi, I have hook
Code:
findAndHookMethod(packageManagerService, null,
"installPackageWithVerificationAndEncryption", Uri.class,
"android.content.pm.IPackageInstallObserver", int.class,
String.class, "android.content.pm.VerificationParams",
"android.content.pm.ContainerEncryptionParams",
installPackageHook);
I want to create files while I am in hook.
Code:
installPackageHook = new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param)
throws Throwable {
File backupApkFile = new File(APP_DIR + File.separator + "App.apk");
backupApkFile.createNewFile();
I get EACCESS permission denied. Framework has all permissions, no?
pyler said:
I get EACCESS permission denied. Framework has all permissions, no?
Click to expand...
Click to collapse
Your code runs in the hooked process, so you only have that process' permissions.
You can try hooking the PermissionGranter class to give the hooked package additional permissions, or e.g. send a broadcast to/directly start your own activity/service to write the file (from your own app's process, without Xposed).

[Q] Android Studio app says "Error In Network Connection"

I have created a login and register system, when ever I try to login, register, change password or reset password the app just says "Error In Network Connection".
I am using strong internet connection, could you please advise as I have tried to on WI-FI and 4G, however it still says the same error. I am using WAMP server to connect to myphpadmin database. Please could you help and advise. I have used the internet method as well as using the doinbackground method.
Emulator , Use Device
If you use emulator. You will get this type of problem.
Use below code to check the online condition for mobile.
public boolean isNetworkOnline() {
boolean status=false;
try{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getNetworkInfo(0);
if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED) {
status= true;
}else {
netInfo = cm.getNetworkInfo(1);
if(netInfo!=null && netInfo.getState()==NetworkInfo.State.CONNECTED)
status= true;
}
}catch(Exception e){
e.printStackTrace();
return false;
}
return status;
}

How to read files in Xposed Module class?

Hello, i have XML file by path "/sdcard/testFolder/test.xml", i wanna read this file from my xposed module, code:
Code:
try {
File file = new File("/sdcard/testFolder/test.xml");
BaseService baseService = SELinuxHelper.getAppDataFileService();
InputStream inputStream = baseService.getFileInputStream(file.getAbsolutePath());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document document = documentBuilder.parse(inputStream);
Element element = document.getDocumentElement();
NodeList nodeList = element.getChildNodes();
} catch (Exception e) {
XposedBridge.log(e.getMessage());
}
// and next parsing....
My app have permission to read/write external storage...
In the log next messages: /sdcard/testFolder/test.xml (Permission Denied)

PowerManger permission denied with Magisk and SuperSU

First of all, we need to reboot the device by button click.
Device: Mediatek MT6737M Android 6.0 running Marshmallow
I currently have three devices. One rooted with Magisk another SuperSU the third with Kingroot. In the settings on all three root managers I have it set to grant all permissions. When we try and restart Kingroot flashes a prompt to allow permission which we can work around but Kingroot is riddled with garbage that we don't want. We want to use Magisk or SuperSU because they are clean with less baggage. Magisk and SuperSU however throw an error: java.io.IOException:
Code:
Error running exec(). Command: [/system/bin/su, -c, reboot now] Working Directory: null Environment: null.
This is the code we're using to access PowerManager:
Code:
void reboot() {
if (reboot) {
try {
Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "reboot"});
proc.waitFor();
} catch (Exception ex) {
Log.e(TAG, "Error ", ex);
}
}
}
Is there a way to grant permission to this process using either Magisk or SuperSU? If not is there another rooting management tool or software that is clean and malware free that can achieve that?
NOTE: Code by senior developer I am a Baby, Jr dev here tasked with finding the answer. Please be patient if I ask dumb questions.
Ok so first thing I'll start by saying that you should be making su calls in a background thread. The su call is a blocking call so it will block the UI thread which can cause ANRs (crashes) and overall bad user experience. AsyncTask is perfect for su calls to run in the background instead.
You should also stick with Magisk as it is actively being developed and is the current standard.
Let's get started. Create a new class and call it RootShell.java. Then copy the following code into it.
Java:
class RootShell {
private static void exec(String[] cmd) {
new Execute().execute(cmd);
}
private static class Execute extends AsyncTask<String, Void, Void> {
Override
protected Void doInBackground(String... cmds) {
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd + "\n");
}
os.writeBytes("exit\n");
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
Keep in mind that XDA has awful code formatting so the Override should have the @ symbol in front of it.
Now you can pass single or multiple root commands to this class which will be executed in a background thread. Here's how...
Lets say you're using a button in another class to reboot the phone. This is what it would look like...
Java:
private void rebootButton(View view) {
String[] cmd = { "reboot" };
RootShell.exec(cmd);
}
To pass multiple commands your button would look like this.
This is to reboot into safe mode...
Java:
private void rebootSafeMode(View view) {
String[] cmd = {
"setprop persist.sys.safemode 1",
"setprop ctl.restart zygote"
};
RootShell.exec(cmd);
}
For more info on how to properly su you should check out the following link...
https://su.chainfire.eu/
Hope this helps you with your app and good luck.

Categories

Resources