Your SlideShare is downloading. ×
0
<ul>Exploiting the Linux Dynamic Loader with LD_PRELOAD </ul><ul>David Kaplan [email_address] DC9723 – June 2011 </ul>
<ul>The Executable and linking format (ELF) </ul><ul>} </ul><ul>linkers </ul><ul>loaders </ul><ul>libraries </ul>
<ul>Linkers </ul><ul>combine compiled code fragments into single memory-loadable executable </ul><ul><ul><li>$ ld obj1.o o...
<ul>Loaders </ul><ul>copy code and data into memory </ul><ul>memory allocation/mapping </ul><ul>relocation </ul><ul>Also p...
<ul>Libraries </ul><ul>statically-linked </ul><ul>dynamically-linked (shared)* </ul><ul>collections of reusable compiled c...
<ul>Statically-linked libraries </ul><ul>code copied into final binary </ul><ul>be aware of: cyclic dependencies, multiple...
<ul>Dynamically-linked libraries </ul><ul>dynamic loader (ld.so) resolves symbols at exectime </ul><ul><ul><li>Process: </...
- 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 </li></ul></ul></ul><ul>can be called from within the application at runtime <...
<ul>So what is LD_PRELOAD? </ul><ul>environment var queried by dynamic linker on exec </ul><ul>allows dynamic linker to pr...
<ul>Attack enablers </ul><ul>OS  ‘features’ </ul><ul>weak   system   security </ul><ul>good   coding   practices </ul><ul>...
<ul>Attack advantages </ul><ul>easy, effective on  unprotected  systems </ul><ul>code  interception </ul><ul>code   inject...
<ul>Attack disadvantages </ul><ul>} </ul><ul>can be  protected  against </ul><ul>requires  access   to executable </ul><ul...
<ul>Example 1 – Hello World </ul><ul>$ nm -D make_goodbye.so 000000000000069c T printf U stdout U vfprintf </ul><ul>$ nm -...
<ul>Example 1 – Hello World – cont. </ul><ul>*in practice it works slightly differently – this is just a conceptual explan...
<ul>Example 2 – OpenSSH MITM </ul><ul>dynamically links openssl checks public key against known_hosts with BN_cmp() BN_cmp...
<ul>Example 3 – OpenSSH password logger </ul><ul>catch write() w/ string literal “’s password” log read()s until ‘n’ </ul>
<ul>Example 4 – Extending ‘cat’ functionality </ul><ul>intercept __snprintf_check() to add to usage() wrap getopt_long() t...
<ul>provides reusable library of function sigs </ul><ul>(sorry about the code quality!) </ul><ul>./preloader </ul><ul>redu...
Upcoming SlideShare
Loading in...5
×

LD_PRELOAD Exploitation - DC9723

3,269

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,269
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
40
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "LD_PRELOAD Exploitation - DC9723"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×