-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMacTrojanAlerts.swift
49 lines (41 loc) · 1.69 KB
/
MacTrojanAlerts.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
// $t@$h
// This script does a one time scan of Downloads then polls
// at regular intervals and inspects Downloads on MacOS.
// It does NOT modify the system in any way.
// TODO: What I would like to do with this is add known
// hashes of trojan'd artifacts and search upon those. It's
// not absolute due to polymorphism but a step up from this
import Foundation
let pollingInterval: TimeInterval = 60
var lastCheckedDate: Date?
func checkForRecentPKGFiles() {
let fileManager = FileManager.default
let downloadsPath = NSHomeDirectory() + "/Downloads/"
do {
let files = try fileManager.contentsOfDirectory(atPath: downloadsPath)
let pkgFiles = files.filter { $0.hasSuffix(".pkg") }
if !pkgFiles.isEmpty {
print("!!!Caution, .PKGs found in Downloads:")
pkgFiles.forEach { print($0) }
}
let sortedFiles = pkgFiles.map { (fileName) -> (String, Date) in
let filePath = downloadsPath + fileName
let attributes = try? fileManager.attributesOfItem(atPath: filePath)
let modificationDate = attributes?[.modificationDate] as? Date ?? Date.distantPast
return (fileName, modificationDate)
}
let recentFile = sortedFiles.max(by: { $0.1 < $1.1 })
if let recentFileName = recentFile?.0 { print("Most recent PKG file: \(recentFileName)") }
lastCheckedDate = Date()
} catch {
print("Error checking Downloads.")
}
}
checkForRecentPKGFiles() // One-time
// Continuous poll
while true {
if let lastCheckedDate = lastCheckedDate, Date().timeIntervalSince(lastCheckedDate) >= pollingInterval {
checkForRecentPKGFiles()
}
sleep(UInt32(pollingInterval))
}