SwiftHook

0.5.0

⚓️ A Swift Library for hooking swift methods and functions.
p-x9/swift-hook

What's New

v0.5.0

2024-04-06T13:30:00Z

What's Changed

  • Setup ci by @p-x9 in #11
  • setup dependabot by @p-x9 in #12
  • Bump github.com/p-x9/machokit-spm from 0.13.0 to 0.13.1 by @dependabot in #13
  • Bump github.com/p-x9/machokit-spm from 0.13.1 to 0.14.0 by @dependabot in #14
  • Bump github.com/p-x9/machokit-spm from 0.14.0 to 0.15.0 by @dependabot in #15

New Contributors

Full Changelog: 0.4.0...0.5.0

SwiftHook

A Swift Library for hooking swift methods and functions.

Github issues Github forks Github stars Github top language

How works

  • Function / Struct Method Hook by facebook/fishhook.

  • Objective-C Class Method Simply, Objective-C runtime is used.

  • Swift Class Method Hook by rewriting Vtable.

Usage

Note

To hook a function that exists in your own image, you must specify the following linker flag. "-Xlinker -interposable"

Reference: johnno1962/SwiftTrace

Hook

Function / Struct Method

The targetFunction implementation is replaced by the replacementFunction implementation. The original implementation is moved to originalFunction.

It is possible to call an originalFunction within a replacementFunction.

try SwiftHook.hookFunction(
    "SwiftHookTests.targetFunction() -> Swift.String",
    "SwiftHookTests.replacementFunction() -> Swift.String",
    "SwiftHookTests.originalFunction() -> Swift.String",
    isMangled: false
)

/* using mangled symbol names */
try SwiftHook.hookFunction(
    "$s14SwiftHookTests14targetFunctionSSyF",
    "$s14SwiftHookTests19replacementFunctionSSyF",
    "$s14SwiftHookTests16originalFunctionSSyF",
    isMangled: true
)

Exchange Implementation

Function / Struct Method

SwiftHook.exchangeFuncImplementation(
    "SwiftHookTests.StructItem.printA() -> Swift.String",
    "SwiftHookTests.StructItem.printB() -> Swift.String",
    isMangled: false
)

/* using mangled symbol names */
SwiftHook.exchangeFuncImplementation(
    "$s14SwiftHookTests21hook_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_SSAISus6UInt32VtF",
    "$s14SwiftHookTests25XXXXhook_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_A2ISus6UInt32VtF",
    isMangled: false
)

Class Method

/// Swift Class
try SwiftHook.exchangeMethodImplementation(
    "SwiftHookTests.SwiftClassItem.mul2(Swift.Int) -> Swift.Int",
    "SwiftHookTests.SwiftClassItem.add2(Swift.Int) -> Swift.Int",
    for: SwiftClassItem.self
)

// Objective-C Class
try SwiftHook.exchangeMethodImplementation(
    "mul2:",
    "add2:",
    for: ObjCClassItem.self
)

License

swift-hook is released under the MIT License. See LICENSE

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Thu Nov 28 2024 07:49:54 GMT-1000 (Hawaii-Aleutian Standard Time)