SlideShare una empresa de Scribd logo
1 de 30
Descargar para leer sin conexión
CUANDO ESTÉS EN LÍOS CON LIBRERÍAS
DINÁMICAS, ¡DAME UN SILBIDITO!
Demos - https://github.com/pamarcos/betabeers
Slides - https://goo.gl/B4Qr42
$WHOAMI
Pablo Marcos Oltra
@pablomarc0s
Software Developer 

en Toptal
Anteriormente en
Intel, Gemalto,Agilent
Technologies
2
LIBRERÍAS DINÁMICAS
• ¿Qué son las librerías dinámicas?
• ¿En qué se diferencian de las librerías estáticas?
• ¿Cuáles son sus ventajas e inconvenientes?
3
COMPILACIÓN 101
4
// file.h
void log(const char*, …) {
// Log to stdout
}
void foo() {
LOG("foo");
}
#define LOG(...) log(__VA_ARGS__)
#define MY_FLAG
// file.c
#include “file.h”
void bar() {
#ifdef MY_FLAG
LOG("bar");
#endif
foo();
}
COMPILACIÓN 101
5
…
# 1 “file.c" 2
# 1 "./header.h" 1
void log(const char*, …) {
}
void foo() {
LOG("foo");
}
# 2 “file.c” 2
void bar() {
log("bar");
foo();
}
g++ -c -E -o file file.c
Translation Unit
Símbolo
Símbolo
Símbolo
COMPILACIÓN 101
g++ -o v3.o -c v3.cpp
g++ -o particle.o -c particle.cpp
g++ -o main.o -c main.cpp
g++ -o executable v3.o particle.o main.o
6
COMPILACIÓN 101
g++ -o v3.o -c v3.cpp
g++ -o particle.o -c particle.cpp
g++ -o main.o -c main.cpp
g++ -o executable v3.o particle.o main.o
7
Compilación
COMPILACIÓN 101
g++ -o v3.o -c v3.cpp
g++ -o particle.o -c particle.cpp
g++ -o main.o -c main.cpp
g++ -o executable v3.o particle.o main.o
8
Enlazado
COMPILACIÓN 101
g++ -o v3.o -c v3.cpp
g++ -o particle.o -c particle.cpp
g++ -o main.o -c main.cpp
g++ -o executable v3.o particle.o main.o
g++ -o executable v3.cpp particle.cpp main.cpp
9
Compilación
+ Enlazado
EMPAQUETANDO OBJECTOS
10
Ya he compilado varios objectos, ¿qué puedo hacer con ellos?
v3.o
particle.o
main.o
Ejecutable
v3.o
particle.o
Librería estática
v3.o
particle.o
Librería dinámica
Binario
Windows - PE (.exe)
Linux - ELF
macOS - Mach-O
Archivo de objectos
MSVC - (.lib)
GCC-like - (.a)
Binario
Windows - PE (.dll)
Linux - ELF (.so)
macOS - Mach-O (.dylib)
EMPAQUETANDO OBJECTOS
11
v3.o
particle.o
main.o
Ejecutable
Binario
Windows - PE (.exe)
Linux - ELF
macOS - Mach-O
g++ -o executable v3.o particle.o main.o
¿Cómo creo un ejecutable?
EMPAQUETANDO OBJECTOS
12
v3.o
particle.o
Librería estática
g++ -static -o libStatic.a v3.o particle.o
¿Cómo creo una librería estática?
Archivo de objectos
MSVC - (.lib)
GCC-like - (.a)
EMPAQUETANDO OBJECTOS
13
¿Cómo creo una librería dinámica?
v3.o
particle.o
Librería dinámica
Binario
Windows - PE (.dll)
Linux - ELF (.so)
macOS - Mach-O (.dylib)
g++ -shared -o libShared.so v3.o particle.o
Ojo!Todos los objectos de una librería dinámica deben
soportar PIC (Position-independent Code)
g++ -c -fPIC -o v3.o v3.cpp
g++ -c -fPIC -o particle.o particle.cpp
LIBRERÍA ESTÁTICA VS
DINÁMICA
14
App A
Librería
estática
App B
Librería
estática
App A App B
Librería
dinámica
USANDO LIBRERÍAS DINÁMICAS
1. En tiempo de compilación
• g++ -o executable main.o -lsharedLib.so
2. En tiempo de ejecución
• Windows: LoadLibrary y GetProcAddress
• Unix: dlopen y dlsym
15
USANDO LIBRERÍAS DINÁMICAS
16
En tiempo de ejecución
#include <dlfcn.h>
typedef void (foo_ptr*)(int);
…
void *handle = dlopen("libShared.so", RTLD_LAZY);
…
foo_ptr foo = dlsym(handle, "foo");
…
foo(5);
USANDO LIBRERÍAS DINÁMICAS
17
¿Para qué sirve cargarlas en tiempo de ejecución?
Para cargar librerías que no se conocen en tiempo
de compilación y poder extender la aplicación
Plugins y add-ons
Cambiar el comportamiento sin cerrar la aplicación
Sólo cargar la librería cuando se necesite
18
DEMO 1
https://github.com/pamarcos/Urho3D/tree/RCCpp/
¿DÓNDE BUSCA LAS LIBRERÍAS
EN TIEMPO DE EJECUCIÓN?
19
Unix - Linux y macOS
1. Directorios del rpath
2. LD_LIBRARY_PATH (Linux) y DYLD_LIBRARY_PATH
(macOS)
3. Directorios del system search path: /etc/ld.so.conf y 

