Skip to content

Instantly share code, notes, and snippets.

@nabla-c0d3
Last active December 22, 2022 12:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nabla-c0d3/f952c6fcc1e9d359dbfe to your computer and use it in GitHub Desktop.
Save nabla-c0d3/f952c6fcc1e9d359dbfe to your computer and use it in GitHub Desktop.
Hooking a variadic function with Cydia Substrate
//
// 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);
}
@ta01st
Copy link

ta01st commented Oct 1, 2016

how to hook NSString’s + (instancetype)stringWithFormat:(NSString *)format, ... ?

@Naeemullah1
Copy link

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