# ELF Tricks {% hint style="success" %} Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Support HackTricks * Check the [**subscription plans**](https://github.com/sponsors/carlospolop)! * **Join the** πŸ’¬ [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.** * **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
{% endhint %} ## Program Headers ELFλ₯Ό λ©”λͺ¨λ¦¬μ— λ‘œλ“œν•˜λŠ” 방법을 λ‘œλ”μ—κ²Œ μ„€λͺ…ν•©λ‹ˆλ‹€: ```bash readelf -lW lnstat Elf file type is DYN (Position-Independent Executable file) Entry point 0x1c00 There are 9 program headers, starting at offset 64 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align PHDR 0x000040 0x0000000000000040 0x0000000000000040 0x0001f8 0x0001f8 R 0x8 INTERP 0x000238 0x0000000000000238 0x0000000000000238 0x00001b 0x00001b R 0x1 [Requesting program interpreter: /lib/ld-linux-aarch64.so.1] LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x003f7c 0x003f7c R E 0x10000 LOAD 0x00fc48 0x000000000001fc48 0x000000000001fc48 0x000528 0x001190 RW 0x10000 DYNAMIC 0x00fc58 0x000000000001fc58 0x000000000001fc58 0x000200 0x000200 RW 0x8 NOTE 0x000254 0x0000000000000254 0x0000000000000254 0x0000e0 0x0000e0 R 0x4 GNU_EH_FRAME 0x003610 0x0000000000003610 0x0000000000003610 0x0001b4 0x0001b4 R 0x4 GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x10 GNU_RELRO 0x00fc48 0x000000000001fc48 0x000000000001fc48 0x0003b8 0x0003b8 R 0x1 Section to Segment mapping: Segment Sections... 00 01 .interp 02 .interp .note.gnu.build-id .note.ABI-tag .note.package .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 03 .init_array .fini_array .dynamic .got .data .bss 04 .dynamic 05 .note.gnu.build-id .note.ABI-tag .note.package 06 .eh_frame_hdr 07 08 .init_array .fini_array .dynamic .got ``` The previous program has **9 program headers**, then, the **segment mapping** indicates in which program header (from 00 to 08) **each section is located**. ### PHDR - Program HeaDeR Contains the program header tables and metadata itself. ### INTERP Indicates the path of the loader to use to load the binary into memory. ### LOAD These headers are used to indicate **how to load a binary into memory.**\ Each **LOAD** header indicates a region of **memory** (size, permissions and alignment) and indicates the bytes of the ELF **binary to copy in there**. For example, the second one has a size of 0x1190, should be located at 0x1fc48 with permissions read and write and will be filled with 0x528 from the offset 0xfc48 (it doesn't fill all the reserved space). This memory will contain the sections `.init_array .fini_array .dynamic .got .data .bss`. ### DYNAMIC This header helps to link programs to their library dependencies and apply relocations. Check the **`.dynamic`** section. ### NOTE This stores vendor metadata information about the binary. ### GNU\_EH\_FRAME Defines the location of the stack unwind tables, used by debuggers and C++ exception handling-runtime functions. ### GNU\_STACK Contains the configuration of the stack execution prevention defense. If enabled, the binary won't be able to execute code from the stack. ### GNU\_RELRO Indicates the RELRO (Relocation Read-Only) configuration of the binary. This protection will mark as read-only certain sections of the memory (like the `GOT` or the `init` and `fini` tables) after the program has loaded and before it begins running. In the previous example it's copying 0x3b8 bytes to 0x1fc48 as read-only affecting the sections `.init_array .fini_array .dynamic .got .data .bss`. Note that RELRO can be partial or full, the partial version do not protect the section **`.plt.got`**, which is used for **lazy binding** and needs this memory space to have **write permissions** to write the address of the libraries the first time their location is searched. ### TLS Defines a table of TLS entries, which stores info about thread-local variables. ## Section Headers Section headers gives a more detailed view of the ELF binary ``` objdump lnstat -h lnstat: file format elf64-littleaarch64 Sections: Idx Name Size VMA LMA File off Algn 0 .interp 0000001b 0000000000000238 0000000000000238 00000238 2**0 CONTENTS, ALLOC, LOAD, READONLY, DATA 1 .note.gnu.build-id 00000024 0000000000000254 0000000000000254 00000254 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 2 .note.ABI-tag 00000020 0000000000000278 0000000000000278 00000278 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 3 .note.package 0000009c 0000000000000298 0000000000000298 00000298 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 4 .gnu.hash 0000001c 0000000000000338 0000000000000338 00000338 2**3 CONTENTS, ALLOC, LOAD, READONLY, DATA 5 .dynsym 00000498 0000000000000358 0000000000000358 00000358 2**3 CONTENTS, ALLOC, LOAD, READONLY, DATA 6 .dynstr 000001fe 00000000000007f0 00000000000007f0 000007f0 2**0 CONTENTS, ALLOC, LOAD, READONLY, DATA 7 .gnu.version 00000062 00000000000009ee 00000000000009ee 000009ee 2**1 CONTENTS, ALLOC, LOAD, READONLY, DATA 8 .gnu.version_r 00000050 0000000000000a50 0000000000000a50 00000a50 2**3 CONTENTS, ALLOC, LOAD, READONLY, DATA 9 .rela.dyn 00000228 0000000000000aa0 0000000000000aa0 00000aa0 2**3 CONTENTS, ALLOC, LOAD, READONLY, DATA 10 .rela.plt 000003c0 0000000000000cc8 0000000000000cc8 00000cc8 2**3 CONTENTS, ALLOC, LOAD, READONLY, DATA 11 .init 00000018 0000000000001088 0000000000001088 00001088 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 12 .plt 000002a0 00000000000010a0 00000000000010a0 000010a0 2**4 CONTENTS, ALLOC, LOAD, READONLY, CODE 13 .text 00001c34 0000000000001340 0000000000001340 00001340 2**6 CONTENTS, ALLOC, LOAD, READONLY, CODE 14 .fini 00000014 0000000000002f74 0000000000002f74 00002f74 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 15 .rodata 00000686 0000000000002f88 0000000000002f88 00002f88 2**3 CONTENTS, ALLOC, LOAD, READONLY, DATA 16 .eh_frame_hdr 000001b4 0000000000003610 0000000000003610 00003610 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 17 .eh_frame 000007b4 00000000000037c8 00000000000037c8 000037c8 2**3 CONTENTS, ALLOC, LOAD, READONLY, DATA 18 .init_array 00000008 000000000001fc48 000000000001fc48 0000fc48 2**3 CONTENTS, ALLOC, LOAD, DATA 19 .fini_array 00000008 000000000001fc50 000000000001fc50 0000fc50 2**3 CONTENTS, ALLOC, LOAD, DATA 20 .dynamic 00000200 000000000001fc58 000000000001fc58 0000fc58 2**3 CONTENTS, ALLOC, LOAD, DATA 21 .got 000001a8 000000000001fe58 000000000001fe58 0000fe58 2**3 CONTENTS, ALLOC, LOAD, DATA 22 .data 00000170 0000000000020000 0000000000020000 00010000 2**3 CONTENTS, ALLOC, LOAD, DATA 23 .bss 00000c68 0000000000020170 0000000000020170 00010170 2**3 ALLOC 24 .gnu_debugaltlink 00000049 0000000000000000 0000000000000000 00010170 2**0 CONTENTS, READONLY 25 .gnu_debuglink 00000034 0000000000000000 0000000000000000 000101bc 2**2 CONTENTS, READONLY ``` It also indicates the location, offset, permissions but also the **type of data** it section has. ### Meta Sections * **String table**: ELF νŒŒμΌμ— ν•„μš”ν•œ λͺ¨λ“  λ¬Έμžμ—΄μ„ ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€(ν•˜μ§€λ§Œ ν”„λ‘œκ·Έλž¨μ—μ„œ μ‹€μ œλ‘œ μ‚¬μš©λ˜λŠ” λ¬Έμžμ—΄μ€ μ•„λ‹™λ‹ˆλ‹€). 예λ₯Ό λ“€μ–΄, `.text` λ˜λŠ” `.data`와 같은 μ„Ήμ…˜ 이름을 ν¬ν•¨ν•©λ‹ˆλ‹€. 그리고 λ¬Έμžμ—΄ ν…Œμ΄λΈ”μ—μ„œ `.text`κ°€ 45의 μ˜€ν”„μ…‹μ— μžˆλ‹€λ©΄ **name** ν•„λ“œμ—μ„œ 숫자 **45**λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. * λ¬Έμžμ—΄ ν…Œμ΄λΈ”μ΄ 어디에 μžˆλŠ”μ§€ μ°ΎκΈ° μœ„ν•΄ ELFλŠ” λ¬Έμžμ—΄ ν…Œμ΄λΈ”μ— λŒ€ν•œ 포인터λ₯Ό ν¬ν•¨ν•©λ‹ˆλ‹€. * **Symbol table**: 이름(λ¬Έμžμ—΄ ν…Œμ΄λΈ”μ˜ μ˜€ν”„μ…‹), μ£Όμ†Œ, 크기 및 κΈ°ν˜Έμ— λŒ€ν•œ μΆ”κ°€ 메타데이터와 같은 κΈ°ν˜Έμ— λŒ€ν•œ 정보λ₯Ό ν¬ν•¨ν•©λ‹ˆλ‹€. ### Main Sections * **`.text`**: μ‹€ν–‰ν•  ν”„λ‘œκ·Έλž¨μ˜ λͺ…λ Ήμ–΄μž…λ‹ˆλ‹€. * **`.data`**: ν”„λ‘œκ·Έλž¨μ—μ„œ μ •μ˜λœ 값을 가진 μ „μ—­ λ³€μˆ˜μž…λ‹ˆλ‹€. * **`.bss`**: μ΄ˆκΈ°ν™”λ˜μ§€ μ•Šμ€ μ „μ—­ λ³€μˆ˜(λ˜λŠ” 0으둜 μ΄ˆκΈ°ν™”λ¨). μ—¬κΈ°μ˜ λ³€μˆ˜λŠ” μžλ™μœΌλ‘œ 0으둜 μ΄ˆκΈ°ν™”λ˜λ―€λ‘œ 이진 νŒŒμΌμ— μ“Έλͺ¨μ—†λŠ” 0이 μΆ”κ°€λ˜λŠ” 것을 λ°©μ§€ν•©λ‹ˆλ‹€. * **`.rodata`**: μƒμˆ˜ μ „μ—­ λ³€μˆ˜(읽기 μ „μš© μ„Ήμ…˜)μž…λ‹ˆλ‹€. * **`.tdata`** 및 **`.tbss`**: μŠ€λ ˆλ“œ 둜컬 λ³€μˆ˜κ°€ μ‚¬μš©λ  λ•Œ .data 및 .bss와 κ°™μŠ΅λ‹ˆλ‹€(`__thread_local` in C++ λ˜λŠ” `__thread` in C). * **`.dynamic`**: μ•„λž˜λ₯Ό μ°Έμ‘°ν•˜μ‹­μ‹œμ˜€. ## Symbols SymbolsλŠ” ν”„λ‘œκ·Έλž¨ λ‚΄μ˜ λͺ…λͺ…λœ μœ„μΉ˜λ‘œ, ν•¨μˆ˜, μ „μ—­ 데이터 객체, μŠ€λ ˆλ“œ 둜컬 λ³€μˆ˜ 등이 될 수 μžˆμŠ΅λ‹ˆλ‹€. ``` readelf -s lnstat Symbol table '.dynsym' contains 49 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000001088 0 SECTION LOCAL DEFAULT 12 .init 2: 0000000000020000 0 SECTION LOCAL DEFAULT 23 .data 3: 0000000000000000 0 FUNC GLOBAL DEFAULT UND strtok@GLIBC_2.17 (2) 4: 0000000000000000 0 FUNC GLOBAL DEFAULT UND s[...]@GLIBC_2.17 (2) 5: 0000000000000000 0 FUNC GLOBAL DEFAULT UND strlen@GLIBC_2.17 (2) 6: 0000000000000000 0 FUNC GLOBAL DEFAULT UND fputs@GLIBC_2.17 (2) 7: 0000000000000000 0 FUNC GLOBAL DEFAULT UND exit@GLIBC_2.17 (2) 8: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _[...]@GLIBC_2.34 (3) 9: 0000000000000000 0 FUNC GLOBAL DEFAULT UND perror@GLIBC_2.17 (2) 10: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_deregisterT[...] 11: 0000000000000000 0 FUNC WEAK DEFAULT UND _[...]@GLIBC_2.17 (2) 12: 0000000000000000 0 FUNC GLOBAL DEFAULT UND putc@GLIBC_2.17 (2) [...] ``` 각 심볼 ν•­λͺ©μ€ λ‹€μŒμ„ ν¬ν•¨ν•©λ‹ˆλ‹€: * **이름** * **바인딩 속성** (μ•½ν•œ, 둜컬 λ˜λŠ” μ „μ—­): 둜컬 심볼은 ν”„λ‘œκ·Έλž¨ μžμ²΄μ—μ„œλ§Œ μ ‘κ·Όν•  수 있으며, μ „μ—­ 심볼은 ν”„λ‘œκ·Έλž¨ μ™ΈλΆ€μ—μ„œ κ³΅μœ λ©λ‹ˆλ‹€. μ•½ν•œ κ°μ²΄λŠ” 예λ₯Ό λ“€μ–΄ λ‹€λ₯Έ ν•¨μˆ˜λ‘œ μž¬μ •μ˜λ  수 μžˆλŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. * **μœ ν˜•**: NOTYPE (μœ ν˜•μ΄ μ§€μ •λ˜μ§€ μ•ŠμŒ), OBJECT (μ „μ—­ 데이터 λ³€μˆ˜), FUNC (ν•¨μˆ˜), SECTION (μ„Ήμ…˜), FILE (λ””λ²„κ±°μš© μ†ŒμŠ€ μ½”λ“œ 파일), TLS (μŠ€λ ˆλ“œ 둜컬 λ³€μˆ˜), GNU\_IFUNC (재배치λ₯Ό μœ„ν•œ κ°„μ ‘ ν•¨μˆ˜) * **μ„Ήμ…˜** 인덱슀 (μœ„μΉ˜) * **κ°’** (λ©”λͺ¨λ¦¬ λ‚΄ μ£Όμ†Œ) * **크기** ## 동적 μ„Ήμ…˜ ``` readelf -d lnstat Dynamic section at offset 0xfc58 contains 28 entries: Tag Type Name/Value 0x0000000000000001 (NEEDED) Shared library: [libc.so.6] 0x0000000000000001 (NEEDED) Shared library: [ld-linux-aarch64.so.1] 0x000000000000000c (INIT) 0x1088 0x000000000000000d (FINI) 0x2f74 0x0000000000000019 (INIT_ARRAY) 0x1fc48 0x000000000000001b (INIT_ARRAYSZ) 8 (bytes) 0x000000000000001a (FINI_ARRAY) 0x1fc50 0x000000000000001c (FINI_ARRAYSZ) 8 (bytes) 0x000000006ffffef5 (GNU_HASH) 0x338 0x0000000000000005 (STRTAB) 0x7f0 0x0000000000000006 (SYMTAB) 0x358 0x000000000000000a (STRSZ) 510 (bytes) 0x000000000000000b (SYMENT) 24 (bytes) 0x0000000000000015 (DEBUG) 0x0 0x0000000000000003 (PLTGOT) 0x1fe58 0x0000000000000002 (PLTRELSZ) 960 (bytes) 0x0000000000000014 (PLTREL) RELA 0x0000000000000017 (JMPREL) 0xcc8 0x0000000000000007 (RELA) 0xaa0 0x0000000000000008 (RELASZ) 552 (bytes) 0x0000000000000009 (RELAENT) 24 (bytes) 0x000000000000001e (FLAGS) BIND_NOW 0x000000006ffffffb (FLAGS_1) Flags: NOW PIE 0x000000006ffffffe (VERNEED) 0xa50 0x000000006fffffff (VERNEEDNUM) 2 0x000000006ffffff0 (VERSYM) 0x9ee 0x000000006ffffff9 (RELACOUNT) 15 0x0000000000000000 (NULL) 0x0 ``` NEEDED λ””λ ‰ν† λ¦¬λŠ” ν”„λ‘œκ·Έλž¨μ΄ 계속 μ§„ν–‰ν•˜κΈ° μœ„ν•΄ **μ–ΈκΈ‰λœ 라이브러리λ₯Ό λ‘œλ“œν•΄μ•Ό 함**을 λ‚˜νƒ€λƒ…λ‹ˆλ‹€. NEEDED λ””λ ‰ν† λ¦¬λŠ” 곡유 **λΌμ΄λΈŒλŸ¬λ¦¬κ°€ μ™„μ „νžˆ μž‘λ™ν•˜κ³  μ‚¬μš© μ€€λΉ„κ°€ λ˜μ—ˆμ„ λ•Œ** μ™„λ£Œλ©λ‹ˆλ‹€. ## Relocations λ‘œλ”λŠ” λ˜ν•œ λ‘œλ“œν•œ ν›„ 쒅속성을 μž¬λ°°μΉ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€. μ΄λŸ¬ν•œ μž¬λ°°μΉ˜λŠ” REL λ˜λŠ” RELA ν˜•μ‹μ˜ 재배치 ν…Œμ΄λΈ”μ— ν‘œμ‹œλ˜λ©°, 재배치 μˆ˜λŠ” 동적 μ„Ήμ…˜ RELSZ λ˜λŠ” RELASZ에 μ œκ³΅λ©λ‹ˆλ‹€. ``` readelf -r lnstat Relocation section '.rela.dyn' at offset 0xaa0 contains 23 entries: Offset Info Type Sym. Value Sym. Name + Addend 00000001fc48 000000000403 R_AARCH64_RELATIV 1d10 00000001fc50 000000000403 R_AARCH64_RELATIV 1cc0 00000001fff0 000000000403 R_AARCH64_RELATIV 1340 000000020008 000000000403 R_AARCH64_RELATIV 20008 000000020010 000000000403 R_AARCH64_RELATIV 3330 000000020030 000000000403 R_AARCH64_RELATIV 3338 000000020050 000000000403 R_AARCH64_RELATIV 3340 000000020070 000000000403 R_AARCH64_RELATIV 3348 000000020090 000000000403 R_AARCH64_RELATIV 3350 0000000200b0 000000000403 R_AARCH64_RELATIV 3358 0000000200d0 000000000403 R_AARCH64_RELATIV 3360 0000000200f0 000000000403 R_AARCH64_RELATIV 3370 000000020110 000000000403 R_AARCH64_RELATIV 3378 000000020130 000000000403 R_AARCH64_RELATIV 3380 000000020150 000000000403 R_AARCH64_RELATIV 3388 00000001ffb8 000a00000401 R_AARCH64_GLOB_DA 0000000000000000 _ITM_deregisterTM[...] + 0 00000001ffc0 000b00000401 R_AARCH64_GLOB_DA 0000000000000000 __cxa_finalize@GLIBC_2.17 + 0 00000001ffc8 000f00000401 R_AARCH64_GLOB_DA 0000000000000000 stderr@GLIBC_2.17 + 0 00000001ffd0 001000000401 R_AARCH64_GLOB_DA 0000000000000000 optarg@GLIBC_2.17 + 0 00000001ffd8 001400000401 R_AARCH64_GLOB_DA 0000000000000000 stdout@GLIBC_2.17 + 0 00000001ffe0 001e00000401 R_AARCH64_GLOB_DA 0000000000000000 __gmon_start__ + 0 00000001ffe8 001f00000401 R_AARCH64_GLOB_DA 0000000000000000 __stack_chk_guard@GLIBC_2.17 + 0 00000001fff8 002e00000401 R_AARCH64_GLOB_DA 0000000000000000 _ITM_registerTMCl[...] + 0 Relocation section '.rela.plt' at offset 0xcc8 contains 40 entries: Offset Info Type Sym. Value Sym. Name + Addend 00000001fe70 000300000402 R_AARCH64_JUMP_SL 0000000000000000 strtok@GLIBC_2.17 + 0 00000001fe78 000400000402 R_AARCH64_JUMP_SL 0000000000000000 strtoul@GLIBC_2.17 + 0 00000001fe80 000500000402 R_AARCH64_JUMP_SL 0000000000000000 strlen@GLIBC_2.17 + 0 00000001fe88 000600000402 R_AARCH64_JUMP_SL 0000000000000000 fputs@GLIBC_2.17 + 0 00000001fe90 000700000402 R_AARCH64_JUMP_SL 0000000000000000 exit@GLIBC_2.17 + 0 00000001fe98 000800000402 R_AARCH64_JUMP_SL 0000000000000000 __libc_start_main@GLIBC_2.34 + 0 00000001fea0 000900000402 R_AARCH64_JUMP_SL 0000000000000000 perror@GLIBC_2.17 + 0 00000001fea8 000b00000402 R_AARCH64_JUMP_SL 0000000000000000 __cxa_finalize@GLIBC_2.17 + 0 00000001feb0 000c00000402 R_AARCH64_JUMP_SL 0000000000000000 putc@GLIBC_2.17 + 0 00000001feb8 000d00000402 R_AARCH64_JUMP_SL 0000000000000000 opendir@GLIBC_2.17 + 0 00000001fec0 000e00000402 R_AARCH64_JUMP_SL 0000000000000000 fputc@GLIBC_2.17 + 0 00000001fec8 001100000402 R_AARCH64_JUMP_SL 0000000000000000 snprintf@GLIBC_2.17 + 0 00000001fed0 001200000402 R_AARCH64_JUMP_SL 0000000000000000 __snprintf_chk@GLIBC_2.17 + 0 00000001fed8 001300000402 R_AARCH64_JUMP_SL 0000000000000000 malloc@GLIBC_2.17 + 0 00000001fee0 001500000402 R_AARCH64_JUMP_SL 0000000000000000 gettimeofday@GLIBC_2.17 + 0 00000001fee8 001600000402 R_AARCH64_JUMP_SL 0000000000000000 sleep@GLIBC_2.17 + 0 00000001fef0 001700000402 R_AARCH64_JUMP_SL 0000000000000000 __vfprintf_chk@GLIBC_2.17 + 0 00000001fef8 001800000402 R_AARCH64_JUMP_SL 0000000000000000 calloc@GLIBC_2.17 + 0 00000001ff00 001900000402 R_AARCH64_JUMP_SL 0000000000000000 rewind@GLIBC_2.17 + 0 00000001ff08 001a00000402 R_AARCH64_JUMP_SL 0000000000000000 strdup@GLIBC_2.17 + 0 00000001ff10 001b00000402 R_AARCH64_JUMP_SL 0000000000000000 closedir@GLIBC_2.17 + 0 00000001ff18 001c00000402 R_AARCH64_JUMP_SL 0000000000000000 __stack_chk_fail@GLIBC_2.17 + 0 00000001ff20 001d00000402 R_AARCH64_JUMP_SL 0000000000000000 strrchr@GLIBC_2.17 + 0 00000001ff28 001e00000402 R_AARCH64_JUMP_SL 0000000000000000 __gmon_start__ + 0 00000001ff30 002000000402 R_AARCH64_JUMP_SL 0000000000000000 abort@GLIBC_2.17 + 0 00000001ff38 002100000402 R_AARCH64_JUMP_SL 0000000000000000 feof@GLIBC_2.17 + 0 00000001ff40 002200000402 R_AARCH64_JUMP_SL 0000000000000000 getopt_long@GLIBC_2.17 + 0 00000001ff48 002300000402 R_AARCH64_JUMP_SL 0000000000000000 __fprintf_chk@GLIBC_2.17 + 0 00000001ff50 002400000402 R_AARCH64_JUMP_SL 0000000000000000 strcmp@GLIBC_2.17 + 0 00000001ff58 002500000402 R_AARCH64_JUMP_SL 0000000000000000 free@GLIBC_2.17 + 0 00000001ff60 002600000402 R_AARCH64_JUMP_SL 0000000000000000 readdir64@GLIBC_2.17 + 0 00000001ff68 002700000402 R_AARCH64_JUMP_SL 0000000000000000 strndup@GLIBC_2.17 + 0 00000001ff70 002800000402 R_AARCH64_JUMP_SL 0000000000000000 strchr@GLIBC_2.17 + 0 00000001ff78 002900000402 R_AARCH64_JUMP_SL 0000000000000000 fwrite@GLIBC_2.17 + 0 00000001ff80 002a00000402 R_AARCH64_JUMP_SL 0000000000000000 fflush@GLIBC_2.17 + 0 00000001ff88 002b00000402 R_AARCH64_JUMP_SL 0000000000000000 fopen64@GLIBC_2.17 + 0 00000001ff90 002c00000402 R_AARCH64_JUMP_SL 0000000000000000 __isoc99_sscanf@GLIBC_2.17 + 0 00000001ff98 002d00000402 R_AARCH64_JUMP_SL 0000000000000000 strncpy@GLIBC_2.17 + 0 00000001ffa0 002f00000402 R_AARCH64_JUMP_SL 0000000000000000 __assert_fail@GLIBC_2.17 + 0 00000001ffa8 003000000402 R_AARCH64_JUMP_SL 0000000000000000 fgets@GLIBC_2.17 + 0 ``` ### 정적 재배치 ν”„λ‘œκ·Έλž¨μ΄ **μ„ ν˜Έν•˜λŠ” μ£Όμ†Œ**(보톡 0x400000)와 λ‹€λ₯Έ μœ„μΉ˜μ— λ‘œλ“œλ˜λ©΄(μ£Όμ†Œκ°€ 이미 μ‚¬μš© μ€‘μ΄κ±°λ‚˜ **ASLR** λ˜λŠ” 기타 이유둜), 정적 μž¬λ°°μΉ˜λŠ” **포인터λ₯Ό μˆ˜μ •**ν•˜μ—¬ 이진 파일이 μ„ ν˜Έν•˜λŠ” μ£Όμ†Œμ— λ‘œλ“œλ  κ²ƒμœΌλ‘œ μ˜ˆμƒν–ˆλ˜ 값을 μˆ˜μ •ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, `R_AARCH64_RELATIV` μœ ν˜•μ˜ μ„Ήμ…˜μ€ 재배치 편ν–₯에 μΆ”κ°€ κ°’(addend value)을 λ”ν•œ μ£Όμ†Œλ₯Ό μˆ˜μ •ν•΄μ•Ό ν•©λ‹ˆλ‹€. ### 동적 재배치 및 GOT μž¬λ°°μΉ˜λŠ” μ™ΈλΆ€ 기호(μ’…μ†μ„±μ˜ ν•¨μˆ˜μ™€ 같은)λ₯Ό μ°Έμ‘°ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, libC의 malloc ν•¨μˆ˜μž…λ‹ˆλ‹€. 그런 λ‹€μŒ λ‘œλ”λŠ” libCλ₯Ό λ‘œλ“œν•  λ•Œ malloc ν•¨μˆ˜κ°€ λ‘œλ“œλœ μ£Όμ†Œλ₯Ό ν™•μΈν•˜κ³ , 이 μ£Όμ†Œλ₯Ό GOT(μ „μ—­ μ˜€ν”„μ…‹ ν…Œμ΄λΈ”) ν…Œμ΄λΈ”(재배치 ν…Œμ΄λΈ”μ— ν‘œμ‹œλ¨)에 κΈ°λ‘ν•©λ‹ˆλ‹€. μ—¬κΈ°μ„œ malloc의 μ£Όμ†Œκ°€ μ§€μ •λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€. ### ν”„λ‘œμ‹œμ € 링크 ν…Œμ΄λΈ” PLT μ„Ήμ…˜μ€ 지연 바인딩을 μˆ˜ν–‰ν•  수 있게 ν•΄μ£Όλ©°, μ΄λŠ” ν•¨μˆ˜μ˜ μœ„μΉ˜ 해석이 처음 μ ‘κ·Όν•  λ•Œ μˆ˜ν–‰λœλ‹€λŠ” 것을 μ˜λ―Έν•©λ‹ˆλ‹€. λ”°λΌμ„œ ν”„λ‘œκ·Έλž¨μ΄ malloc을 ν˜ΈμΆœν•  λ•Œ, μ‹€μ œλ‘œλŠ” PLT의 `malloc`에 ν•΄λ‹Ήν•˜λŠ” μœ„μΉ˜(`malloc@plt`)λ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€. 처음 ν˜ΈμΆœν•  λ•Œ `malloc`의 μ£Όμ†Œλ₯Ό ν•΄μ„ν•˜κ³  μ €μž₯ν•˜λ―€λ‘œ λ‹€μŒμ— `malloc`이 호좜될 λ•ŒλŠ” PLT μ½”λ“œ λŒ€μ‹  κ·Έ μ£Όμ†Œκ°€ μ‚¬μš©λ©λ‹ˆλ‹€. ## ν”„λ‘œκ·Έλž¨ μ΄ˆκΈ°ν™” ν”„λ‘œκ·Έλž¨μ΄ λ‘œλ“œλœ ν›„ μ‹€ν–‰ν•  μ‹œκ°„μž…λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ μ‹€ν–‰λ˜λŠ” 첫 번째 μ½”λ“œλŠ” **항상 `main`** ν•¨μˆ˜κ°€ μ•„λ‹™λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄ C++μ—μ„œ **μ „μ—­ λ³€μˆ˜κ°€ 클래슀의 객체**인 경우, 이 κ°μ²΄λŠ” main이 μ‹€ν–‰λ˜κΈ° **전에** **μ΄ˆκΈ°ν™”**λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€. ```cpp #include // g++ autoinit.cpp -o autoinit class AutoInit { public: AutoInit() { printf("Hello AutoInit!\n"); } ~AutoInit() { printf("Goodbye AutoInit!\n"); } }; AutoInit autoInit; int main() { printf("Main\n"); return 0; } ``` μ΄λŸ¬ν•œ μ „μ—­ λ³€μˆ˜λŠ” `.data` λ˜λŠ” `.bss`에 μœ„μΉ˜ν•˜μ§€λ§Œ, `__CTOR_LIST__` 및 `__DTOR_LIST__` λͺ©λ‘μ—λŠ” μ΄ˆκΈ°ν™” 및 μ†Œλ©Έν•  객체가 μ €μž₯λ˜μ–΄ 이듀을 좔적할 수 μžˆμŠ΅λ‹ˆλ‹€. C μ½”λ“œμ—μ„œλŠ” GNU ν™•μž₯을 μ‚¬μš©ν•˜μ—¬ λ™μΌν•œ κ²°κ³Όλ₯Ό 얻을 수 μžˆμŠ΅λ‹ˆλ‹€: ```c __attributte__((constructor)) //Add a constructor to execute before __attributte__((destructor)) //Add to the destructor list ``` From a compiler perspective, to execute these actions before and after the `main` function is executed, it's possible to create a `init` function and a `fini` function which would be referenced in the dynamic section as **`INIT`** and **`FIN`**. and are placed in the `init` and `fini` sections of the ELF. 컴파일러 κ΄€μ μ—μ„œ, `main` ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜κΈ° μ „κ³Ό 후에 μ΄λŸ¬ν•œ μž‘μ—…μ„ μ‹€ν–‰ν•˜κΈ° μœ„ν•΄ `init` ν•¨μˆ˜μ™€ `fini` ν•¨μˆ˜λ₯Ό 생성할 수 있으며, μ΄λŠ” 동적 μ„Ήμ…˜μ—μ„œ **`INIT`** 및 **`FIN`**으둜 μ°Έμ‘°λ©λ‹ˆλ‹€. 그리고 ELF의 `init` 및 `fini` μ„Ήμ…˜μ— λ°°μΉ˜λ©λ‹ˆλ‹€. The other option, as mentioned, is to reference the lists **`__CTOR_LIST__`** and **`__DTOR_LIST__`** in the **`INIT_ARRAY`** and **`FINI_ARRAY`** entries in the dynamic section and the length of these are indicated by **`INIT_ARRAYSZ`** and **`FINI_ARRAYSZ`**. Each entry is a function pointer that will be called without arguments. μ–ΈκΈ‰λœ λ‹€λ₯Έ μ˜΅μ…˜μ€ 동적 μ„Ήμ…˜μ˜ **`INIT_ARRAY`** 및 **`FINI_ARRAY`** ν•­λͺ©μ—μ„œ **`__CTOR_LIST__`** 및 **`__DTOR_LIST__`** λͺ©λ‘μ„ μ°Έμ‘°ν•˜λŠ” 것이며, μ΄λ“€μ˜ κΈΈμ΄λŠ” **`INIT_ARRAYSZ`** 및 **`FINI_ARRAYSZ`**둜 ν‘œμ‹œλ©λ‹ˆλ‹€. 각 ν•­λͺ©μ€ 인수 없이 호좜될 ν•¨μˆ˜ ν¬μΈν„°μž…λ‹ˆλ‹€. Moreover, it's also possible to have a **`PREINIT_ARRAY`** with **pointers** that will be executed **before** the **`INIT_ARRAY`** pointers. λ˜ν•œ **`INIT_ARRAY`** 포인터보닀 **λ¨Όμ €** 싀행될 **포인터**κ°€ μžˆλŠ” **`PREINIT_ARRAY`**λ₯Ό κ°€μ§ˆ μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. ### Initialization Order 1. The program is loaded into memory, static global variables are initialized in **`.data`** and unitialized ones zeroed in **`.bss`**. 2. All **dependencies** for the program or libraries are **initialized** and the the **dynamic linking** is executed. 3. **`PREINIT_ARRAY`** functions are executed. 4. **`INIT_ARRAY`** functions are executed. 5. If there is a **`INIT`** entry it's called. 6. If a library, dlopen ends here, if a program, it's time to call the **real entry point** (`main` function). ### μ΄ˆκΈ°ν™” μˆœμ„œ 1. ν”„λ‘œκ·Έλž¨μ΄ λ©”λͺ¨λ¦¬μ— λ‘œλ“œλ˜κ³ , 정적 μ „μ—­ λ³€μˆ˜κ°€ **`.data`**μ—μ„œ μ΄ˆκΈ°ν™”λ˜λ©°, μ΄ˆκΈ°ν™”λ˜μ§€ μ•Šμ€ λ³€μˆ˜λŠ” **`.bss`**μ—μ„œ 0으둜 μ„€μ •λ©λ‹ˆλ‹€. 2. ν”„λ‘œκ·Έλž¨ λ˜λŠ” λΌμ΄λΈŒλŸ¬λ¦¬μ— λŒ€ν•œ λͺ¨λ“  **쒅속성**이 **μ΄ˆκΈ°ν™”**되고 **동적 링크**κ°€ μ‹€ν–‰λ©λ‹ˆλ‹€. 3. **`PREINIT_ARRAY`** ν•¨μˆ˜κ°€ μ‹€ν–‰λ©λ‹ˆλ‹€. 4. **`INIT_ARRAY`** ν•¨μˆ˜κ°€ μ‹€ν–‰λ©λ‹ˆλ‹€. 5. **`INIT`** ν•­λͺ©μ΄ 있으면 ν˜ΈμΆœλ©λ‹ˆλ‹€. 6. 라이브러리인 경우, dlopen은 μ—¬κΈ°μ„œ λλ‚˜κ³ , ν”„λ‘œκ·Έλž¨μΈ 경우 **μ‹€μ œ μ§„μž…μ **(`main` ν•¨μˆ˜)을 ν˜ΈμΆœν•  μ‹œκ°„μž…λ‹ˆλ‹€. ## Thread-Local Storage (TLS) They are defined using the keyword **`__thread_local`** in C++ or the GNU extension **`__thread`**. **`__thread_local`** ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ C++μ—μ„œ μ •μ˜λ˜κ±°λ‚˜ GNU ν™•μž₯ **`__thread`**λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. Each thread will maintain a unique location for this variable so only the thread can access its variable. 각 μŠ€λ ˆλ“œλŠ” 이 λ³€μˆ˜μ— λŒ€ν•΄ κ³ μœ ν•œ μœ„μΉ˜λ₯Ό μœ μ§€ν•˜λ―€λ‘œ 였직 ν•΄λ‹Ή μŠ€λ ˆλ“œλ§Œ μžμ‹ μ˜ λ³€μˆ˜λ₯Ό μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€. When this is used the sections **`.tdata`** and **`.tbss`** are used in the ELF. Which are like `.data` (initialized) and `.bss` (not initialized) but for TLS. 이것이 μ‚¬μš©λ˜λ©΄ ELFμ—μ„œ **`.tdata`** 및 **`.tbss`** μ„Ήμ…˜μ΄ μ‚¬μš©λ©λ‹ˆλ‹€. μ΄λŠ” TLS에 λŒ€ν•œ `.data` (μ΄ˆκΈ°ν™”λ¨) 및 `.bss` (μ΄ˆκΈ°ν™”λ˜μ§€ μ•ŠμŒ)와 μœ μ‚¬ν•©λ‹ˆλ‹€. Each variable will hace an entry in the TLS header specifying the size and the TLS offset, which is the offset it will use in the thread's local data area. 각 λ³€μˆ˜λŠ” TLS 헀더에 크기와 TLS μ˜€ν”„μ…‹μ„ μ§€μ •ν•˜λŠ” ν•­λͺ©μ„ 가지며, μ΄λŠ” μŠ€λ ˆλ“œμ˜ 둜컬 데이터 μ˜μ—­μ—μ„œ μ‚¬μš©ν•  μ˜€ν”„μ…‹μž…λ‹ˆλ‹€. The `__TLS_MODULE_BASE` is a symbol used to refer to the base address of the thread local storage and points to the area in memory that contains all the thread-local data of a module. `__TLS_MODULE_BASE`λŠ” μŠ€λ ˆλ“œ 둜컬 μ €μž₯μ†Œμ˜ κΈ°λ³Έ μ£Όμ†Œλ₯Ό μ°Έμ‘°ν•˜λŠ” 데 μ‚¬μš©λ˜λŠ” 기호이며, λͺ¨λ“ˆμ˜ λͺ¨λ“  μŠ€λ ˆλ“œ 둜컬 데이터λ₯Ό ν¬ν•¨ν•˜λŠ” λ©”λͺ¨λ¦¬ μ˜μ—­μ„ κ°€λ¦¬ν‚΅λ‹ˆλ‹€. {% hint style="success" %} Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Support HackTricks * Check the [**subscription plans**](https://github.com/sponsors/carlospolop)! * **Join the** πŸ’¬ [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.** * **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
{% endhint %}