Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add REQUEST_IGNORE_BATTERY_OPTIMIZATIONS android permission #566

Merged
merged 1 commit into from
May 3, 2022

Conversation

timwr
Copy link
Contributor

@timwr timwr commented Apr 27, 2022

This change adds the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission, which is useful for preventing the app from being killed by Android Doze.
Android Doze was introduced in Android 6.0, and will kill background apps that perform network activity outside set intervals (e.g meterpreter).
Enabling this permission allows us to automatically request (however user interaction is required) that the app ignore these battery optimisations and run freely in the background.

Verification

  • Rebuild with these changes (note the RECORD_AUDIO permission was duplicated and can safely be removed).
  • Get an android meterpreter session on Android 6.0 and above (I tested with Android 9.0 on a samsung device).
  • Run meterpreter > activity_start 'intent:package:com.metasploit.stage#Intent;action=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS;end'
  • Approve the UI on the device to disable battery optimisation.
  • With adb, run adb shell dumpsys deviceidle force-idle, the session should die without this change or if the battery optimization request was denied.
  • Verify the session is not killed when then battery optimisation is disabled.
  • Run adb shell dumpsys deviceidle unforce to disable doze mode.

TODO

@timwr
Copy link
Contributor Author

timwr commented Apr 27, 2022

@@ -29,6 +28,7 @@
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: most applications should not use this; there are many facilities provided by the platform for applications to operate correctly in the various power saving modes. This is only for unusual applications that need to deeply control their own execution, at the potential expense of the user's battery life. Note that these applications greatly run the risk of showing to the user as high power consumers on their device.

Seems like a necessary trade-off

@adfoster-r7
Copy link
Contributor

I wasn't able to force-idle:

shell dumpsys deviceidle force-idle
Unable to go idle; not enabled

This required an extra enable step:

adb shell dumpsys deviceidle enable 

Idle mode enabled

Then it worked; Meterpreter dying:

meterpreter > dir

SEND: #<Rex::Post::Meterpreter::Packet type=Request         tlvs=[
  #<Rex::Post::Meterpreter::Tlv type=COMMAND_ID      meta=INT        value=1008 command=stdapi_fs_getwd>
  #<Rex::Post::Meterpreter::Tlv type=REQUEST_ID      meta=STRING     value="95161577017814001632525063297531">
]>

[-] Error running command dir: Rex::TimeoutError Operation timed out.

Just verifying this PR now 🤞

@adfoster-r7 adfoster-r7 merged commit 2dc2bd2 into rapid7:master May 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants