android emulator root access - android

I need to execute a C program in my App by simply adding the executable to the android project and building the .apk. Then I try to execute the program in my application like this:
Process result = Runtime.getRuntime().exec("su");
String cmd = "PROGRAM_NAME";
DataOutputStream dos = new DataOutputStream(result.getOutputStream());
DataInputStreamdis = new DataInputStream(result.getInputStream());
dos.writeBytes(cmd + "\n");
dos.writeBytes("exit\n");
dos.flush();
I know I need root access to do this so I installed Superuser.apk but that didn't work. Is there another possible way to do this? Btw the code is not fully extended it should just give a look at the way the program should be executed
I'm running the emulator with Android 4.2.1
Edit:
Checking root permission first with
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
DataInputStream osRes = new DataInputStream(suProcess.getInputStream());
if (null != os && null != osRes) {
os.writeBytes("id\n");
os.flush();
String currUid = osRes.readLine();
boolean exitSu = false;
if (null == currUid) {
Log.d("ROOT", "Can't get root access or denied by user");
}

I have got the same problem as you. you can find many answers like here , but they are too old and they are not working anymore (in the new sdk).
I found the best answer here which says that the Security Tips of Android are not allowing to any developer to have root access :
A central design point of the Android security architecture is that no application, by default,
has permission to perform any operations that would adversely impact other applications, the
operating system, or the user. This includes reading or writing the user's private data (such as
contacts or e-mails), reading or writing another application's files, performing network access,
keeping the device awake, etc.
So the only access that you have under the Application Layer is by permissions.

Related

Android - uninstall other system app as system app

My app is running as a system app on a custom AOSP image. I am able to uninstall/install other APKs that I'm downloading from my backend. However, I cannot uninstall other system apps (which is a hard requirement for my purpose). My current mechanism is to use the deletePackage mechanism from the Android package manager via reflection:
val cPackageManager = Class.forName("android.content.pm.PackageManager")
cPackageDeleteObserver = Class.forName("android.content.pm.IPackageDeleteObserver")
deletePackage = cPackageManager.getMethod("deletePackage", String::class.java, cPackageDeleteObserver, Integer.TYPE)
deletePackage!!.invoke(context.packageManager, packageName, deleteObserver, DELETE_ALL_USERS)
Does anybody know how to achieve what I'm trying to do?
Don't you need to be a rooted device to uninstall System apps, or have you found a way to do this without any rooting?
You must remount the system, since you run SELinux, to be able to rearrange the locked files in the system.
3.You can run this code in your app instead
try{
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes("pm uninstall com.package.name");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
}catch(IOException e){
throw new Exception(e);
}catch(InterruptedException e){
throw new Exception(e);
}
if pm uninstall doesn't work, use the rm -rf path/deletefolder then reboot the system

Why can't my Android APP use tcpdump eventhough I change the permission of tcpdump to 777?

I change the permission of /system/xbin/tcpdump to 777 and write the code in my Android APP like below.
But I still get the error message "stderr=tcpdump: any: You don't have permission to capture on that device" and "stderr=(socket: Operation not permitted)".
Is there anyone know what's the problem here?
Thanks.
Process process
String[] cmd = {"/system/xbin/tcpdump", "-i any", "-w /sdcard/tcpdump.pkt"};
TextUtils.join(" ", cmd);
process = Runtime.getRuntime().exec(cmd);
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = errorReader.readLine()) != null) {
Log.d(TAG, "stderr="+line);
}
Android does not support raw sockets, which would include the ability to collect raw frames using a sniffer, by any user space process on non-rooted devices.
If you check the main support site for TCPDump on Android, you'll find that one of the very first requirements is a rooted Android device.

What kind of access can we get after signing an application with platform signature?

