[TUT]Execute Commands with SU Permission - C++ or Other Android Development Languages

You want to execute commands with super-user rights? Just use below.
Code:
......
using Java.IO;
......
Code:
private string RunCmdsAsRoot(string[] Cmds)
{
Java.Lang.Process Process = Java.Lang.Runtime.GetRuntime().Exec("su");
Java.IO.OutputStreamWriter OutputStream = new OutputStreamWriter(Process.OutputStream);
foreach(string cmd in Cmds)
{
OutputStream.Write(cmd + "\n");
OutputStream.Flush();
}
OutputStream.Write("exit\n");
OutputStream.Flush();
System.IO.StreamReader OutputStreamReader = new System.IO.StreamReader(Process.InputStream);
string Output = OutputStreamReader.ReadToEnd();
OutputStreamReader.Close();
return Output;
}
HAAPPY CODING!

Example:-
Code:
string[] commands = {"mount", "ls"};
RunCmdsAsRoot(commands);
Sent from my SonyX8 using Tapatalk 2

Related

Modify apk to add a file in a specified path

Hello all. I need some information on how i can modify an existing apk and add a file to a specified path during installation.
Better...i need that installation creates a file in data/data/com.myprogram.android/myfile where com.myprogram.android is the path of program data. Is that possible???
thx
*bump* - after one year, this is exactly what i also need!
any answer to this? since i modify an existing .apk, changing the java-code is not an option. solution must be pure .apk based / via the manifest
you could do it in a few steps
1) copy the apk to somewhere safe (/mnt/sdcard)
2) uninstall the apk, maybe use PackageManager
3) unzip the apk to a folder, add your changed/new files, zip up the folder again (extension .apk). Use busybox's zip/unzip if you need
4)sign the new apk using this (i tried it it works)
5)install the new+signed apk, maybe use PackageManager
fl3xo said:
Hello all. I need some information on how i can modify an existing apk and add a file to a specified path during installation.
Better...i need that installation creates a file in data/data/com.myprogram.android/myfile where com.myprogram.android is the path of program data. Is that possible???
thx
Click to expand...
Click to collapse
The way I've done this in the past is just store whatever it is you want to add in the res/raw directory of the project, then when the program first runs, copy the raw resource wherever you want it in the tree.
Code:
// Copy the helper app from resources to an executable in our classpath
protected void CopyExtraBin() {
// check if it's already there
File helper = new File("/data/data/com.mypath.myprog/helper_app");
if (helper.exists()) {
// already there, nothing to do.
return;
}
InputStream setdbStream = getResources().openRawResource(R.raw.helper_app);
try {
byte[] bytes = new byte[setdbStream.available()];
DataInputStream dis = new DataInputStream(setdbStream);
dis.readFully(bytes);
FileOutputStream setdbOutStream = new FileOutputStream(
"/data/data/com.mypath.myprog/helper_app");
setdbOutStream.write(bytes);
setdbOutStream.close();
// set executable permissions on our helper
Process process = Runtime.getRuntime().exec("sh");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("chmod 755 /data/data/com.mypath.myprog/helper_app\n");
os.writeBytes("exit\n");
os.flush();
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
return;
}
}
Gene Poole said:
The way I've done this in the past is just store whatever it is you want to add in the res/raw directory of the project, then when the program first runs, copy the raw resource wherever you want it in the tree.
Code:
// Copy the helper app from resources to an executable in our classpath
protected void CopyExtraBin() {
// check if it's already there
File helper = new File("/data/data/com.mypath.myprog/helper_app");
if (helper.exists()) {
// already there, nothing to do.
return;
}
InputStream setdbStream = getResources().openRawResource(R.raw.helper_app);
try {
byte[] bytes = new byte[setdbStream.available()];
DataInputStream dis = new DataInputStream(setdbStream);
dis.readFully(bytes);
FileOutputStream setdbOutStream = new FileOutputStream(
"/data/data/com.mypath.myprog/helper_app");
setdbOutStream.write(bytes);
setdbOutStream.close();
// set executable permissions on our helper
Process process = Runtime.getRuntime().exec("sh");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("chmod 755 /data/data/com.mypath.myprog/helper_app\n");
os.writeBytes("exit\n");
os.flush();
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
return;
}
}
Click to expand...
Click to collapse
where to insert it?
Moved to Q&A.
mishanet said:
Gene Poole said:
The way I've done this in the past is just store whatever it is you want to add in the res/raw directory of the project, then when the program first runs, copy the raw resource wherever you want it in the tree.
Code:
// Copy the helper app from resources to an executable in our classpath
protected void CopyExtraBin() {
// check if it's already there
File helper = new File("/data/data/com.mypath.myprog/helper_app");
if (helper.exists()) {
// already there, nothing to do.
return;
}
InputStream setdbStream = getResources().openRawResource(R.raw.helper_app);
try {
byte[] bytes = new byte[setdbStream.available()];
DataInputStream dis = new DataInputStream(setdbStream);
dis.readFully(bytes);
FileOutputStream setdbOutStream = new FileOutputStream(
"/data/data/com.mypath.myprog/helper_app");
setdbOutStream.write(bytes);
setdbOutStream.close();
// set executable permissions on our helper
Process process = Runtime.getRuntime().exec("sh");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("chmod 755 /data/data/com.mypath.myprog/helper_app\n");
os.writeBytes("exit\n");
os.flush();
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
return;
}
}
Click to expand...
Click to collapse
where to insert it?
Click to expand...
Click to collapse
Yeah I have the same question. I'm wanting to add some files to /data/data/<appdirectory>/lib but since it's permissions are set to drwxr-xr-x system system I can't write to it without changing apk itself.
I tried putting this into some code and I just alot of cannot find symbol errors and DataInputStream and Toast. I'm sure It's just my limited knowledge and maybe changes in android but this example code doesn't seem to work for me.
Gene Poole said:
The way I've done this in the past is just store whatever it is you want to add in the res/raw directory of the project, then when the program first runs, copy the raw resource wherever you want it in the tree.
Code:
// Copy the helper app from resources to an executable in our classpath
protected void CopyExtraBin() {
// check if it's already there
File helper = new File("/data/data/com.mypath.myprog/helper_app");
if (helper.exists()) {
// already there, nothing to do.
return;
}
InputStream setdbStream = getResources().openRawResource(R.raw.helper_app);
try {
byte[] bytes = new byte[setdbStream.available()];
DataInputStream dis = new DataInputStream(setdbStream);
dis.readFully(bytes);
FileOutputStream setdbOutStream = new FileOutputStream(
"/data/data/com.mypath.myprog/helper_app");
setdbOutStream.write(bytes);
setdbOutStream.close();
// set executable permissions on our helper
Process process = Runtime.getRuntime().exec("sh");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("chmod 755 /data/data/com.mypath.myprog/helper_app\n");
os.writeBytes("exit\n");
os.flush();
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
return;
}
}
Click to expand...
Click to collapse

