Skip to content

Instantly share code, notes, and snippets.

@samuelkarani
Last active December 20, 2022 11:39
Show Gist options
  • Save samuelkarani/3b2f60e935ab4d8f5d534d1bca23635b to your computer and use it in GitHub Desktop.
Save samuelkarani/3b2f60e935ab4d8f5d534d1bca23635b to your computer and use it in GitHub Desktop.
const nvt = require('node-virustotal'); // read more https://www.npmjs.com/package/node-virustotal
const fs = require('fs');
const crypto = require("crypto");
app.post('/upload', async (req, res) => {
const randomId = crypto.randomBytes(16).toString("hex");
const filePath = __dirname + "/uploads/" + randomId;
const writeStream = fs.createWriteStream(filePath);
writeStream.on('finish', () => {
try {
await scanFile(filePath, randomId, "");
// upload to SW
} catch {
// malware detected
}
})
request.pipe(writeStream);
});
function scanFile(filePath, fileName, mimeType) {
return new Promise((resolve, reject) => {
const defaultTimedInstance = nvt.makeAPI();
const aMaliciousFile = fs.readFileSync(filePath);
const theSameObject = defaultTimedInstance.uploadFile(aMaliciousFile, fileName, mimeType, function(err, res){
if (err) reject(err);
else resolve(res);
});
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment