Share
Embed
Como deixar os meus apps mais seguros
Salmo Junior
February 27, 2018
Transcript
23 slides
-
Salmo Junior » Dev iOS desde 2011 » Chapter Leader
do CocoaHeadsBH » Viciado em queijo » Aspirante a corredor [email protected] @salmojr -
Três perspectivas para se preocupar em seu aplicativo » Dados
dos usuários » Conexão segura com API » Seu código -
Deixe os dados ainda mais seguros no Keychain » Configure
o Accessibility » Crie Keys dinâmicas por usuários » Use Keys com hash e Values quando possível -
Keychain com accessibility configurado Exemplo usando o wrapper KeychainAccess let
keychain = Keychain(service: "com.example.github-token") .accessibility(.whenUnlockedThisDeviceOnly) keychain["kishikawakatsumi"] = "01234567-89ab-cdef-0123-456789abcdef" -
Tipos de Keychain Accessibility kSecAttrAccessibleWhenUnlocked (default) kSecAttrAccessibleAfterFirstUnlock kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly kSecAttrAccessibleAlways kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
kSecAttrAccessibleAlwaysThisDeviceOnly kSecAttrAccessibleWhenUnlockedThisDeviceOnly -
Keychain com Common Crypto func sha256(string: String) -> String? {
guard let messageData = string.data(using:String.Encoding.utf8) else { return nil } var digestData = Data(count: Int(CC_SHA256_DIGEST_LENGTH)) _ = digestData.withUnsafeMutableBytes {digestBytes in messageData.withUnsafeBytes {messageBytes in CC_SHA256(messageBytes, CC_LONG(messageData.count), digestBytes) } } return digestData.map { String(format: "%02hhx", $0) }.joined() } "hello world" = b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 -
Verificação de Jailbreak // Verify Jailbreak static func isJailBroken() ->
Bool { guard TARGET_OS_SIMULATOR != 1 else { return false } // Check 1: existence of files that are common for jailbroken devices guard let cydiaURL = URL(string: "cydia://") else { return false } if FileManager.default.fileExists(atPath: "/Applications/Cydia.app") || FileManager.default.fileExists(atPath: "/Library/MobileSubstrate/MobileSubstrate.dylib") || FileManager.default.fileExists(atPath: "/bin/bash") || FileManager.default.fileExists(atPath: "/usr/sbin/sshd") || FileManager.default.fileExists(atPath: "/etc/apt") || FileManager.default.fileExists(atPath: "/private/var/lib/apt/") || UIApplication.shared.canOpenURL(cydiaURL) { // Device is jailbroken return true } -
Verificação de Jailbreak . . . // Check 2: Reading
and writing in system directories (sandbox violation) do { let stringToWrite = "Jailbreak Test" try stringToWrite.write(toFile: "/private/JailbreakTest.txt", atomically: true, encoding: String.Encoding.utf8) // Device is jailbroken return true } catch { return false } } -
App Transport Security » TLS/SSL » Exceções como na imagem
abaixo, somente em alguns casos (ex: ambiente de mock) -
Certificate Pinning Com o acesso a redes wifi inseguras em
todo lugar, é necessário uma camada extra de segurança na comunicação com o backend, evitando assim ataque como o MITM. -
Cache Policy public enum CachePolicy : UInt { case useProtocolCachePolicy
case reloadIgnoringLocalCacheData case reloadIgnoringLocalAndRemoteCacheData case returnCacheDataElseLoad case returnCacheDataDontLoad case reloadRevalidatingCacheData public static var reloadIgnoringCacheData: NSURLRequest.CachePolicy { get } } let request = NSMutableURLRequest() ... request.cachePolicy = .reloadIgnoringCacheData -
Código seguro » Evite Secret Keys fixas no código »
Cuidado com o nível de acesso de Classes/Métodos » Crie chaves de criptografia de forma aleatória » Ofusque Strings sensíveis -
Ofuscação de Strings sensíveis A biblioteca Obfuscator.swift transforma strings em
array de bytes import Obfuscator let o = Obfuscator(withSalt: [AppDelegate.self, NSObject.self, NSString.self]) let bytes = o.bytesByObfuscatingString(string: “555555556ABC”) print(bytes) /*[110, 97, 80, 70, 65, 122, 87, 83, 67, 50, 33, 34]* -
Ofuscação de Strings sensíveis Código sem strings, evitando a fácil
identicação ao fazer uma varredura do código struct Constants { static let GOOGLE_API_KEY: [UInt8] = [106, 102, 86, 66, 69, 123, 87, 80, 52, 49, 32] static let FACEBOOK_API_KEY: [UInt8] = [110, 97, 80, 70, 65, 122, 87, 83, 67, 50, 33, 34] } let value = o.reveal(key: Constants.FACEBOOK_API_KEY) print(value) /*555555556ABC*/ -
Referências » https://developer.apple.com/security » https://medium.com/theappspace/increase-the-security-of- your-ios-app-by-obfuscating-sensitive-strings-swift- c915896711e6 » https://infinum.co/the-capsized-eight/how-to-make-your- ios-apps-more-secure-with-ssl-pinning
» https://pt-br.eventials.com/Globalcode/tdc-poa-2017- stadium-sabado-6/