Skip to content

Instantly share code, notes, and snippets.

@KiranPanesar
Created April 15, 2015 22:47
Show Gist options
  • Save KiranPanesar/2df6f5888b7cd534295d to your computer and use it in GitHub Desktop.
Save KiranPanesar/2df6f5888b7cd534295d to your computer and use it in GitHub Desktop.
// Utility functions
#import "CallTracer.h"
#import "SQLiteStorage.h"
#import "PlistObjectConverter.h"
#import "CallStackInspector.h"
static NSString *preferenceFilePath = @"/private/var/mobile/Library/Preferences/com.isecpartners.introspy.plist";
// Hooks for C functions directly use MobileSubstrate
#import "hooks/SecurityHooks.h"
#import "hooks/KeychainHooks.h"
#import "hooks/CommonCryptorHooks.h"
#import "hooks/CommonHMACHooks.h"
#import "hooks/CommonKeyDerivationHooks.h"
#import "hooks/CommonDigestHooks.h"
#import "hooks/LibCHooks.h"
// Hooks for ObjC methods rely on the Logos pre-processor
// H4ck to split Logos hooking code into separate files: we're including actual code, not headers
SQLiteStorage *traceStorage;
NSString *objectTypeNotSupported = @"Introspy - Not supported";
// Delegate hooks => We proxy the delegate objects
#import "hooks/DelegateProxies.h"
// Regular hooks
%group URLSchemes
#include "hooks/UIApplicationHooks.xm"
%end
%group FileSystemHooks
#include "hooks/NSDataHooks.xm"
#include "hooks/NSFileHandleHooks.xm"
#include "hooks/NSFileManagerHooks.xm"
#include "hooks/NSInputStreamHooks.xm"
#include "hooks/NSOutputStreamHooks.xm"
%end
%group HTTPHooks
#include "hooks/NSURLConnectionHooks.xm"
#include "hooks/NSHTTPCookieHooks.xm"
#include "hooks/NSURLCredentialHooks.xm"
%end
%group UserPreferencesHooks
#include "hooks/NSUserDefaultsHooks.xm"
%end
%group PasteboardHooks
#include "hooks/UIPasteboardHooks.xm"
%end
%group NSStringHooks
#include "hooks/NSStringHooks.xm"
%end
%group XMLHooks
#include "hooks/NSXMLParserHooks.xm"
%end
// Utility function to parse the preference file
static BOOL getBoolFromPreferences(NSMutableDictionary *preferences, NSString *preferenceValue) {
id value = [preferences objectForKey:preferenceValue];
if (value == nil) {
return YES; // default to YES
}
return [value boolValue];
}
// Log all custom URL schemes registered
// TODO: should we refactor this out of the main Tweak?
static void traceURISchemes() {
NSArray *url_schemes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
for (id schemeBundle in url_schemes) {
NSString *name = [schemeBundle objectForKey:@"CFBundleURLName"];
NSNumber *isPrivate = [schemeBundle objectForKey:@"CFBundleURLIsPrivate"];
for (id scheme in [schemeBundle objectForKey:@"CFBundleURLSchemes"]) {
CallTracer *tracer = [[CallTracer alloc] initWithClass:@"CFBundleURLTypes" andMethod:@"CFBundleURLSchemes"];
[tracer addArgFromPlistObject:name withKey:@"CFBundleURLName"];
[tracer addArgFromPlistObject:isPrivate withKey:@"CFBundleURLIsPrivate"];
[tracer addArgFromPlistObject:scheme withKey:@"CFBundleURLScheme"];
[traceStorage saveTracedCall:tracer];
[tracer release];
}
}
}
// Tweak starts here
%ctor {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Only hook Apps the user has selected in Introspy's settings panel
NSString *appId = [[NSBundle mainBundle] bundleIdentifier];
// Load Introspy preferences
NSMutableDictionary *preferences = [[NSMutableDictionary alloc] initWithContentsOfFile:preferenceFilePath];
id shouldHook = [preferences objectForKey:appId];
if ( (shouldHook == nil) || (! [shouldHook boolValue]) ) {
NSLog(@"Introspy - Profiling disabled for %@", appId);
[preferences release];
[pool drain];
return;
}
// Initialize DB storage
NSLog(@"Introspy - Profiling enabled for %@", appId);
BOOL shouldLog = getBoolFromPreferences(preferences, @"LogToTheConsole");
traceStorage = [[SQLiteStorage alloc] initWithDefaultDBFilePathAndLogToConsole: shouldLog];
if (traceStorage != nil) {
// Initialize hooks
// Data Storage hooks
if (getBoolFromPreferences(preferences, @"FileSystemHooks")) {
%init(FileSystemHooks);
}
if (getBoolFromPreferences(preferences, @"UserPreferencesHooks")) {
%init(UserPreferencesHooks);
}
if (getBoolFromPreferences(preferences, @"KeyChainHooks")) {
[KeychainHooks enableHooks];
}
// Crypto hooks
if (getBoolFromPreferences(preferences, @"CommonCryptoHooks")) {
[LibCHooks enableHooks]; // Not really part of CommonCrypto
[CommonCryptorHooks enableHooks];
[CommonHMACHooks enableHooks];
[CommonKeyDerivationHooks enableHooks];
[CommonDigestHooks enableHooks];
}
if (getBoolFromPreferences(preferences, @"SecurityHooks")) {
[SecurityHooks enableHooks];
}
// Network hooks
if (getBoolFromPreferences(preferences, @"HTTPHooks")) {
%init(HTTPHooks);
}
// IPC hooks
if (getBoolFromPreferences(preferences, @"PasteboardHooks")) {
%init(PasteboardHooks);
}
if (getBoolFromPreferences(preferences, @"URLSchemesHooks")) {
traceURISchemes();
%init(URLSchemes);
}
// Misc hooks
if (getBoolFromPreferences(preferences, @"XMLHooks")) {
%init(XMLHooks);
}
if (getBoolFromPreferences(preferences, @"NSStringHooks")) {
%init(NSStringHooks);
}
}
else {
NSLog(@"Introspy - DB Initialization error; disabling hooks.");
}
[preferences release];
[pool drain];
}
/* vim: set filetype=objc : */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment