• This repository has been archived on 25/Jan/2018
  • Stars
    star
    586
  • Rank 76,279 (Top 2 %)
  • Language
    Swift
  • License
    Do What The F*ck ...
  • Created over 10 years ago
  • Updated over 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PoC of function hooking in Swift

SWRoute is a tiny Swift wrapper over rd_route(). It allows you to route (hook) quite any function/method with another function/method or even a closure.

This code hasn't been updated since Xcode beta 1, so it may be outdated. Remember, it's just a proof-of-concept of what you can do in Swift.

If you're curious how it works, check out an article „Function hooking in Swift“.

Going to use it in your iOS project? Please, read the corresponding section below.

Exported interface

class SwiftRoute {
    class func replace<MethodT>(function targetMethod : MethodT, with replacement : MethodT) -> Int
}
Arguments
Argument Type (in/out) Description
function in (required) a function/method to override
with in (required) any other function/method or closure to overrride a function with
Return value

KERN_SUCCESS (== 0) upon success, > 0 otherwise.

Example usage

(see SWRouteTests/SWRouteTests.swift for more)

class DemoClass {
    func demoMethod(arg: Int) -> Int {
        return (42 + arg);
    }
}

var err = SwiftRoute.replace(function: DemoClass().demoMethod, with: {
    (arg : Int) -> Int in
        return (90 + arg)
})

SWRoute and iOS

Unfortunately rd_route (the back-end of SWRoute) doesn't work well on iOS (until jailbroken), because it does some tricks with memory pages that aren't allowed there. But you can choose any other library for function hooking instead! I recommend libevil by Landon Fuller.

You'll only need rd_get_func_impl.c source file included into your project to create your version of SWRoute:

// Route functions in Swift using libevil and rd_get_func_impl()
import Darwin

@asmname("_rd_get_func_impl")
    func rd_get_func_impl<Q>(Q) -> UInt64;
@asmname("evil_init")
    func evil_init();
@asmname("evil_override_ptr")
    func evil_override_ptr(UInt64, UInt64, CMutablePointer<UInt64>) -> CInt;

class EvilRoute {
    struct onceToken {
        static var token : dispatch_once_t = 0
    }

    class func replace<MethodT>(function targetMethod : MethodT, with replacement : MethodT) -> Int
    {
        dispatch_once(&onceToken.token, {
            evil_init()
        })

        let err: CInt = evil_override_ptr(rd_get_func_impl(DemoClass().demoMethod),
                                          rd_get_func_impl(someFunction),
                                          nil)

        return Int(err)
    }
}

License

WTFPL.

//  Copyright © 2014 Dmitry Rodionov <[email protected]>
//  This work is free. You can redistribute it and/or modify it under the
//  terms of the Do What The Fuck You Want To Public License, Version 2,
//  as published by Sam Hocevar. See the COPYING file for more details.

If you found any bug(s) or something, please open an issue or a pull request — I'd appreciate your help! (^,,^)


Dmitry Rodionov, 2014
[email protected]

More Repositories

1

rd_route

Function hooking for macOS
C
184
star
2

liblorgnette

Interprocess dlsym() for OS X & iOS
C
175
star
3

shortcuts

A CLI manager for your text replacements on macOS
Objective-C
66
star
4

cuckoo-osx-analyzer

An OS X analyzer for Cuckoo Sandbox project
Python
57
star
5

task_vaccine

Yet another code injection library for macOS
C
52
star
6

RDInjectionWizard

[Deprecated] Painless code injection
Objective-C
52
star
7

RDProcess

Re-implementation of NSProcessInfo+NSRunningApplication with all missed stuff (incl. Sandbox information and other things)
Objective-C
37
star
8

machobot

A Python toolbox for Mach-O files analysis.
Python
22
star
9

Locatr

OS X app for faking your location
Objective-C
17
star
10

selfieo

Dump the current process image to a file (OS X)
C
14
star
11

NeverGonnaGiveYouUp

An OS X kernel module that protects a userland process from being terminated in any way
C
13
star
12

rd_get_symbols

A modern replacement for nlist() that works for both i386 and x86_64 processes
C
11
star
13

ABetterPlaceForTweetbot

A positivity filter for Tweetbot in the morning.
Objective-C
9
star
14

Cegta

A tiny DSL for TDD/BDD written in C
C++
7
star
15

dotfiles

My dotfiles
Shell
6
star
16

Daruma

A Mac app for browsing japanese emoticons
Objective-C
5
star
17

dns-monitor

A simple DNS monitor/locker for macOS based on SystemConfiguration framework
Objective-C
4
star
18

Meetings

OS X Notifications showcase: schedule meetings and receive reminders about upcoming events
Swift
4
star
19

homebrew-taps

My very own Homebrew tap repository
Ruby
3
star
20

DropboxStatusFetcher

Query a local file sync status on OS X
Objective-C
2
star
21

YetAnotherAStar

A* path finder in Swift
Swift
1
star
22

rodionovd.github.io

my tiny corner of the web
HTML
1
star
23

HSCore.framework

HoneySound Core framework
1
star