How to Scan Files for Viruses and Zero-Day Threats in Swift

During this age of technology, protecting your business from potential cyber attacks has become an essential piece of security. In this post, we will be discussing a virus scanning solution that will provide 360-degree content protections across viruses, malware, and even zero-day threats. Zero-day threats pose one of the biggest risks due to their immunity to normal definition-based virus scanning, and their often-customized approach to your site. To offer a defense against these and other attacks, we will demonstrate how to set up an advanced virus scanning API in Swift.

The following example code will allow you to input your file, configure your API key, and call the virus scan function:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
var semaphore = DispatchSemaphore (value: 0)let parameters = [
[
"key": "inputFile",
"src": "/path/to/file",
"type": "file"
]] as [[String : Any]]
let boundary = "Boundary-\(UUID().uuidString)"
var body = ""
var error: Error? = nil
for param in parameters {
if param["disabled"] == nil {
let paramName = param["key"]!
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
if param["contentType"] != nil {
body += "\r\nContent-Type: \(param["contentType"] as! String)"
}
let paramType = param["type"] as! String
if paramType == "text" {
let paramValue = param["value"] as! String
body += "\r\n\r\n\(paramValue)\r\n"
} else {
let paramSrc = param["src"] as! String
let fileData = try NSData(contentsOfFile:paramSrc, options:[]) as Data
let fileContent = String(data: fileData, encoding: .utf8)!
body += "; filename=\"\(paramSrc)\"\r\n"
+ "Content-Type: \"content-type header\"\r\n\r\n\(fileContent)\r\n"
}
}
}
body += "--\(boundary)--\r\n";
let postData = body.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/virus/scan/file/advanced")!,timeoutInterval: Double.infinity)
request.addValue("<boolean>", forHTTPHeaderField: "allowExecutables")
request.addValue("<boolean>", forHTTPHeaderField: "allowInvalidFiles")
request.addValue("<boolean>", forHTTPHeaderField: "allowScripts")
request.addValue("<boolean>", forHTTPHeaderField: "allowPasswordProtectedFiles")
request.addValue("<boolean>", forHTTPHeaderField: "allowMacros")
request.addValue("<string>", forHTTPHeaderField: "restrictFileTypes")
request.addValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")
request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()

Note that if we want to block zero-day threats, we should ensure that allowExecutables is set to false (the default), as this will identify all incoming files regardless of their extension in a sandbox environment. The other optional parameters such allowScripts and allowMacros are set to false as a default as well, so if you want to allow them for whatever reason, be sure to change them to true.