macOS Universal binaries & Mach-O Format
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 ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
๊ธฐ๋ณธ ์ ๋ณด
Mac OS ๋ฐ์ด๋๋ฆฌ๋ ๋ณดํต universal binaries๋ก ์ปดํ์ผ๋ฉ๋๋ค. universal binary๋ ๋จ์ผ ํ์ผ์์ ์ฌ๋ฌ ์ํคํ ์ฒ๋ฅผ ์ง์ํ ์ ์์ต๋๋ค.
์ด๋ฌํ ๋ฐ์ด๋๋ฆฌ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ค์์ผ๋ก ๊ตฌ์ฑ๋ Mach-O ๊ตฌ์กฐ๋ฅผ ๋ฐ๋ฆ ๋๋ค:
- Header
- Load Commands
- Data
.png)
Fat Header
๋ค์์ผ๋ก ํ์ผ์ ๊ฒ์ํ์ธ์: mdfind fat.h | grep -i mach-o | grep -E "fat.h$"
#define FAT_MAGIC 0xcafebabe
#define FAT_CIGAM 0xbebafeca /* NXSwapLong(FAT_MAGIC) */
struct fat_header {
uint32_t magic; /* FAT_MAGIC or FAT_MAGIC_64 */
uint32_t nfat_arch; /* number of structs that follow */
};
struct fat_arch {
cpu_type_t cputype; /* cpu specifier (int) */
cpu_subtype_t cpusubtype; /* machine specifier (int) */
uint32_t offset; /* file offset to this object file */
uint32_t size; /* size of this object file */
uint32_t align; /* alignment as a power of 2 */
};
ํค๋๋ magic ๋ฐ์ดํธ์ ๋ค๋ฐ๋ฅด๋ ํ์ผ์ด ํฌํจํ๋ archs์ ๊ฐ์ (nfat_arch)๋ฅผ ํฌํจํ๋ฉฐ, ๊ฐ arch๋ fat_arch ๊ตฌ์กฐ์ฒด๋ฅผ ๊ฐ์ง๋๋ค.
๋ค์์ผ๋ก ํ์ธํ์ธ์:
% file /bin/ls
/bin/ls: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64e:Mach-O 64-bit executable arm64e]
/bin/ls (for architecture x86_64): Mach-O 64-bit executable x86_64
/bin/ls (for architecture arm64e): Mach-O 64-bit executable arm64e
% otool -f -v /bin/ls
Fat headers
fat_magic FAT_MAGIC
nfat_arch 2
architecture x86_64
cputype CPU_TYPE_X86_64
cpusubtype CPU_SUBTYPE_X86_64_ALL
capabilities 0x0
offset 16384
size 72896
align 2^14 (16384)
architecture arm64e
cputype CPU_TYPE_ARM64
cpusubtype CPU_SUBTYPE_ARM64E
capabilities PTR_AUTH_VERSION USERSPACE 0
offset 98304
size 88816
align 2^14 (16384)
๋๋ Mach-O View ๋๊ตฌ๋ฅผ ์ฌ์ฉํ์ฌ:
.png)
์๋ง ์๊ฐํ์ จ๊ฒ ์ง๋ง, ๋ณดํต 2๊ฐ ์ํคํ ์ฒ์ฉ์ผ๋ก ์ปดํ์ผ๋ universal binary๋ ๋จ์ผ ์ํคํ ์ฒ์ฉ์ผ๋ก ์ปดํ์ผ๋ ๊ฒ๋ณด๋ค ํฌ๊ธฐ๊ฐ 2๋ฐฐ๊ฐ ๋ฉ๋๋ค.
Mach-O Header
ํค๋์๋ Mach-O ํ์ผ๋ก ์๋ณํ๊ธฐ ์ํ magic ๋ฐ์ดํธ์ ๋์ ์ํคํ
์ฒ์ ๋ํ ์ ๋ณด ๋ฑ ํ์ผ์ ๊ธฐ๋ณธ ์ ๋ณด๊ฐ ํฌํจ๋ฉ๋๋ค. ๋ค์์์ ์ฐพ์ ์ ์์ต๋๋ค: mdfind loader.h | grep -i mach-o | grep -E "loader.h$"
#define MH_MAGIC 0xfeedface /* the mach magic number */
#define MH_CIGAM 0xcefaedfe /* NXSwapInt(MH_MAGIC) */
struct mach_header {
uint32_t magic; /* mach magic number identifier */
cpu_type_t cputype; /* cpu specifier (e.g. I386) */
cpu_subtype_t cpusubtype; /* machine specifier */
uint32_t filetype; /* type of file (usage and alignment for the file) */
uint32_t ncmds; /* number of load commands */
uint32_t sizeofcmds; /* the size of all the load commands */
uint32_t flags; /* flags */
};
#define MH_MAGIC_64 0xfeedfacf /* the 64-bit mach magic number */
#define MH_CIGAM_64 0xcffaedfe /* NXSwapInt(MH_MAGIC_64) */
struct mach_header_64 {
uint32_t magic; /* mach magic number identifier */
int32_t cputype; /* cpu specifier */
int32_t cpusubtype; /* machine specifier */
uint32_t filetype; /* type of file */
uint32_t ncmds; /* number of load commands */
uint32_t sizeofcmds; /* the size of all the load commands */
uint32_t flags; /* flags */
uint32_t reserved; /* reserved */
};
Mach-O ํ์ผ ํ์
์ฌ๋ฌ ๊ฐ์ง ํ์ผ ํ์์ด ์์ผ๋ฉฐ, source code for example here์์ ์ ์๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค. ๊ฐ์ฅ ์ค์ํ ๊ฒ๋ค์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
MH_OBJECT: ์ฌ๋ฐฐ์น ๊ฐ๋ฅํ ์ค๋ธ์ ํธ ํ์ผ (์ปดํ์ผ์ ์ค๊ฐ ์ฐ์ถ๋ฌผ๋ก ์์ง ์คํ ํ์ผ์ด ์๋).MH_EXECUTE: ์คํ ํ์ผ.MH_FVMLIB: ๊ณ ์ VM ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ์ผ.MH_CORE: ์ฝ๋ ๋คํMH_PRELOAD: ์ฌ์ ๋ก๋๋ ์คํ ํ์ผ (ํ์ฌ XNU์์ ๋ ์ด์ ์ง์๋์ง ์์)MH_DYLIB: ๋์ ๋ผ์ด๋ธ๋ฌ๋ฆฌMH_DYLINKER: ๋์ ๋ง์ปคMH_BUNDLE: โํ๋ฌ๊ทธ์ธ ํ์ผโ. -bundle in gcc๋ก ์์ฑ๋๋ฉฐ ๋ช ์์ ์ผ๋กNSBundle๋๋dlopen์ผ๋ก ๋ก๋๋ฉ๋๋ค.MH_DYSM: ๋๋ฐ.dSymํ์ผ (๋๋ฒ๊น ์ ์ํ ์ฌ๋ณผ์ด ํฌํจ๋ ํ์ผ).MH_KEXT_BUNDLE: ์ปค๋ ํ์ฅ.
# Checking the mac header of a binary
otool -arch arm64e -hv /bin/ls
Mach header
magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
MH_MAGIC_64 ARM64 E USR00 EXECUTE 19 1728 NOUNDEFS DYLDLINK TWOLEVEL PIE
๋๋ Mach-O View:
.png)
Mach-O ํ๋๊ทธ
์์ค ์ฝ๋์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ก๋ฉ์ ์ ์ฉํ ์ฌ๋ฌ ํ๋๊ทธ๋ ์ ์๋์ด ์๋ค:
MH_NOUNDEFS: ์ ์๋์ง ์์ ์ฐธ์กฐ ์์ (์์ ๋งํฌ๋จ)MH_DYLDLINK: Dyld ๋งํฌMH_PREBOUND: ๋์ ์ฐธ์กฐ๊ฐ ์ฌ์ ๋ฐ์ธ๋ฉ๋จMH_SPLIT_SEGS: ํ์ผ์ด r/o ๋ฐ r/w ์ธ๊ทธ๋จผํธ๋ก ๋ถ๋ฆฌ๋จMH_WEAK_DEFINES: ๋ฐ์ด๋๋ฆฌ์ weak๋ก ์ ์๋ ์ฌ๋ณผ์ด ์์MH_BINDS_TO_WEAK: ๋ฐ์ด๋๋ฆฌ๊ฐ weak ์ฌ๋ณผ์ ์ฌ์ฉํจMH_ALLOW_STACK_EXECUTION: ์คํ์ ์คํ ๊ฐ๋ฅํ๊ฒ ํจMH_NO_REEXPORTED_DYLIBS: ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ LC_REEXPORT ๋ช ๋ น์ด ์์MH_PIE: ์์น ๋ ๋ฆฝ ์คํ ํ์ผMH_HAS_TLV_DESCRIPTORS: ์ค๋ ๋ ๋ก์ปฌ ๋ณ์ ์น์ ์ด ์์MH_NO_HEAP_EXECUTION: ํ/๋ฐ์ดํฐ ํ์ด์ง์์ ์คํ ๋ถ๊ฐMH_HAS_OBJC: ๋ฐ์ด๋๋ฆฌ์ Objective-C ์น์ ์ด ์์MH_SIM_SUPPORT: ์๋ฎฌ๋ ์ดํฐ ์ง์MH_DYLIB_IN_CACHE: shared library cache์ ์๋ dylib/framework์ ์ฌ์ฉ๋จ.
Mach-O ๋ก๋ ๋ช ๋ น
ํ์ผ์ ๋ฉ๋ชจ๋ฆฌ ๋ ์ด์์์ด ์ฌ๊ธฐ์ ์ง์ ๋๋ฉฐ, ์ฌ๋ณผ ํ ์ด๋ธ์ ์์น, ์คํ ์์ ์ ๋ฉ์ธ ์ค๋ ๋์ ์ปจํ ์คํธ, ๊ทธ๋ฆฌ๊ณ ํ์ํ ๊ณต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด ์์ธํ ๊ธฐ์ ๋๋ค. ๋ฐ์ด๋๋ฆฌ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋ํ๋ ๊ณผ์ ์ ๋ํด ๋์ ๋ก๋(dyld)์ ์ง์นจ์ ์ ๊ณตํ๋ค.
์ฌ๊ธฐ์๋ ์์ ์ธ๊ธํ **loader.h**์ ์ ์๋ load_command ๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ค:
struct load_command {
uint32_t cmd; /* type of load command */
uint32_t cmdsize; /* total size of command in bytes */
};
There are about 50 different types of load commands that the system handles differently. The most common ones are: LC_SEGMENT_64, LC_LOAD_DYLINKER, LC_MAIN, LC_LOAD_DYLIB, and LC_CODE_SIGNATURE.
LC_SEGMENT/LC_SEGMENT_64
Tip
๊ธฐ๋ณธ์ ์ผ๋ก, ์ด ์ ํ์ Load Command๋ ๋ฐ์ด๋๋ฆฌ๊ฐ ์คํ๋ ๋ ๋ฐ์ดํฐ ์น์ ์ ํ์๋ ์คํ์ ์ ๋ฐ๋ผ __TEXT(์คํ ์ฝ๋) ๋ฐ __DATA(ํ๋ก์ธ์ค์ ๋ฐ์ดํฐ) ์ธ๊ทธ๋จผํธ๋ฅผ ์ด๋ป๊ฒ ๋ก๋ํ๋์ง ์ ์ํฉ๋๋ค.
These commands define segments that are mapped into the virtual memory space of a process when it is executed.
There are different types of segments, such as the __TEXT segment, which holds the executable code of a program, and the __DATA segment, which contains data used by the process. These segments are located in the data section of the Mach-O file.
Each segment can be further divided into multiple sections. The load command structure contains information about these sections within the respective segment.
In the header first you find the segment header:
struct segment_command_64 { /* for 64-bit architectures */
uint32_t cmd; /* LC_SEGMENT_64 */
uint32_t cmdsize; /* includes sizeof section_64 structs */
char segname[16]; /* segment name */
uint64_t vmaddr; /* memory address of this segment */
uint64_t vmsize; /* memory size of this segment */
uint64_t fileoff; /* file offset of this segment */
uint64_t filesize; /* amount to map from the file */
int32_t maxprot; /* maximum VM protection */
int32_t initprot; /* initial VM protection */
uint32_t nsects; /* number of sections in segment */
uint32_t flags; /* flags */
};
Example of segment header:
.png)
This header defines the number of sections whose headers appear after it:
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 */
};
์: ์น์ ํค๋:
.png)
๋ง์ฝ ๋ํ๋ฉด ์น์
์คํ์
(0x37DC) + arch๊ฐ ์์ํ๋ ์คํ์
, ์ด ๊ฒฝ์ฐ 0x18000 โ> 0x37DC + 0x18000 = 0x1B7DC
.png)
๋ํ ํค๋ ์ ๋ณด๋ฅผ command line์์ ์ป์ ์๋ ์์ต๋๋ค:
otool -lv /bin/ls
Common segments loaded by this cmd:
__PAGEZERO: ์ปค๋์๊ฒ **์ฃผ์ 0(address zero)**๋ฅผ **๋งคํ(map)**ํ๋๋ก ์ง์ํ์ฌ ์ฝ๊ธฐ/์ฐ๊ธฐ/์คํ์ด ๋ถ๊ฐ๋ฅํ๋๋ก ํฉ๋๋ค. ๊ตฌ์กฐ์ฒด์ maxprot ๋ฐ minprot ๋ณ์๋ ์ด ํ์ด์ง์ ๋ํ ์ฝ๊ธฐ-์ฐ๊ธฐ-์คํ ๊ถํ์ด ์์์ ๋ํ๋ด๊ธฐ ์ํด 0์ผ๋ก ์ค์ ๋ฉ๋๋ค.- ์ด ํ ๋น์ NULL pointer dereference vulnerabilities๋ฅผ ์ํํ๋ ๋ฐ ์ค์ํฉ๋๋ค. ์ด๋ XNU๊ฐ ํ๋ ํ์ด์ง ์ ๋ก๋ฅผ ์ ์ฉํ์ฌ ๋ฉ๋ชจ๋ฆฌ์ ์ฒซ ๋ฒ์งธ ํ์ด์ง(์ฒซ ํ์ด์ง๋ง)๋ฅผ ์ ๊ทผ ๋ถ๊ฐ๋ก ๋ง๋ค๊ธฐ ๋๋ฌธ์
๋๋ค(i386 ์ ์ธ). ๋ฐ์ด๋๋ฆฌ๋ ์์ __PAGEZERO(
-pagezero_size์ฌ์ฉ)๋ฅผ ๋ง๋ค์ด ์ฒ์ 4K๋ฅผ ๋ฎ๊ณ ๋๋จธ์ง 32๋นํธ ๋ฉ๋ชจ๋ฆฌ๋ ์ ์ ๋ฐ ์ปค๋ ๋ชจ๋์์ ์ ๊ทผ ๊ฐ๋ฅํ๋๋ก ํ์ฌ ์ด ์๊ตฌ์ฌํญ์ ์ถฉ์กฑํ ์ ์์ต๋๋ค. __TEXT: ์คํ ๊ฐ๋ฅํ(executable) **์ฝ๋(code)**๋ฅผ ํฌํจํ๋ฉฐ ์ฝ๊ธฐ(read) ๋ฐ ์คํ(execute) ๊ถํ์ ๊ฐ์ง๋๋ค(์ฐ๊ธฐ ๊ถํ ์์). ์ด ์ธ๊ทธ๋จผํธ์ ์ผ๋ฐ ์น์ :__text: ์ปดํ์ผ๋ ๋ฐ์ด๋๋ฆฌ ์ฝ๋__const: ์์ ๋ฐ์ดํฐ(์ฝ๊ธฐ ์ ์ฉ)__[c/u/os_log]string: C, Unicode ๋๋ os ๋ก๊ทธ ๋ฌธ์์ด ์์__stubsand__stubs_helper: ๋์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ก๋ฉ ๊ณผ์ ์ ๊ด์ฌ__unwind_info: ์คํ ์ธ์์ธ๋ ๋ฐ์ดํฐ- ์ด ๋ชจ๋ ๋ด์ฉ์ ์๋ช ๋์ด ์๋ ์คํ ๊ฐ๋ฅ์ผ๋ก ํ์๋์ด ์๋ค๋ ์ ์ ์ ์ํ์ธ์(๋ฌธ์์ด ์ ์ฉ ์น์ ์ฒ๋ผ ๋ฐ๋์ ์คํ ๊ถํ์ ํ์๋ก ํ์ง ์๋ ์น์ ์ ์ ์ฉํ ์ ์๋ ๊ฐ๋ฅ์ฑ์ด ์ฆ๊ฐํฉ๋๋ค).
__DATA: ์ฝ๊ธฐ(readable) ๋ฐ ์ฐ๊ธฐ(writable) ๊ฐ๋ฅํ ๋ฐ์ดํฐ๋ฅผ ํฌํจํฉ๋๋ค(์คํ ๋ถ๊ฐ).__got: ์ ์ญ ์คํ์ ํ ์ด๋ธ(Global Offset Table)__nl_symbol_ptr: Non-lazy(๋ก๋ฉ ์ ๋ฐ์ธ๋ฉ) ์ฌ๋ณผ ํฌ์ธํฐ__la_symbol_ptr: Lazy(์ฌ์ฉ ์ ๋ฐ์ธ๋ฉ) ์ฌ๋ณผ ํฌ์ธํฐ__const: ์๋๋ ์ฝ๊ธฐ ์ ์ฉ ๋ฐ์ดํฐ์ฌ์ผ ํจ(์ค์ ๋ก ๊ทธ๋ ์ง ์์)__cfstring: CoreFoundation ๋ฌธ์์ด__data: ์ด๊ธฐํ๋ ์ ์ญ ๋ณ์__bss: ์ด๊ธฐํ๋์ง ์์ ์ ์ ๋ณ์__objc_*(__objc_classlist, __objc_protolist, ๋ฑ): Objective-C ๋ฐํ์์์ ์ฌ์ฉํ๋ ์ ๋ณด__DATA_CONST: __DATA.__const๋ ์์(์ฐ๊ธฐ ๊ธ์ง)๋ผ๊ณ ๋ณด์ฅ๋์ง ์์ผ๋ฉฐ, ๋ค๋ฅธ ํฌ์ธํฐ๋ค๊ณผ GOT ๋ํ ๋ง์ฐฌ๊ฐ์ง์ ๋๋ค. ์ด ์น์ ์__const, ์ผ๋ถ ์ด๋์ ๋ผ์ด์ ๋ฐ (ํด๊ฒฐ๋ ํ์) GOT ํ ์ด๋ธ์mprotect๋ฅผ ์ฌ์ฉํด ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ๋ง๋ญ๋๋ค.__LINKEDIT: ๋ง์ปค(dyld)๋ฅผ ์ํ ์ ๋ณด(์ฌ๋ณผ, ๋ฌธ์์ด, ์ฌ๋ฐฐ์น ํ ์ด๋ธ ์ํธ๋ฆฌ ๋ฑ)๋ฅผ ํฌํจํฉ๋๋ค.__TEXT๋__DATA์ ์ํ์ง ์๋ ๋ด์ฉ์ ๋ด๋ ์ผ๋ฐ ์ปจํ ์ด๋์ด๋ฉฐ ๊ทธ ๋ด์ฉ์ ๋ค๋ฅธ ๋ก๋ ์ปค๋งจ๋์์ ๊ธฐ์ ๋ฉ๋๋ค.- dyld ์ ๋ณด: Rebase, Non-lazy/lazy/weak binding opcodes ๋ฐ export ์ ๋ณด
- Functions starts: ํจ์๋ค์ ์์ ์ฃผ์ ํ
- Data In Code: ๋ฐ์ดํฐ ์ฌ๋ค in __text
- Symbol Table: ๋ฐ์ด๋๋ฆฌ ๋ด์ ์ฌ๋ณผ
- Indirect Symbol Table: ํฌ์ธํฐ/์คํ ์ฌ๋ณผ
- String Table
- Code Signature
__OBJC: Objective-C ๋ฐํ์์์ ์ฌ์ฉํ๋ ์ ๋ณด๋ฅผ ํฌํจํฉ๋๋ค. ์ด ์ ๋ณด๋ __DATA ์ธ๊ทธ๋จผํธ ๋ด์ ๋ค์ํ __objc_* ์น์ ์์๋ ์ฐพ์ ์ ์์ต๋๋ค.__RESTRICT: ๋ด์ฉ์ด ์๋ ์ธ๊ทธ๋จผํธ๋ก, **__restrict**๋ผ๋ ๋จ์ผ ์น์ (์ญ์ ๋น์ด ์์)์ ๊ฐ์ง๋ฉฐ, ๋ฐ์ด๋๋ฆฌ ์คํ ์ DYLD ํ๊ฒฝ ๋ณ์๋ฅผ ๋ฌด์ํ๋๋ก ๋ณด์ฅํฉ๋๋ค.
As it was possible to see in the code, segments also support flags (although they arenโt used very much):
SG_HIGHVM: Core ์ ์ฉ(์ฌ์ฉ ์ ํจ)SG_FVMLIB: ์ฌ์ฉ๋์ง ์์SG_NORELOC: ์ธ๊ทธ๋จผํธ์ ์ฌ๋ฐฐ์น ์์SG_PROTECTED_VERSION_1: ์ํธํ. ์๋ฅผ ๋ค์ด Finder๊ฐ ํ ์คํธ__TEXT์ธ๊ทธ๋จผํธ๋ฅผ ์ํธํํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
LC_UNIXTHREAD/LC_MAIN
**LC_MAIN**์ entryoff attribute์ ์ํธ๋ฆฌํฌ์ธํธ๋ฅผ ํฌํจํฉ๋๋ค. ๋ก๋ ์, dyld๋ ๋จ์ํ ์ด ๊ฐ์ (๋ฉ๋ชจ๋ฆฌ ์์) **๋ฐ์ด๋๋ฆฌ์ ๋ฒ ์ด์ค(base of the binary)**์ ๋ํ ํ ํด๋น ๋ช
๋ น์ผ๋ก **์ ํ(jumps)**ํ์ฌ ๋ฐ์ด๋๋ฆฌ ์ฝ๋๋ฅผ ์คํํฉ๋๋ค.
**LC_UNIXTHREAD**๋ ๋ฉ์ธ ์ค๋ ๋๋ฅผ ์์ํ ๋ ๋ ์ง์คํฐ๊ฐ ๊ฐ์ ธ์ผ ํ ๊ฐ๋ค์ ํฌํจํฉ๋๋ค. ์ด๋ ์ด๋ฏธ deprecated ๋์์ง๋ง **dyld**๋ ์ฌ์ ํ ์ด๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ด๋ก ์ธํด ์ค์ ๋ ๋ ์ง์คํฐ ๊ฐ๋ค์ ๋ค์์ ํตํด ํ์ธํ ์ ์์ต๋๋ค:
otool -l /usr/lib/dyld
[...]
Load command 13
cmd LC_UNIXTHREAD
cmdsize 288
flavor ARM_THREAD_STATE64
count ARM_THREAD_STATE64_COUNT
x0 0x0000000000000000 x1 0x0000000000000000 x2 0x0000000000000000
x3 0x0000000000000000 x4 0x0000000000000000 x5 0x0000000000000000
x6 0x0000000000000000 x7 0x0000000000000000 x8 0x0000000000000000
x9 0x0000000000000000 x10 0x0000000000000000 x11 0x0000000000000000
x12 0x0000000000000000 x13 0x0000000000000000 x14 0x0000000000000000
x15 0x0000000000000000 x16 0x0000000000000000 x17 0x0000000000000000
x18 0x0000000000000000 x19 0x0000000000000000 x20 0x0000000000000000
x21 0x0000000000000000 x22 0x0000000000000000 x23 0x0000000000000000
x24 0x0000000000000000 x25 0x0000000000000000 x26 0x0000000000000000
x27 0x0000000000000000 x28 0x0000000000000000 fp 0x0000000000000000
lr 0x0000000000000000 sp 0x0000000000000000 pc 0x0000000000004b70
cpsr 0x00000000
[...]
LC_CODE_SIGNATURE
Mach O Entitlements And Ipsw Indexing
Contains information about the code signature of the Macho-O file. It only contains an offset that points to the signature blob. This is typically at the very end of the file.
However, you can find some information about this section in this blog post and this gists.
LC_ENCRYPTION_INFO[_64]
๋ฐ์ด๋๋ฆฌ ์ํธํ๋ฅผ ์ง์ํฉ๋๋ค. ๊ทธ๋ฌ๋ ๊ณต๊ฒฉ์๊ฐ ํ๋ก์ธ์ค๋ฅผ ์นจํดํ๋ฉด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ํธํ๋์ง ์์ ์ํ๋ก ๋คํํ ์ ์์ต๋๋ค.
LC_LOAD_DYLINKER
๊ณต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํ๋ก์ธ์ค ์ฃผ์ ๊ณต๊ฐ์ ๋งตํํ๋ dynamic linker ์คํ ํ์ผ์ ๊ฒฝ๋ก๋ฅผ ํฌํจํฉ๋๋ค. ๊ฐ์ ํญ์ /usr/lib/dyld๋ก ์ค์ ๋ฉ๋๋ค. macOS์์๋ dylib ๋งคํ์ด ์ปค๋ ๋ชจ๋๊ฐ ์๋๋ผ ์ฌ์ฉ์ ๋ชจ๋์์ ๋ฐ์ํ๋ค๋ ์ ์ ์ ์ํ์ธ์.
LC_IDENT
๊ตฌ์์ด์ง๋ง, panic ์ ๋คํ๋ฅผ ์์ฑํ๋๋ก ๊ตฌ์ฑํ๋ฉด Mach-O ์ฝ์ด ๋คํ๊ฐ ์์ฑ๋๊ณ ์ปค๋ ๋ฒ์ ์ด LC_IDENT ๋ช
๋ น์ ์ค์ ๋ฉ๋๋ค.
LC_UUID
๋ฌด์์ UUID์ ๋๋ค. ์์ฒด์ ์ผ๋ก๋ ์ง์ ์ ์ธ ์ฉ๋๋ ์ ํ์ ์ด์ง๋ง XNU๊ฐ ์ด๋ฅผ ๋ค๋ฅธ ํ๋ก์ธ์ค ์ ๋ณด์ ํจ๊ป ์บ์ํฉ๋๋ค. ํฌ๋์ ๋ฆฌํฌํธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
LC_DYLD_ENVIRONMENT
ํ๋ก์ธ์ค๊ฐ ์คํ๋๊ธฐ ์ ์ dyld์ ํ๊ฒฝ ๋ณ์๋ฅผ ์ง์ ํ ์ ์๊ฒ ํฉ๋๋ค. ์ด๋ ํ๋ก์ธ์ค ๋ด๋ถ์์ ์์์ ์ฝ๋๋ฅผ ์คํํ ์ ์๊ฒ ํ๋ฏ๋ก ๋งค์ฐ ์ํํ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ ์ด load command๋ #define SUPPORT_LC_DYLD_ENVIRONMENT๋ก ๋น๋๋ dyld์์๋ง ์ฌ์ฉ๋๋ฉฐ, ์ฒ๋ฆฌ๋ ๋ก๋ ๊ฒฝ๋ก๋ฅผ ์ง์ ํ๋ DYLD_..._PATH ํํ์ ๋ณ์๋ก๋ง ์ ํ๋ฉ๋๋ค.
LC_LOAD_DYLIB
์ด load command๋ loader(dyld)์๊ฒ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ก๋ํ๊ณ ๋งํฌํ๋๋ก ์ง์ํ๋ dynamic library ์์กด์ฑ์ ์ค๋ช
ํฉ๋๋ค. Mach-O ๋ฐ์ด๋๋ฆฌ๊ฐ ํ์๋ก ํ๋ ๊ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ง๋ค ํ๋์ LC_LOAD_DYLIB load command๊ฐ ์์ต๋๋ค.
- This load command is a structure of type
dylib_command(which contains a struct dylib, describing the actual dependent dynamic library):
struct dylib_command {
uint32_t cmd; /* LC_LOAD_{,WEAK_}DYLIB */
uint32_t cmdsize; /* includes pathname string */
struct dylib dylib; /* the library identification */
};
struct dylib {
union lc_str name; /* library's path name */
uint32_t timestamp; /* library's build time stamp */
uint32_t current_version; /* library's current version number */
uint32_t compatibility_version; /* library's compatibility vers number*/
};
.png)
๋๋ cli์์ ๋ค์๊ณผ ๊ฐ์ด ์ด ์ ๋ณด๋ฅผ ์ป์ ์๋ ์์ต๋๋ค:
otool -L /bin/ls
/bin/ls:
/usr/lib/libutil.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1319.0.0)
์ผ๋ถ ์ ์ฌ์ ์ ์ฑ์ฝ๋ ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ:
- DiskArbitration: USB ๋๋ผ์ด๋ธ ๋ชจ๋ํฐ๋ง
- AVFoundation: ์ค๋์ค ๋ฐ ๋น๋์ค ์บก์ฒ
- CoreWLAN: Wifi ์ค์บ.
Tip
A Mach-O binary can contain one or more constructors, that will be executed before the address specified in LC_MAIN.
The offsets of any constructors are held in the __mod_init_func section of the __DATA_CONST segment.
Mach-O ๋ฐ์ดํฐ
ํ์ผ์ ํต์ฌ์๋ load-commands ์์ญ์ ์ ์๋ ์ฌ๋ฌ ์ธ๊ทธ๋จผํธ๋ก ๊ตฌ์ฑ๋ ๋ฐ์ดํฐ ์์ญ์ด ์์ต๋๋ค. ๊ฐ ์ธ๊ทธ๋จผํธ ์์๋ ๋ค์ํ ๋ฐ์ดํฐ ์น์ ์ด ์กด์ฌํ ์ ์์ผ๋ฉฐ, ๊ฐ ์น์ ์ ํน์ ํ์ ์ ๋ํ ์ฝ๋ ๋๋ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ดํฉ๋๋ค.
Tip
๋ฐ์ดํฐ๋ ๊ธฐ๋ณธ์ ์ผ๋ก LC_SEGMENTS_64 ๋ก๋ ์ปค๋งจ๋์ ์ํด ๋ก๋๋๋ ๋ชจ๋ ์ ๋ณด๋ฅผ ํฌํจํ๋ ๋ถ๋ถ์ ๋๋ค.
 (3).png)
์ด์๋ ๋ค์์ด ํฌํจ๋ฉ๋๋ค:
- ํจ์ ํ ์ด๋ธ: ํ๋ก๊ทธ๋จ ํจ์์ ๋ํ ์ ๋ณด๋ฅผ ๋ณด๊ดํฉ๋๋ค.
- ์ฌ๋ณผ ํ ์ด๋ธ: ๋ฐ์ด๋๋ฆฌ์์ ์ฌ์ฉ๋๋ ์ธ๋ถ ํจ์์ ๋ํ ์ ๋ณด๋ฅผ ํฌํจํฉ๋๋ค.
- ๋ด๋ถ ํจ์๋ ๋ณ์ ์ด๋ฆ ๋ฑ๋ ํฌํจ๋ ์ ์์ต๋๋ค.
ํ์ธํ๋ ค๋ฉด Mach-O View ๋๊ตฌ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค:
.png)
๋๋ CLI์์:
size -m /bin/ls
Objetive-C ๊ณตํต ์น์
__TEXT ์ธ๊ทธ๋จผํธ (r-x):
__objc_classname: ํด๋์ค ์ด๋ฆ(๋ฌธ์์ด)__objc_methname: ๋ฉ์๋ ์ด๋ฆ(๋ฌธ์์ด)__objc_methtype: ๋ฉ์๋ ํ์ (๋ฌธ์์ด)
__DATA ์ธ๊ทธ๋จผํธ (rw-):
__objc_classlist: ๋ชจ๋ Objetive-C ํด๋์ค์ ๋ํ ํฌ์ธํฐ__objc_nlclslist: Non-Lazy Objective-C ํด๋์ค์ ๋ํ ํฌ์ธํฐ__objc_catlist: ์นดํ ๊ณ ๋ฆฌ์ ๋ํ ํฌ์ธํฐ__objc_nlcatlist: Non-Lazy ์นดํ ๊ณ ๋ฆฌ์ ๋ํ ํฌ์ธํฐ__objc_protolist: ํ๋กํ ์ฝ ๋ชฉ๋ก__objc_const: ์์ ๋ฐ์ดํฐ__objc_imageinfo,__objc_selrefs,objc__protorefsโฆ
Swift
_swift_typeref,_swift3_capture,_swift3_assocty,_swift3_types, _swift3_proto,_swift3_fieldmd,_swift3_builtin,_swift3_reflstr
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 ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


