ELF मूल जानकारी
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 समूह या टेलीग्राम समूह में शामिल हों या हमें Twitter 🐦 @hacktricks_live** पर फॉलो करें।**
- हैकिंग ट्रिक्स साझा करें और HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में PRs सबमिट करें।
प्रोग्राम हेडर्स
ये लोडर को बताते हैं कि ELF को मेमोरी में कैसे लोड किया जाए:
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
प्रोग्राम हेडर तालिकाएँ और स्वयं मेटाडेटा इसमें शामिल होते हैं।
INTERP
बाइनरी को मेमोरी में लोड करने के लिए उपयोग किए जाने वाले loader का path दर्शाता है।
Tip: Statically linked or static-PIE binaries won’t have an
INTERPentry. In those cases there is no dynamic loader involved, which disables techniques that rely on it (e.g.,ret2dlresolve).
LOAD
ये headers यह संकेत करने के लिए उपयोग किए जाते हैं कि बाइनरी को मेमोरी में कैसे लोड किया जाए।
प्रत्येक LOAD header मेमोरी के एक क्षेत्र का संकेत देता है (size, permissions and alignment) और यह बताता है कि ELF बाइनरी के कौन से bytes वहां कॉपी किए जाएँगे।
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
यह header प्रोग्राम्स को उनकी library dependencies से लिंक करने और relocations लागू करने में मदद करता है। .dynamic सेक्शन देखें।
NOTE
यह बाइनरी के vendor metadata जानकारी को संग्रहीत करता है।
- On x86-64,
readelf -nwill showGNU_PROPERTY_X86_FEATURE_1_*flags inside.note.gnu.property. If you seeIBTand/orSHSTK, the binary was built with CET (Indirect Branch Tracking and/or Shadow Stack). This impacts ROP/JOP because indirect branch targets must start with anENDBR64instruction and returns are checked against a shadow stack. See the CET page for details and bypass notes.
GNU_EH_FRAME
stack unwind तालिकाओं का स्थान परिभाषित करता है, जिसका उपयोग debuggers और C++ exception handling-runtime कार्यों द्वारा किया जाता है।
GNU_STACK
stack execution prevention defense की configuration रखता है। यदि सक्षम है, तो binary stack से code execute नहीं कर पाएगा।
- Check with
readelf -l ./bin | grep GNU_STACK. To forcibly toggle it during tests you can useexecstack -s|-c ./bin.
GNU_RELRO
बाइनरी की RELRO (Relocation Read-Only) configuration को दर्शाता है। यह सुरक्षा प्रोग्राम के लोड होने के बाद और चलने से पहले मेमोरी के कुछ सेक्शन्स (जैसे GOT या init और fini tables) को read-only के रूप में चिन्हित कर देगा।
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.
For exploitation techniques and up-to-date bypass notes, check the dedicated page:
TLS
TLS entries की तालिका को परिभाषित करता है, जो thread-local variables के बारे में जानकारी संग्रहीत करती है।
Section Headers
Section headers 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
यह स्थान, ऑफ़सेट, अनुमति बताता है और साथ ही उस सेक्शन में मौजूद डेटा का प्रकार भी बताता है।
मेटा सेक्शन
- String table: इसमें ELF file के लिए ज़रूरी सभी strings होते हैं (लेकिन वे जो असल में प्रोग्राम द्वारा उपयोग किए जाते हैं, वे नहीं)। उदाहरण के लिए इसमें सेक्शन नाम जैसे
.textया.dataहोते हैं। और अगर.textstrings table में ऑफ़सेट 45 पर है तो name फ़ील्ड में संख्या 45 प्रयुक्त होगी। - String table कहाँ है यह पता करने के लिए, ELF में string table का एक pointer होता है।
- Symbol table: इसमें symbols के बारे में जानकारी होती है जैसे name (strings table में ऑफ़सेट), address, size और symbol के बारे में अन्य metadata।
मुख्य सेक्शन
.text: प्रोग्राम को चलाने के लिए निर्देश।.data: प्रोग्राम में परिभाषित मान वाले global variables।.bss: वे global variables जो uninitialized छोड़े गए होते हैं (या शून्य से इनिशियालाइज़)। यहाँ के variables स्वतः शून्य से इनिशियालाइज़ होते हैं इसलिए बाइनरी में बेकार के zeros जोड़ने की आवश्यकता नहीं रहती।.rodata: स्थायी global variables (read-only सेक्शन)।.tdataऔर.tbss: तब .data और .bss जैसे ही होते हैं जब thread-local variables उपयोग किए जाते हैं (__thread_localin C++ or__threadin C)।.dynamic: नीचे देखें।
Symbols
Symbols प्रोग्राम में एक नामांकित स्थान होते हैं जो एक function, एक global data object, thread-local variables आदि हो सकते हैं।
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)
[...]
प्रत्येक symbol एंट्री में शामिल होते हैं:
- नाम
- Binding attributes (weak, local or global): एक local symbol केवल प्रोग्राम द्वारा ही एक्सेस किया जा सकता है जबकि global symbol प्रोग्राम के बाहर साझा किए जाते हैं। उदाहरण के लिए, एक weak object ऐसा ऑब्जेक्ट हो सकता है जैसे कोई फंक्शन जिसे किसी दूसरे द्वारा ओवरराइड किया जा सकता है।
- Type: NOTYPE (कोई प्रकार निर्दिष्ट नहीं), OBJECT (global data var), FUNC (function), SECTION (section), FILE (source-code file for debuggers), TLS (thread-local variable), GNU_IFUNC (indirect function for relocation)
- Section वह सेक्शन (index) जहाँ यह स्थित है
- Value (मेमोरी में पता)
- आकार
GNU IFUNC (अप्रत्यक्ष फ़ंक्शन)
- GCC
STT_GNU_IFUNCsymbols को__attribute__((ifunc("resolver")))एक्सटेंशन के साथ इमिट कर सकता है। डायनामिक लोडर लोड टाइम पर resolver को कॉल करता है ताकि ठोस implementation चुना जा सके (आम तौर पर CPU dispatch)। - त्वरित जाँच:
readelf -sW ./bin | rg -i "IFUNC"
GNU Symbol Versioning (dynsym/dynstr/gnu.version)
आधुनिक glibc symbol versions का उपयोग करता है। आप .gnu.version और .gnu.version_r में एंट्रियाँ और strlen@GLIBC_2.17 जैसे symbol नाम देखेंगे। डायनामिक linker किसी symbol को रिज़ॉल्व करते समय किसी विशिष्ट version की माँग कर सकता है। जब आप मैन्युअल relocations (उदा. ret2dlresolve) तैयार कर रहे हों तो आपको सही version index प्रदान करना होगा, वरना resolution विफल रहेगा।
डायनामिक सेक्शन
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
The NEEDED directory संकेत करता है कि प्रोग्राम जारी रखने के लिए उल्लेखित लाइब्रेरी को लोड करने की आवश्यकता है. The NEEDED directory पूरा हो जाता है जब साझा लाइब्रेरी पूरी तरह से कार्यशील और उपयोग के लिए तैयार हो जाती है.
डायनामिक लोडर खोज क्रम (RPATH/RUNPATH, $ORIGIN)
एंट्रीज़ DT_RPATH (अप्रचलित) और/या DT_RUNPATH प्रभावित करती हैं कि डायनामिक लोडर निर्भरताओं की खोज कहां करता है। सामान्य क्रम:
LD_LIBRARY_PATH(setuid/sgid या अन्यथा “secure-execution” प्रोग्राम्स के लिए अनदेखा किया जाता है)DT_RPATH(केवल अगरDT_RUNPATHअनुपस्थित हो)DT_RUNPATHld.so.cache- डिफ़ॉल्ट डायरेक्टरीज़ जैसे
/lib64,/usr/lib64, आदि।
$ORIGIN को RPATH/RUNPATH के अंदर मुख्य ऑब्जेक्ट की डायरेक्टरी का संदर्भ देने के लिए इस्तेमाल किया जा सकता है। हमलावर के नज़रिए से यह तब मायने रखता है जब आप फ़ाइलसिस्टम लेआउट या पर्यावरण को नियंत्रित करते हैं। कठोरीकृत बाइनरीज़ (AT_SECURE) में अधिकांश पर्यावरण चर लोडर द्वारा अनदेखा किए जाते हैं।
- जांचें:
readelf -d ./bin | egrep -i 'r(path|unpath)' - त्वरित परीक्षण:
LD_DEBUG=libs ./bin 2>&1 | grep -i find(खोज पथ निर्णय दिखाता है)
Priv-esc tip: writable RUNPATHs या गलत कॉन्फ़िगर किए गए
$ORIGIN-relative पाथ्स जिनके मालिक आप हैं, का दुरुपयोग करना प्राथमिकता दें। LD_PRELOAD/LD_AUDIT secure-execution (setuid) संदर्भों में अनदेखा किए जाते हैं।
रिलोकेशन्स
लोडर को इन्हें लोड करने के बाद निर्भरताओं को भी relocate करना होता है। ये रिलोकेशंस relocation table में formats REL या RELA में संकेतित होते हैं और रिलोकेशंस की संख्या dynamic sections 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
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
Packed relative relocations (RELR)
- Modern linkers
-z pack-relative-relocsके साथ कम्पैक्ट relative relocations जनरेट कर सकते हैं। यह PIEs/shared libraries के dynamic section मेंDT_RELR,DT_RELRSZ, औरDT_RELRENTएंट्रीज़ जोड़ता है (non-PIE executables के लिए इसे इग्नोर किया जाता है)। - Recon:
readelf -d ./bin | egrep -i "DT_RELR|RELRSZ|RELRENT"
Static Relocations
यदि program is loaded in a place different से preferred address (सामान्यतः 0x400000) क्योंकि वह address पहले से उपयोग में है या ASLR या किसी अन्य कारण से, तो एक static relocation उन pointers को corrects करता है जिनके values इस उम्मीद से बने थे कि binary preferred address पर लोड होगा।
उदाहरण के लिए किसी भी section का type R_AARCH64_RELATIV होना चाहिए, उसे relocation bias और addend value जोड़ कर address modify किया गया हो।
Dynamic Relocations and GOT
रिलोकेशन किसी external symbol (जैसे किसी dependency का function) को भी reference कर सकता है। जैसे libC का malloc। तब, loader जब libC को किसी address पर लोड करता है और चेक करता है कि malloc कहाँ लोड हुआ है, तो वह उस address को GOT (Global Offset Table) में लिख देगा (जो relocation table में संकेतित है) जहाँ malloc का address रखा जाना चाहिए।
Procedure Linkage Table
PLT section lazy binding की अनुमति देता है, जिसका मतलब है कि किसी function के location का resolution पहली बार जब उसे access किया जाता है तब किया जाता है।
तो जब प्रोग्राम malloc को कॉल करता है, तो वह असल में PLT में malloc के corresponding स्थान को कॉल करता है (malloc@plt)। पहली बार कॉल होने पर यह malloc के address को resolve करता है और store कर देता है ताकि अगली बार malloc कॉल होने पर उस resolve किए गए address का उपयोग हो, न कि PLT कोड का।
Modern linking behaviors that impact exploitation
-z now(Full RELRO) lazy binding को disable कर देता है; PLT entries अभी भी मौजूद होते हैं पर GOT/PLT read-only mapped होती है, इसलिए techniques जैसे GOT overwrite और ret2dlresolve main binary के खिलाफ काम नहीं करेंगी (libraries अभी भी आंशिक रूप से RELRO हो सकती हैं)। See:
-
-fno-plt कंपाइलर को external functions को GOT entry directly के माध्यम से कॉल करने पर मजबूर करता है बजाय PLT stub के। आप call sequences जैसे mov reg, [got]; call reg देखेंगे बजाय call func@plt के। यह speculative-execution abuse को कम करता है और PLT stubs के आसपास ROP gadget hunting को थोड़ा बदल देता है।
-
PIE vs static-PIE: PIE (ET_DYN with INTERP) को dynamic loader की आवश्यकता होती है और यह सामान्य PLT/GOT machinery को सपोर्ट करता है। Static-PIE (ET_DYN without INTERP) पर relocations kernel loader द्वारा apply होते हैं और no ld.so होता; runtime पर PLT resolution की उम्मीद न रखें।
अगर GOT/PLT विकल्प नहीं है, तो अन्य writeable code-pointers पर pivot करें या classic ROP/SROP को libc में उपयोग करें।
Program Initialization
प्रोग्राम लोड हो जाने के बाद उसे चलाने का समय आता है। हालांकि, पहला चलने वाला कोड हमेशा main नहीं होता। ऐसा इसलिए है क्योंकि उदाहरण के लिए C++ में अगर कोई global variable किसी class का object है, तो उस object को main से पहले initialized किया जाना चाहिए, जैसे:
#include <stdio.h>
// 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__ में आरंभ (initialize) और नष्ट (destruct) करने के लिए ऑब्जेक्ट्स उनके ट्रैक रखने के क्रम में संग्रहीत होते हैं।
C कोड से GNU एक्सटेंशन्स का उपयोग करके वही परिणाम प्राप्त करना संभव है :
__attribute__((constructor)) //Add a constructor to execute before
__attribute__((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 FINI. and are placed in the init and fini sections of the ELF.
कम्पाइलर के दृष्टिकोण से, main फ़ंक्शन के चलने से पहले और बाद इन क्रियाओं को निष्पादित करने के लिए init और fini फ़ंक्शन बनाए जा सकते हैं जिन्हें dynamic section में INIT और FINI के रूप में संदर्भित किया जाएगा और ये 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.
दूसरा विकल्प, जैसा कि बताया गया है, dynamic section में INIT_ARRAY और FINI_ARRAY एंट्रियों में सूची __CTOR_LIST__ और __DTOR_LIST__ को संदर्भित करना है और इनकी लंबाई INIT_ARRAYSZ और FINI_ARRAYSZ से दर्शाई जाती है। प्रत्येक एंट्री एक function pointer होती है जिसे बिना arguments के बुलाया जाएगा।
Moreover, it’s also possible to have a PREINIT_ARRAY with pointers that will be executed before the INIT_ARRAY pointers.
इसके अलावा, एक PREINIT_ARRAY भी हो सकता है जिसमें pointers होते हैं जो INIT_ARRAY pointers से पहले निष्पादित होंगे।
Exploitation note
-
Under Partial RELRO these arrays live in pages that are still writable before
ld.soflipsPT_GNU_RELROto read-only. If you get an arbitrary write early enough or you can target a library’s writable arrays, you can hijack control flow by overwriting an entry with a function of your choice. Under Full RELRO they are read-only at runtime. -
Under Partial RELRO ये arrays उन pages में रहते हैं जो
ld.soद्वाराPT_GNU_RELROको read-only में बदलने से पहले तक writable रहते हैं। यदि आप पर्याप्त जल्दी कोई arbitrary write प्राप्त कर लें या किसी library की writable arrays को target कर सकें, तो आप किसी entry को अपनी पसंदीदा function से overwrite करके control flow को hijack कर सकते हैं। Under Full RELRO में वे runtime पर read-only होते हैं। -
For lazy binding abuse of the dynamic linker to resolve arbitrary symbols at runtime, see the dedicated page:
Initialization Order
- The program is loaded into memory, static global variables are initialized in
.dataand unitialized ones zeroed in.bss. - All dependencies for the program or libraries are initialized and the the dynamic linking is executed.
PREINIT_ARRAYfunctions are executed.INIT_ARRAYfunctions are executed.- If there is a
INITentry it’s called. - If a library, dlopen ends here, if a program, it’s time to call the real entry point (
mainfunction).
Initialization Order
- प्रोग्राम memory में लोड किया जाता है; static global variables को
.dataमें initialize किया जाता है और uninitialized ones को.bssमें zero किया जाता है। - प्रोग्राम या libraries की सभी dependencies initialize होती हैं और dynamic linking निष्पादित होता है।
PREINIT_ARRAYfunctions निष्पादित होते हैं।INIT_ARRAYfunctions निष्पादित होते हैं।- यदि कोई
INITएंट्री है तो उसे कॉल किया जाता है। - यदि यह एक library है तो dlopen यहीं समाप्त होता है; यदि एक program है तो अब real entry point (
mainfunction) को कॉल करने का समय होता है।
Thread-Local Storage (TLS)
They are defined using the keyword __thread_local in C++ or the GNU extension __thread.
वे C++ में keyword __thread_local या GNU extension __thread का उपयोग करके परिभाषित होते हैं।
Each thread will maintain a unique location for this variable so only the thread can access its variable.
प्रत्येक thread इस variable के लिए एक unique स्थान रखता है ताकि केवल वही thread अपने 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 (initialized) और .bss (not initialized) जैसे होते हैं।
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.
प्रत्येक variable का TLS header में एक entry होगी जो size और TLS offset निर्दिष्ट करती है, जो उस thread के local data क्षेत्र में इसका उपयोग किया जाने वाला offset है।
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 एक symbol है जिसका उपयोग thread local storage के base address को संदर्भित करने के लिए किया जाता है और यह memory के उस क्षेत्र की ओर संकेत करता है जो किसी module का सारा thread-local data रखता है।
Auxiliary Vector (auxv) and vDSO
The Linux kernel passes an auxiliary vector to processes containing useful addresses and flags for the runtime:
Linux kernel प्रक्रियाओं को एक auxiliary vector पास करता है जिसमें runtime के लिए उपयोगी addresses और flags होते हैं:
-
AT_RANDOM: points to 16 random bytes used by glibc for the stack canary and other PRNG seeds. -
AT_SYSINFO_EHDR: base address of the vDSO mapping (handy to find__kernel_*syscalls and gadgets). -
AT_EXECFN,AT_BASE,AT_PAGESZ, etc. -
AT_RANDOM: 16 random bytes की ओर संकेत करता है जिसे glibc stack canary और अन्य PRNG seeds के लिए उपयोग करता है। -
AT_SYSINFO_EHDR: vDSO mapping का base address (__kernel_*syscalls और gadgets खोजने के लिए handy)। -
AT_EXECFN,AT_BASE,AT_PAGESZ, आदि।
As an attacker, if you can read memory or files under /proc, you can often leak these without an infoleak in the target process:
एक attacker के रूप में, यदि आप memory या /proc के अंदर files पढ़ सकते हैं, तो आप अक्सर इन्हें target process में किसी infoleak के बिना leak कर सकते हैं:
# Show the auxv of a running process
cat /proc/$(pidof target)/auxv | xxd
# From your own process (helper snippet)
#include <sys/auxv.h>
#include <stdio.h>
int main(){
printf("AT_RANDOM=%p\n", (void*)getauxval(AT_RANDOM));
printf("AT_SYSINFO_EHDR=%p\n", (void*)getauxval(AT_SYSINFO_EHDR));
}
Leaking AT_RANDOM आपको canary value देता है अगर आप उस pointer को dereference कर सकते हैं; AT_SYSINFO_EHDR आपको gadgets खोजने या सीधे fast syscalls कॉल करने के लिए एक vDSO base देता है।
संदर्भ
- GCC Common Function Attributes (ifunc / STT_GNU_IFUNC): https://gcc.gnu.org/onlinedocs/gcc-14.3.0/gcc/Common-Function-Attributes.html
- GNU ld
-z pack-relative-relocs/DT_RELRdocs: https://sourceware.org/binutils/docs/ld.html - ld.so(8) – Dynamic Loader search order, RPATH/RUNPATH, secure-execution rules (AT_SECURE): https://man7.org/linux/man-pages/man8/ld.so.8.html
- getauxval(3) – Auxiliary vector and AT_* constants: https://man7.org/linux/man-pages/man3/getauxval.3.html
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 समूह या टेलीग्राम समूह में शामिल हों या हमें Twitter 🐦 @hacktricks_live** पर फॉलो करें।**
- हैकिंग ट्रिक्स साझा करें और HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में PRs सबमिट करें।