I have built a custom ROM and obtained platform key for that build. Now I am creating a system application signed with the platform key.
Now, I want to make the application write some thing in the system/priv-app folder. When I run the application with superuser access in a rooted phone the code works.
But, is such access possible for the application which is signed with platform's key?
I have tried the following :
public class MainActivity extends AppCompatActivity {
private static final String DEBUG_TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Log.i(DEBUG_TAG, "Here");
Process process = Runtime.getRuntime().exec("chmod 777 /system/priv-app");
DataOutputStream dataOutputStream = new DataOutputStream(process.getOutputStream());
dataOutputStream.writeBytes("touch /system/priv-app/foo.txt\n");
dataOutputStream.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine())!= null) {
stringBuilder.append(line + "\n");
}
Log.i(DEBUG_TAG, stringBuilder.toString());
} catch (Exception e) {
e.printStackTrace();
Log.i(DEBUG_TAG, "IO Error");
}
}
}
I do not get any output from the runtime in my application, not even an error but the file is not created in the directory.
Is there an implementation error or such an access is not possible?
A system application has system level access. To remount the system partition you need to have root access, So you won't be able to write to the priv-app directory without "external" help.
If you are building your own custom ROM, you can create a Daemon that will run on the lower levels of the Android stack with root access (refer to Android Booting for details on how to run your executable Daemon on start up). You can communicate with the Daemon via a local socket and use it to remount the system partition and copy the files you want.
You can refer to the following tutorial that explains how to create a native server that will open a socket and wait for incoming connections:
Unix Sockets.
THe system folder is mounted as read-only by default. With root access you can remount it, but I wouldn't suggest running like that for longer than necessary (longer than it takes to install your app in the priv-app folder). For one thing if its mounted rw and you do a factory reset you'll brick your device (unless you have a recovery partition).

Read the system log on Android 4.1

Now , I want to read the system log on android 4.1 . But just can only get the application itself log . My code as following:
Process logcatProc = Runtime.getRuntime().exec(new String[] { "logcat","ActivityManager:V","*:S" });
reader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
Log.e("log",line);
}
it Seems can work on android 4.0 or previous version. anybody have tried this ? Or i need the root permission ?
System logs changed in 4.1 and are now accessible only by system apps. (Source: Google I/O Video.) This is an exception on rooted devices, of course, if the installed app is set to be a system app.
Aside from this, there is no way to access the system logs on Android 4.1+. Note: End users can access the system log by using the (very finicky) shortcut of Power + Vol Up + Vol Down.

required root permissions to an android application

I am writing an application to access the many of the system device nodes. To open the device nodes, I wrote native methods, When I am trying to execute it, I am unable to open the device node as there is no root permissions to my application. Could any one please tell give root permission to my android application. device details: android 2.0.1 - motorola milestone.
rtc_fd=open("/dev/rtc",0777);
if(rtc_fd == -1) {
__android_log_write(ANDROID_LOG_ERROR, "","UNABLE TO OPEN THE DEVICE....");
strcpy(result_string,"Fail: /dev/rtc open error\n");
__android_log_write(ANDROID_LOG_ERROR, ""," DEVICE...ERROR. ");
return result_string;
}
ret = ioctl(rtc_fd, RTC_RD_TIME, &rtc_tm);
if (ret == -1) {
strcpy(result_string,"Fail: rtc ioctl RTC_RD_TIME error\r\n");
return result_string;
}
It is always saying UNABLE TO OPEN DEVICE, could any one please suggest a solution to open a device node.
First and foremost, you will obviously need a rooted phone for any of this to work.
That being said, Android does not allow for a user-application to gain super-user rights, even on a rooted phone. Instead, it allows you to launch a new process with super-user rights.
The easiest method for running things as super-user is to create a virtual terminal, as follows:
Process proc = Runtime.getRuntime().exec("su");
DataOutputStream standard_in = new DataOutputStream(proc.getOutputStream());
DataInputStream standard_out = new DataInputStream(proc.getInputStream());
Using the input and output streams you now effectively have console access as root, which you can use to run typical command-line commands, or to run the process that accesses your device for you.

Categories

Resources