/lib (Linux), /usr/lib, /usr/local/lib, etc
Windows
1. Directorio del ejecutable
2. Directorio del sistema: C:WindowsSystem32
3. Directorio de Windows:  C:Windows
4. El directorio actual: CWD
5. Directorios listados en la variable de entorno PATH
RPATH
• Acrónimo de run-time search path
• Codifica el path en el que buscar librerías dinámicas tanto en
ejecutables como en otras librerías dinámicas
• ¿Por qué se usa? Para hacer los ejecutables/librerías más flexibles.
Distintas distros pueden desplegar librerías en sitios diferentes
20
Librería
@rpath/libShared.so
Ejecutable
rpath=/my/rpath
rpath=@executable_path
INYECTANDO UNA LIBRERÍA
En Unix se pueden inyectar librerías que el dynamic
loader cargará antes de buscar en esos search paths
Linux - LD_PRELOAD (1)
macOS - DYLD_INSERT_LIBRARIES (2)
(1) Linux sin SELinux. Por defecto suele estar deshabilitado

(2) macOS sin “Debugging Restrictions”. Por defecto está habilitado
21
SI SOMOS BUENA
GENTE…
22
DEMO 2
…PERO, ¿Y SI NO LO
SOMOS?
23
DEMO 3
https://github.com/pamarcos/betabeers/tree/master/open
AÚN SE PUEDE LIAR
MÁS
24
DEMO 4
https://github.com/pamarcos/betabeers/tree/master/rpath
EJEMPLO REAL: MI MAC
25
LIBRERÍAS DINÁMICAS

(RELOADED)
• ¿Qué son las librerías dinámicas?
• ¿En qué se diferencian de las librerías estáticas?
• ¿Cuáles son sus ventajas e inconvenientes?
26
UTILIDADES
• Linux - ldd
• macOS - otool
• Windows - dependency walker
27
RECURSOS
• Dynamic libraries en macOS - https://developer.apple.com/library/content/
documentation/DeveloperTools/Conceptual/DynamicLibraries/000-
Introduction/Introduction.html#//apple_ref/doc/uid/TP40001908-SW1
• DLL Hijacking en macOS - https://s3.amazonaws.com/s3.synack.com/
canSecW.pdf
• Dylib Hijack Scanner en macOS - https://objective-see.com/products/
dhs.html
• https://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.
85).aspx#standard_search_order_for_desktop_applications
28
¿PREGUNTAS?
29
¡GRACIAS!
30

Más contenido relacionado

Último

Tipos de suelo y su clasificación y ejemplos
Tipos de suelo y su clasificación y ejemplosTipos de suelo y su clasificación y ejemplos
Tipos de suelo y su clasificación y ejemplos
andersonsubero28
 
Tema ilustrado 9.2.docxbbbbbbbbbbbbbbbbbbb
Tema ilustrado 9.2.docxbbbbbbbbbbbbbbbbbbbTema ilustrado 9.2.docxbbbbbbbbbbbbbbbbbbb
Tema ilustrado 9.2.docxbbbbbbbbbbbbbbbbbbb
antoniolfdez2006
 
SESION 11 SUPERVISOR SSOMA SEGURIDAD Y SALUD OCUPACIONAL
SESION 11 SUPERVISOR SSOMA SEGURIDAD Y SALUD OCUPACIONALSESION 11 SUPERVISOR SSOMA SEGURIDAD Y SALUD OCUPACIONAL
SESION 11 SUPERVISOR SSOMA SEGURIDAD Y SALUD OCUPACIONAL
EdwinC23
 

Último (20)

