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をサポートする

基本情報

Xamarin は、.NET と C# フレームワークを使って iOS、Android、Windows 向けのアプリを構築するための オープンソースのプラットフォーム です。多数のツールや拡張機能にアクセスでき、モダンなアプリを効率的に作成できます。

Xamarin のアーキテクチャ

  • For Android, Xamarin integrates with Android and Java namespaces through .NET bindings, operating within the Mono execution environment alongside the Android Runtime (ART). Managed Callable Wrappers (MCW) and Android Callable Wrappers (ACW) facilitate communication between Mono and ART, both of which are built on the Linux kernel.
  • For iOS, applications run under the Mono runtime, utilizing full Ahead of Time (AOT) compilation to convert C# .NET code into ARM assembly language. This process runs alongside the Objective-C Runtime on a UNIX-like kernel.

.NET Runtime and Mono Framework

The .NET framework includes assemblies, classes, and namespaces for application development, with the .NET Runtime managing code execution. It offers platform independence and backward compatibility. The Mono Framework is an open-source version of the .NET framework, initiated in 2005 to extend .NET to Linux, now supported by Microsoft and led by Xamarin.

Reverse Engineering Xamarin Apps

Decompilation of Xamarin Assemblies

Decompilation transforms compiled code back into source code. In Windows, the Modules window in Visual Studio can identify modules for decompilation, allowing for direct access to third-party code and extraction of source code for analysis.

JIT vs AOT Compilation

  • Android supports Just-In-Time (JIT) and Ahead-Of-Time (AOT) compilation, with a Hybrid AOT mode for optimal execution speed. Full AOT is exclusive to Enterprise licenses.
  • iOS solely employs AOT compilation due to Apple’s restrictions on dynamic code execution.

Extracting dll Files from APK/IPA

To access the assemblies in an APK/IPA, unzip the file and explore the assemblies directory. For Android, tools like XamAsmUnZ and xamarin-decompress can uncompress dll files.

python3 xamarin-decompress.py -o /path/to/decompressed/apk

APKをデコンパイルした後、unknown/assemblies/ フォルダ内に .dll ファイルが見える場合は、dnSpy を使ってその .dlls を直接解析できます。しかし、場合によっては unknown/assemblies/ フォルダ内に assemblies.blobassemblies.manifest が含まれていることがあります。ツール pyxamstore は Xamarin アプリの assemblies.blob ファイルを展開でき、.NET アセンブリへアクセスしてさらに解析することが可能です:

pyxamstore unpack -d /path/to/decompressed/apk/assemblies/
# After patching DLLs, rebuild the store
pyxamstore pack

最近の一部の Xamarin/MAUI ビルドでは、圧縮されたアセンブリが XALZ 形式で /assemblies.blob または /resources/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 は逆コンパイルが容易に可能で、アプリケーションコードの大部分が明らかになります。これらは異なるプラットフォーム間で共通のベースを共有していることが多いです。

AOT on iOS: managed IL はネイティブの *.aotdata.* ファイルにコンパイルされます。DLL をパッチするだけではロジックは変わりません。IL 本体は空のプレースホルダなので、ネイティブスタブをフックする必要があります(例: Frida など)。

Static Analysis

Once the .dlls are obtained it’s possible to analyze the .Net code statically using tools such as dnSpy or ILSpy that will allow modifying the code of the app. This can be super useful to tamper the application to bypass protections for example.
Note that after modifying the app you will need to pack it back again and sign it again.

dnSpy is archived; maintained forks like dnSpyEx keep working with .NET 8/MAUI assemblies and preserve debug symbols when re-saving.

Dynamic Analysis

Dynamic analysis involves checking for SSL pinning and using tools like Fridax for runtime modifications of the .NET binary in Xamarin apps. Frida scripts are available to bypass root detection or SSL pinning, enhancing analysis capabilities.

Other interesting Frida scripts:

Updated Frida-xamarin-unpin (Mono >=6) hooks System.Net.Http.HttpClient.SendAsync and swaps the handler to a permissive one, so it still works even when pinning is implemented in custom handlers. Run it after the app starts:

frida -U -l dist/xamarin-unpin.js com.target.app --no-pause

バンドルされた frida-mono-api を使って managed メソッドをフックする簡易テンプレート:

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に署名する作業を簡略化し、変更を加えた後にアプリを再署名するために使用できます。

参考資料

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をサポートする