A highly configurable and performant tool for obfuscating Swift literals embedded in the application code that you should protect from static code analysis, making the app more resistant to reverse engineering.
Simply integrate the tool with your Swift package or Xcode project, configure your own obfuscation algorithm along with the list of secret literals, and build the project 🚀
Swift Confidential can save you a lot of time, especially if you are developing an iOS app and seeking to meet OWASP MASVS-RESILIENCE requirements.
Motivation
Pretty much every single app has at least few literals embedded in code, those include: URLs, various client identifiers (e.g. API keys or API tokens), pinning data (e.g. PEM certificates or SPKI digests), Keychain item identifiers, RASP-related literals (e.g. list of suspicious dylibs or list of suspicious file paths for jailbreak detection), and many other context-specific literals. While the listed examples of code literals might seem innocent, not obfuscating them, in many cases, can be considered as giving a handshake to the potential threat actor. This is especially true in security-sensitive apps, such as mobile banking apps, 2FA authenticator apps and password managers. As a responsible software engineer, you should be aware that extracting source code literals from the app package is generally easy enough that even less expirienced malicious users can accomplish this with little effort.
A sneak peek at the __TEXT.__cstring section in a sample Mach-O file reveals a lot of interesting information about the app.
This tool aims to provide an elegant and maintainable solution to the above problem by introducing the composable obfuscation techniques that can be freely combined to form an algorithm for obfuscating selected Swift literals.
NOTE: While Swift Confidential certainly makes the static analysis of the code more challenging, it is by no means the only code hardening technique that you should employ to protect your app against reverse engineering and tampering. To achieve a decent level of security, we highly encourage you to supplement this tool’s security measures with runtime application self-protection (RASP) checks, as well as Swift code obfuscation. With that said, no security measure can ever guarantee absolute security. Any motivated and skilled enough attacker will eventually bypass all security protections. For this reason, always keep your threat models up to date.
Getting started
Begin by creating a confidential.yml YAML configuration file in the root directory of your SwiftPM target’s sources or Xcode project (depending on the preferred installation method). At minimum, the configuration must contain obfuscation algorithm and one or more secret definitions.
For example, a configuration file for the hypothetical RASP module could look like this:
WARNING: The algorithm from the above configuration serves as example only, do not use this particular algorithm in your production code. Instead, compose your own algorithm from the obfuscation techniques described below and don’t share your algorithm with anyone. Moreover, following the secure SDLC best practices, consider not to commit the production algorithm in your repository, but instead configure your CI/CD pipeline to run a custom script (ideally just before the build step), which will modify the configuration file by replacing the algorithm value with the one retrieved from the secrets vault.
You can then, for example, iterate over a deobfuscated array of suspicious dynamic libraries in your own code using the projected value of the generated suspiciousDynamicLibraries property:
let suspiciousLibraries = RASP.Literals.$suspiciousDynamicLibraries
.map { $0.lowercased() }
let checkPassed = loadedLibraries
.allSatisfy { !suspiciousLibraries.contains(where: $0.lowercased().contains) }
Installation
Swift Confidential can be used with both SwiftPM and Xcode targets, depending on your needs. Please see the relevant section below for detailed installation instructions.
SwiftPM
To use Swift Confidential with your SwiftPM target, add the ConfidentialKit library along with Confidential plugin to the package’s dependencies and then to your target’s dependencies and plugins respectively:
Please make sure to add a path to the confidential.yml configuration file to target’s exclude list to explicitly exclude this file from the target’s resources.
Xcode
To integrate Swift Confidential directly with your Xcode target:
Add swift-confidential and swift-confidential-plugin packages to your Xcode project. Please refer to the official documentation for step-by-step instructions on how to add package dependencies. When asked to choose swift-confidential package products to be added to your target, make sure to select the ConfidentialKit library.
Then, navigate to your target’s Build Phases pane, and in the Run Build Tool Plug-ins section, click the + button, select the Confidential plugin, and click the Add button.
For convenience, you can also add the confidential.yml configuration file to your Xcode project, but be sure not to add it to any of the Xcode targets.
Once set up, build your target and the Confidential plugin will automatically generate a Swift source file with obfuscated secret literals. In addition, the plugin will regenerate the obfuscated secret literals every time it detects a change to confidential.yml configuration file or when you clean build your project.
NOTE: Make sure to use the same version requirements for both swift-confidential and swift-confidential-plugin packages. See Versioning section for more information about API stability.
Configuration
Swift Confidential supports a number of configuration options, all of which are stored in a single YAML configuration file.
YAML configuration keys
The table below lists the keys to include in the configuration file along with the type of information to include in each. Any other keys in the configuration file are ignored by the CLI tool.
Key
Value type
Description
algorithm
List of strings
The list of obfuscation techniques representing individual steps that are composed together to form the obfuscation algorithm. See Obfuscation techniques section for usage details. Required.
defaultAccessModifier
String
The default access modifier applied to each generated secret literal, unless the secret definition states otherwise. The default value is internal. See Access modifiers section for usage details.
defaultNamespace
String
The default namespace in which to enclose all the generated secret literals without explicitly assigned namespace. The default value is extend Obfuscation.Secret from ConfidentialKit. See Namespaces section for usage details.
secrets
List of objects
The list of objects defining the secret literals to be obfuscated. See Secrets section for usage details. Required.
WARNING: The algorithm from the above configuration serves as example only, do not use this particular algorithm in your production code.
Obfuscation techniques
The obfuscation techniques are the composable building blocks from which you can create your own obfuscation algorithm. You can compose them in any order you want, so that no one exept you knows how the secret literals are obfuscated.
Compression
This technique involves data compression using the algorithm of your choice. In general, the compression technique is non-polymorphic, meaning that given the same input data, the same output data is produced with each run. However, Swift Confidential applies additional polymorphic obfuscation routines to mask the bytes identifying the compression algorithm used.
Syntax
compress using <algorithm>
The supported algorithms are shown in the following table:
| Algorithm | Description |
|——————|———————————————————–|
| lzfse | The LZFSE compression algorithm. |
| lz4 | The LZ4 compression algorithm. |
| lzma | The LZMA compression algorithm. |
| zlib | The zlib compression algorithm. |
Encryption
This technique involves data encryption using the algorithm of your choice. The encryption technique is polymorphic, meaning that given the same input data, different output data is produced with each run.
Syntax
encrypt using <algorithm>
The supported algorithms are shown in the following table:
| Algorithm | Description |
|——————|————————————————————————————————-|
| aes-128-gcm | The Advanced Encryption Standard (AES) algorithm in Galois/Counter Mode (GCM) with 128-bit key. |
| aes-192-gcm | The Advanced Encryption Standard (AES) algorithm in Galois/Counter Mode (GCM) with 192-bit key. |
| aes-256-gcm | The Advanced Encryption Standard (AES) algorithm in Galois/Counter Mode (GCM) with 256-bit key. |
| chacha20-poly | The ChaCha20-Poly1305 algorithm. |
Randomization
This technique involves data randomization. The randomization technique is polymorphic, meaning that given the same input data, different output data is produced with each run.
NOTE: Randomization technique is best suited for secrets of which size does not exceed 256 bytes.
For larger secrets, the size of the obfuscated data will grow from 2N to 3N, where N is the input data size in bytes,
or even 5N (32-bit platform) or 9N (64-bit platform) if the size of input data is larger than 65 536 bytes.
For this reason, the internal implementation of this technique is a subject to change in next releases.
Syntax
shuffle
Secrets
The configuration file utilizes YAML objects to describe the secret literals, which are to be obfuscated. The table below lists the keys to define secret literal along with the type of information to include in each.
Key
Value type
Description
accessModifier
String
The access modifier of the generated Swift property containing obfuscated secret literal’s data. The supported values are internal and public. If not specified, the top-level defaultAccessModifier value is used. See Access modifiers section for usage details.
name
String
The name of the generated Swift property containing obfuscated secret literal’s data. This value is used as-is, without validity checking. Thus, make sure to use a valid property name. Required.
namespace
String
The namespace in which to enclose the generated secret literal declaration. See Namespaces section for usage details.
value
String or List of strings
The plain value of the secret literal, which is to be obfuscated. The YAML data types are mapped to String and Array<String> in Swift, respectively. Required.
Example secret definition
Supposing that you would like to obfuscate the tag used to reference the private key stored in Keychain or Secure Enclave:
The above YAML secret definition will result in the following Swift code being generated:
import Crypto
// ... other imports
extension Crypto.KeychainAccess.Key {
@ConfidentialKit.Obfuscated<Swift.String>(deobfuscateData)
internal static var secretVaultKeyTag: ConfidentialKit.Obfuscation.Secret = .init(data: [/* obfuscated data */], nonce: /* cryptographically secure random number */)
// ... other secret declarations
}
You may also need to obfuscate a list of related values, such as a list of trusted SPKI digests to pin against:
name: trustedSPKIDigests
value:
- 7a6820614ee600bbaed493522c221c0d9095f3b4d7839415ffab16cbf61767ad
- cf84a70a41072a42d0f25580b5cb54d6a9de45db824bbb7ba85d541b099fd49f
- c1a5d45809269301993d028313a5c4a5d8b2f56de9725d4d1af9da1ccf186f30
accessModifier: public
namespace: extend Pinning from Crypto
With the above YAML secret definition, the following Swift code will be generated:
import Crypto
// ... other imports
extension Crypto.Pinning {
@ConfidentialKit.Obfuscated<Swift.Array<Swift.String>>(deobfuscateData)
public static var trustedSPKIDigests: ConfidentialKit.Obfuscation.Secret = .init(data: [/* obfuscated data */], nonce: /* cryptographically secure random number */)
// ... other secret declarations
}
Namespaces
In accordance with Swift programming best practices, Swift Confidential encapsulates generated secret literal declarations in namespaces (typically caseless enums). The namespaces syntax allows you to either create a new namespace or extend an existing one.
Syntax
create <namespace> # creates new namespace
extend <namespace> [from <module>] # extends existing namespace, optionally specifying
# the module to which this namespace belongs
Example usage
Assuming that you would like to keep the generated secret literal declaration(s) in a new namespace named Secrets, use the following YAML code:
create Secrets
The above namespace definition will result in the following Swift code being generated:
NOTE: The creation of the nested namespaces is currently not supported.
If, however, you would rather like to keep the generated secret literal declaration(s) in an existing namespace named Pinning and imported from Crypto module, use the following YAML code instead:
extend Pinning from Crypto
With the above namespace definition, the following Swift code will be generated:
You can specify the access modifiers for generated Swift code, both globally and on per secret basis. Yet, the general recommendation is to use the default internal access level, so as to keep your code well encapsulated.
Syntax
<access_modifier>
The supported access modifiers are shown in the following table:
| Access modifier | Description |
|——————|————————————————————————————————-|
| internal | The generated declarations are accessible only within their defining module. |
| public | The generated declarations are accessible within their defining module and any module that imports the defining module. |
Example usage
Supposing that you would like to keep all your secret literals in a single shared Swift module used by other project modules/targets, you can do so with a configuration similar to this one:
WARNING: The algorithm from the above configuration serves as example only, do not use this particular algorithm in your production code.
With defaultAccessModifier set to public, all of the Swift properties generated based on the secrets list are accessible outside their defining module:
import ConfidentialKit
import Foundation
public enum Secrets {
@ConfidentialKit.Obfuscated<Swift.String>(deobfuscateData)
public static var apiKey: ConfidentialKit.Obfuscation.Secret = .init(data: [/* obfuscated data */], nonce: /* cryptographically secure random number */)
@ConfidentialKit.Obfuscated<Swift.Array<Swift.String>>(deobfuscateData)
public static var trustedSPKIDigests: ConfidentialKit.Obfuscation.Secret = .init(data: [/* obfuscated data */], nonce: /* cryptographically secure random number */)
// ...
}
Additionally, if you need more fine-grained control, you can override defaultAccessModifier by specifying the access modifier in the secret definition as described in Secrets section.
Additional considerations for Confidential Swift Package plugin
The Confidential plugin expects the configuration file to be named confidential.yml or confidential.yaml, and it assumes a single configuration file per SwiftPM target/Xcode project. If you use the plugin with SwiftPM target and you define multiple configuration files in different subdirectories, then the plugin will use the first one it finds, and which one is undefined. Whereas, if you apply the plugin to the Xcode project’s target, the configuration file is expected to be located in the project’s top-level directory (all other configuration files are ignored).
Versioning
This project follows semantic versioning. While still in major version 0, source-stability is only guaranteed within minor versions (e.g. between 0.2.0 and 0.2.1). If you want to guard against potentially source-breaking package updates, you can specify your package dependency using source control requirement (e.g. .upToNextMinor(from: "0.2.0")).
License
This tool and code is released under Apache License v2.0 with Runtime Library Exception.
Please see LICENSE for more information.
Swift Confidential
A highly configurable and performant tool for obfuscating Swift literals embedded in the application code that you should protect from static code analysis, making the app more resistant to reverse engineering.
Simply integrate the tool with your Swift package or Xcode project, configure your own obfuscation algorithm along with the list of secret literals, and build the project 🚀
Swift Confidential can save you a lot of time, especially if you are developing an iOS app and seeking to meet OWASP MASVS-RESILIENCE requirements.
Motivation
Pretty much every single app has at least few literals embedded in code, those include: URLs, various client identifiers (e.g. API keys or API tokens), pinning data (e.g. PEM certificates or SPKI digests), Keychain item identifiers, RASP-related literals (e.g. list of suspicious dylibs or list of suspicious file paths for jailbreak detection), and many other context-specific literals. While the listed examples of code literals might seem innocent, not obfuscating them, in many cases, can be considered as giving a handshake to the potential threat actor. This is especially true in security-sensitive apps, such as mobile banking apps, 2FA authenticator apps and password managers. As a responsible software engineer, you should be aware that extracting source code literals from the app package is generally easy enough that even less expirienced malicious users can accomplish this with little effort.
This tool aims to provide an elegant and maintainable solution to the above problem by introducing the composable obfuscation techniques that can be freely combined to form an algorithm for obfuscating selected Swift literals.
Getting started
Begin by creating a
confidential.yml
YAML configuration file in the root directory of your SwiftPM target’s sources or Xcode project (depending on the preferred installation method). At minimum, the configuration must contain obfuscation algorithm and one or more secret definitions.For example, a configuration file for the hypothetical
RASP
module could look like this:Having created the configuration file, you can use the Confidential build tool plugin (see Installation section below) to generate Swift code with obfuscated secret literals.
Under the hood, the Confidential plugin invokes the
swift-confidential
CLI tool by issuing the following command:Upon successful command execution, the generated
Confidential.generated.swift
file will contain code similar to the following:You can then, for example, iterate over a deobfuscated array of suspicious dynamic libraries in your own code using the projected value of the generated
suspiciousDynamicLibraries
property:Installation
Swift Confidential can be used with both SwiftPM and Xcode targets, depending on your needs. Please see the relevant section below for detailed installation instructions.
SwiftPM
To use Swift Confidential with your SwiftPM target, add the
ConfidentialKit
library along withConfidential
plugin to the package’s dependencies and then to your target’s dependencies and plugins respectively:Please make sure to add a path to the
confidential.yml
configuration file to target’sexclude
list to explicitly exclude this file from the target’s resources.Xcode
To integrate Swift Confidential directly with your Xcode target:
swift-confidential
andswift-confidential-plugin
packages to your Xcode project. Please refer to the official documentation for step-by-step instructions on how to add package dependencies. When asked to chooseswift-confidential
package products to be added to your target, make sure to select theConfidentialKit
library.Build Phases
pane, and in theRun Build Tool Plug-ins
section, click the+
button, select theConfidential
plugin, and click theAdd
button.For convenience, you can also add the
confidential.yml
configuration file to your Xcode project, but be sure not to add it to any of the Xcode targets.Once set up, build your target and the Confidential plugin will automatically generate a Swift source file with obfuscated secret literals. In addition, the plugin will regenerate the obfuscated secret literals every time it detects a change to
confidential.yml
configuration file or when you clean build your project.Configuration
Swift Confidential supports a number of configuration options, all of which are stored in a single YAML configuration file.
YAML configuration keys
The table below lists the keys to include in the configuration file along with the type of information to include in each. Any other keys in the configuration file are ignored by the CLI tool.
Required.
internal
. See Access modifiers section for usage details.extend Obfuscation.Secret from ConfidentialKit
. See Namespaces section for usage details.Required.
Example configuration
Obfuscation techniques
The obfuscation techniques are the composable building blocks from which you can create your own obfuscation algorithm. You can compose them in any order you want, so that no one exept you knows how the secret literals are obfuscated.
Compression
This technique involves data compression using the algorithm of your choice. In general, the compression technique is non-polymorphic, meaning that given the same input data, the same output data is produced with each run. However, Swift Confidential applies additional polymorphic obfuscation routines to mask the bytes identifying the compression algorithm used.
Syntax
The supported algorithms are shown in the following table: | Algorithm | Description | |——————|———————————————————–| | lzfse | The LZFSE compression algorithm. | | lz4 | The LZ4 compression algorithm. | | lzma | The LZMA compression algorithm. | | zlib | The zlib compression algorithm. |
Encryption
This technique involves data encryption using the algorithm of your choice. The encryption technique is polymorphic, meaning that given the same input data, different output data is produced with each run.
Syntax
The supported algorithms are shown in the following table: | Algorithm | Description | |——————|————————————————————————————————-| | aes-128-gcm | The Advanced Encryption Standard (AES) algorithm in Galois/Counter Mode (GCM) with 128-bit key. | | aes-192-gcm | The Advanced Encryption Standard (AES) algorithm in Galois/Counter Mode (GCM) with 192-bit key. | | aes-256-gcm | The Advanced Encryption Standard (AES) algorithm in Galois/Counter Mode (GCM) with 256-bit key. | | chacha20-poly | The ChaCha20-Poly1305 algorithm. |
Randomization
This technique involves data randomization. The randomization technique is polymorphic, meaning that given the same input data, different output data is produced with each run.
Syntax
Secrets
The configuration file utilizes YAML objects to describe the secret literals, which are to be obfuscated. The table below lists the keys to define secret literal along with the type of information to include in each.
internal
andpublic
. If not specified, the top-leveldefaultAccessModifier
value is used. See Access modifiers section for usage details.Required.
String
andArray<String>
in Swift, respectively.Required.
Example secret definition
Supposing that you would like to obfuscate the tag used to reference the private key stored in Keychain or Secure Enclave:
The above YAML secret definition will result in the following Swift code being generated:
You may also need to obfuscate a list of related values, such as a list of trusted SPKI digests to pin against:
With the above YAML secret definition, the following Swift code will be generated:
Namespaces
In accordance with Swift programming best practices, Swift Confidential encapsulates generated secret literal declarations in namespaces (typically caseless enums). The namespaces syntax allows you to either create a new namespace or extend an existing one.
Syntax
Example usage
Assuming that you would like to keep the generated secret literal declaration(s) in a new namespace named
Secrets
, use the following YAML code:The above namespace definition will result in the following Swift code being generated:
If, however, you would rather like to keep the generated secret literal declaration(s) in an existing namespace named
Pinning
and imported fromCrypto
module, use the following YAML code instead:With the above namespace definition, the following Swift code will be generated:
Access modifiers
You can specify the access modifiers for generated Swift code, both globally and on per secret basis. Yet, the general recommendation is to use the default
internal
access level, so as to keep your code well encapsulated.Syntax
The supported access modifiers are shown in the following table: | Access modifier | Description | |——————|————————————————————————————————-| | internal | The generated declarations are accessible only within their defining module. | | public | The generated declarations are accessible within their defining module and any module that imports the defining module. |
Example usage
Supposing that you would like to keep all your secret literals in a single shared Swift module used by other project modules/targets, you can do so with a configuration similar to this one:
With
defaultAccessModifier
set topublic
, all of the Swift properties generated based on thesecrets
list are accessible outside their defining module:Additionally, if you need more fine-grained control, you can override
defaultAccessModifier
by specifying the access modifier in the secret definition as described in Secrets section.Additional considerations for Confidential Swift Package plugin
The Confidential plugin expects the configuration file to be named
confidential.yml
orconfidential.yaml
, and it assumes a single configuration file per SwiftPM target/Xcode project. If you use the plugin with SwiftPM target and you define multiple configuration files in different subdirectories, then the plugin will use the first one it finds, and which one is undefined. Whereas, if you apply the plugin to the Xcode project’s target, the configuration file is expected to be located in the project’s top-level directory (all other configuration files are ignored).Versioning
This project follows semantic versioning. While still in major version
0
, source-stability is only guaranteed within minor versions (e.g. between0.2.0
and0.2.1
). If you want to guard against potentially source-breaking package updates, you can specify your package dependency using source control requirement (e.g..upToNextMinor(from: "0.2.0")
).License
This tool and code is released under Apache License v2.0 with Runtime Library Exception. Please see LICENSE for more information.