Skip to content

Commit 10749cd

Browse files
committed
Strong zip protector
1 parent 4d3cfe3 commit 10749cd

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (C) 2022 github.com/REAndroid
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.reandroid.apkeditor.protect;
17+
18+
import com.reandroid.apk.ApkModule;
19+
import com.reandroid.apk.DexFileInputSource;
20+
import com.reandroid.app.AndroidManifest;
21+
import com.reandroid.archive.block.CentralEntryHeader;
22+
import com.reandroid.archive.block.DataDescriptor;
23+
import com.reandroid.archive.block.LocalFileHeader;
24+
import com.reandroid.archive.writer.ApkFileWriter;
25+
import com.reandroid.archive.writer.HeaderInterceptor;
26+
import com.reandroid.arsc.chunk.TableBlock;
27+
28+
import java.io.File;
29+
import java.io.IOException;
30+
import java.util.HashSet;
31+
import java.util.Set;
32+
33+
public class ProtectedFileWriter implements HeaderInterceptor {
34+
35+
private final ApkModule apkModule;
36+
private final File file;
37+
private final Set<String> mProtectedFiles;
38+
39+
public ProtectedFileWriter(ApkModule apkModule, File file) {
40+
this.apkModule = apkModule;
41+
this.file = file;
42+
this.mProtectedFiles = new HashSet<>();
43+
}
44+
45+
public void write() throws IOException {
46+
ApkFileWriter writer = apkModule.createApkFileWriter(this.file);
47+
writer.getInterceptorChain().setHeaderInterceptor(this);
48+
writer.write();
49+
writer.close();
50+
}
51+
52+
@Override
53+
public void onWriteLfh(LocalFileHeader lfh) {
54+
String name = lfh.getFileName();
55+
if (needsProtection(name)) {
56+
mProtectedFiles.add(name);
57+
lfh.getGeneralPurposeFlag().setEncryption(true);
58+
}
59+
}
60+
61+
@Override
62+
public void onWriteDD(DataDescriptor dataDescriptor) {
63+
}
64+
65+
@Override
66+
public void onWriteCeh(CentralEntryHeader ceh) {
67+
if (mProtectedFiles.contains(ceh.getFileName())) {
68+
ceh.getGeneralPurposeFlag().setEncryption(true);
69+
}
70+
}
71+
72+
private boolean needsProtection(String name) {
73+
if (AndroidManifest.FILE_NAME.equals(name)) {
74+
return true;
75+
}
76+
if (TableBlock.FILE_NAME.equals(name)) {
77+
return true;
78+
}
79+
if (name.startsWith("lib/") && name.endsWith(".so")) {
80+
return true;
81+
}
82+
return DexFileInputSource.isDexName(name);
83+
}
84+
}

src/main/java/com/reandroid/apkeditor/protect/Protector.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ public void runCommand() throws IOException {
6161
new TableConfuser(this).confuse();
6262
module.getTableBlock().refresh();
6363
logMessage("Writing apk ...");
64-
module.writeApk(options.outputFile);
64+
if (!options.skipZip) {
65+
logMessage("Protecting zip structure");
66+
new ProtectedFileWriter(module, options.outputFile).write();
67+
} else {
68+
module.writeApk(options.outputFile);
69+
}
6570
module.close();
6671
logMessage("Saved to: " + options.outputFile);
6772
}

src/main/java/com/reandroid/apkeditor/protect/ProtectorOptions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public class ProtectorOptions extends Options {
3535
@OptionArg(name = "-skip-manifest", flag = true, description = "protect_skip_manifest")
3636
public boolean skipManifest;
3737

38+
@OptionArg(name = "-skip-zip", flag = true, description = "protect_skip_zip")
39+
public boolean skipZip;
40+
3841
@OptionArg(name = "-keep-type", description = "protect_keep_type")
3942
public final Set<String> keepTypes = new HashSet<>();
4043

src/main/resources/strings/strings.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ protect_description=Protects/Obfuscates apk resource files. Using unique obfusca
104104
protect_example_1=[Basic]\n java -jar APKEditor.jar p -i path/input.apk -o path/output.apk
105105
protect_keep_type=Keep specific resource type names (e.g drawable), By default keeps only <font> resource type.\n *Can be multiple
106106
protect_skip_manifest=Do not protect manifest.
107+
protect_skip_zip=Do not protect zip structure.
107108
raw_dex=Copy raw dex files / skip smali.
108109
res_dir_name=Sets resource files root dir name. e.g. for obfuscation to move files from 'res/*' to 'r/*' or vice versa.
109110
refactor_description=Refactors obfuscated resource names

0 commit comments

Comments
 (0)