G4 - CASO DE ESTUDIO - VOLUMEN DE UN RESERVORIO (1).pptx
G4 - CASO DE ESTUDIO - VOLUMEN DE UN RESERVORIO (1).pptxG4 - CASO DE ESTUDIO - VOLUMEN DE UN RESERVORIO (1).pptx
G4 - CASO DE ESTUDIO - VOLUMEN DE UN RESERVORIO (1).pptx
 
Arquitecto cambio de uso de suelo Limache
Arquitecto cambio de uso de suelo LimacheArquitecto cambio de uso de suelo Limache
Arquitecto cambio de uso de suelo Limache
 
APORTES A LA ARQUITECTURA DE WALTER GROPIUS Y FRANK LLOYD WRIGHT
APORTES A LA ARQUITECTURA DE WALTER GROPIUS Y FRANK LLOYD WRIGHTAPORTES A LA ARQUITECTURA DE WALTER GROPIUS Y FRANK LLOYD WRIGHT
APORTES A LA ARQUITECTURA DE WALTER GROPIUS Y FRANK LLOYD WRIGHT
 
Tipos de suelo y su clasificación y ejemplos
Tipos de suelo y su clasificación y ejemplosTipos de suelo y su clasificación y ejemplos
Tipos de suelo y su clasificación y ejemplos
 
libro de ingeniería de petróleos y operaciones
libro de ingeniería de petróleos y operacioneslibro de ingeniería de petróleos y operaciones
libro de ingeniería de petróleos y operaciones
 
CAPACITACIÓN EN AGUA Y SANEAMIENTO EN ZONAS RURALES
CAPACITACIÓN EN AGUA Y SANEAMIENTO EN ZONAS RURALESCAPACITACIÓN EN AGUA Y SANEAMIENTO EN ZONAS RURALES
CAPACITACIÓN EN AGUA Y SANEAMIENTO EN ZONAS RURALES
 
27311861-Cuencas-sedimentarias-en-Colombia.ppt
27311861-Cuencas-sedimentarias-en-Colombia.ppt27311861-Cuencas-sedimentarias-en-Colombia.ppt
27311861-Cuencas-sedimentarias-en-Colombia.ppt
 
3er Informe Laboratorio Quimica General (2) (1).pdf
3er Informe Laboratorio Quimica General  (2) (1).pdf3er Informe Laboratorio Quimica General  (2) (1).pdf
3er Informe Laboratorio Quimica General (2) (1).pdf
 
1. Equipos Primarios de una Subestaciones electricas
1. Equipos Primarios de una Subestaciones electricas1. Equipos Primarios de una Subestaciones electricas
1. Equipos Primarios de una Subestaciones electricas
 
NTC 3883 análisis sensorial. metodología. prueba duo-trio.pdf
NTC 3883 análisis sensorial. metodología. prueba duo-trio.pdfNTC 3883 análisis sensorial. metodología. prueba duo-trio.pdf
NTC 3883 análisis sensorial. metodología. prueba duo-trio.pdf
 
Aportes a la Arquitectura de Le Corbusier y Mies Van Der Rohe.pdf
Aportes a la Arquitectura de Le Corbusier y Mies Van Der Rohe.pdfAportes a la Arquitectura de Le Corbusier y Mies Van Der Rohe.pdf
Aportes a la Arquitectura de Le Corbusier y Mies Van Der Rohe.pdf
 
portafolio final manco 2 1816827 portafolio de evidencias
portafolio final manco 2 1816827 portafolio de evidenciasportafolio final manco 2 1816827 portafolio de evidencias
portafolio final manco 2 1816827 portafolio de evidencias
 
Tippens fisica 7eDIAPOSITIVAS TIPENS Tippens_fisica_7e_diapositivas_33.ppt
Tippens fisica 7eDIAPOSITIVAS TIPENS Tippens_fisica_7e_diapositivas_33.pptTippens fisica 7eDIAPOSITIVAS TIPENS Tippens_fisica_7e_diapositivas_33.ppt
Tippens fisica 7eDIAPOSITIVAS TIPENS Tippens_fisica_7e_diapositivas_33.ppt
 
5. MATERIALES petreos para concreto.pdf.
5. MATERIALES petreos para concreto.pdf.5. MATERIALES petreos para concreto.pdf.
5. MATERIALES petreos para concreto.pdf.
 
Cereales tecnología de los alimentos. Cereales
Cereales tecnología de los alimentos. CerealesCereales tecnología de los alimentos. Cereales
Cereales tecnología de los alimentos. Cereales
 