[Q] own global settings (Settings.System)

Hi all,
I am currently doing some changes to the CM7-Framework and therefore need a custom option.
I wrote a simple app to test it and it doesn't work. The app has 3 Buttons
turnOn, turnOff and check
and one TextView for output.
Clicking turnOn does:
Code:
boolean success = Settings.System.putInt(getContentResolver(), "USE_BUTTONS_ON_NOTIFICATON", 1);
textView1.setText("set USE_BUTTONS_ON_NOTIFICATION to 1, success: " + success);
Clicking turnOff does:
Code:
boolean success = Settings.System.putInt(getContentResolver(), "USE_BUTTONS_ON_NOTIFICATON", 0);
textView1.setText("set USE_BUTTONS_ON_NOTIFICATION to 0, success: " + success);
and check does:
Code:
try {
int value = Settings.System.getInt(getContentResolver(), "USE_BUTTONS_ON_NOTIFICATION");
textView1.setText("USE_BUTTONS_ON_NOTIFICATION is: " + value);
} catch (SettingNotFoundException e) {
textView1.setText("Setting not found");
e.printStackTrace();
}
Both buttons return true, but check always says that the option is 1.
Is it even possible to change Settings like this? The app has the WRITE_SETTINGS permission.
If it is not possible, what would be a good way to ìmplement an option set by an app and read by a class inside the framework?

ADB Tools with C#

Hello XDA,
I am writing ADB TOOLs in C# but having a problem with PATH recognition
Code:
private void button6_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "Select Kernel File";
openFileDialog1.FileName = "Choose File";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckFileExists = true;
openFileDialog1.Filter = ".IMG|*.img";
if (openFileDialog1 .ShowDialog () == DialogResult .OK)
{
label1.Text = openFileDialog1 .FileName;
var process = Process.Start("CMD.exe", "/c fastboot flash boot " [COLOR="Red"]+ textBox2.Text[/COLOR] );
process.WaitForExit();
}
The red marked code has a problem i think, when i select a kernel image file located inside multiple directory to flash the CMD opens and just closes in a second nothing actually happens in a phone but when i use a file from a desktop it flashes w/o any problem.
I tried using path.combine( ) but really don't know how to use it, just started writing things in C#.
Thanks!
Bump!:fingers-crossed:
Bump Bump:crying:

Runtime exec not behaving like adb exec

I've built a C++ native Android app with NDK toolcahain.
I've first tested the app using the adb tool and got the following output:
Code:
generic:/ $ run-as cardservice.hr.cardservice
generic:/data/data/cardservice.hr.cardservice $ cd card/
generic:/data/data/cardservice.hr.cardservice/card/ $ ./cardService
2016-11-30 20:36:06,245 INFO [default] Starting CardService
App continues to run
But problem starts when I try to do the same thing from a Android application.
The following code:
Code:
private void exec_command() throws IOException, InterruptedException {
String command = "./data/data/cardservice.hr.cardservice/card/cardService"
Process check_card_service = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(check_card_service.getInputStream()));
String line;
String content = "";
while ((line = in.readLine()) != null) {
System.out.println(line);
content = content + line;
}
check_card_service.waitFor();
};
It starts the application but the application fails, and stops working.
Code:
2016-11-30 19:25:11,417 INFO [default] Starting CardService
2016-11-30 19:25:11,447 INFO [default] Underlying Transport Error, Code: websocketpp.transport.asio:3
So my question is what is different in my approaches, what changes if i run it from my app or from the adb shell?
The application does not need root access.
Do you need any extra information?

How to use the root permission screen capture?

This is my code.
Code:
Process p = Runtime.getRuntime().exec("su");
InputStream inputStream = p.getInputStream();
OutputStream outputStream = p.getOutputStream();
outputStream.write("screencap\n".getBytes());
outputStream.flush();
sleep(1000);
byte[] b = new byte[inputStream.read()];
inputStream.read(b);
System.out.println(b.length);
But b is empty.
I can get back by running screencap with adb. And I don't need to save to disk; I need to identify the image content.

Categories

Resources