Xamarin 应用
Tip
学习和实践 AWS 黑客技术:
HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)
学习和实践 Azure 黑客技术:
HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。
基本信息
Xamarin 是一个面向开发者的 开源平台,用于使用 .NET 和 C# 框架为 iOS、Android 和 Windows 构建应用程序。该平台提供了大量工具和扩展,以高效创建现代应用。
Xamarin 的架构
- 对于 Android,Xamarin 通过 .NET 绑定与 Android 和 Java 命名空间集成,在 Mono 执行环境中与 Android Runtime (ART) 一起运行。Managed Callable Wrappers (MCW) 和 Android Callable Wrappers (ACW) 促进 Mono 与 ART 之间的通信,两者都基于 Linux 内核。
- 对于 iOS,应用在 Mono 运行时下运行,使用完整的 Ahead of Time (AOT) 编译将 C# .NET 代码转换为 ARM 汇编语言。此过程在类 UNIX 内核上与 Objective-C Runtime 并行运行。
.NET 运行时和 Mono 框架
.NET 框架包含用于应用开发的 assembly、class 和 namespace,由 .NET Runtime 管理代码执行。它提供平台无关性和向后兼容性。Mono Framework 是 .NET 框架的开源实现,始于 2005 年,旨在将 .NET 扩展到 Linux,目前由 Microsoft 支持并由 Xamarin 领导。
逆向工程 Xamarin 应用
Xamarin 程序集的反编译
反编译是将已编译的代码还原为源代码的过程。在 Windows 中,Visual Studio 的 Modules 窗口可以识别用于反编译的模块,允许直接访问第三方代码并提取源代码以供分析。
JIT 与 AOT 编译
- Android 支持 Just-In-Time (JIT) 和 Ahead-Of-Time (AOT) 编译,并提供 Hybrid AOT 模式以实现最佳执行速度。完全 AOT 仅在 Enterprise 许可证中可用。
- iOS 由于 Apple 对动态代码执行的限制,仅使用 AOT 编译。
从 APK/IPA 提取 dll 文件
要访问 APK/IPA 中的程序集,解压该文件并浏览 assemblies 目录。对于 Android,像 XamAsmUnZ 和 xamarin-decompress 这样的工具可以解压 dll 文件。
python3 xamarin-decompress.py -o /path/to/decompressed/apk
在对 APK 反编译后,如果可以看到 unknown/assemblies/ 文件夹且其中包含 .dll 文件,则可以直接使用 dnSpy 对这些 .dll 进行分析。不过,有时 unknown/assemblies/ 文件夹中包含 assemblies.blob 和 assemblies.manifest 文件。工具 pyxamstore 可以在 Xamarin 应用中解包 assemblies.blob 文件,从而获取 .NET 程序集以便进一步分析:
pyxamstore unpack -d /path/to/decompressed/apk/assemblies/
# After patching DLLs, rebuild the store
pyxamstore pack
.NET MAUI 9 / .NET for Android 程序集存储在 ELF .so 中
近期的 Android MAUI 9 构建不再直接暴露 assemblies.blob。取而代之的是,每个 ABI 都会包含一个 ELF 容器,例如 lib/arm64-v8a/libassemblies.arm64-v8a.blob.so。这是一个有效的共享库,带有一个自定义的 payload 节,其中包含托管的程序集存储。
快速工作流程:
unzip app.apk -d app_unpacked
llvm-readelf --section-headers app_unpacked/lib/arm64-v8a/libassemblies.arm64-v8a.blob.so
llvm-objcopy --dump-section=payload=payload.bin \
app_unpacked/lib/arm64-v8a/libassemblies.arm64-v8a.blob.so
hexdump -c -n 4 payload.bin # XABA
如果 llvm-readelf 显示有 payload 节,导出它并验证提取的 blob 以 XABA (0x41424158) 开头。该 payload 是 .NET for Android 文档中描述的 assembly store,而不是单个 DLL。
当需要手动从中提取 assemblies 或验证提取器时,store 的布局很有用:
- Header:
struct.unpack('<5I', ...)用于读取magic、version、entry_count、index_entry_count、index_size - Descriptors: 有
entry_count条记录,使用struct.unpack('<7I', ...),包含data_offset/data_size以及可选的 debug/config 偏移 - Index: 跳过
index_size字节 - Names:
uint32 length+ UTF-8 字节 - Data: 定位到每个
data_offset并将data_size字节写为<name>.dll
Some extracted entries still won’t open directly in dnSpy/ILSpy/dotPeek because they are additionally wrapped with XALZ. In that case:
- Check the first 4 bytes of each extracted file for
XALZ - Read the uncompressed size from bytes
8:12as little-endianuint32 - Decompress bytes
12:withlz4.block.decompress(...)
最小解压逻辑:
import struct
import lz4.block
def decompress_xalz(data):
size = struct.unpack('<I', data[8:12])[0]
return lz4.block.decompress(data[12:], uncompressed_size=size)
如果你不想手动解析 store,pymauistore 会自动化 ELF payload extraction、XABA store parsing 和 XALZ decompression,适用于 MAUI 9 APKs。
某些较旧的 Xamarin/MAUI 构建会在 /assemblies.blob 或 /resources/assemblies 中使用 XALZ 格式存储压缩的 assemblies。你可以使用 xamarout 库快速将它们解压:
from xamarout import xalz
import os
for root, _, files in os.walk("."):
for f in files:
if open(os.path.join(root, f), 'rb').read(4) == b"XALZ":
xa = xalz.XamarinCompressedAssembly(os.path.join(root, f))
xa.write("decompressed/" + f)
iOS dll files are readily accessible for decompilation, revealing significant portions of the application code, which often shares a common base across different platforms.
AOT on iOS: 托管的 IL 会被编译成本地的
*.aotdata.*文件。仅修补 DLL 并不会改变逻辑;需要 hook 本地存根(例如使用 Frida),因为 IL 的主体只是空的占位符。
静态分析
一旦获得 .dll,就可以使用诸如 dnSpy 或 ILSpy 等工具对 .Net 代码进行静态分析,这些工具允许修改应用的代码。这在篡改应用以绕过保护机制时非常有用。
请注意,在修改应用后,需要重新打包并重新签名。
dnSpy 已归档;像 dnSpyEx 这样的维护分支仍然可以处理 .NET 8/MAUI 的程序集,并在重新保存时保留调试符号。
动态分析
动态分析包括检查 SSL pinning,并使用像 Fridax 这样的工具对 Xamarin 应用中的 .NET 二进制进行运行时修改。可使用 Frida 脚本来绕过 root detection 或 SSL pinning,从而增强分析能力。
其他有趣的 Frida 脚本:
更新的 Frida-xamarin-unpin (Mono >=6) 钩取 System.Net.Http.HttpClient.SendAsync 并将处理器替换为更宽松的实现,因此即使 pinning 在自定义处理器中实现,它仍然有效。请在应用启动后运行:
frida -U -l dist/xamarin-unpin.js com.target.app --no-pause
使用随附的 frida-mono-api hook 托管方法的快速模板:
const mono = require('frida-mono-api');
Mono.ensureInitialized();
Mono.enumerateLoadedImages().forEach(i => console.log(i.name));
const klass = Mono.classFromName("Namespace", "Class");
const m = Mono.methodFromName(klass, "Method", 2);
Mono.intercept(m, { onEnter(args){ console.log(args[1].toInt32()); } });
重新签名
工具 Uber APK Signer 简化了使用相同密钥为多个 APK 签名的过程,并可在对应用进行修改后用于重新签名。
参考资料
- https://www.appknox.com/security/xamarin-reverse-engineering-a-guide-for-penetration-testers
- https://thecobraden.com/posts/unpacking_xamarin_assembly_stores/
- https://medium.com/@justmobilesec/introduction-to-the-exploitation-of-xamarin-apps-fde4619a51bf
- https://github.com/jakev/pyxamstore
- https://pypi.org/project/xamarout/
- https://github.com/GoSecure/frida-xamarin-unpin
- https://gist.github.com/Diefunction/e26fce039efcab57aac342a4b2d48ff6
- https://reverseengineering.stackexchange.com/questions/31716/deobfuscating-ios-dll-file-i-think-arm64
- https://mwalkowski.com/post/decompiling-an-android-application-written-in-net-maui-9-xamarin/
- https://github.com/dotnet/android/blob/main/Documentation/project-docs/AssemblyStores.md
- https://github.com/dotnet/android/blob/main/Documentation/project-docs/ApkSharedLibraries.md
- https://github.com/mwalkowski/pymauistore/tree/main
Tip
学习和实践 AWS 黑客技术:
HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)
学习和实践 Azure 黑客技术:
HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。