docsity-manzaneo-y-lotizacion para habilitacopm urbana
docsity-manzaneo-y-lotizacion para habilitacopm urbanadocsity-manzaneo-y-lotizacion para habilitacopm urbana
docsity-manzaneo-y-lotizacion para habilitacopm urbana
 
Tema ilustrado 9.2.docxbbbbbbbbbbbbbbbbbbb
Tema ilustrado 9.2.docxbbbbbbbbbbbbbbbbbbbTema ilustrado 9.2.docxbbbbbbbbbbbbbbbbbbb
Tema ilustrado 9.2.docxbbbbbbbbbbbbbbbbbbb
 
ESPECIFICACIONES TECNICAS COMPLEJO DEPORTIVO
ESPECIFICACIONES TECNICAS COMPLEJO DEPORTIVOESPECIFICACIONES TECNICAS COMPLEJO DEPORTIVO
ESPECIFICACIONES TECNICAS COMPLEJO DEPORTIVO
 
SESION 11 SUPERVISOR SSOMA SEGURIDAD Y SALUD OCUPACIONAL
SESION 11 SUPERVISOR SSOMA SEGURIDAD Y SALUD OCUPACIONALSESION 11 SUPERVISOR SSOMA SEGURIDAD Y SALUD OCUPACIONAL
SESION 11 SUPERVISOR SSOMA SEGURIDAD Y SALUD OCUPACIONAL
 
ingenieria grafica para la carrera de ingeniera .pptx
ingenieria grafica para la carrera de ingeniera .pptxingenieria grafica para la carrera de ingeniera .pptx
ingenieria grafica para la carrera de ingeniera .pptx
 

