How to Scan Files for Viruses, Malware, and Malicious Scripts using Go
Threat actors often inject malicious scripts into seemingly trustworthy files to remotely execute code within our system. Identifying malicious scripts requires the application of rigorous content verification policies, and the free-to-use API provided below is designed for exactly that purpose. This API will scan each file for millions of virus and malware threats — including ransomware, spyware, trojans, and more — and automatically detect scripts hidden within file contents (such as PHP, Python, and other scripting languages). You can easily block files containing scripts by setting the (“allowScripts”, “<boolean>”) parameter to “false.”
To structure your API call, copy and paste the below code examples:
package main
import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.cloudmersive.com/virus/scan/file/advanced"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("allowExecutables", "<boolean>")
req.Header.Add("allowInvalidFiles", "<boolean>")
req.Header.Add("allowScripts", "<boolean>")
req.Header.Add("allowPasswordProtectedFiles", "<boolean>")
req.Header.Add("allowMacros", "<boolean>")
req.Header.Add("allowXmlExternalEntities", "<boolean>")
req.Header.Add("allowInsecureDeserialization", "<boolean>")
req.Header.Add("allowHtml", "<boolean>")
req.Header.Add("restrictFileTypes", "<string>")
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
You can authenticate your request for free with a free-tier API key (get one by registering a free account on the Cloudmersive website; this comes with 800 API calls per month and no additional commitment). That’s all there is to it — no more code required!