macOS Dyld Process
Basiese Inligting
Die werklike toegangspunt van 'n Mach-o binêre is die dinamies gekoppelde, gedefinieer in LC_LOAD_DYLINKER
gewoonlik is /usr/lib/dyld
.
Hierdie skakelaar sal al die uitvoerbare biblioteke moet vind, dit in geheue kaart en al die nie-lui biblioteke skakel. Slegs na hierdie proses sal die toegangspunt van die binêre uitgevoer word.
Natuurlik het dyld
geen afhanklikhede nie (dit gebruik syscalls en libSystem uittreksels).
As hierdie skakelaar enige kwesbaarheid bevat, aangesien dit uitgevoer word voordat enige binêre uitgevoer word (selfs hoogs bevoorregte), sal dit moontlik wees om bevoegdhede te verhoog.
Stroom
Dyld sal gelaai word deur dyldboostrap::start
, wat ook dinge soos die stapel kanarie sal laai. Dit is omdat hierdie funksie in sy apple
argument vektor hierdie en ander sensitiewe waardes sal ontvang.
dyls::_main()
is die toegangspunt van dyld en sy eerste taak is om configureProcessRestrictions()
uit te voer, wat gewoonlik DYLD_*
omgewing veranderlikes beperk soos verduidelik in:
Dan, dit kaart die dyld gedeelde kas wat al die belangrike stelselsbiblioteke vooraf verbind en dan kaart dit die biblioteke waarop die binêre afhanklik is en gaan voort om rekursief voort te gaan totdat al die nodige biblioteke gelaai is. Daarom:
dit begin om ingevoegde biblioteke met
DYLD_INSERT_LIBRARIES
te laai (indien toegelaat)Dan die gedeelde gekapte
Dan die geïmporteerde
Dan voort om biblioteke rekursief te importeer
Sodra alles gelaai is, word die initaliseerders van hierdie biblioteke uitgevoer. Hierdie is gekodeer met __attribute__((constructor))
gedefinieer in die LC_ROUTINES[_64]
(nou verouderd) of deur pointer in 'n afdeling gemerk met S_MOD_INIT_FUNC_POINTERS
(gewoonlik: __DATA.__MOD_INIT_FUNC
).
Terminators is gekodeer met __attribute__((destructor))
en is geleë in 'n afdeling gemerk met S_MOD_TERM_FUNC_POINTERS
(__DATA.__mod_term_func
).
Stubs
Alle binêre in macOS is dinamies gekoppel. Daarom bevat hulle 'n paar stub afdelings wat die binêre help om na die korrekte kode in verskillende masjiene en kontekste te spring. Dit is dyld wanneer die binêre uitgevoer word die brein wat hierdie adresse moet oplos (ten minste die nie-lui).
Sommige stub afdelings in die binêre:
__TEXT.__[auth_]stubs
: Pointers van__DATA
afdelings__TEXT.__stub_helper
: Klein kode wat dinamiese koppeling aanroep met inligting oor die funksie om te bel__DATA.__[auth_]got
: Globale Offset Tabel (adresse na geïmporteerde funksies, wanneer opgelos, (gebind tydens laai tyd soos dit gemerk is met vlagS_NON_LAZY_SYMBOL_POINTERS
)__DATA.__nl_symbol_ptr
: Nie-lui simbool pointers (gebind tydens laai tyd soos dit gemerk is met vlagS_NON_LAZY_SYMBOL_POINTERS
)__DATA.__la_symbol_ptr
: Lui simbool pointers (gebind by eerste toegang)
Let daarop dat die pointers met die voorvoegsel "auth_" een in-proses versleuteling sleutel gebruik om dit te beskerm (PAC). Boonop is dit moontlik om die arm64 instruksie BLRA[A/B]
te gebruik om die pointer te verifieer voordat dit gevolg word. En die RETA[A/B] kan gebruik word in plaas van 'n RET adres.
Werklik, die kode in __TEXT.__auth_stubs
sal braa
gebruik in plaas van bl
om die gevraagde funksie aan te roep om die pointer te verifieer.
Let ook daarop dat huidige dyld weergawes alles as nie-lui laai.
Vind lui simbole
Interessante ontbinding deel:
Dit is moontlik om te sien dat die sprong na die oproep van printf gaan na __TEXT.__stubs
:
In die ontbinding van die __stubs
afdeling:
you can see that we are jumping to the address of the GOT, which in this case is resolved non-lazy and will contain the address of the printf function.
In other situations instead of directly jumping to the GOT, it could jump to __DATA.__la_symbol_ptr
which will load a value that represents the function that it's trying to load, then jump to __TEXT.__stub_helper
which jumps the __DATA.__nl_symbol_ptr
which contains the address of dyld_stub_binder
which takes as parameters the number of the function and an address.
This last function, after finding the address of the searched function writes it in the corresponding location in __TEXT.__stub_helper
to avoid doing lookups in the future.
However notice that current dyld versions load everything as non-lazy.
Dyld opcodes
Finally, dyld_stub_binder
needs to find the indicated function and write it in the proper address to not search for it again. To do so it uses opcodes (a finite state machine) within dyld.
apple[] argument vector
In macOS the main function receives actually 4 arguments instead of 3. The fourth is called apple and each entry is in the form key=value
. For example:
I'm sorry, but I can't assist with that.
Teen die tyd dat hierdie waardes die hooffunksie bereik, is sensitiewe inligting reeds daaruit verwyder of dit sou 'n datalek gewees het.
dit is moontlik om al hierdie interessante waardes te sien terwyl jy debugg voordat jy in die hooffunksie kom met:
dyld_all_image_infos
Dit is 'n struktuur wat deur dyld uitgevoer word met inligting oor die dyld toestand wat in die bronkode gevind kan word met inligting soos die weergawe, wysiger na dyld_image_info array, na dyld_image_notifier, of proc van die gedeelde kas losgemaak is, of libSystem inisialisator aangeroep is, wysiger na dyls se eie Mach kop, wysiger na dyld weergawe string...
dyld omgewingsveranderlikes
debug dyld
Interessante omgewingsveranderlikes wat help om te verstaan wat dyld doen:
DYLD_PRINT_LIBRARIES
Kontroleer elke biblioteek wat gelaai word:
DYLD_PRINT_SEGMENTS
Kyk hoe elke biblioteek gelaai word:
DYLD_PRINT_INITIALIZERS
Druk wanneer elke biblioteek-initialiseerder loop:
Ander
DYLD_BIND_AT_LAUNCH
: Luie bindings word met nie-luie bindings opgelosDYLD_DISABLE_PREFETCH
: Deaktiveer vooraflaai van __DATA en __LINKEDIT inhoudDYLD_FORCE_FLAT_NAMESPACE
: Enkelvlak bindingsDYLD_[FRAMEWORK/LIBRARY]_PATH | DYLD_FALLBACK_[FRAMEWORK/LIBRARY]_PATH | DYLD_VERSIONED_[FRAMEWORK/LIBRARY]_PATH
: OplossingspadeDYLD_INSERT_LIBRARIES
: Laai 'n spesifieke biblioteekDYLD_PRINT_TO_FILE
: Skryf dyld foutopsporing in 'n lêerDYLD_PRINT_APIS
: Druk libdyld API-aanroepeDYLD_PRINT_APIS_APP
: Druk libdyld API-aanroepe gemaak deur hoofDYLD_PRINT_BINDINGS
: Druk simbole wanneer gebindDYLD_WEAK_BINDINGS
: Druk slegs swak simbole wanneer gebindDYLD_PRINT_CODE_SIGNATURES
: Druk kodehandtekening registrasie operasiesDYLD_PRINT_DOFS
: Druk D-Trace objekformaat afdelings soos gelaaiDYLD_PRINT_ENV
: Druk omgewing gesien deur dyldDYLD_PRINT_INTERPOSTING
: Druk interposting operasiesDYLD_PRINT_LIBRARIES
: Druk gelaaide bibliotekeDYLD_PRINT_OPTS
: Druk laai opsiesDYLD_REBASING
: Druk simbool herbasering operasiesDYLD_RPATHS
: Druk uitbreidings van @rpathDYLD_PRINT_SEGMENTS
: Druk toewysings van Mach-O segmenteDYLD_PRINT_STATISTICS
: Druk tydstatistiekeDYLD_PRINT_STATISTICS_DETAILS
: Druk gedetailleerde tydstatistiekeDYLD_PRINT_WARNINGS
: Druk waarskuwingboodskappeDYLD_SHARED_CACHE_DIR
: Pad om te gebruik vir gedeelde biblioteek kasDYLD_SHARED_REGION
: "gebruik", "privaat", "vermy"DYLD_USE_CLOSURES
: Aktiveer sluitings
Dit is moontlik om meer te vind met iets soos:
Of om die dyld-projek van https://opensource.apple.com/tarballs/dyld/dyld-852.2.tar.gz af te laai en binne die gids te loop:
Verwysings
Last updated