版本记录
版本号 | 时间 |
---|---|
V1.0 | 2019.06.07 星期五 |
前言
在这个信息爆炸的年代,特别是一些敏感的行业,比如金融业和银行卡相关等等,这都对
app
的安全机制有更高的需求,很多大公司都有安全 部门,用于检测自己产品的安全性,但是及时是这样,安全问题仍然被不断曝出,接下来几篇我们主要说一下app
的安全机制。感兴趣的看我上面几篇。
1. APP安全机制(一)—— 几种和安全性有关的情况
2. APP安全机制(二)—— 使用Reveal查看任意APP的UI
3. APP安全机制(三)—— Base64加密
4. APP安全机制(四)—— MD5加密
5. APP安全机制(五)—— 对称加密
6. APP安全机制(六)—— 非对称加密
7. APP安全机制(七)—— SHA加密
8. APP安全机制(八)—— 偏好设置的加密存储
9. APP安全机制(九)—— 基本iOS安全之钥匙链和哈希(一)
10. APP安全机制(十)—— 基本iOS安全之钥匙链和哈希(二)
11. APP安全机制(十一)—— 密码工具:提高用户安全性和体验(一)
12. APP安全机制(十二)—— 密码工具:提高用户安全性和体验(二)
13. APP安全机制(十三)—— 密码工具:提高用户安全性和体验(三)
14. APP安全机制(十四) —— Keychain Services API使用简单示例(一)
15. APP安全机制(十五) —— Keychain Services API使用简单示例(二)
16. APP安全机制(十六) —— Keychain Services API使用简单示例(三)
17. APP安全机制(十七) —— 阻止使用SSL Pinning 和 Alamofire的中间人攻击(一)
源码
1. Swift
首先看下文章组织结构
下面就是看sb中的内容
这里还可以看一下.der
证书
下面就是源码了
1. Model.swift
import Foundation
struct UserList: Codable {
let users: [User]
enum CodingKeys: String, CodingKey {
case users = "items"
}
}
struct BadgeCounts: Codable {
let bronze: Int
let silver: Int
let gold: Int
enum CodingKeys: String, CodingKey {
case bronze
case silver
case gold
}
}
struct User: Codable {
let displayName: String
let reputation: Double
let badgeCounts: BadgeCounts
enum CodingKeys: String, CodingKey {
case displayName = "display_name"
case reputation
case badgeCounts = "badge_counts"
}
}
2. NetworkClient.swift
import Foundation
import Alamofire
enum Router: URLRequestConvertible {
case users
static let baseURLString = "https://api.stackexchange.com/2.2"
func asURLRequest() throws -> URLRequest {
let path: String
switch self {
case .users:
path = "/users?order=desc&sort=reputation&site=stackoverflow"
}
let url = URL(string: Router.baseURLString + path)!
return URLRequest(url: url)
}
}
final class NetworkClient {
// 1
let evaluators = [
"api.stackexchange.com":
PinnedCertificatesTrustEvaluator(certificates: [
Certificates.stackExchange
])
]
let session: Session
// 2
private init() {
session = Session(
serverTrustManager: ServerTrustManager(evaluators: evaluators)
)
}
// MARK: - Static Definitions
private static let shared = NetworkClient()
static func request(_ convertible: URLRequestConvertible) -> DataRequest {
return shared.session.request(convertible)
}
}
struct Certificates {
static let stackExchange =
Certificates.certificate(filename: "stackexchange.com")
private static func certificate(filename: String) -> SecCertificate {
let filePath = Bundle.main.path(forResource: filename, ofType: "der")!
let data = try! Data(contentsOf: URL(fileURLWithPath: filePath))
let certificate = SecCertificateCreateWithData(nil, data as CFData)!
return certificate
}
}
3. UIViewController+Error.swift
import UIKit
extension UIViewController {
func presentError(withTitle title: String,
message: String,
actions: [UIAlertAction] = [UIAlertAction(title: "OK", style: .default)]) {
let alertController = UIAlertController(title: title,
message: message,
preferredStyle: .alert)
actions.forEach { action in
alertController.addAction(action)
}
present(alertController, animated: true)
}
}
4. ViewController.swift
import UIKit
import Alamofire
class ViewController: UIViewController {
@IBOutlet var tableView: UITableView!
private var selectedUser: User?
var users: [User] = [] {
didSet {
tableView.isHidden = false
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Stack Overflow Users"
tableView.isHidden = true
tableView.dataSource = self
NetworkClient.request(Router.users)
.responseDecodable { (response: DataResponse<UserList>) in
switch response.result {
case .success(let value):
self.users = value.users
case .failure(let error):
let isServerTrustEvaluationError =
error.asAFError?.isServerTrustEvaluationError ?? false
let message: String
if isServerTrustEvaluationError {
message = "Certificate Pinning Error"
} else {
message = error.localizedDescription
}
self.presentError(withTitle: "Oops!", message: message)
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetailSegue",
let destination = segue.destination as? DetailViewController,
let cell = sender as? UITableViewCell,
let indexPath = tableView.indexPath(for: cell) {
destination.user = users[indexPath.item]
cell.isSelected = false
}
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return users.count
}
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",
for: indexPath)
cell.textLabel?.text = users[indexPath.item].displayName
return cell
}
}
5. DetailViewController.swift
import UIKit
class DetailViewController: UIViewController {
@IBOutlet var nameLabel: UILabel!
@IBOutlet var reputationLabel: UILabel!
@IBOutlet var bronzeLabel: UILabel!
@IBOutlet var silverLabel: UILabel!
@IBOutlet var goldLabel: UILabel!
var user: User!
override func viewDidLoad() {
super.viewDidLoad()
nameLabel.text = user.displayName
reputationLabel.text = format(value: user.reputation)
bronzeLabel.text = "\(user.badgeCounts.bronze)"
silverLabel.text = "\(user.badgeCounts.silver)"
goldLabel.text = "\(user.badgeCounts.gold)"
}
private func format(value: Double) -> String {
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .decimal
currencyFormatter.locale = Locale.current
return currencyFormatter.string(from: NSNumber(value: value)) ?? "n.a."
}
}
后记
本篇主要讲述了阻止使用SSL Pinning 和 Alamofire的中间人攻击,感兴趣的给个赞或者关注~~~