Skip to content

Commit

Permalink
fix with Swift symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
jintao committed Jun 3, 2019
1 parent 8354984 commit 3a181b5
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 37 deletions.
10 changes: 6 additions & 4 deletions Demo/FishHookProtect.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -392,20 +392,21 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = S8CXSB3NT6;
DEVELOPMENT_TEAM = ZUK9CU4C3W;
INFOPLIST_FILE = FishHookProtect/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = com.tmt.PushDebugTest;
PRODUCT_BUNDLE_IDENTIFIER = jin.PushDebugTest;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "FishHookProtect/FishHookProtect-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
VALID_ARCHS = arm64;
};
name = Debug;
};
Expand All @@ -417,19 +418,20 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = S8CXSB3NT6;
DEVELOPMENT_TEAM = ZUK9CU4C3W;
INFOPLIST_FILE = FishHookProtect/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = com.tmt.PushDebugTest;
PRODUCT_BUNDLE_IDENTIFIER = jin.PushDebugTest;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "FishHookProtect/FishHookProtect-Bridging-Header.h";
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
VALID_ARCHS = arm64;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<key>orderHint</key>
<integer>4</integer>
</dict>
<key>FishHookProtect.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>4</integer>
</dict>
</dict>
</dict>
</plist>
Binary file not shown.
8 changes: 4 additions & 4 deletions Demo/FishHookProtect/AntiFishHookTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import MachO
import antiFishhook

// protect
public func protectNSLog() {
resetSymbol("NSLog")
public func protectPrint() {
resetSymbol("printf")
}

// protect BaseTest Framework's dladdr
Expand All @@ -32,11 +32,11 @@ public func protectDladdr() {
}


public func verificationDyld() {
public func verificationDladdr() {
if let testImp = class_getMethodImplementation(BaseTest.self, #selector(BaseTest.baseTest)) {
var info = Dl_info()
if dladdr(UnsafeRawPointer(testImp), &info) == -999 {
print("dladdr --------- fishhook")
print("dladdr method had been fishhooked")
} else if dladdr(UnsafeRawPointer(testImp), &info) == 1 {
print("dladdr fname---------", String(cString: info.dli_fname))
}
Expand Down
28 changes: 10 additions & 18 deletions Demo/FishHookProtect/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,20 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

print("origation: ")
// origation
NSLog("origation: Hello, World")
verificationDyld()
// hook printf method
fishhookPrint(newMethod: TestHelp.getNewPrintMehod())
TestHelp.print(withStr: "printf Tanner") // print result

// fishhook
fishhookNSLog(newMethod: TestHelp.getNewNSLogMehod())
fishhookDladdr(newMethod: TestHelp.getNewDladdrMethod())
protectPrint()
TestHelp.print(withStr: "printf Jin\n") // print result

print("\nverifiacate fishhook: ")
// verifiacate fishhook
NSLog("verifiacate fishhook: Hello, World")
verificationDyld()

// fishhook protect
protectNSLog()
// hook dladdr method
fishhookDladdr(newMethod: TestHelp.getNewDladdrMethod())
verificationDladdr() // print reslut

protectDladdr()

print("\nverifiacate anti-fishhook : ")
// verifiacate fishhook protect
NSLog("verifiacate anti-fishhook: Hello, World")
verificationDyld()
verificationDladdr() // print result

return true
}
Expand Down
3 changes: 2 additions & 1 deletion Demo/FishHookProtect/TestHelp.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ NS_ASSUME_NONNULL_BEGIN

@interface TestHelp : NSObject

+ (void *)getNewNSLogMehod;
+ (void)printWithStr: (NSString *)str;
+ (void *)getNewPrintMehod;
+ (void *)getNewDladdrMethod;

@end
Expand Down
16 changes: 10 additions & 6 deletions Demo/FishHookProtect/TestHelp.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@

@implementation TestHelp

+ (void *)getNewNSLogMehod {
return newNSLog;
+ (void)printWithStr:(NSString *)str {
const char *str2 = [str UTF8String];
printf("%s", str2);
}

+ (void *)getNewPrintMehod {
return newPrint;
}

+ (void *)getNewDladdrMethod {
return newDladdr;
}

// new NSlog method
void newNSLog(NSString *format, ...) {
printf("NSLog --------- fishhook");
printf("\n");
// new printf method
void newPrint(const char * str, ...) {
NSLog(@"printf method has replaced with NSLog");
}

// new dladdr method
Expand Down
2 changes: 2 additions & 0 deletions Demo/InsertDyld/InsertDyld.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
VALID_ARCHS = arm64;
};
name = Debug;
};
Expand Down Expand Up @@ -365,6 +366,7 @@
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
VALID_ARCHS = arm64;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<key>orderHint</key>
<integer>3</integer>
</dict>
<key>InsertDyld.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>3</integer>
</dict>
</dict>
</dict>
</plist>
6 changes: 3 additions & 3 deletions Demo/InsertDyld/InsertDyld/FishHookTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import Foundation
import MachO
import antiFishhook

// fishhook NSLog
public func fishhookNSLog(newMethod: UnsafeMutableRawPointer) {
// fishhook printf
public func fishhookPrint(newMethod: UnsafeMutableRawPointer) {
var oldMethod: UnsafeMutableRawPointer?
replaceSymbol("NSLog", newMethod: newMethod, oldMethod: &oldMethod)
replaceSymbol("printf", newMethod: newMethod, oldMethod: &oldMethod)
}

// fishhook dladdr
Expand Down
2 changes: 2 additions & 0 deletions Source/AntiFishHook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ private let __stub_helper_name: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8
// __Linkedit
private let __linkedit_seg_name: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) = (0x5f, 0x5f, 0x4c, 0x49, 0x4e, 0x4b, 0x45, 0x44, 0x49, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)

@inlinable
public func resetSymbol(_ symbol: String)
{
guard let symbolBytes = symbol.data(using: String.Encoding.utf8)?.map({ $0 }) else { return }
Expand All @@ -26,6 +27,7 @@ public func resetSymbol(_ symbol: String)
}
}

@inlinable
public func resetSymbol(_ symbol: [UInt8],
image: UnsafePointer<mach_header>,
imageSlide slide: Int)
Expand Down
3 changes: 3 additions & 0 deletions Source/FishHook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Foundation
import MachO

@inlinable
public func replaceSymbol(_ symbol: String,
newMethod: UnsafeMutableRawPointer,
oldMethod: inout UnsafeMutableRawPointer?)
Expand All @@ -21,6 +22,7 @@ public func replaceSymbol(_ symbol: String,
}
}

@inlinable
public func replaceSymbol(_ symbol: [UInt8],
image: UnsafePointer<mach_header>,
imageSlide slide: Int,
Expand All @@ -30,6 +32,7 @@ public func replaceSymbol(_ symbol: [UInt8],
rebindSymbolForImage(image, imageSlide: slide, symbolBytes: symbol, newMethod: newMethod, oldMethod: &oldMethod)
}

@inlinable
public func rebindSymbolForImage(_ image: UnsafePointer<mach_header>,
imageSlide slide: Int,
symbolBytes: [UInt8],
Expand Down
2 changes: 1 addition & 1 deletion antiFishhook.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'antiFishhook'
s.version = '0.6.0'
s.version = '0.7.0'
s.summary = 'anti-fishhook'
s.homepage = 'https://github.com/TannerJin/anti-fishhook'
s.license = { :type => "MIT" }
Expand Down

0 comments on commit 3a181b5

Please sign in to comment.