forked from kam800/MachObfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kam800#33] Blacklisting symbols using the source code
- Loading branch information
Showing
18 changed files
with
203 additions
and
120 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
49 changes: 0 additions & 49 deletions
49
MachObfuscator/HeadersParsing/SimpleHeaderSymbolsLoader.swift
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
MachObfuscator/HeadersParsing/SimpleSourceSymbolsLoader.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Foundation | ||
|
||
class SimpleSourceSymbolsLoader: SourceSymbolsLoader { | ||
func load(forFrameworkURL frameworkURL: URL) throws -> SourceSymbols { | ||
return try load(forFrameworkURL: frameworkURL, fileManager: FileManager.default) | ||
} | ||
|
||
func load(forFrameworkURL frameworkURL: URL, fileManager: FileManager) throws -> SourceSymbols { | ||
let headers = fileManager.listSourceFilesRecursively(atURL: frameworkURL) | ||
return headers.map(SourceSymbols.load(url:)).flatten() | ||
} | ||
} | ||
|
||
private extension FileManager { | ||
func listSourceFilesRecursively(atURL url: URL) -> [URL] { | ||
return listFilesRecursively(atURL: url) | ||
.filter { $0.isSourceFile } | ||
} | ||
} | ||
|
||
private extension URL { | ||
private static let sourceFileExtensionSet: Set<String> = ["h", "m"] | ||
var isSourceFile: Bool { | ||
return URL.sourceFileExtensionSet.contains(pathExtension) | ||
} | ||
} | ||
|
||
private extension SourceSymbols { | ||
static func load(url: URL) -> SourceSymbols { | ||
let sourceContents: String | ||
do { | ||
sourceContents = try String(contentsOf: url, encoding: .ascii) | ||
} catch { | ||
fatalError("Could not read \(url) because: \(error.localizedDescription)") | ||
} | ||
let selectors = Set(sourceContents.objCMethodNames) | ||
.union(sourceContents.objCPropertyNames) | ||
let classNames = Set(sourceContents.objCTypeNames) | ||
return SourceSymbols(selectors: selectors, classNames: classNames) | ||
} | ||
} | ||
|
||
extension Sequence where Element == SourceSymbols { | ||
func flatten() -> SourceSymbols { | ||
return reduce(into: SourceSymbols(selectors: [], classNames: [])) { result, nextSymbols in | ||
result.classNames.formUnion(nextSymbols.classNames) | ||
result.selectors.formUnion(nextSymbols.selectors) | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...scator/HeadersParsing/HeaderSymbols.swift → ...scator/HeadersParsing/SourceSymbols.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
struct HeaderSymbols { | ||
struct SourceSymbols { | ||
var selectors: Set<String> | ||
var classNames: Set<String> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Foundation | ||
|
||
protocol SourceSymbolsLoader { | ||
func load(forFrameworkURL frameworkURL: URL) throws -> SourceSymbols | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
MachObfuscatorTests/HeaderParsingTests/Samples/LibrarySourceCode.bundle/File1.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@interface PublicClass : NSObject | ||
|
||
@property (nonatomic, readonly) NSString *publicProperty; | ||
|
||
- (void)publicMethod; | ||
|
||
@end |
13 changes: 13 additions & 0 deletions
13
MachObfuscatorTests/HeaderParsingTests/Samples/LibrarySourceCode.bundle/File1.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#import "File1.h" | ||
|
||
@interface PrivateClass : NSObject | ||
|
||
@property (nonatomic, readonly) NSString *privateProperty; | ||
|
||
- (void)privateMethod; | ||
|
||
@end | ||
|
||
@implementation PublicClass | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...meworkURL_allSystemFrameworks_Tests.swift → ...meworkURL_allSystemFrameworks_Tests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...fuscatorTests/HeaderParsingTests/SimpleSourceSymbolsLoader_loadFromSourcesURL_Tests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import XCTest | ||
|
||
class SimpleSourceSymbolsLoader_loadFromSourcesURL_Tests: XCTestCase { | ||
|
||
var symbols: SourceSymbols! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
let sut = SimpleSourceSymbolsLoader() | ||
symbols = try! sut.load(forFrameworkURL: URL.librarySourceCode) | ||
} | ||
|
||
override func tearDown() { | ||
symbols = nil | ||
|
||
super.tearDown() | ||
} | ||
|
||
func test_shouldParseSelectors() { | ||
let expectedMethods: Set<String> = [ | ||
"publicMethod", | ||
"privateMethod", | ||
] | ||
|
||
let expectedPropertyNames: Set<String> = [ | ||
"publicProperty", | ||
"privateProperty", | ||
] | ||
|
||
let expectedSelectors = | ||
expectedMethods.union(expectedPropertyNames) | ||
|
||
expectedSelectors.forEach { | ||
XCTAssert(symbols.selectors.contains($0), "Should contain: \($0)") | ||
} | ||
let unexpectedSelectors = symbols.selectors.subtracting(expectedSelectors) | ||
XCTAssertEqual(unexpectedSelectors, [], "Detected unexpected selectors") | ||
} | ||
|
||
func test_shouldParceClassNames() { | ||
let expectedClassNames: Set<String> = [ | ||
"PublicClass", | ||
"PrivateClass", | ||
] | ||
XCTAssertEqual(symbols.classNames.symmetricDifference(expectedClassNames), []) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.