@@ -89,35 +89,43 @@ Some compilers/linker specific flags are needed to compile the shellcode.
8989- Visibility: ` -fvisibility=hidden -fvisibility-inlines-hidden `
9090- Make linker complain about missing symbols: ` -Wl,--no-allow-shlib-undefined,--no-undefined `
9191- Disable lazy binding: ` -Wl,-z,defs,-z,now,-z,relro `
92- - Remove unused sections: ` -Wl,--gc-sections `
92+ - Specify the linker script: ` -T,shellcode.ld `
93+ - Remove unused code: ` -Wl,--gc-sections `
9394
9495An example command to compile the shellcode:
9596
9697``` shell
9798/path/to/clang++ -shared -fPIC -std=c++14 -O3 \
9899-fvisibility=hidden -fvisibility-inlines-hidden -fno-omit-frame-pointer -Wall \
99100-fno-rtti -fno-exceptions -nostdlib \
100- -Wl,-Bsymbolic,--no-allow-shlib-undefined,--no-undefined,-z,defs,-z,now,-z,relro,--gc-sections \
101+ -Wl,-Bsymbolic,--no-allow-shlib-undefined,--no-undefined,-z,defs,-z,now,-z,relro,--gc-sections,-T,shellcode.ld \
101102-I/path/to/jni/include -I/path/to/linux-syscall-support \
102103all_in_one.cc -o libcore_syscall.so
103104```
104105
105- The ` file *.so` output may look like this:
106+ The ` readelf --dynamic *.so` output may look like this:
106107
107108``` text
108- shellcode-arm.so: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), static-pie linked, not stripped
109- shellcode-arm64.so: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), static-pie linked, not stripped
110- shellcode-mips.so: ELF 32-bit LSB shared object, MIPS, MIPS32 version 1 (SYSV), static-pie linked, not stripped
111- shellcode-mips64.so: ELF 64-bit LSB shared object, MIPS, MIPS64 rel6 version 1 (SYSV), static-pie linked, not stripped
112- shellcode-riscv64.so: ELF 64-bit LSB shared object, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), static-pie linked, not stripped
113- shellcode-x86.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), static-pie linked, not stripped
114- shellcode-x86_64.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), static-pie linked, not stripped
109+ Dynamic section at offset 0x1000 contains 9 entries:
110+ Tag Type Name/Value
111+ 0x000000000000001e (FLAGS) SYMBOLIC BIND_NOW
112+ 0x000000006ffffffb (FLAGS_1) Flags: NOW
113+ 0x0000000000000006 (SYMTAB) 0x1090
114+ 0x000000000000000b (SYMENT) 24 (bytes)
115+ 0x0000000000000005 (STRTAB) 0x1258
116+ 0x000000000000000a (STRSZ) 444 (bytes)
117+ 0x000000006ffffef5 (GNU_HASH) 0x14b8
118+ 0x0000000000000004 (HASH) 0x1414
119+ 0x0000000000000000 (NULL) 0x0
115120```
116121
117- Note that they should be ` static-pie linked ` , not ` dynamically linked ` .
122+ Note that there should be no ` DT_NEEDED ` entry in the dynamic section .
118123
119124Get the symbol table: ` llvm-objdump -T libcore_syscall.so `
120125
121- Dump the text section: ` llvm-objcopy -O binary --only-section=.text libcore_syscall.so shellcode.bin `
126+ If you are using your own linker script, make sure that the ` .text ` and ` .rodata ` sections are in the right place.
127+
128+ Dump the .text and .rodata sections:
129+ ` llvm-objcopy -O binary --only-section=.text --only-section=.rodata libcore_syscall.so shellcode.bin `
122130
123131That's all for the shellcode.
0 commit comments