Apktool安全性研究
字数 1475 2025-08-18 11:36:47
Apktool安全性研究教学文档
1. Apktool简介
Apktool是一个用于逆向工程Android APK文件的工具,它能够解码资源到近乎原始的形式,并在修改后重新构建它们。项目地址:https://github.com/iBotPeaches/Apktool
2. Apktool历史漏洞分析
2.1 XML外部实体漏洞(XXE)
漏洞描述:
Apktool在解析AndroidManifest.xml文件时,不会禁用外部实体引用,导致存在XML外部实体注入攻击(XXE)漏洞。
影响版本:
2.2.2及以下版本
漏洞原理:
在fixingPublicAttrsInProviderAttributes方法中,解析文件使用loadDocument(file)函数:
private static Document loadDocument(File file)
throws IOException, SAXException, ParserConfigurationException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
return docBuilder.parse(file);
}
没有禁用外部实体,导致XXE漏洞。
修复方案:
修复后的代码增加了安全限制:
public static Document loadDocument(File file)
throws IOException, SAXException, ParserConfigurationException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setFeature(FEATURE_DISABLE_DOCTYPE_DECL, true);
docFactory.setFeature(FEATURE_LOAD_DTD, false);
try {
docFactory.setAttribute(ACCESS_EXTERNAL_DTD, " ");
docFactory.setAttribute(ACCESS_EXTERNAL_SCHEMA, " ");
} catch (IllegalArgumentException ex) {
LOGGER.warning("JAXP 1.5 Support is required to validate XML");
}
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
try (FileInputStream inputStream = new FileInputStream(file)) {
return docBuilder.parse(inputStream);
}
}
利用方法:
- 生成正常APK
- 使用apktool decode后修改AndroidManifest.xml
- 添加恶意XXE payload:
<!DOCTYPE manifest <!ENTITY xxe SYSTEM 'https://target/sayhi;'>]>
&xxe;
2.2 unknownFiles路径穿越漏洞
漏洞描述:
Apktool在2.2.2版本以下存在目录穿越漏洞,可以打包一个文件到恶意的apk中,在apktool解包时造成任意文件写入。
漏洞代码:
在decodeUnknownFiles方法中:
public void decodeUnknownFiles(ExtFile apkFile, File outDir, ResTable resTable)
throws AndrolibException {
File unknownOut = new File(outDir, UNK_DIRNAME);
Directory unk = apkFile.getDirectory();
Set<String> files = unk.getFiles(true);
for (String file : files) {
if (!isAPKFileNames(file) && !file.endsWith(".dex")) {
unk.copyToDir(unknownOut, file);
mResUnknownFiles.addUnknownFileInfo(file, String.valueOf(unk.getCompressionLevel(file)));
}
}
}
漏洞利用:
创建一个名为../../../aaa的恶意APK文件,在解包时会逃逸到上级目录。
修复方案:
新版本增加了路径检测:
if (name.equals(getPath()) || ! name.startsWith(getPath()) || name.contains(".." + separator)) {
continue;
}
2.3 CVE-2024-21633 解码时任意文件写入漏洞
漏洞描述:
通过构造特殊的资源文件名,可以在解码时实现任意文件写入。
漏洞原理:
在ResFileDecoder中,资源文件名可以被构造为路径穿越形式,如../../../../../../../../../../../../tmp/poc。
利用方法:
- 使用010 Editor修改APK文件中的资源文件名
- 构造恶意路径如
../../../../../../../../../../../../tmp/poc - 使用apktool解码时会写入指定路径
修复方案:
增加了detectPossibleDirectoryTraversal检测方法:
public static boolean detectPossibleDirectoryTraversal(String entry) {
if (OSDetection.isWindows()) {
return entry.contains("..\\") || entry.contains("\\..");
}
return entry.contains("../") || entry.contains("/..");
}
漏洞绕过:
在Windows系统上,可以使用../来绕过检测,因为Windows路径兼容斜杠和反斜杠。
3. 安全建议
- 始终使用最新版本的Apktool
- 在沙箱环境中运行Apktool
- 不要使用不受信任的APK文件
- 定期检查Apktool的安全公告
4. 参考链接
- https://github.com/iBotPeaches/Apktool/security/advisories/GHSA-2hqv-2xv4-5h5w
- https://research.checkpoint.com/2017/parsedroid-targeting-android-development-research-community/
- https://github.com/iBotPeaches/Apktool/commit/d348c43b24a9de350ff6e5bd610545a10c1fc712
- https://thinkycx.me/2017-12-18-Apktool-XXE-analysis.html
- https://omasko.github.io/2018/03/17/apktool漏洞利用分析/