Add option to remove existing permissions

master
Jonas Letzbor 2023-11-12 20:15:30 +01:00
parent b88a560ddb
commit 19356cfabf
Signed by: RPJosh
GPG Key ID: 46D72F589702E55A
9 changed files with 76 additions and 12 deletions

4
.gitignore vendored
View File

@ -13,3 +13,7 @@
/captures
.externalNativeBuild
.cxx
/.project
/.classpath
/.settings
/bin

View File

@ -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进行签名的功能。

3
run.sh
View File

@ -2,4 +2,5 @@
./gradlew build
# Run the app (add your options here)
java -jar build/dist/ManifestEditor.jar --help
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

View File

@ -60,6 +60,10 @@ public class ManifestEditorMain extends BaseCommand {
"name to the manifest file, multi option is supported", argName = "uses-permission-name")
private List<String> 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<String> 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) {

View File

@ -10,6 +10,7 @@ import java.util.List;
*/
public class ModificationProperty {
private List<String> deleteUsesPermissionList = new ArrayList<>();
private List<String> usesPermissionList = new ArrayList<>();
private List<MetaData> metaDataList = new ArrayList<>();
private List<MetaData> deleteMetaDataList = new ArrayList<>();
@ -22,11 +23,21 @@ public class ModificationProperty {
return usesPermissionList;
}
public List<String> 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<AttributeItem> getApplicationAttributeList() {
return applicationAttributeList;
}

View File

@ -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);
}

View File

@ -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<String> deletedPermissions;
UserPermissionTagVisitor(NodeVisitor nv, IUsesPermissionGetter permissionGetter, String permissionTobeAdded) {
UserPermissionTagVisitor(NodeVisitor nv, IUsesPermissionGetter permissionGetter, String permissionTobeAdded, List<String> 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;
}
}

View File

@ -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) {

View File

@ -24,6 +24,8 @@ public abstract class NodeVisitor {
public static final int TYPE_STRING = 0x03;
protected NodeVisitor nv;
public boolean shouldChildBeWritten = true;
public NodeVisitor() {
super();
}