How to Block Hidden Executable File Uploads in Nodejs
Hidden executable content can lead to devastating attacks, and it’s difficult for traditional AV solutions to detect.
JPG files, for example, can be injected with executable content that runs when the file is rendered or processed by a vulnerable library.
If we have a client-side file upload form in our web application that allows JPG uploads, we might unknowingly pass malicious JPGs downstream towards JPG rendering and processing technologies, resulting in a devastating breach when the file is executed.
Thankfully, we can identify hidden executable content through an in-depth deterministic threat scan. Using code examples provided below, we can call a free API that looks “under the hood” of our file uploads to verify the contents within, ensuring files with valid extensions and invalid contents don’t sneak past our Nodejs file upload processing applications.
Conveniently, this solution also references files against a continuously updated list of virus and malware signatures, so we can still benefit from traditional AV threat detection.
To structure our API call, we can begin by installing the client SDK. Let’s run the below NPM command:
npm install cloudmersive-virus-api-client --save
Next, let’s quickly turn our attention to API call authorization. We’ll need a free Cloudmersive API key to authorize our requests (this will allow a limit of 800 API calls per month with zero commitments).
Now we can copy the below code to call the function, and we can paste our API key in the ‘YOUR API KEY’
snippet. Note that the deterministic threat scan will also look out for invalid files, scripts, macros, unsafe archives, and a few other threats.
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);
If we test this solution with a (approved for testing) JPG file hiding executable content, we’ll get a response that looks like the below JSON example (resulting from a test with an approved JPG executable):
{
"CleanResult": false,
"ContainsExecutable": true,
"ContainsInvalidFile": false,
"ContainsScript": false,
"ContainsPasswordProtectedFile": false,
"ContainsRestrictedFileFormat": false,
"ContainsMacros": false,
"ContainsXmlExternalEntities": false,
"ContainsInsecureDeserialization": false,
"ContainsHtml": false,
"ContainsUnsafeArchive": false,
"ContainsOleEmbeddedObject": false,
"VerifiedFileFormat": "",
"FoundViruses": null,
"ContentInformation": {
"ContainsJSON": false,
"ContainsXML": false,
"ContainsImage": false,
"RelevantSubfileName": null
}
}
The “CleanResult”: false
response tells us the file we scanned failed either the deterministic scan OR the AV scan, and the “ContainsExecutable”: true
response confirms executable content as the culprit.