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 all 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
70 changes: 63 additions & 7 deletions swiftshield-Sources/AutomaticSwiftShield.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class AutomaticSwiftShield: Protector {
let projectToBuild: String
let schemeToBuild: String
let modulesToIgnore: Set<String>

var publicProtocols: Set<String>!
Copy link
Owner

Choose a reason for hiding this comment

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

Do not use force unwrap - If storing the protocols here are needed, we can pre-init it for safety reasons 🙂
var publicProtocols: Set<String> = []

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

let excludePublic: Bool

var isWorkspace: Bool {
return projectToBuild.hasSuffix(".xcworkspace")
}
Expand All @@ -14,10 +16,12 @@ class AutomaticSwiftShield: Protector {
projectToBuild: String,
schemeToBuild: String,
modulesToIgnore: Set<String>,
protectedClassNameSize: Int) {
protectedClassNameSize: Int,
excludePublic: Bool) {
self.projectToBuild = projectToBuild
self.schemeToBuild = schemeToBuild
self.modulesToIgnore = modulesToIgnore
self.excludePublic = excludePublic
super.init(basePath: basePath, protectedClassNameSize: protectedClassNameSize)
if self.schemeToBuild.isEmpty || self.projectToBuild.isEmpty {
Logger.log(.helpText)
Expand All @@ -33,7 +37,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 @@ -44,7 +48,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 @@ -59,10 +63,14 @@ class AutomaticSwiftShield: Protector {
Logger.log(.indexing(file: file))
let resp = index(sourceKit: sourceKit, file: file, args: compilerArgs)
let dict = SKApi.sourcekitd_response_get_value(resp)

publicProtocols = Set<String>()

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 @@ -88,6 +96,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 @@ -100,22 +122,56 @@ 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
}
guard let name = dict.getString(key: sourceKit.nameID)?.trueName, let usr = dict.getString(key: sourceKit.usrID) else {
return nil
}

if excludePublic {
Copy link
Owner

Choose a reason for hiding this comment

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

To avoid making getNameData get super big, we can move this logic to a separate shouldIgnoreSymbol()-like method, makes sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

let attributesDict = SKApi.sourcekitd_variant_dictionary_get_value(dict, sourceKit.attributesID)
let attributesData = dict.getAttributes(dict: attributesDict, subKey: sourceKit.attributeID)


//Check if variant is public
Copy link
Owner

Choose a reason for hiding this comment

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

Let's avoid comments unless explaining an intent is needed - what is happening should be clear enough through the name of the properties we're dealing with.

let isPublic = attributesData.filter { item in
return item.contains("public")
Copy link
Owner

Choose a reason for hiding this comment

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

I would avoid dealing with strings directly. We can make an enum to handle visibility identifiers. I already did that for kinds, so we can follow the same pattern 🙂

}.count != 0

//Add to publicProtocols array
if kind == "source.lang.swift.decl.protocol" && isPublic {
Copy link
Owner

Choose a reason for hiding this comment

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

Same thing here (not dealing with strings directly), SKAPI already has a method to return what an identifier is - I think we just need to add the protocol case.

publicProtocols.insert(name)
}

//Don't Obfuscate public methods/properties..
if isPublic {
return nil
}

//Handle public protocol's functions
for protocolName in publicProtocols {
if usr.contains(protocolName) {
Copy link
Owner

Choose a reason for hiding this comment

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

If I understood this correctly, shouldn't this be simply publicProtocols.contains(usr) instead of a loop? This is running in O(nˆ2)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, because the receiver_usr isn't returning just a name, It's something like that
s:17ObfuscationSource14ViewControllerC5countSivp

Copy link
Owner

Choose a reason for hiding this comment

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

I see now. The problem is that using contains inside an usr will likely not work in a bigger app because nothing stops other modules from having private protocols with the same name as your public one. I think you need to store the protocol usrs in publicProtocols instead of their names to avoid this loop.

But I'm getting some bad vibes, is this deterministic? I'm having the impression that a public protocol's method would be wrongly obfuscated if the order of the files changed. If you look at isReferencingInternal(), I had to even make it recursive to make it work. Things are slightly more difficult here because it happens before everything, but I think it's gonna work nicely in the end.

return nil
}
}
}
guard let protected = obfuscationData.obfuscationDict[name] else {
//if !shouldRemoveSuffixTags {
Copy link
Owner

Choose a reason for hiding this comment

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

We can remove these leftovers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will remove it.

let newName = String.random(length: self.protectedClassNameSize, excluding: obfuscationData.allObfuscatedNames)
obfuscationData.obfuscationDict[name] = newName
return (name, usr, newName)
}

return (name, usr, protected)
}

func findReferencesInIndexed(obfuscationData: AutomaticObfuscationData) {
let SK = SourceKit()
Logger.log(.searchingReferencesOfUsr)
Expand Down
14 changes: 14 additions & 0 deletions swiftshield-Sources/SourceKit+Variant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,18 @@ extension sourcekitd_variant_t {
func getDictionary(key: sourcekitd_uid_t) -> sourcekitd_variant_t {
return SKApi.sourcekitd_variant_dictionary_get_value(self, key)
}

func getAttributes(dict: sourcekitd_variant_t, subKey: sourcekitd_uid_t) -> [String] {

var data = [String]()

let _ = SKApi.sourcekitd_variant_array_apply(dict) { (_, attributesDict) in
Copy link

Choose a reason for hiding this comment

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

Is this safe? Wouldn't it crash on none-array dict?


let accessControl = attributesDict.getUUIDString(key: subKey)
data.append(accessControl)
return true
}

return data
}
}
11 changes: 7 additions & 4 deletions swiftshield-Sources/SourceKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ final class SourceKit {
"struct",
"protocol":
return .object
// case "var.instance",
// "var.class":
// return .property
case "var.instance",
"var.static",
"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 Expand Up @@ -73,7 +74,9 @@ final class SourceKit {
lazy var lineID = SKApi.sourcekitd_uid_get_from_cstr("key.line")!
lazy var colID = SKApi.sourcekitd_uid_get_from_cstr("key.column")!
lazy var usrID = SKApi.sourcekitd_uid_get_from_cstr("key.usr")!

lazy var attributesID = SKApi.sourcekitd_uid_get_from_cstr("key.attributes")!
lazy var attributeID = SKApi.sourcekitd_uid_get_from_cstr("key.attribute")!

func array(argv: [String]) -> sourcekitd_object_t {
let objects = argv.map { SKApi.sourcekitd_request_string_create($0) }
return SKApi.sourcekitd_request_array_create(objects, objects.count)!
Expand Down
9 changes: 8 additions & 1 deletion swiftshield-Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ 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 excludePublic = CommandLine.arguments.contains("-exclude-public")

protector = AutomaticSwiftShield(basePath: basePath, projectToBuild: projectToBuild, schemeToBuild: schemeToBuild, modulesToIgnore: Set(modulesToIgnore), protectedClassNameSize: protectedClassNameSize, excludePublic: excludePublic)
} else {
let tag = UserDefaults.standard.string(forKey: "tag") ?? "__s"
protector = ManualSwiftShield(basePath: basePath, tag: tag, protectedClassNameSize: protectedClassNameSize)
Expand All @@ -53,4 +55,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()
Binary file not shown.