Destacado

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Destacado (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

Slides de Librerías dinámicas en Betabeers Murcia

  • 1. CUANDO ESTÉS EN LÍOS CON LIBRERÍAS DINÁMICAS, ¡DAME UN SILBIDITO! Demos - https://github.com/pamarcos/betabeers Slides - https://goo.gl/B4Qr42
  • 2. $WHOAMI Pablo Marcos Oltra @pablomarc0s Software Developer 
 en Toptal Anteriormente en Intel, Gemalto,Agilent Technologies 2
  • 3. LIBRERÍAS DINÁMICAS • ¿Qué son las librerías dinámicas? • ¿En qué se diferencian de las librerías estáticas? • ¿Cuáles son sus ventajas e inconvenientes? 3
  • 4. COMPILACIÓN 101 4 // file.h void log(const char*, …) { // Log to stdout } void foo() { LOG("foo"); } #define LOG(...) log(__VA_ARGS__) #define MY_FLAG // file.c #include “file.h” void bar() { #ifdef MY_FLAG LOG("bar"); #endif foo(); }
  • 5. COMPILACIÓN 101 5 … # 1 “file.c" 2 # 1 "./header.h" 1 void log(const char*, …) { } void foo() { LOG("foo"); } # 2 “file.c” 2 void bar() { log("bar"); foo(); } g++ -c -E -o file file.c Translation Unit Símbolo Símbolo Símbolo
  • 6. COMPILACIÓN 101 g++ -o v3.o -c v3.cpp g++ -o particle.o -c particle.cpp g++ -o main.o -c main.cpp g++ -o executable v3.o particle.o main.o 6
  • 7. COMPILACIÓN 101 g++ -o v3.o -c v3.cpp g++ -o particle.o -c particle.cpp g++ -o main.o -c main.cpp g++ -o executable v3.o particle.o main.o 7 Compilación
  • 8. COMPILACIÓN 101 g++ -o v3.o -c v3.cpp g++ -o particle.o -c particle.cpp g++ -o main.o -c main.cpp g++ -o executable v3.o particle.o main.o 8 Enlazado
  • 9. COMPILACIÓN 101 g++ -o v3.o -c v3.cpp g++ -o particle.o -c particle.cpp g++ -o main.o -c main.cpp g++ -o executable v3.o particle.o main.o g++ -o executable v3.cpp particle.cpp main.cpp 9 Compilación + Enlazado
  • 10. EMPAQUETANDO OBJECTOS 10 Ya he compilado varios objectos, ¿qué puedo hacer con ellos? v3.o particle.o main.o Ejecutable v3.o particle.o Librería estática v3.o particle.o Librería dinámica Binario Windows - PE (.exe) Linux - ELF macOS - Mach-O Archivo de objectos MSVC - (.lib) GCC-like - (.a) Binario Windows - PE (.dll) Linux - ELF (.so) macOS - Mach-O (.dylib)
  • 11. EMPAQUETANDO OBJECTOS 11 v3.o particle.o main.o Ejecutable Binario Windows - PE (.exe) Linux - ELF macOS - Mach-O g++ -o executable v3.o particle.o main.o ¿Cómo creo un ejecutable?
  • 12. EMPAQUETANDO OBJECTOS 12 v3.o particle.o Librería estática g++ -static -o libStatic.a v3.o particle.o ¿Cómo creo una librería estática? Archivo de objectos MSVC - (.lib) GCC-like - (.a)
  • 13. EMPAQUETANDO OBJECTOS 13 ¿Cómo creo una librería dinámica? v3.o particle.o Librería dinámica Binario Windows - PE (.dll) Linux - ELF (.so) macOS - Mach-O (.dylib) g++ -shared -o libShared.so v3.o particle.o Ojo!Todos los objectos de una librería dinámica deben soportar PIC (Position-independent Code) g++ -c -fPIC -o v3.o v3.cpp g++ -c -fPIC -o particle.o particle.cpp
  • 14. LIBRERÍA ESTÁTICA VS DINÁMICA 14 App A Librería estática App B Librería estática App A App B Librería dinámica
  • 15. USANDO LIBRERÍAS DINÁMICAS 1. En tiempo de compilación • g++ -o executable main.o -lsharedLib.so 2. En tiempo de ejecución • Windows: LoadLibrary y GetProcAddress • Unix: dlopen y dlsym 15
  • 16. USANDO LIBRERÍAS DINÁMICAS 16 En tiempo de ejecución #include <dlfcn.h> typedef void (foo_ptr*)(int); … void *handle = dlopen("libShared.so", RTLD_LAZY); … foo_ptr foo = dlsym(handle, "foo"); … foo(5);
  • 17. USANDO LIBRERÍAS DINÁMICAS 17 ¿Para qué sirve cargarlas en tiempo de ejecución? Para cargar librerías que no se conocen en tiempo de compilación y poder extender la aplicación Plugins y add-ons Cambiar el comportamiento sin cerrar la aplicación Sólo cargar la librería cuando se necesite
  • 19. ¿DÓNDE BUSCA LAS LIBRERÍAS EN TIEMPO DE EJECUCIÓN? 19 Unix - Linux y macOS 1. Directorios del rpath 2. LD_LIBRARY_PATH (Linux) y DYLD_LIBRARY_PATH (macOS) 3. Directorios del system search path: /etc/ld.so.conf y 
 /lib (Linux), /usr/lib, /usr/local/lib, etc Windows 1. Directorio del ejecutable 2. Directorio del sistema: C:WindowsSystem32 3. Directorio de Windows:  C:Windows 4. El directorio actual: CWD 5. Directorios listados en la variable de entorno PATH
  • 20. RPATH • Acrónimo de run-time search path • Codifica el path en el que buscar librerías dinámicas tanto en ejecutables como en otras librerías dinámicas • ¿Por qué se usa? Para hacer los ejecutables/librerías más flexibles. Distintas distros pueden desplegar librerías en sitios diferentes 20 Librería @rpath/libShared.so Ejecutable rpath=/my/rpath rpath=@executable_path
  • 21. INYECTANDO UNA LIBRERÍA En Unix se pueden inyectar librerías que el dynamic loader cargará antes de buscar en esos search paths Linux - LD_PRELOAD (1) macOS - DYLD_INSERT_LIBRARIES (2) (1) Linux sin SELinux. Por defecto suele estar deshabilitado
 (2) macOS sin “Debugging Restrictions”. Por defecto está habilitado 21
  • 23. …PERO, ¿Y SI NO LO SOMOS? 23 DEMO 3 https://github.com/pamarcos/betabeers/tree/master/open
  • 24. AÚN SE PUEDE LIAR MÁS 24 DEMO 4 https://github.com/pamarcos/betabeers/tree/master/rpath
  • 26. LIBRERÍAS DINÁMICAS
 (RELOADED) • ¿Qué son las librerías dinámicas? • ¿En qué se diferencian de las librerías estáticas? • ¿Cuáles son sus ventajas e inconvenientes? 26
  • 27. UTILIDADES • Linux - ldd • macOS - otool • Windows - dependency walker 27
  • 28. RECURSOS • Dynamic libraries en macOS - https://developer.apple.com/library/content/ documentation/DeveloperTools/Conceptual/DynamicLibraries/000- Introduction/Introduction.html#//apple_ref/doc/uid/TP40001908-SW1 • DLL Hijacking en macOS - https://s3.amazonaws.com/s3.synack.com/ canSecW.pdf • Dylib Hijack Scanner en macOS - https://objective-see.com/products/ dhs.html • https://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs. 85).aspx#standard_search_order_for_desktop_applications 28