# Flutter Reverse Engineering บน iOS ภาค Mach-O Binary

## Reverse Engineering

คำไทยจะเรียกว่า วิศวกรรมย้อนกลับ ผมแปลให้เข้าใจง่ายๆ คือการวิเคราะห์กระบวนทำงานของโปรแกรม เพื่อทำความเข้าใจการทำงานของโปรแกรมนั้นๆ เช่น การโปรแกรมนี้ถูกออกแบบมายังไง ตอนที่มันเปิดใช้งานมันทำอะไรบ้าง ผมยกตัวอย่างง่ายๆ เช่น โกงเงินเกมส์

หลักการก็คือ ตอนที่เราเปิดเกมส์ขึ้นมา มันจะต้องมี memory address ที่เก็บว่าเรามีเงินในเกมส์เท่าไหร่ (ถ้าเป็นเกมส์ออฟไลน์นะ) ถ้าเราวิเคราะห์ได้ว่าจำนวนเงิน  มันอยู่ที่ memory address อะไร เราก็ไปแก้จำนวนเงินใน memory address นั้น เราก็จะเสกเงินได้

## Flutter  Binary File

{% hint style="info" %}
Binary File : คือไฟล์ที่ได้จากการที่เราเขียนโปรแกรม จากนั้น compiler จะแปลงเป็น Binary File เพื่อให้คอมพิวเตอร์มันเข้าใจตอนเอาไปรัน หรือ มันคือไฟล์ภาษาเครื่องนั้นแหละครับ&#x20;
{% endhint %}

แอปพลิเคชันที่เขียนด้วย Flutter เวลาที่เรา Build เพื่อไปรันบนอุปกรณ์ iOS หรือ Android เราจะได้ไฟล์ .IPA และ APK เพื่อนำไปติดตั้ง ซึ่งภายในไฟล์จะมีสิ่งที่เรียกว่า Binary File ซึ่งจะมี  Binary File สองส่วนที่สำคัญได้แก่

1. Flutter Engine: ส่วนที่เป็นโค้ดหลักของ Flutter
2. Application Logic: ส่วนที่เป็นโค้ดหลักที่เราเขียนขึ้นเอง

ซึ่ง Binary ของ Flutter บน iOS คือ Mach-O Binary

## Mach-O Binary

Mach-O เป็น Binary ไฟล์ ที่รันบนอุปกรณ์ของ Apple เช่น iPhone Mac

โครงสร้างไฟล์แบ่งออกเป็น 3 ส่วนคือ Header, Load commands และ Data

<figure><img src="/files/kXYnuW1Lq5XomSaio9lh" alt=""><figcaption></figcaption></figure>

* Header

ข้อมูลทั่วไปเกี่ยวกับไฟล์  เช่น ส่วนที่เอาไว้บอกว่าไฟล์นี้คือไฟล์ Mach-O และ architecture ที่จะรันเป็นแบบไหน

* Load commands

แปลตรงตัวเลยก็คือส่วนของการโหลดคำสั่ง  เอาไว้บอกระบบปฏิบัติการ ว่าจะโหลดข้อมูลและรันโค้ดที่อยู่ในไฟล์ Mach-O ได้ยังไง

* Data

ส่วนของ Data เป็นส่วนของที่เก็บข้อมูล Memory ที่เอาไว้เก็บพวก executable code และ static data หรือพูดง่ายๆ คือ โค้ดส่วนที่จะรันนั้นแหละ

## รู้จักกับ **symbol table**&#x20;

เป็นตารางที่ใช้อ้างอิงว่า โค้ดที่เราจะรันมันอยู่ตรงไหน ซึ่งโดยปกติแล้วตัว compiler มันจะสร้างและเก็บไว้ใน Binary ไฟล์อยู่แล้ว เมื่อโปรแกรมมันทำงาน ระบบปฏิบัติการมันก็จะใช้ symbol table ค้นหา memory  address&#x20;

ยกตัวอย่างเช่น เรามีตัวแปร x และตอนที่โปรแกรมจะใช้งาน symbol table ก็จะบอกว่า x มันอยู่ที่ memory  address  อะไร

<figure><img src="/files/I3k8q9kFHaXf98poKoIl" alt=""><figcaption></figcaption></figure>

## **Analysis**

ถ้าเราจะปรับเปลี่ยนกระบวนการทำงานของแอปพลิเคชัน แน่นอนว่าสิ่งที่เราต้องสนใจใน Mach-O Binary คือส่วน Data ซึ่ง Data จะถูกแบ่งเป็น segments&#x20;

{% hint style="info" %}
&#x20;segments  คือ พื้นที่หน่วยความจำใน binary file ที่มีโค้ดหรือข้อมูลที่จะถูกนำไปรัน
{% endhint %}

และภายใต้ segments จะมี  section ย่อยๆ&#x20;

<figure><img src="/files/B8vxtR601eU8cAUndDYA" alt=""><figcaption></figcaption></figure>

โดยโครงสร้างของ section มีหน้าตาดังนี้

```c
struct section_64 {          /* for 64-bit architectures */
   char       sectname[16];  /* name of this section */
   char       segname[16];   /* segment this section goes in */
   uint64_t   addr;          /* memory address of this section */
   uint64_t   size;          /* size in bytes of this section */
   uint32_t   offset;        /* file offset of this section */
   uint32_t   align;         /* section alignment (power of 2) */
   uint32_t   reloff;        /* file offset of relocation entries */
   uint32_t   nreloc;        /* number of relocation entries */
   uint32_t   flags;         /* flags (section type and attributes)*/
   uint32_t   reserved1;     /* reserved (for offset or index) */
   uint32_t   reserved2;     /* reserved (for count or sizeof) */
   uint32_t   reserved3;     /* reserved */
};
```

ซึ่งส่วนของ section นี่แหละจะถูกนำไปสร้างเป็น [**symbol table**](#symbol-table)  **สำหรับ section ที่เราจะสนใจก็คือ** \_\_text section

#### \_\_text section

\_\_text section จะมีโค้ดของโปรแกรมที่จะถูกรัน (executable code) ดังนั้นเราสามารถนำ  \_\_text section  มา reverse engineering เพื่อทำความเข้าใจกระบวนการทำงานของโปรแกรมได้

สำหรับการ Reverse Engineering โปรดรอ ภาค ต่อไป &#x20;

## อ้างอิง

{% embed url="<https://alexdremov.me/mystery-of-mach-o-object-file-builders/>" %}

{% embed url="<https://yurylapitsky.com/exploring_mach-o_binaries>" %}

{% embed url="<https://www.youtube.com/watch?v=Dd3DWRpqI40>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://research.xpwn.dev/flutter/flutter-reverse-engineering-ios-mach-o-binary.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
