Last active
December 20, 2022 11:39
-
-
Save samuelkarani/3b2f60e935ab4d8f5d534d1bca23635b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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