Android command line java

Starting Java Service on Android through Command Line

To initiate a service or activity, it is necessary to include a tag in manifest.xml. One possible solution is to use the ADB shell to change the dalvik-cache directory to a writable directory. Another solution involves running shell commands programmatically, which can be achieved by following the steps outlined in this Stack Overflow answer: https://stackoverflow.com/a/3350332/2425851.

Why starting a service from command line in Android needs root access (su)?

Create an Intent object with ACTION_VIEW action, and specify the package name as «com.ang.chapter_2_service» that you want to launch.

To start the desired service, specify its full name using the variable className, which is assigned the value «com.ang.chapter_2.poolBinder.BinderPoolService». Then, use the setClassName method on the intent object to set the class name to the specified value. Finally, either start the service using startService or bind to it using bindService with the appropriate parameters.

the activity is same to this.

It is necessary to include a tag in the manifest.xml file for the service or activity that you wish to initiate.

Java — How can I return to the start of a line in a console?, If you just want to write a new line to the console, you should use the println () method: System.out.println (mystuff); However, this will not delete what is already on the line. Actually, since System.out is a PrintStream, which is a type of OutputStream, that is basically hard to do, although you may find terminal …

Running android java based command-line utility from adb shell [duplicate]

Using the ADB shell, it is possible to assign a different directory for the dalvik-cache with the ability to write to it.

Before running your module, give this a try.

  • Create a directory called «dalvik-cache» within the «/data/local/tmp» folder.
  • Set the environment variable for Android data to the directory path «/data/local/tmp».

The Android package manager is not handling your recent command, so the VM will attempt to generate the /data/dalvik-cache code upon the initial execution. However, to do this, you must have root access.

An alternative approach is to incorporate the command as a standard application, where the main activity remains inactive, and the package manager selects the location to execute the command-line entry point class. Additionally, this approach enables you to control and remove the application from the settings user interface.

Читайте также:  Java массивы добавление элементов

In case you are not planning to widely distribute it to others, this post provides the solution if you are only using it for personal use.

Java — Executing commands inside my app, Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. java android command-line. Share. Follow edited May 21, 2018 at 17:16. Phantômaxx. 37.4k

Executing commands inside my app

Achieving this task may require rooting your app. Therefore, it is advisable to attempt it on a rooted device.

After successfully rooting, you may verify root permissions before proceeding with the tasks at hand.

To verify root privileges as stated in this location:

 public boolean isRooted() < // get from build info String buildTags = android.os.Build.TAGS; if (buildTags != null && buildTags.contains("test-keys")) < return true; >// check if /system/app/Superuser.apk is present try < File file = new File("/system/app/Superuser.apk"); if (file.exists()) < return true; >> catch (Exception exception) < // ignore exception.printStackTrace(); >String[] commands = < "/system/xbin/which su", "/system/bin/which su", "which su" >; for (String command : commands) < try < Runtime.getRuntime().exec(command); return true; >catch (Exception exception) < exception.printStackTrace(); >> Log.d("message-startUp: ", "RootUtil"); return false; > 

I’ve encountered the null working directory error multiple times, and each time it was due to an issue with my command. Here’s a suggestion to help resolve it:

Process proc = Runtime.getRuntime().exec(new String[] < "su", "-c", "input keyevent 26">); proc.waitFor(); 

At the very least, this should resolve the error related to a nonexistent environment.

Can’t compile Android/Java project using command line, Yes. It’s at the location in Program Files. – JHowzer. Jun 9, 2012 at 20:48. @JHowser, If you know where your Android SDK is, then CLASSPATH should be set to that directory where the JAR files are. The CLASSPATH may contain several directories. Simply keep adding them until no more compile errors. – The …

How to run terminal command in Android application?

To invoke android.os.Exec.createSubprocess(), reflection is required.

public String ls () < Class execClass = Class.forName("android.os.Exec"); Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class); int[] pid = new int[1]; FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(null, "/system/bin/ls", "/", null, pid); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fd))); String output = ""; try < String line; while ((line = reader.readLine()) != null) < output += line + "\n"; >> catch (IOException e) <> return output; > 

There are various solutions available at http://code.google.com/p/market-enabler/wiki/ShellCommands, which I haven’t tried yet.

Consider this solution for executing shell commands on Android programmatically, which can be found at https://stackoverflow.com/a/3350332/2425851.

Java — execute shell command from android, I’m trying to execute this command from the application emulator terminal (you can find it in google play) in this app i write su and press enter, so write: screenrecord —time-limit 10 /sdcard/MyVideo.mp4. and press again enter and start the recording of the screen using the new function of android kitkat.

Читайте также:  Net java games input jar

