From 19356cfabf6237aaaebeac19b53392a93738fc54 Mon Sep 17 00:00:00 2001 From: RPJosh Date: Sun, 12 Nov 2023 20:15:30 +0100 Subject: [PATCH] Add option to remove existing permissions --- .gitignore | 4 +++ README.md | 5 ++++ run.sh | 3 +- .../com/wind/meditor/ManifestEditorMain.java | 8 +++++ .../property/ModificationProperty.java | 11 +++++++ .../meditor/visitor/ManifestTagVisitor.java | 10 ++++--- .../visitor/UserPermissionTagVisitor.java | 29 +++++++++++++++++-- .../java/pxb/android/axml/AxmlWriter.java | 16 +++++++--- .../java/pxb/android/axml/NodeVisitor.java | 2 ++ 9 files changed, 76 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 4b97423..4e6bcd9 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,7 @@ /captures .externalNativeBuild .cxx +/.project +/.classpath +/.settings +/bin \ No newline at end of file diff --git a/README.md b/README.md index 8e7df5d..e2df202 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ # **ManifestEditor** + +Fork from [WindySha/ManifestEditor](https://github.com/WindySha/ManifestEditor) with additional feature to remove previously defined permissions in android Manifest file. + +--- + This is a tool used to modify Android Manifest binary file. 此工具用于修改AndroidManifest二进制文件。比如,更改Manifest文件中的app包名,版本号,更改或新增app入口Application的类名,更改或新增debuggable的属性,增加usesPermission标签,增加meta-data标签等。 同时,为了更方便使用,提供了直接修改Apk包中的Manifest文件,并对修改后的Apk进行签名的功能。 diff --git a/run.sh b/run.sh index 662153e..d144dd4 100755 --- a/run.sh +++ b/run.sh @@ -2,4 +2,5 @@ ./gradlew build # Run the app (add your options here) -java -jar build/dist/ManifestEditor.jar --help \ No newline at end of file +java -jar build/dist/ManifestEditor.jar /mnt/entwicklung/git/RPdb/client/android/app/build/outputs/apk/debug/AndroidManifest.xml --deleteUsesPermission "android.permission.REQUEST_INSTALL_PACKAGES" --output \ + /mnt/entwicklung/git/RPdb/client/android/app/build/outputs/apk/debug/AndroidManifest2.xml --force \ No newline at end of file diff --git a/src/main/java/com/wind/meditor/ManifestEditorMain.java b/src/main/java/com/wind/meditor/ManifestEditorMain.java index 3d17c84..87005a3 100644 --- a/src/main/java/com/wind/meditor/ManifestEditorMain.java +++ b/src/main/java/com/wind/meditor/ManifestEditorMain.java @@ -60,6 +60,10 @@ public class ManifestEditorMain extends BaseCommand { "name to the manifest file, multi option is supported", argName = "uses-permission-name") private List usesPermissionList = new ArrayList<>(); + @Opt(opt = "dup", longOpt = "deleteUsesPermission", description = "delete the app uses permission " + + "name to the manifest file, multi option is supported", argName = "delete-uses-permission-name") + private List deletePermissionList = new ArrayList<>(); + @Opt(opt = "ma", longOpt = "manifestAttribute", description = "set the app manifest attribute, " + " name and value should be separated by " + MULTI_NAME_SEPERATER + " , if name is in android namespace, prefix \"" + ANDROID_NAMESPACE + "\" should be set" + @@ -226,6 +230,10 @@ public class ManifestEditorMain extends BaseCommand { property.addUsesPermission(permission); } + for (String permission: deletePermissionList ) { + property.deleteAddUsesPermission(permission); + } + for (String manfestAttr : manifestAttributeList) { String[] nameValue = manfestAttr.split(MULTI_NAME_SEPERATER); if (nameValue.length == 2) { diff --git a/src/main/java/com/wind/meditor/property/ModificationProperty.java b/src/main/java/com/wind/meditor/property/ModificationProperty.java index cd600ba..eccb4cc 100644 --- a/src/main/java/com/wind/meditor/property/ModificationProperty.java +++ b/src/main/java/com/wind/meditor/property/ModificationProperty.java @@ -10,6 +10,7 @@ import java.util.List; */ public class ModificationProperty { + private List deleteUsesPermissionList = new ArrayList<>(); private List usesPermissionList = new ArrayList<>(); private List metaDataList = new ArrayList<>(); private List deleteMetaDataList = new ArrayList<>(); @@ -22,11 +23,21 @@ public class ModificationProperty { return usesPermissionList; } + public List getDeleteUsesPermissionList() { + return deleteUsesPermissionList; + } + + public ModificationProperty addUsesPermission(String permissionName) { usesPermissionList.add(permissionName); return this; } + public ModificationProperty deleteAddUsesPermission(String permissionName) { + deleteUsesPermissionList.add(permissionName); + return this; + } + public List getApplicationAttributeList() { return applicationAttributeList; } diff --git a/src/main/java/com/wind/meditor/visitor/ManifestTagVisitor.java b/src/main/java/com/wind/meditor/visitor/ManifestTagVisitor.java index 2c8b461..cafd550 100644 --- a/src/main/java/com/wind/meditor/visitor/ManifestTagVisitor.java +++ b/src/main/java/com/wind/meditor/visitor/ManifestTagVisitor.java @@ -9,6 +9,9 @@ import java.util.List; import pxb.android.axml.NodeVisitor; +/** + * Base class where all manifest tags are processed and delegated + */ public class ManifestTagVisitor extends ModifyAttributeVisitor { private ModificationProperty properties; @@ -24,11 +27,9 @@ public class ManifestTagVisitor extends ModifyAttributeVisitor { @Override public NodeVisitor child(String ns, String name) { - Log.d(" ManifestTagVisitor child --> ns = " + ns + " name = " + name); - if (ns != null && (NodeValue.UsesPermission.TAG_NAME).equals(name)) { NodeVisitor child = super.child(null, NodeValue.UsesPermission.TAG_NAME); - return new UserPermissionTagVisitor(child, null, ns); + return new UserPermissionTagVisitor(child, null, ns, properties.getDeleteUsesPermissionList()); } NodeVisitor child = super.child(ns, name); @@ -42,7 +43,7 @@ public class ManifestTagVisitor extends ModifyAttributeVisitor { } if (NodeValue.UsesPermission.TAG_NAME.equals(name)) { - return new UserPermissionTagVisitor(child, getUsesPermissionGetter(), null); + return new UserPermissionTagVisitor(child, getUsesPermissionGetter(), null, properties.getDeleteUsesPermissionList()); } return child; } @@ -51,6 +52,7 @@ public class ManifestTagVisitor extends ModifyAttributeVisitor { public void attr(String ns, String name, int resourceId, int type, Object obj) { Log.d(" ManifestTagVisitor attr --> ns = " + ns + " name = " + name + " resourceId=" + resourceId + " obj = " + obj); + super.attr(ns, name, resourceId, type, obj); } diff --git a/src/main/java/com/wind/meditor/visitor/UserPermissionTagVisitor.java b/src/main/java/com/wind/meditor/visitor/UserPermissionTagVisitor.java index c75690e..968bbe1 100644 --- a/src/main/java/com/wind/meditor/visitor/UserPermissionTagVisitor.java +++ b/src/main/java/com/wind/meditor/visitor/UserPermissionTagVisitor.java @@ -1,5 +1,7 @@ package com.wind.meditor.visitor; +import java.util.List; + import com.wind.meditor.property.AttributeItem; import com.wind.meditor.utils.NodeValue; import com.wind.meditor.utils.Utils; @@ -9,10 +11,12 @@ import pxb.android.axml.NodeVisitor; class UserPermissionTagVisitor extends NodeVisitor { private IUsesPermissionGetter permissionGetter; + private List deletedPermissions; - UserPermissionTagVisitor(NodeVisitor nv, IUsesPermissionGetter permissionGetter, String permissionTobeAdded) { + UserPermissionTagVisitor(NodeVisitor nv, IUsesPermissionGetter permissionGetter, String permissionTobeAdded, List deletedPermission) { super(nv); this.permissionGetter = permissionGetter; + this.deletedPermissions = deletedPermission; if (!Utils.isNullOrEmpty(permissionTobeAdded)) { AttributeItem attributeItem = new AttributeItem(NodeValue.UsesPermission.NAME, permissionTobeAdded); @@ -26,13 +30,32 @@ class UserPermissionTagVisitor extends NodeVisitor { @Override public void attr(String ns, String name, int resourceId, int type, Object obj) { - if (obj instanceof String && permissionGetter != null) { - permissionGetter.onPermissionGetted((String) obj); + if (obj instanceof String) { + // Check if the permission is contained in the list of permission to delete + if (deletedPermissions != null && !deletedPermissions.isEmpty()) { + if (deletedPermissions.stream().filter( p -> p.equals((String) obj)).map(p -> true).findFirst().orElse(false) ) { + // Return out of this function without calling super function + System.out.println("Removing permission " + obj.toString()); + super.nv.shouldChildBeWritten = false; + return; + } + } + + if(permissionGetter != null) permissionGetter.onPermissionGetted((String) obj); } + super.nv.end(); super.attr(ns, name, resourceId, type, obj); } public interface IUsesPermissionGetter { void onPermissionGetted(String permissionName); } + + @Override + public NodeVisitor child(String ns, String name) { + if (nv != null) { + return nv.child(ns, name); + } + return null; + } } diff --git a/src/main/java/pxb/android/axml/AxmlWriter.java b/src/main/java/pxb/android/axml/AxmlWriter.java index 443754c..dceda94 100755 --- a/src/main/java/pxb/android/axml/AxmlWriter.java +++ b/src/main/java/pxb/android/axml/AxmlWriter.java @@ -197,7 +197,9 @@ public class AxmlWriter extends AxmlVisitor { int size = 24 + 36 + attrs.size() * 20;// 24 for end tag,36+x*20 for // start tag for (NodeImpl child : children) { - size += child.prepare(axmlWriter); + if (child.shouldChildBeWritten) { + size += child.prepare(axmlWriter); + } } if (text != null) { size += 28; @@ -265,7 +267,9 @@ public class AxmlWriter extends AxmlVisitor { // children for (NodeImpl child : children) { - child.write(out); + if (child.shouldChildBeWritten) { + child.write(out); + } } // end tag @@ -328,7 +332,9 @@ public class AxmlWriter extends AxmlVisitor { int size = 0; for (NodeImpl first : firsts) { - size += first.prepare(this); + if (first.shouldChildBeWritten) { + size += first.prepare(this); + } } { int a = 0; @@ -399,7 +405,9 @@ public class AxmlWriter extends AxmlVisitor { } for (NodeImpl first : firsts) { - first.write(out); + if (first.shouldChildBeWritten) { + first.write(out); + } } while (stack.size() > 0) { diff --git a/src/main/java/pxb/android/axml/NodeVisitor.java b/src/main/java/pxb/android/axml/NodeVisitor.java index fe6179b..0d09807 100755 --- a/src/main/java/pxb/android/axml/NodeVisitor.java +++ b/src/main/java/pxb/android/axml/NodeVisitor.java @@ -23,6 +23,8 @@ public abstract class NodeVisitor { public static final int TYPE_REFERENCE = 0x01; public static final int TYPE_STRING = 0x03; protected NodeVisitor nv; + + public boolean shouldChildBeWritten = true; public NodeVisitor() { super();