Skip to main content

DroidHack

A toolkit that provides easy access with procfs and devfs

How to use

First, follow the guide I provided in Setup section, so you can run/debug your python code in any rooted android system (real system, NOT chrooted)

After python dev env being successfully setup, this python package will empower you to control your android phone fully by python.

With this package, you can:

  • search/edit process memory in your phone
  • read/write opcodes in a process, or hook them.
  • dump a module from a running process

With python dev env, you can (DIY):

  • run system calls and library functions with ctypes, e.g libc
  • do any other cool stuffs based on syscalls and libraries.

Setup

GTFO if you cant understand steps below.

DO NOT FOLLOW THE STEPS UNLESS YOU REALLY UNDERSTAND THEM.
or you might just fuck up your phone.

Android Side:

  1. Download & install termux on your ROOTED android system
  2. Verify root by running su in termux. Remember root for termux so magisk automatically allow root requests.
  3. In termux, install packages by running pkg install tsu openssh python cmake clang
  4. In termux, configure openssh by:
    • writing your public key to ~/.ssh/authorized_keys
    • edit ~/../usr/etc/ssh/sshd_config, append StrictModes no and AllowTcpForwarding yes
  5. In termux, run tsu to enter su with termux envs, then run sshd to start openssh server as root.
  6. In termux, run pip install droidhack

PC Side

  1. Install PyCharms
  2. Settings -> Deployment, setup your remote android ssh server here.
    • Server: ssh root@<android_ip> -p 8022
    • Root Path: /
    • Deployment Path: /data/local/tmp/your_project or something similar
  3. Settings -> Project -> Python Interpreter: add deployment target with python interpreter path: /data/data/com.termux/files/usr/bin/python
  4. Debug your pycharm project with remote python interpreter created above.

Note

If you have trouble connecting by remote ip address of android phone, adb forward tcp:8022 tcp:8022 will forward 8022 port from remote to your local address. So you can connected to 127.0.0.1:8022 instead.

PyCharms 2020.1 has a bug in .pycharm_helper, remember to fix the bug manually.

Usage

from capstone import *
from keystone import *
from droidhack.procfs import *
from droidhack.devfs import *

# get a random process
procs = Processes()
msg = procs.find('messaging')
assert len(msg) > 0
msg = msg[0]
# dump a module
msg.dump('libopenjdk.so', '/data/local/tmp/libopenjdk.so')

# search the whole memory
google_msg_ptrs = msg.search(pattern="GNU", limit=5, verbose=False)
for ptr in google_msg_ptrs:
    print(msg.mem.readcstr(ptr).replace('\r', '\n'))

# search memory by module
for m in msg.maps:
    if '.so' in m.path and 'r-' in m.perm:
        # advanced usage
        copyright_addrs = msg.search(pattern="CREATE ", addr=m.addr, size=m.size, limit=1)
        if copyright_addrs:
            print(m.path)
        for ca in copyright_addrs:
            print('    ' + msg.mem.readcstr(ca))


# patch code dynamically
asm = Ks(KS_ARCH_ARM64, KS_MODE_LITTLE_ENDIAN)
dism = Cs(CS_ARCH_ARM64, KS_MODE_LITTLE_ENDIAN)
opcodes = """
    MOV             X19, X0
    MOV             X20, X1
"""
target_codes = asm.asm(opcodes, as_bytes=True)[0]
print(f'finding {opcodes}    => {target_codes}')
possible_targets = msg.search(pattern=target_codes, verbose=True)
# possible_targets = qq.search(pattern=bytearray(target_opcodes).hex(), pattern_type='aob') # example for AOB search
for ptr in possible_targets:
    print(f'found at {ptr:x}, region: {msg.get_ptr_info(ptr)}\ndumping opcodes:')
    for i in dism.disasm(msg.mem.readbuf(ptr, 4 * 4), offset=ptr):
        print(i)
    print('hacking...')
    old_perm = msg.get_perm(ptr)
    msg.set_perm(ptr, 'rwx')
    old_opcodes = msg.mem.readbuf(ptr + 4 * 2, 4)
    msg.mem.writebuf(ptr + 4 * 2, asm.asm('mov x0, x1', as_bytes=True)[0])
    print(f'modified instructions at {ptr:x}, dumping opcodes:')
    for i in dism.disasm(msg.mem.readbuf(ptr, 4 * 4), offset=ptr):
        print(i)
    # restore opcodes
    msg.mem.writebuf(ptr + 4 * 2, old_opcodes)
    print('restore perm')
    msg.set_perm(ptr, old_perm)
    print(f'current ptr info: {msg.get_ptr_info(ptr)}')


# interactive search
candidates = msg.search(int(input('current value:')), limit=0, verbose=True)
while True:
    cand = []
    for c in candidates:
        if msg.search(pattern=int(input('current value:')), addr=c, size=4):
            cand.append(c)
    candidates = cand
    print(f'found {len(candidates)} results.')
    if len(candidates) < 10:
        break

# play with found candidates
pass


# [[unlikely]] RKmem operation
kmem = DevKmem()
sys_open_addr = kmem.get_sym('sys_open')[0].addr
print(f'{sys_open_addr:x}')

Metadata

Release files for droidhack 1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for droidhack 1.3
File Size Uploaded Provenance Actions
droidhack-1.3.tar.gz 13.3 kB Without provenance Details for droidhack-1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for droidhack 1.3
File Interpreter ABI Platform Actions
droidhack-1.3-py3-none-any.whl Python 3 none any Details for droidhack-1.3-py3-none-any.whl

Total release size: 27.4 kB

Release files / droidhack-1.3.tar.gz

Download URL droidhack-1.3.tar.gz
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b92804acdca60a882c733b94a6cc950eeb93e829d6a6579b952c5e97675e8ac1
BLAKE2b-256 checksum
How to use checksums
21f00b8aaf85c4116cba9098d6905342e85321e035969cdedc6c76d7c125f17a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.7.1 requests/2.26.0 setuptools/57.4.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.6.9

Release files / droidhack-1.3-py3-none-any.whl

Download URL droidhack-1.3-py3-none-any.whl
Size 14.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf2c5f2cb32875250c504e41a1a952c7bd175ecbdfd46abb8f24b870282c1d1d
BLAKE2b-256 checksum
How to use checksums
94fd258f529b39d4fb8a632c90ce8476a566f27c454d1d92698c5a8902172c62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.7.1 requests/2.26.0 setuptools/57.4.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.6.9

Security for droidhack 1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page