Skip to content

[面试]如何对资源文件进行混淆? #15

@supengchao

Description

@supengchao

借鉴美团的做法:

1、思路:

对系统aapt(Android Asset Packaging Tool)进行干预,自定义文件混淆的规则。然后重新打包,系统会掉用我们自定义的aapt去打包,打包出来的apk反编译后就会发现原来的资源名全部替换为a,b,c,d...

2、具体的做法如下:

(1)修改AAPT在处理资源文件相关的源码
Resource.cpp中makeFileResources()

    static status_t makeFileResources(Bundle* bundle, const sp<AaptAssets>& assets,                                   

    ResourceTable* table, const sp<ResourceTypeSet>& set,const char* resType)  {

    String8 type8(resType);
    String16 type16(resType);

    bool hasErrors = false;

    ResourceDirIterator it(set, String8(resType));
    ssize_t res;
    while ((res=it.next()) == NO_ERROR) {
        if (bundle->getVerbose()) {
            printf("    (new resource id %s from %s)\n",
                   it.getBaseName().string(), it.getFile()->getPrintableSource().string());
        }
        String16 baseName(it.getBaseName());
        const char16_t* str = baseName.string();
        const char16_t* const end = str + baseName.size();
        while (str < end) {
            if (!((*str >= 'a' && *str <= 'z')
                    || (*str >= '0' && *str <= '9')
                    || *str == '_' || *str == '.')) {
                fprintf(stderr, "%s: Invalid file name: must contain only [a-z0-9_.]\n",
                        it.getPath().string());
                hasErrors = true;
            }
            str++;
        }
        String8 resPath = it.getPath();
        resPath.convertToResPath();

        String8 obfuscationName;
        String8 obfuscationPath = getObfuscationName(resPath, obfuscationName);

        table->addEntry(SourcePos(it.getPath(), 0), String16(assets->getPackage()),
                        type16,
                        baseName, // String16(obfuscationName),
                        String16(obfuscationPath), // resPath
                        NULL,
                        &it.getParams());
        assets->addResource(it.getLeafName(), obfuscationPath/*resPath*/, it.getFile(), type8);
    }

    return hasErrors ? UNKNOWN_ERROR : NO_ERROR;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions