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

プログラムヘッダ

ローダに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

program header テーブルとメタデータそのものを格納します。

INTERP

バイナリをメモリにロードする際に使用する loader のパスを示します。

ヒント: Statically linked や static-PIE のバイナリは INTERP エントリを持ちません。その場合、dynamic loader が関与しないため、それに依存する技法(例: ret2dlresolve)は使えなくなります。

LOAD

これらのヘッダは、バイナリをメモリにどのようにロードするかを示すために使われます。
LOAD ヘッダは、メモリの領域(サイズ・権限・整列)を示し、ELF のどのバイト列をその領域にコピーするかを示します。

例えば、2番目のLOADはサイズが0x1190で、0x1fc48に配置され、読み書き権限があり、オフセット0xfc48から0x528バイトで埋められます(予約された領域全体は埋められません)。このメモリにはセクション .init_array .fini_array .dynamic .got .data .bss が含まれます。

DYNAMIC

このヘッダはプログラムをライブラリ依存関係にリンクし、リロケーションを適用するのに役立ちます。.dynamic セクションを確認してください。

NOTE

これはバイナリに関するベンダーのメタデータ情報を格納します。

  • x86-64 では、readelf -n.note.gnu.property の中にある GNU_PROPERTY_X86_FEATURE_1_* フラグを表示します。IBT および/または SHSTK が見られる場合、そのバイナリは CET (Indirect Branch Tracking and/or Shadow Stack) でビルドされています。これは ROP/JOP に影響します。なぜなら間接分岐のターゲットは ENDBR64 命令で始まる必要があり、returns は shadow stack と照合されるからです。詳細とバイパスについては CET のページを参照してください。

CET & Shadow Stack

GNU_EH_FRAME

デバッガや C++ の例外処理ランタイムで使用されるスタックアンワインドテーブルの位置を定義します。

GNU_STACK

スタック実行防止の設定を含みます。有効な場合、バイナリはスタック上のコードを実行できなくなります。

  • 確認は readelf -l ./bin | grep GNU_STACK で行います。テスト中に強制的に切り替えるには execstack -s|-c ./bin を使用できます。

GNU_RELRO

バイナリの RELRO (Relocation Read-Only) 設定を示します。この保護は、プログラムがロードされた後かつ実行開始前に、メモリの特定のセクション(GOTinit/fini テーブルなど)を読み取り専用としてマークします。

前の例では、0x3b8 バイトをアドレス 0x1fc48 に読み取り専用としてコピーしており、セクション .init_array .fini_array .dynamic .got .data .bss に影響します。

RELRO は partial または full の場合があり、partial 版は .plt.got セクションを保護しません。これは lazy binding に使われ、最初にライブラリの位置が検索されたときにアドレスを書き込むため、このメモリ領域は 書き込み権限 を持つ必要があります。

エクスプロイト技術や最新のバイパスノートについては、専用ページを参照してください:

Relro

TLS

TLS エントリのテーブルを定義し、スレッドローカル変数に関する情報を格納します。

Section Headers

セクションヘッダは ELF バイナリのより詳細な情報を示します。

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がstrings table内のオフセット45にあれば、nameフィールドには数値45が使われます。
  • In order to find where the string table is, the ELF contains a pointer to the string table.
  • Symbol table: シンボルに関する情報を含みます(名前:strings table内のオフセット、アドレス、サイズ、その他シンボルのメタデータなど)。

Main Sections

  • .text: プログラムが実行する命令群。
  • .data: プログラム内で定義された値を持つグローバル変数。
  • .bss: 初期化されていない(またはゼロで初期化される)グローバル変数。ここにある変数は自動的にゼロで初期化されるため、不要なゼロをバイナリに追加することを防ぎます。
  • .rodata: 定数グローバル変数(読み取り専用セクション)。
  • .tdata and .tbss: スレッドローカル変数が使われるときの .data および .bss に相当するセクション(__thread_local in C++ や __thread in C)。
  • .dynamic: 後述。

Symbols

Symbols is a named location in the program which could be a function, a 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)
[...]

各シンボルエントリには以下が含まれます:

  • 名前
  • バインディング属性 (weak, local or global): ローカルシンボルはプログラム自身からのみアクセス可能ですが、グローバルシンボルはプログラム外部と共有されます。weak オブジェクトは例えば別のものによって上書きできる関数です。
  • : NOTYPE (no type specified), 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: 配置されているセクションのインデックス
  • Value (メモリ上のアドレス)
  • サイズ

GNU IFUNC (indirect functions)

  • GCC can emit STT_GNU_IFUNC symbols with the __attribute__((ifunc("resolver"))) extension. 動的ローダはロード時に resolver を呼び出して具象実装を選択します(一般的には CPU ディスパッチ)。
  • Quick triage: readelf -sW ./bin | rg -i "IFUNC"

GNU Symbol Versioning (dynsym/dynstr/gnu.version)

現代の glibc はシンボルバージョンを使用します。.gnu.version.gnu.version_r にエントリがあり、strlen@GLIBC_2.17 のようなシンボル名が見られます。動的リンカはシンボル解決時に特定のバージョンを要求することがあります。手動でリロケーションを作成する際(例: ret2dlresolve)には正しいバージョンインデックスを供給する必要があり、さもないと解決は失敗します。

動的セクション

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 ディレクトリは、プログラムが続行するために指定されたライブラリをロードする必要があることを示します。共有ライブラリが完全に動作準備できているときに NEEDED ディレクトリは完了します。

動的ローダの検索順序 (RPATH/RUNPATH, $ORIGIN)

DT_RPATH(非推奨)や DT_RUNPATH のエントリは、動的ローダが依存関係を検索する場所に影響します。大まかな順序:

  • LD_LIBRARY_PATH (setuid/sgid またはその他の “secure-execution” プログラムでは無視される)
  • DT_RPATH (DT_RUNPATH が存在しない場合のみ)
  • DT_RUNPATH
  • ld.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 な RUNPATH や、あなたが所有する誤設定された $ORIGIN 相対パスを悪用することを優先してください。LD_PRELOAD/LD_AUDIT は secure-execution(setuid)コンテキストでは無視されます。

リロケーション

ローダは、依存関係をロードした後にそれらをリロケートする必要があります。これらのリロケーションは 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
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

パックされた相対リロケーション (RELR)

  • 最新のリンカは -z pack-relative-relocs を使ってコンパクトな 相対 リロケーションを出力できます。これは PIE や共有ライブラリの動的セクションに DT_RELRDT_RELRSZDT_RELRENT のエントリを追加します(非-PIE 実行ファイルでは無視されます)。
  • Recon: readelf -d ./bin | egrep -i "DT_RELR|RELRSZ|RELRENT"

Static Relocations

もし プログラムが推奨アドレス(通常は 0x400000)とは異なる場所にロードされる と、アドレスが既に使用されている、ASLR の影響などの理由であっても、static relocation はバイナリが推奨アドレスにロードされることを期待していたポインタの値を 修正 します。

例えば、R_AARCH64_RELATIV 型のセクションは、リロケーションのベース(bias)に加えられた addend の位置にあるアドレスを書き換えられているはずです。

Dynamic Relocations and GOT

リロケーションは外部シンボル(依存ライブラリの関数など)を参照することもあります。例えば libC の malloc のような関数です。ロード時に loader が libC をロードして malloc がどのアドレスにあるかを確認すると、そのアドレスをリロケーションテーブルで示された GOT (Global Offset Table) のエントリに書き込みます。そこに malloc のアドレスが格納されるべきです。

Procedure Linkage Table

PLT セクションは lazy binding を可能にします。つまり、関数の実体の解決はその関数が初めて参照された時に行われます。

したがってプログラムが malloc を呼ぶと、実際には PLT 内の対応する malloc の位置(malloc@plt)を呼び出します。初回呼び出し時に malloc のアドレスが解決され保存されるため、次回以降は PLT のコードの代わりにその解決済みアドレスが使われます。

Modern linking behaviors that impact exploitation

  • -z now (Full RELRO) は lazy binding を無効化します;PLT エントリ自体は存在しますが GOT/PLT は読み取り専用としてマップされるため、GOT overwriteret2dlresolve のような技術はメインバイナリに対しては機能しません(ライブラリは部分的に RELRO のままの場合があります)。参照:

Relro

  • -fno-plt はコンパイラが PLT stub を経由する代わりに GOT エントリを直接 通して外部関数を呼ぶようにします。mov reg, [got]; call reg のような呼び出し列が見られ、call func@plt の形は減ります。これは speculative-execution の悪用を減らし、PLT stub 周辺の ROP ガジェット探索をわずかに変化させます。

  • PIE と static-PIE の違い:PIE (ET_DYN with INTERP) は dynamic loader を必要とし、通常の PLT/GOT 機構をサポートします。Static-PIE (ET_DYN without INTERP) はカーネルローダによってリロケーションが適用され ld.so が存在しないため、ランタイムでの PLT 解決は期待できません。

If GOT/PLT is not an option, pivot to other writeable code-pointers or use classic ROP/SROP into libc.

WWW2Exec - GOT/PLT

Program Initialization

プログラムがロードされた後、実行が始まります。しかし、最初に実行されるコードが必ずしも main 関数では ない ことがあります。例えば C++ では グローバル変数がクラスのオブジェクトである 場合、そのオブジェクトは main が実行される 前に 初期化されなければなりません。以下のように:

#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__ のリストには、初期化および破棄すべきオブジェクトが順序を保持するために格納されていることに注意してください。

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.

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.

Moreover, it’s also possible to have a PREINIT_ARRAY with pointers that will be executed before the INIT_ARRAY pointers.

Exploitation note

  • Under Partial RELRO these arrays live in pages that are still writable before ld.so flips PT_GNU_RELRO to 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.

  • For lazy binding abuse of the dynamic linker to resolve arbitrary symbols at runtime, see the dedicated page:

Ret2dlresolve

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).

Thread-Local Storage (TLS)

They are defined using the keyword __thread_local in C++ or the GNU extension __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.

Each variable will have 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.

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.

Auxiliary Vector (auxv) and vDSO

The Linux kernel passes an auxiliary vector to processes containing useful addresses and flags for the runtime:

  • 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.

As an attacker, if you can read memory or files under /proc, you can often leak these without an infoleak in the target process:

# 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 値を取得できます。AT_SYSINFO_EHDR は gadgets を探索したり、fast syscalls を直接呼び出すための vDSO ベースを与えます。

参考資料

  • GCC の共通関数属性 (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_RELR ドキュメント: https://sourceware.org/binutils/docs/ld.html
  • ld.so(8) – Dynamic Loader の検索順序、RPATH/RUNPATH、secure-execution のルール (AT_SECURE): https://man7.org/linux/man-pages/man8/ld.so.8.html
  • getauxval(3) – 補助ベクタと AT_* 定数: 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をサポートする