-
Notifications
You must be signed in to change notification settings - Fork 7
/
wipasswd.swift
executable file
·65 lines (58 loc) · 1.45 KB
/
wipasswd.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env xcrun swift -target x86_64-apple-macosx10.10
// vi: ft=swift
import Security
import Foundation
import CoreWLAN
func getCurrentSsid() -> String? {
return CWWiFiClient.shared().interface()?.ssid()
}
func getPasswd(ssid: String) -> String? {
let query: [NSString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: "AirPort",
kSecAttrAccount: ssid,
kSecReturnData: true,
kSecMatchLimit: kSecMatchLimitOne
]
var result: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)
if status == noErr, let data = result as? Data {
return String(data: data, encoding: .utf8)
}
return nil
}
func main() -> Int32 {
let args = CommandLine.arguments
var ssid: String?
var onlyPasswd = false
if args.count > 1 {
if args[1] == "-w" {
onlyPasswd = true
}
}
if args.count > 2 {
if onlyPasswd {
ssid = args[2]
} else {
ssid = args[1]
}
} else {
ssid = getCurrentSsid()
}
if let s = ssid {
if let pass = getPasswd(ssid: s) {
if onlyPasswd {
print(pass)
} else {
print("SSID: \(s)")
print("PASS: \(pass)")
}
return 0
} else {
print("No WiFi password found for \(s)")
return 1
}
}
return 0
}
exit(main())