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
Prev Previous commit
Next Next commit
Add -clear-suffix-tags argument
  • Loading branch information
hadiidbouk committed Nov 7, 2018
commit 25ba5aa420230d0b4df82e16fdf2c330e73d6f8a
43 changes: 34 additions & 9 deletions swiftshield-Sources/AutomaticSwiftShield.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class AutomaticSwiftShield: Protector {
let projectBuilder = XcodeProjectBuilder(projectToBuild: projectToBuild, schemeToBuild: schemeToBuild, modulesToIgnore: modulesToIgnore)
let modules = projectBuilder.getModulesAndCompilerArguments()
let obfuscationData = AutomaticObfuscationData(modules: modules)
index(obfuscationData: obfuscationData)
index(obfuscationData: obfuscationData, shouldRemoveSuffixTags: false)
findReferencesInIndexed(obfuscationData: obfuscationData)
if obfuscationData.referencesDict.isEmpty {
Logger.log(.foundNothingError)
Expand All @@ -53,7 +53,7 @@ class AutomaticSwiftShield: Protector {
return obfuscationData
}

func index(obfuscationData: AutomaticObfuscationData) {
func index(obfuscationData: AutomaticObfuscationData, shouldRemoveSuffixTags: Bool) {
let sourceKit = SourceKit()
var fileDataArray: [(file: File, module: Module)] = []
for module in obfuscationData.modules {
Expand All @@ -71,7 +71,8 @@ class AutomaticSwiftShield: Protector {
sourceKit.recurseOver(childID: sourceKit.entitiesID, resp: dict) { [unowned self] dict in
guard let data = self.getNameData(from: dict,
obfuscationData: obfuscationData,
sourceKit: sourceKit) else {
sourceKit: sourceKit,
shouldRemoveSuffixTags: shouldRemoveSuffixTags) else {
return
}
let name = data.name
Expand All @@ -97,6 +98,20 @@ class AutomaticSwiftShield: Protector {
}
writeToFile(data: data, path: path, info: "Automatic mode for \(path)")
}

func removeSuffixTags() {
Copy link
Owner

Choose a reason for hiding this comment

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

I do not understand what this is supposed to be doing, is it a leftover from something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

leftover, I will remove it.

let projectBuilder = XcodeProjectBuilder(projectToBuild: projectToBuild, schemeToBuild: schemeToBuild, modulesToIgnore: modulesToIgnore)
let modules = projectBuilder.getModulesAndCompilerArguments()
let obfuscationData = AutomaticObfuscationData(modules: modules)
index(obfuscationData: obfuscationData, shouldRemoveSuffixTags: true)
findReferencesInIndexed(obfuscationData: obfuscationData)
if obfuscationData.referencesDict.isEmpty {
Logger.log(.foundNothingError)
exit(error: true)
}
obfuscateNSPrincipalClassPlists(obfuscationData: obfuscationData)
overwriteFiles(obfuscationData: obfuscationData)
}
}

extension AutomaticSwiftShield {
Expand All @@ -109,7 +124,10 @@ extension AutomaticSwiftShield {
return resp
}

private func getNameData(from dict: sourcekitd_variant_t, obfuscationData: ObfuscationData, sourceKit: SourceKit) -> (name: String, usr: String, obfuscatedName: String)? {
private func getNameData(from dict: sourcekitd_variant_t,
obfuscationData: ObfuscationData,
sourceKit: SourceKit,
shouldRemoveSuffixTags: Bool) -> (name: String, usr: String, obfuscatedName: String)? {
let kind = dict.getUUIDString(key: sourceKit.kindID)
guard sourceKit.declarationType(for: kind) != nil else {
return nil
Expand All @@ -122,19 +140,26 @@ extension AutomaticSwiftShield {
return nil
}

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

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

guard let protected = obfuscationData.obfuscationDict[name] else {
let newName = String.random(length: self.protectedClassNameSize, excluding: obfuscationData.allObfuscatedNames)
obfuscationData.obfuscationDict[name] = newName
return (name, usr, newName)
if !shouldRemoveSuffixTags {
let newName = String.random(length: self.protectedClassNameSize, excluding: obfuscationData.allObfuscatedNames)
obfuscationData.obfuscationDict[name] = newName
return (name, usr, newName)
} else {
let newName = name.components(separatedBy: self.excludedSuffixTag).first!
obfuscationData.obfuscationDict[name] = newName
return (name, usr, newName)
}
}

return (name, usr, protected)
}

Expand Down
5 changes: 5 additions & 0 deletions swiftshield-Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ protector.protectStoryboards(data: obfuscationData)
protector.writeToFile(data: obfuscationData)
protector.markProjectsAsProtected()
Logger.log(.finished)

if automatic && CommandLine.arguments.contains("-clear-suffix-tags") {
Copy link
Owner

Choose a reason for hiding this comment

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

I think that's also a leftover right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

leftover, I will remove it.

(protector as! AutomaticSwiftShield).removeSuffixTags()
}

exit()