-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from kam800/master
Upstream sync
- Loading branch information
Showing
4 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
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
25 changes: 25 additions & 0 deletions
25
MachObfuscator/SymbolMangling/ExportTrieMangler/ExportTrieMangler.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,25 @@ | ||
import Foundation | ||
|
||
protocol ExportTrieMangling: AnyObject { | ||
func mangle(trie: Trie, fillingRootLabelWith labelFill: UInt8) -> Trie | ||
} | ||
|
||
final class ExportTrieMangler: ExportTrieMangling { | ||
func mangle(trie: Trie, fillingRootLabelWith labelFill: UInt8) -> Trie { | ||
var trieCopy = trie | ||
|
||
trieCopy.label = trieCopy.label.map { _ in | ||
labelFill | ||
} | ||
|
||
trieCopy.children = trieCopy.children.enumerated().map { (index, child) -> Trie in | ||
guard let labelFill = UInt8(exactly: index + 1) else { | ||
fatalError("Trie label values probably exhausted at \(child.labelRange)") | ||
} | ||
|
||
return mangle(trie: child, fillingRootLabelWith: labelFill) | ||
} | ||
|
||
return trieCopy | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
MachObfuscatorTests/SymbolManglingTests/ExportTrieMangler_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,64 @@ | ||
import XCTest | ||
|
||
class ExportTrieMangler_Tests: XCTestCase { | ||
|
||
var sut: ExportTrieMangler! | ||
|
||
var rootTrie: Trie! | ||
|
||
override func setUp() { | ||
sut = ExportTrieMangler() | ||
var childrenLayer0 = trieChildren(number: 3) | ||
var childrenLayer1 = trieChildren(number: 2) | ||
var childrenLayer2 = trieChildren(number: 2) | ||
let childrenLayer3 = trieChildren(number: 2) | ||
|
||
childrenLayer2 = assignChildren(childrenLayer3, to: childrenLayer2) | ||
childrenLayer1 = assignChildren(childrenLayer2, to: childrenLayer1) | ||
childrenLayer0 = assignChildren(childrenLayer1, to: childrenLayer0) | ||
|
||
rootTrie = Trie(exportsSymbol: false, | ||
labelRange: 0..<0, | ||
label: [], | ||
children: childrenLayer0) | ||
} | ||
|
||
override func tearDown() { | ||
sut = nil | ||
rootTrie = nil | ||
} | ||
|
||
func test_exportTrieObfuscation() { | ||
let obfuscatedTrie = sut.mangle(trie: rootTrie, fillingRootLabelWith: 0) | ||
XCTAssertEqual(obfuscatedTrie.label, []) | ||
XCTAssertEqual(obfuscatedTrie.children[0].label, [1, 1, 1]) | ||
XCTAssertEqual(obfuscatedTrie.children[0].children[1].label, [2, 2]) | ||
XCTAssertEqual(obfuscatedTrie.children[1].label, [2, 2, 2]) | ||
XCTAssertEqual(obfuscatedTrie.children[2].label, [3, 3, 3]) | ||
XCTAssertEqual(obfuscatedTrie.children[2].children[0].label, [1, 1]) | ||
XCTAssertEqual(obfuscatedTrie.children[2].children[1].label, [2, 2]) | ||
} | ||
|
||
private func trieChildren(number: Int) -> [Trie] { | ||
return (1...number).map { index in | ||
return Trie(exportsSymbol: index % 2 == 0, | ||
labelRange: 0..<UInt64(number), | ||
label: randomLabels(count: number), | ||
children: []) | ||
} | ||
} | ||
|
||
private func randomLabels(count: Int) -> [UInt8] { | ||
return (0..<count).map { _ in | ||
return (UInt8.random(in: 1...UInt8.max)) | ||
} | ||
} | ||
|
||
private func assignChildren(_ children: [Trie], to parent: [Trie]) -> [Trie] { | ||
return parent.map { (trie) in | ||
var copy = trie | ||
copy.children = children | ||
return copy | ||
} | ||
} | ||
} |