How to Detect Password Protected Excel and Word Document Uploads in Node.js

Cloudmersive
4 min readFeb 22, 2024

--

Office document formats are among the most commonly used file types in the world today. Unfortunately, their popularity also makes them a natural choice for disguising malware — especially when malware obfuscation can be as trivial as applying generic encryption & password-protection policies to a malicious Office file.

Thankfully, using ready-to-run Node.js code provided below, we can take advantage of a free API that combines antivirus scanning with content verification policies, allowing us to simply block password-protected Office file uploads to our most sensitive web applications.

Specifically, we can set a custom threat rule in our request parameters to treat password-protected files the same way we would treat files flagged for malware signatures (and we can set a variety of other custom threat rules, too). We can review the example JSON response object below to better understand the scope of this API’s threat protection:

{
"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"
}
}

Of course, not every password-protected Excel or Word document upload will contain obfuscated malware. However, it’s highly unlikely we want to run the risk of opening client-side file upload contents with unverified client-supplied passwords — and it’s much better to be safe than sorry.

We can easily incorporate this API for any of our Node.js applications in two quick steps. First, we need to run this command to install the SDK:

npm install cloudmersive-virus-api-client --save

Alternatively, we can add this snippet to our package.json:

  "dependencies": {
"cloudmersive-virus-api-client": "^1.1.9"
}

Next, we need to get a free API key to authorize our API calls. Free API keys will allow a limit of 800 API calls per month with no commitments (our total will simply reset the following month).

Lastly, we can copy the below code into our file to call the function. We can easily parse file upload bytes into the indicated snippet, and we can set our anti-password-protection policy by setting allowPasswordProtectedFiles to “false”:

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);

Let’s not forget to enter our API key into the indicated snippet!

Once we do that, we’re all done — no more code required. Now we have a low-code, dynamic file scanning solution to prevent commonly obfuscated file upload threats from reaching sensitive server locations.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.