How to Check .DOTX Files for Malware, Macros, and OLE in Node.js
While DOTX is simply a template format for generating standardized Word DOCX files, it’s still susceptible to macro and OLE-based attacks.
Technically, DOTX shouldn’t even be able to support macros. Unfortunately, however, sophisticated threat actors have proven they can find ways around such technicalities.
When they do, the juice is often worth the squeeze after all; many prospective victims aren’t expecting innocuous DOTX templates to contain macro malware. Once malicious DOTX files are opened, disguised buttons can quietly enable macros, and those macros can establish connections with C&C servers for remote malware download.
Thankfully, using a few lines of Node.js code, we can call a free API that detects macro and OLE threats in DOTX files. This scan will also check files for virus and malware signatures, as well as identify a wide range of content types within the document (including scripts, HTML, images, XML, JSON, and more).
We can structure our API call in a few quick steps, starting with SDK installation. To install the SDK using NPM install, we can run the below command:
npm install cloudmersive-virus-api-client --save
Or, if we want to go about installation a different way, we can add the Node client to our package.json:
"dependencies": {
"cloudmersive-virus-api-client": "^1.1.9"
}
Next, we can copy the below code to call the function. We’ll need a free Cloudmersive API key to authorize our requests (this will give us a limit of 800 API calls per month with zero commitments):
var CloudmersiveVirusApiClient = require('cloudmersive-virus-api-client');
var defaultClient = CloudmersiveVirusApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveVirusApiClient.ScanApi();
var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
var opts = {
'allowExecutables': true, // Boolean | Set to false to block executable files (program code) from being allowed in the input file. Default is false (recommended).
'allowInvalidFiles': true, // Boolean | Set to false to block invalid files, such as a PDF file that is not really a valid PDF file, or a Word Document that is not a valid Word Document. Default is false (recommended).
'allowScripts': true, // Boolean | Set to false to block script files, such as a PHP files, Python scripts, and other malicious content or security threats that can be embedded in the file. Set to true to allow these file types. Default is false (recommended).
'allowPasswordProtectedFiles': true, // Boolean | Set to false to block password protected and encrypted files, such as encrypted zip and rar files, and other files that seek to circumvent scanning through passwords. Set to true to allow these file types. Default is false (recommended).
'allowMacros': true, // Boolean | Set to false to block macros and other threats embedded in document files, such as Word, Excel and PowerPoint embedded Macros, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended).
'allowXmlExternalEntities': true, // Boolean | Set to false to block XML External Entities and other threats embedded in XML files, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended).
'allowInsecureDeserialization': true, // Boolean | Set to false to block Insecure Deserialization and other threats embedded in JSON and other object serialization files, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended).
'allowHtml': true, // Boolean | Set to false to block HTML input in the top level file; HTML can contain XSS, scripts, local file accesses and other threats. Set to true to allow these file types. Default is false (recommended) [for API keys created prior to the release of this feature default is true for backward compatability].
'restrictFileTypes': "restrictFileTypes_example" // String | Specify a restricted set of file formats to allow as clean as a comma-separated list of file formats, such as .pdf,.docx,.png would allow only PDF, PNG and Word document files. All files must pass content verification against this list of file formats, if they do not, then the result will be returned as CleanResult=false. Set restrictFileTypes parameter to null or empty string to disable; default is disabled.
};
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.scanFileAdvanced(inputFile, opts, callback);
We can set custom threat rules in the above request to identify and block macros, OLE, and other threatening content types. Setting ‘allowMacros’
to false
, for example, will return a “CleanResult”: false
response for files containing VBA macros.
Even if we don’t directly set threat rules against these content types, the API response will contain information about each type of content detected within the document and allow us to make important decisions based on that information. We can review the below JSON response example to see what a full diagnostic might look like:
{
"CleanResult": true,
"ContainsExecutable": true,
"ContainsInvalidFile": true,
"ContainsScript": true,
"ContainsPasswordProtectedFile": true,
"ContainsRestrictedFileFormat": true,
"ContainsMacros": true,
"ContainsXmlExternalEntities": true,
"ContainsInsecureDeserialization": true,
"ContainsHtml": true,
"ContainsUnsafeArchive": true,
"ContainsOleEmbeddedObject": true,
"VerifiedFileFormat": "string",
"FoundViruses": [
{
"FileName": "string",
"VirusName": "string"
}
],
"ContentInformation": {
"ContainsJSON": true,
"ContainsXML": true,
"ContainsImage": true,
"RelevantSubfileName": "string"
}
}
As threat actors’ methods become increasingly complex, performing dynamic threat scans becomes increasingly important. This API can help simplify that process into a single web request.