Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude files or code from obfuscation #31

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@

*.profraw
.DS_Store
Index/DataStore/*
Build/Intermediates.noindex/*
Binary file added Build/Products/Debug/swiftshield
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ class AutomaticSwiftShieldTests: XCTestCase {
ReferenceData(name: "MyType", line: 6, column: 28),
ReferenceData(name: "ViewController", line: 10, column: 15),
ReferenceData(name: "fakeMethod", line: 10, column: 30)]

let originalFileData = loadFile("MockOriginalFile", ofType: "txt")
let originalFile = String(data: originalFileData, encoding: .utf8)!
let obfuscatedFile = AutomaticSwiftShield(basePath: "abc", projectToBuild: "abc", schemeToBuild: "abc", modulesToIgnore: [], protectedClassNameSize: 0).generateObfuscatedFile(fromString: originalFile, references: references, obfuscationData: obfuscationData)
let obfuscatedFile = AutomaticSwiftShield(basePath: "abc", projectToBuild: "abc", schemeToBuild: "abc", modulesToIgnore: [], classesToIgnore: [], protectedClassNameSize: 0, excludedPrefixTag: "", excludedSuffixTag: "").generateObfuscatedFile(fromString: originalFile, references: references, obfuscationData: obfuscationData)
let expectedFileData = loadFile("MockObfuscatedFile", ofType: "txt")
let expectedFile = String(data: expectedFileData, encoding: .utf8)!
XCTAssertEqual(obfuscatedFile, expectedFile)
}

func testPlistExtractor() {
let protector = AutomaticSwiftShield(basePath: "abc", projectToBuild: "abc", schemeToBuild: "abc", modulesToIgnore: [], protectedClassNameSize: 0)
let protector = AutomaticSwiftShield(basePath: "abc", projectToBuild: "abc", schemeToBuild: "abc", modulesToIgnore: [], classesToIgnore: [], protectedClassNameSize: 0, excludedPrefixTag: "", excludedSuffixTag: "")
let plist = path(for: "MockPlist", ofType: "plist")
let file = File(filePath: plist)
let data = protector.getPlistVersionAndNumber(file)!
Expand All @@ -45,7 +46,7 @@ class AutomaticSwiftShieldTests: XCTestCase {
}

func testPlistPrincipalClassObfuscation() {
let protector = AutomaticSwiftShield(basePath: "abc", projectToBuild: "abc", schemeToBuild: "abc", modulesToIgnore: [], protectedClassNameSize: 0)
let protector = AutomaticSwiftShield(basePath: "abc", projectToBuild: "abc", schemeToBuild: "abc", modulesToIgnore: [], classesToIgnore: [], protectedClassNameSize: 0, excludedPrefixTag: "", excludedSuffixTag: "")
let plist = path(for: "MockPlist", ofType: "plist")
let file = MockFile(path: plist)
let obfuscationData = AutomaticObfuscationData(modules: [Module(name: "mock", plists: [file])])
Expand Down
26 changes: 24 additions & 2 deletions swiftshield-Sources/AutomaticSwiftShield.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ class AutomaticSwiftShield: Protector {
let projectToBuild: String
let schemeToBuild: String
let modulesToIgnore: Set<String>

let filesToIgnore: Set<String>
let excludedPrefixTag: String
let excludedSuffixTag: String

var isWorkspace: Bool {
return projectToBuild.hasSuffix(".xcworkspace")
}
Expand All @@ -14,10 +17,16 @@ class AutomaticSwiftShield: Protector {
projectToBuild: String,
schemeToBuild: String,
modulesToIgnore: Set<String>,
protectedClassNameSize: Int) {
classesToIgnore: Set<String>,
protectedClassNameSize: Int,
excludedPrefixTag: String,
excludedSuffixTag: String) {
self.projectToBuild = projectToBuild
self.schemeToBuild = schemeToBuild
self.modulesToIgnore = modulesToIgnore
self.filesToIgnore = classesToIgnore
self.excludedPrefixTag = excludedPrefixTag
self.excludedSuffixTag = excludedSuffixTag
super.init(basePath: basePath, protectedClassNameSize: protectedClassNameSize)
if self.schemeToBuild.isEmpty || self.projectToBuild.isEmpty {
Logger.log(.helpText)
Expand Down Expand Up @@ -108,6 +117,19 @@ extension AutomaticSwiftShield {
guard let name = dict.getString(key: sourceKit.nameID)?.trueName, let usr = dict.getString(key: sourceKit.usrID) else {
return nil
}

if self.filesToIgnore.contains(name) {
return nil
}

if self.excludedPrefixTag != "" && name.hasPrefix(self.excludedPrefixTag) {
return nil
}

if self.excludedSuffixTag != "" && name.hasSuffix(excludedSuffixTag) {
return nil
}

guard let protected = obfuscationData.obfuscationDict[name] else {
let newName = String.random(length: self.protectedClassNameSize, excluding: obfuscationData.allObfuscatedNames)
obfuscationData.obfuscationDict[name] = newName
Expand Down
6 changes: 3 additions & 3 deletions swiftshield-Sources/SourceKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ final class SourceKit {
"struct",
"protocol":
return .object
// case "var.instance",
// "var.class":
// return .property
case "var.instance",
"var.class":
return .property
case "function.free",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you enable property, confirm no storyboard property in use

"function.method.instance",
"function.method.static",
Expand Down
6 changes: 5 additions & 1 deletion swiftshield-Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ if automatic {
let schemeToBuild = UserDefaults.standard.string(forKey: "automatic-project-scheme") ?? ""
let projectToBuild = UserDefaults.standard.string(forKey: "automatic-project-file") ?? ""
let modulesToIgnore = UserDefaults.standard.string(forKey: "ignore-modules")?.components(separatedBy: ",") ?? []
protector = AutomaticSwiftShield(basePath: basePath, projectToBuild: projectToBuild, schemeToBuild: schemeToBuild, modulesToIgnore: Set(modulesToIgnore), protectedClassNameSize: protectedClassNameSize)
let excludedSuffixTag = UserDefaults.standard.string(forKey: "excluded-suffix-tag") ?? ""
let excludedPrefixTag = UserDefaults.standard.string(forKey: "excluded-prefix-tag") ?? ""
let filesToIgnore = UserDefaults.standard.string(forKey: "ignore-files")?.components(separatedBy: ",") ?? []

protector = AutomaticSwiftShield(basePath: basePath, projectToBuild: projectToBuild, schemeToBuild: schemeToBuild, modulesToIgnore: Set(modulesToIgnore), classesToIgnore: Set(filesToIgnore), protectedClassNameSize: protectedClassNameSize, excludedPrefixTag: excludedPrefixTag, excludedSuffixTag: excludedSuffixTag)
} else {
let tag = UserDefaults.standard.string(forKey: "tag") ?? "__s"
protector = ManualSwiftShield(basePath: basePath, tag: tag, protectedClassNameSize: protectedClassNameSize)
Expand Down
Binary file not shown.