ngr protect
Obfuscates the names and paths of the resources of an apk and its binary manifest: it is the opposite of
ngr refactor. You use it to leave an apk with resources that are harder to read. The apk needs a
resources.arsc. The output does not verify: it has to be signed again. And ngr can no longer read it.
Usage
ngr protect [options] <apk>
Inputs
| Input | What it is |
|---|---|
<apk> |
An apk file with resources.arsc |
Options
| Option | What it does | Default |
|---|---|---|
-o, --output <file> |
The apk to write | <name>_protected.apk next to the apk |
-f, --force |
Overwrites the output if it already exists | |
--skip-manifest |
Does not touch AndroidManifest.xml. The output is then deterministic |
|
--confuse-zip |
Marks the resource entries as encrypted in the zip headers | |
--keep-type <type> |
A resource type that is not renamed, or all-types. If you give any, the default is lost. Can be repeated |
font |
-h, --help |
Shows the help |
What it writes
The new apk, <name>_protected.apk unless you say otherwise. The final line goes to stderr:
wrote app_protected.apk
What changes in a sample apk, comparing the list of zip entries before and after. Before:
res/anim/fade_in.xml
res/animator/pulse.xml
res/color/text_states.xml
res/drawable/bg_selector.xml
After, with --skip-manifest:
AndroidManifest.xml
AndroidManifest.xml/..xml
/AndroidManifest.xml///.xml
resources.arsc////.xml
/resources.arsc/////.xml
classes.dex/\\\\.xml
...
The resource paths become names such as AndroidManifest.xml/..xml or classes.dex/\\\\.xml. The apk has the same
number of entries.
Without --skip-manifest, each run comes out different: the binary manifest is obfuscated at random, and two
runs on the same apk give files with different sums. With --skip-manifest the manifest stays as it was, and two
runs give the same file byte for byte:
ngr protect --skip-manifest -o p1.apk app.apk
ngr protect --skip-manifest -o p2.apk app.apk
shasum -a 256 p1.apk p2.apk
07fc3615668a... p1.apk
07fc3615668a... p2.apk
With --confuse-zip, the resource entries are marked as encrypted in the zip headers: an ordinary zip program skips
them and asks for a password that does not exist, although ngr still reads the zip.
--keep-type leaves the resources of the type you name unrenamed, and the files of that type keep their path: with
--keep-type anim, res/anim/fade_in.xml stays where it was. With --keep-type all-types no type is renamed: the
paths of all the resource files stay as they were.
Examples
Obfuscate an apk:
ngr protect app.apk
Obfuscate without touching the manifest, to get a reproducible result:
ngr protect --skip-manifest -o app-protected.apk app.apk
Leave the layouts and animations with their names and paths, and obfuscate the rest. --keep-type is repeated once
per type:
ngr protect --keep-type layout --keep-type anim app.apk
Obfuscate and sign, which is what you need to install it:
ngr protect app.apk
ngr sign --ks release.p12 --ks-pass env:KS_PASS app_protected.apk
Exit codes
| Code | When |
|---|---|
| 0 | The apk was written. Also with an apk that was already protected, which is left untouched and writes nothing |
| 1 | The apk does not exist or cannot be read, has no resources.arsc, or the output already exists and you did not pass -f |
| 2 | The options are wrong: <apk> is missing, there is an extra input or the option does not exist |
Things to know
- The output does not verify. It keeps the original signature over content that has changed, and
ngr verifyends with 3. Sign the result withngr signbefore installing it. ngrdoes not read the result. An apk obfuscated byprotecthas entries underAndroidManifest.xml/andclasses.dex/, andngrtreats it as protected:info,resource,xmlandrefactorwriteWARN: PROTECTEDorPROTECTED, give no report and end with 0, anddecodewrites no folder. To see what has changed, list the zip entries. With--keep-type all-typesthe paths are kept, but the resource table is not read either:
ngr info: unable to read 'app.apk': arsc string pool count 7602280 exceeds remaining DEX data.
- A protected apk is not protected again. If you pass
protectan apk that already has those entries, it writes its path andPROTECTEDto stderr, writes nothing and ends with 0. - An apk without
resources.arscis not protected.protectends with 1:
ngr protect: unable to protect 'noarsc.apk': the input APK has no resources.arsc.
- Without
--skip-manifest, each run gives a different apk, even if nothing has changed in the input. --keep-typedoes not validate the name. A type that does not exist gives no error and changes nothing.
See also
ngr refactor, the opposite: gives readable names to the resources.ngr sign, to sign the output.ngr verify, to check it.