Источник

Android Command Line Reverse Shell in Java

You can find a lot of resources on how to inject meterpreter to APK for getting a reverse shell. What I want to address in this post is a simple tutorial on how to get your interactive remote command line access in case you already obtained command execution on the target device. In my case, I needed this to make an exploitable Android application where you got command line code execution by taking advantage of deserialization vulnerability. Because googling on how to do that took me more than 10 minutes and taught me something new, I decided to share it here.

There is nothing novel, it’s just a way of compiling Java code to be executed by ART/Dalvik on Android’s command line.

Copy & paste part

First you need some Java reverse shell code. You can use following, which is basically just a verbose version of one GitHub gist I found:

Then you have compile it to Java bytecode, convert it to DEX format and create JAR file (substitute path to Android SDK’s dx utility for your path and version):

# compile Java source code javac ReverseShell.java # convert to DEX format /path/to/your/android/sdk/build-tools/28.0.3/dx --dex --output classes.dex ReverseShell.class # create fake JAR file zip ars.jar classes.dex 

Now, deliver JAR file to target device. You can use curl through your command execution vulnerability on the device (if you got lucky and your target Android has it). Here, I fake this step using ADB:

Finally, by utilizing your command execution you start Java reverse shell from Android’s CLI using dalvikvm command like this (note the path to ars.jar delivered in previous step):

/system/bin/dalvikvm -cp /sdcard/ars.jar ReverseShell 

Final note

I should also mention that you might use much simpler solution for reverse shell using sh as this one here:

But it might not be working for you and then you can use the solution above.

Источник

Run ADB Commands in Java Code for Device Manipulation

Android Debug Bridge (adb) is a command-line tool. It lets you interact with an Android device. The adb commands facilitate a variety of device actions, such as installing and debugging apps, changing battery level, turning on/off the wifi etc. It has three components:

  • A client , which sends commands. The client runs on your development machine. You can invoke a client from a command-line terminal by issuing an adb command.
  • A daemon (adbd) , which runs commands on a device. The daemon runs as a background process on each device.
  • A server , which manages communication between the client and the daemon. The server runs as a background process on your development machine.
Читайте также:  Nbsp html как сделать

Today I’ll explain how to manipulate your mobile device’s battery level. As you all know, mobile testing is trivial. There are many components that change the state of the device. Those components are the battery, network and many more.

What happens after the battery is below %15? When your phone gets into power saver mode, network turns itself to off state to save your battery power. After you start using the app, IP address change. If there’s an IP restriction on your app, your session will be terminated. That’s a business rule in finance app’s in Turkey.

Generally, those situations are tested manually. So how can we implement those situations in our mobile automation testing process?

Prerequisite

You need to install Android SDK Platform-Tools package. Here is the website to install it.

Battery Commands

dumpsys battery set level %s – It let you change the level from 0 to 100

dumpsys battery set status %s – It let you change the level to Unknown, Charging, Discharging, Not Charging or Full.

dumpsys battery reset – It let you reset the battery change made through adb.

dumpsys battery set usb %s – It let you change the status of USB connection. It can be ON or OFF.

Using those command via command line

Some command requires device Id, some does not. In order to get the unique device id, you should run “ adb devices ” command in command line.

Then you should run your command with below syntax.

adb -s DEVICE_ID shell COMMAND or adb shell COMMAND

So, if you want to change battery level to 5, you should run

adb -s DEVICE_ID shell dumpsys battery set level 5

Here’s the project that we just started.

A sample function from our BatteryCommand class

public class BatteryCommand extends Command < private static final String batteryLevelCommand ="dumpsys battery set level %s"; private static final String batteryStatusCommand ="dumpsys battery set status %s"; private static final String batteryResetCommand ="dumpsys battery reset"; private static final String batteryUSBCommand ="dumpsys battery set usb %s"; public void setBatteryLevel(String level) throws IOException < ProcessExecutor.exec(getDeviceId(),String.format(batteryLevelCommand,level)); System.out.print("Battery leve is set to "+level); >>

Process Executor class to execute ADB commands.

public static void exec(String deviceId,String command) throws IOException < Process process = null; String commandString; if(deviceId != null) < commandString = String.format("%s", "adb -s " + deviceId + " shell " + command); >else commandString = String.format("%s", "adb shell " + command); System.out.print("Command is "+commandString+"\n"); try < process = ProcessHelper.runTimeExec(commandString); >catch (IOException e) < >BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) < System.out.print(line+"\n"); >>

Sample test class to execute our commands.

@Test public void testBattery() throws IOException, InterruptedException

Here’s the result of the execution

GitHub Project

Источник

Оцените статью