Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
supengchao opened this issue Apr 21, 2016 · 0 comments
Open

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

supengchao opened this issue Apr 21, 2016 · 0 comments

Comments

@supengchao
Copy link
Member

supengchao commented Apr 21, 2016

借鉴美团的做法:

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant