SlideShare a Scribd company logo
Exploiting the Linux Dynamic Loader with LD_PRELOAD David Kaplan [email_address] DC9723 – June 2011
The Executable and linking format (ELF) } linkers loaders libraries
Linkers combine compiled code fragments into single memory-loadable executable $ ld obj1.o obj2.o –o linked.o symbol resolution program components reference each other through symbols (ELF .symtab) Relocation adjustment of code/data sections (also performed by the loader)
Loaders copy code and data into memory memory allocation/mapping relocation Also performed by the linker execve()
Libraries statically-linked dynamically-linked (shared)* collections of reusable compiled code *historically: a shared library was something else entirely
Statically-linked libraries code copied into final binary be aware of: cyclic dependencies, multiple symbol definitions $ld obj1.o obj2.o /usr/lib/libname.a CODE my_print() { printf(); } main() { my_print(); } main() { my_print(); } my_print() { printf(); } printf() { vfprintf(); } LIBC STATICALLY LINKED FILESIZE
Dynamically-linked libraries dynamic loader (ld.so) resolves symbols at exectime Process: - execve() loads executable code into memory
- control is passed to the dynamic linker (ld.so) which maps shared objects to program  address space (resolves symbols)
- control is then passed to the application can be called from within the application at runtime By linking ld and calling dlopen(), etc. CODE my_print() { printf(); } main() { my_print(); } main() { my_print(); } my_print() { printf(); } printf(); DYNAMICALLY LINKED FILESIZE LIBC
So what is LD_PRELOAD? environment var queried by dynamic linker on exec allows dynamic linker to prioritize linking defined shared libs $ LD_PRELOAD=“./mylib.so” ./myexec
Attack enablers OS  ‘features’ weak   system   security good   coding   practices general_rule: good_for_devs  ==   good_for_hackers ; goto   general_rule ;
Attack advantages easy, effective on  unprotected  systems code  interception code   injection program  flow manipulation debugging using  wrapper functions
Attack disadvantages } can be  protected  against requires  access   to executable requires relevant  privileges works on  used, imported symbols
Example 1 – Hello World $ nm -D make_goodbye.so 000000000000069c T printf U stdout U vfprintf $ nm -D hello w __gmon_start__ U __libc_start_main U printf Undefined symbol Symbol exists in .text
Example 1 – Hello World – cont. *in practice it works slightly differently – this is just a conceptual explanation NORMAL SYMBOL RESOLUTION: LOADER HELLO printf()  ?? DYNAMIC LINKER LIBC.SO Hello World! REDIRECTED SYMBOL RESOLUTION: LOADER HELLO printf()  ?? DYNAMIC LINKER GOODBYE.SO Goodbye World! LIBC.SO fprintf()  ??
Example 2 – OpenSSH MITM dynamically links openssl checks public key against known_hosts with BN_cmp() BN_cmp() must pass (== 0) for iterations 3 and 5
Example 3 – OpenSSH password logger catch write() w/ string literal “’s password” log read()s until ‘\n’
Example 4 – Extending ‘cat’ functionality intercept __snprintf_check() to add to usage() wrap getopt_long() to catch new command line option catch write(), vfork() and launch browser for each link
provides reusable library of function sigs (sorry about the code quality!) ./preloader reduces repetitive tasks tool that does *some* of the work for you http://www.github.com/2of1/preloader

More Related Content

PDF
Something About Dynamic Linking
PDF
Dynamic Linker
PPTX
06 - ELF format, knowing your friend
PDF
Compilation and Execution
PPTX
Linker and loader upload
PDF
FISL XIV - The ELF File Format and the Linux Loader
PPT
A hands-on introduction to the ELF Object file format
PDF
Program Structure in GNU/Linux (ELF Format)
Something About Dynamic Linking
Dynamic Linker
06 - ELF format, knowing your friend
Compilation and Execution
Linker and loader upload
FISL XIV - The ELF File Format and the Linux Loader
A hands-on introduction to the ELF Object file format
Program Structure in GNU/Linux (ELF Format)

What's hot (20)

PPTX
Yacc (yet another compiler compiler)
DOC
Lex tool manual
PPTX
Loader and Its types
PPT
Compilation
PPTX
Introduction to loaders
PPT
Loaders
PPTX
Lex & yacc
DOCX
LEX & YACC
PDF
loaders and linkers
PDF
Assembler
PPTX
Loaders ( system programming )
PDF
ANSI C Macros
PPTX
PDF
Lexyacc
ODP
Runtime Symbol Resolution
PPTX
Linking in MS-Dos System
PPTX
File handling With Solve Programs
PDF
Strategies to improve embedded Linux application performance beyond ordinary ...
PDF
Whirlwind tour of the Runtime Dynamic Linker
Yacc (yet another compiler compiler)
Lex tool manual
Loader and Its types
Compilation
Introduction to loaders
Loaders
Lex & yacc
LEX & YACC
loaders and linkers
Assembler
Loaders ( system programming )
ANSI C Macros
Lexyacc
Runtime Symbol Resolution
Linking in MS-Dos System
File handling With Solve Programs
Strategies to improve embedded Linux application performance beyond ordinary ...
Whirlwind tour of the Runtime Dynamic Linker
Ad

Viewers also liked (18)

PPTX
Ch 4 linker loader
PDF
Tips of Malloc & Free
PDF
ELF 101
PDF
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
PDF
Effective Modern C++
PPT
Intro reverse engineering
PDF
Symbolic Debugging with DWARF
PPTX
Load-time Hacking using LD_PRELOAD
PDF
DWARF Data Representation
PDF
Crash dump analysis - experience sharing
PPTX
PDF
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
PPTX
Linkers in compiler
PPT
Linkers And Loaders
PPTX
Linkers
PPT
Assembler
PPTX
System Programing Unit 1
Ch 4 linker loader
Tips of Malloc & Free
ELF 101
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
Effective Modern C++
Intro reverse engineering
Symbolic Debugging with DWARF
Load-time Hacking using LD_PRELOAD
DWARF Data Representation
Crash dump analysis - experience sharing
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
Linkers in compiler
Linkers And Loaders
Linkers
Assembler
System Programing Unit 1
Ad

Similar to LD_PRELOAD Exploitation - DC9723 (20)

PDF
The Parenscript Common Lisp to JavaScript compiler
PPT
Purdue CS354 Operating Systems 2008
ODP
Mach-O Internals
PPT
嵌入式Linux課程-GNU Toolchain
PPT
PDF
Specialized Compiler for Hash Cracking
PDF
Linker Wisdom
PPT
Advanced c programming in Linux
PDF
First session quiz
PDF
First session quiz
PPT
bh-europe-01-clowes
PDF
.NET Core, ASP.NET Core Course, Session 3
PPT
COM Introduction
PPT
Os7 2
PPT
From gcc to the autotools
PDF
Shell tutorial
PDF
DLL Tutor maXbox starter28
PPTX
Compiler presentaion
PPTX
[Defcon24] Introduction to the Witchcraft Compiler Collection
PDF
olibc: Another C Library optimized for Embedded Linux
The Parenscript Common Lisp to JavaScript compiler
Purdue CS354 Operating Systems 2008
Mach-O Internals
嵌入式Linux課程-GNU Toolchain
Specialized Compiler for Hash Cracking
Linker Wisdom
Advanced c programming in Linux
First session quiz
First session quiz
bh-europe-01-clowes
.NET Core, ASP.NET Core Course, Session 3
COM Introduction
Os7 2
From gcc to the autotools
Shell tutorial
DLL Tutor maXbox starter28
Compiler presentaion
[Defcon24] Introduction to the Witchcraft Compiler Collection
olibc: Another C Library optimized for Embedded Linux

More from Iftach Ian Amit (20)

PPTX
Cyber Risk Quantification - CyberTLV
PDF
Devsecops at Cimpress
PPTX
BSidesTLV Closing Keynote
PDF
Social Media Risk Metrics
PDF
ISTS12 Keynote
PDF
From your Pocket to your Heart and Back
PDF
Painting a Company Red and Blue
PDF
"Cyber" security - all good, no need to worry?
PDF
Armorizing applications
PDF
Seeing Red In Your Future?
PPTX
Hacking cyber-iamit
PDF
Passwords good badugly181212-2
PDF
PDF
Sexy defense
PDF
Cyber state
PDF
Advanced Data Exfiltration - the way Q would have done it
PDF
Infecting Python Bytecode
PDF
Exploiting Second life
PDF
Dtmf phreaking
PDF
Cheating in Computer Games
Cyber Risk Quantification - CyberTLV
Devsecops at Cimpress
BSidesTLV Closing Keynote
Social Media Risk Metrics
ISTS12 Keynote
From your Pocket to your Heart and Back
Painting a Company Red and Blue
"Cyber" security - all good, no need to worry?
Armorizing applications
Seeing Red In Your Future?
Hacking cyber-iamit
Passwords good badugly181212-2
Sexy defense
Cyber state
Advanced Data Exfiltration - the way Q would have done it
Infecting Python Bytecode
Exploiting Second life
Dtmf phreaking
Cheating in Computer Games

Recently uploaded (20)

PDF
KodekX | Application Modernization Development
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Machine learning based COVID-19 study performance prediction
PDF
cuic standard and advanced reporting.pdf
KodekX | Application Modernization Development
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Unlocking AI with Model Context Protocol (MCP)
Chapter 3 Spatial Domain Image Processing.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Dropbox Q2 2025 Financial Results & Investor Presentation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Understanding_Digital_Forensics_Presentation.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Spectral efficient network and resource selection model in 5G networks
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation_ Review paper, used for researhc scholars
NewMind AI Weekly Chronicles - August'25 Week I
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
The AUB Centre for AI in Media Proposal.docx
Machine learning based COVID-19 study performance prediction
cuic standard and advanced reporting.pdf

LD_PRELOAD Exploitation - DC9723

  • 1. Exploiting the Linux Dynamic Loader with LD_PRELOAD David Kaplan [email_address] DC9723 – June 2011
  • 2. The Executable and linking format (ELF) } linkers loaders libraries
  • 3. Linkers combine compiled code fragments into single memory-loadable executable $ ld obj1.o obj2.o –o linked.o symbol resolution program components reference each other through symbols (ELF .symtab) Relocation adjustment of code/data sections (also performed by the loader)
  • 4. Loaders copy code and data into memory memory allocation/mapping relocation Also performed by the linker execve()
  • 5. Libraries statically-linked dynamically-linked (shared)* collections of reusable compiled code *historically: a shared library was something else entirely
  • 6. Statically-linked libraries code copied into final binary be aware of: cyclic dependencies, multiple symbol definitions $ld obj1.o obj2.o /usr/lib/libname.a CODE my_print() { printf(); } main() { my_print(); } main() { my_print(); } my_print() { printf(); } printf() { vfprintf(); } LIBC STATICALLY LINKED FILESIZE
  • 7. Dynamically-linked libraries dynamic loader (ld.so) resolves symbols at exectime Process: - execve() loads executable code into memory
  • 8. - control is passed to the dynamic linker (ld.so) which maps shared objects to program address space (resolves symbols)
  • 9. - control is then passed to the application can be called from within the application at runtime By linking ld and calling dlopen(), etc. CODE my_print() { printf(); } main() { my_print(); } main() { my_print(); } my_print() { printf(); } printf(); DYNAMICALLY LINKED FILESIZE LIBC
  • 10. So what is LD_PRELOAD? environment var queried by dynamic linker on exec allows dynamic linker to prioritize linking defined shared libs $ LD_PRELOAD=“./mylib.so” ./myexec
  • 11. Attack enablers OS ‘features’ weak system security good coding practices general_rule: good_for_devs == good_for_hackers ; goto general_rule ;
  • 12. Attack advantages easy, effective on unprotected systems code interception code injection program flow manipulation debugging using wrapper functions
  • 13. Attack disadvantages } can be protected against requires access to executable requires relevant privileges works on used, imported symbols
  • 14. Example 1 – Hello World $ nm -D make_goodbye.so 000000000000069c T printf U stdout U vfprintf $ nm -D hello w __gmon_start__ U __libc_start_main U printf Undefined symbol Symbol exists in .text
  • 15. Example 1 – Hello World – cont. *in practice it works slightly differently – this is just a conceptual explanation NORMAL SYMBOL RESOLUTION: LOADER HELLO printf() ?? DYNAMIC LINKER LIBC.SO Hello World! REDIRECTED SYMBOL RESOLUTION: LOADER HELLO printf() ?? DYNAMIC LINKER GOODBYE.SO Goodbye World! LIBC.SO fprintf() ??
  • 16. Example 2 – OpenSSH MITM dynamically links openssl checks public key against known_hosts with BN_cmp() BN_cmp() must pass (== 0) for iterations 3 and 5
  • 17. Example 3 – OpenSSH password logger catch write() w/ string literal “’s password” log read()s until ‘\n’
  • 18. Example 4 – Extending ‘cat’ functionality intercept __snprintf_check() to add to usage() wrap getopt_long() to catch new command line option catch write(), vfork() and launch browser for each link
  • 19. provides reusable library of function sigs (sorry about the code quality!) ./preloader reduces repetitive tasks tool that does *some* of the work for you http://www.github.com/2of1/preloader
  • 20. Reverse Engineering with LD_PRELOAD (Itzik Kotler) http://securityvulns.com/articles/reveng/ Linkers and Loaders (Sandeep Grover) http://www.linuxjournal.com/article/6463 Dynamic Linker (Wikipedia) http://en.wikipedia.org/wiki/Dynamic_linker man ld.so Further reading
  • 21. “ Know your enemy and know yourself and you can fight a thousand battles without disaster” Sun Wu Tzu, The Art of War “ There is no right and wrong. There’s only fun and boring” The Plague, Hackers 1995 Final thoughts