Last active
December 22, 2022 12:47
-
-
Save nabla-c0d3/f952c6fcc1e9d359dbfe to your computer and use it in GitHub Desktop.
Hooking a variadic function with Cydia Substrate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LibC.m | |
// | |
// Created by Alban Diquet on 5/14/14. | |
// Copyright (c) 2014 Alban Diquet. All rights reserved. | |
// | |
#import <CydiaSubstrate.h> | |
#import "LibC.h" | |
// Hook open() to catch the path of all files being accessed | |
static int (*original_open)(const char *path, int oflag, ...); | |
static int replaced_open(const char *path, int oflag, ...) { | |
int result = 0; | |
// Handle the optional third argument | |
if (oflag & O_CREAT) { | |
mode_t mode; | |
va_list args; | |
va_start(args, oflag); | |
mode = (mode_t)va_arg(args, int); | |
va_end(args); | |
result = original_open(path, oflag, mode); | |
} | |
else { | |
result = original_open(path, oflag); | |
} | |
NSLog(@"OPEN: %s", path); | |
return result; | |
} | |
void hookLibC(void) { | |
MSHookFunction(open, replaced_open, (void **) &original_open); | |
} |
With a variable number of args passed to the variadic without a "count" parameter, the hooking becomes impossible without using ASM.
(For future reference for anyone hitting here)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to hook NSString’s + (instancetype)stringWithFormat:(NSString *)format, ... ?