- Today
Memory Modification on Android Without Root: Inside apk-medit
- Taichi Kotake
If a mobile app keeps a score or a currency balance in plaintext in memory, an attacker can find the value and overwrite it. Memory modification is a standard client-side integrity test, and games are the usual target.
The problem on engagements is friction. GameGuardian and similar memory editors are GUI apps that run on the device, usually with root. Mobile games and hardened apps detect root, so you spend time bypassing root checks before you can test whether memory modification works at all.
apk-medit (GitHub) is a command-line memory search and patch tool written in Go. It runs on a non-rooted Android device and does not need the Android NDK. You drive it from your terminal over adb shell, with no overlay on the phone screen.
I wrote apk-medit at Akatsuki Games Inc. for mobile game security assessments, and it was selected for the Black Hat USA 2020 Arsenal. The company later transferred the repository to Sterra Security Co.,Ltd. We maintain it under the MIT License and use it on client engagements and in our training.
How It Works Without Root
You do not need root to inspect an app's memory if the app is debuggable (android:debuggable="true" in AndroidManifest.xml).
Android's run-as command executes a shell under a debuggable app's UID. A process running as that UID can open the app's /proc/<pid>/ entries, and apk-medit uses two of them.
/proc/<pid>/maps lists the process's memory mappings and their permissions. apk-medit parses this file and keeps the regions that are both readable and writable, such as the rw-p lines below. The excerpt comes from a 32-bit ARM device.
$ cat /proc/11283/maps
12c00000-12d40000 rw-p 00000000 00:05 23292 /dev/ashmem/dalvik-main space (region space) (deleted)
12d40000-133c0000 ---p 00140000 00:05 23292 /dev/ashmem/dalvik-main space (region space) (deleted)
13700000-13780000 rw-p 00b00000 00:05 23292 /dev/ashmem/dalvik-main space (region space) (deleted)
6f181000-6f3a6000 rw-p 00000000 fd:01 221 /data/dalvik-cache/arm/system@framework@boot.art
6f3a6000-6f3bc000 r--p 00225000 fd:01 221 /data/dalvik-cache/arm/system@framework@boot.art/proc/<pid>/mem exposes the process's virtual memory as a file. apk-medit reads each region from this file with Go's io.NewSectionReader, so searching does not use ptrace.
By default, patch attaches to the process with ptrace, writes the value with PTRACE_POKEDATA, and detaches. The -without-ptrace flag writes through /proc/<pid>/mem instead (File.WriteAt), which avoids ptrace-based debugger detection, but SELinux blocks that write on Android 10 and later. On a current device, an app that checks for a tracer can detect the patch step.
ptrace is also behind the attach and detach commands. attach stops every thread in the app, which helps when the value you are searching for changes faster than you can type. detach resumes the app.
The whole workflow runs under the app's own UID, so the device stays unrooted and the app's root detection has nothing to find.
The cost is that the APK must be debuggable. If you only have a release build, you repackage and re-sign the APK, and an app that verifies its signature or runs other anti-tampering checks will react to that copy. When the client gives you a debug build, or the app tolerates repackaging, you can start testing memory integrity right away.
Quick Walkthrough
1. Make the APK Debuggable
If you are not testing a debug build, repackage the target APK with android:debuggable="true". Our companion tool, apkutil, decodes the APK, sets the attribute, rebuilds, aligns, and signs the output:
apkutil debuggable target.apkapkutil wraps Apktool and the Android SDK build-tools, and it signs with the keystore you configure in ~/apkutil.json. Set those up first; the apkutil README lists the steps.
Install the rebuilt APK (target.patched.apk) on your non-rooted device or emulator.
2. Push and Launch medit
Download the archive for your target from GitHub Releases. adb shell getprop ro.product.cpu.abi tells you which one you need.
medit_linux_arm64.tar.gzfor physical devices and for emulators on Apple Siliconmedit_linux_amd64.tar.gzfor x86_64 emulators
Extract the medit binary, push it to /data/local/tmp/, and open a shell on the device:
tar -xzf medit_linux_arm64.tar.gz
adb push medit /data/local/tmp/medit
adb shellIn the device shell, switch to the app's UID with run-as. Then copy the binary into the app's data directory and run it. pm list packages lists the package names if you do not know the target's.
run-as com.example.targetapp
cp /data/local/tmp/medit ./medit
./meditOn startup, medit runs its ps command to find the target PID, then shows an interactive prompt.
3. Search, Filter, and Patch
Small or common numbers appear thousands of times in memory, so one search rarely isolates the right address. The usual loop is find, change the value in the app, filter, and then patch:
> find 100
Search UTF-8 String...
Target Value: 100([49 48 48])
Found: 712!!
------------------------
Search Word...
Target Value: 100([100 0])
Found: 6605!!
> filter 94
Check previous results of searching UTF-8 string...
Target Value: 94([57 52])
Found: 0!!
------------------------
Check previous results of searching word...
Target Value: 94([94 0])
Found: 1!!
Address: 0xe7021f70
> patch 5
...
Successfully patched!The demo app below is a game that takes one million taps to crack the egg. The recording shows the app's screen: the counter drops by one per tap until patch sets it to a single digit, and a few more taps clear the game.
Why Go?
Low-level Android tools are usually written in C or C++ and built with the Android NDK. apk-medit is written in Go instead.
No NDK for cross-compilation
GOOS=linux GOARCH=arm64 go build -o medit produces a static binary that runs on Android. GOARCH=amd64 covers x86_64 emulators.
Syscall wrappers
The golang.org/x/sys/unix package wraps the calls apk-medit needs. Attaching to a process is unix.PtraceAttach(pid) instead of C's ptrace(PTRACE_ATTACH, pid, NULL, NULL).
Byte search in the standard library
bytes.Index() scans each region without a hand-written search routine. For the short patterns apk-medit looks for, it runs on assembly implementations on arm64 and amd64.
Recent Maintenance & Improvements
apk-medit was first released in 2020. Recent changes come from what broke on engagements and on students' machines:
x86_64 Android emulator support: Releases now include an
amd64binary alongsidearm64, someditruns on the x86_64 emulators used on Windows and Linux hosts.Windows console compatibility: In Windows Command Prompt and PowerShell, the live command-description panel drifted upward and covered the prompt.
meditnow detects a Windows-like console, prints the command list once at startup, and turns the live panel off. Tab completion still works.
If you also test iOS apps, the iOS counterpart is sterrasec/ipa-medit.
Using apk-medit in Practice
Finding and patching a value in memory does not tell you the impact. Check whether the backend accepts the state you modified or validates it server-side. If find returns nothing for a value you can see on screen, the app may keep it encoded in memory, for example XORed with a key.
In Module 6 of Practical Android App Security Testing, you use apkutil and apk-medit together on a non-rooted device against a lab game APK and a hosted backend API.
apk-medit is open source under the MIT License. Report bugs and send pull requests on GitHub.
Thanks to Akatsuki Games Inc. for supporting the original development of apk-medit and for transferring the project to us.