const fs = require('fs'); const path = require('path'); const axios = require('axios'); const patterns = JSON.parse(fs.readFileSync('patterns.json')).patterns; const aiprompt = "You are AI Code Scanner, your task is to analyze code given by user explain what it does with leaking nearly nothing from it and fully decide whats going on there and if it is malicious! Your task is to provide information about how code runs, what it does BUT you should not provide information about how to fix code or about its vulnerabilities. BUT You should provide information about related files and if the code is potentially dangerous/malicious. I repeat, don't provide information about Security and Potential Issues BUT provide infos if it might be illegal code, breaking some Terms of Service, abusing files eg creating tons of files or doing any other dangerous thing which may harm system." const pathtoscan = "./change/this" const llamaendpoint = "http://0.0.0.0:11434/api/chat" function scanDirectory(directory) { fs.readdir(directory, (err, files) => { if (err) { console.error(`Error reading directory: ${err}`); return; } files.forEach(file => { const filePath = path.join(directory, file); fs.stat(filePath, (err, stats) => { if (err) { console.error(`Error getting stats for file: ${err}`); return; } if (stats.isDirectory()) { scanDirectory(filePath); } else { scanFile(filePath); } }); }); }); } function scanFile(filePath) { fs.readFile(filePath, 'utf8', (err, data) => { if (err) { console.error(`Error reading file: ${err}`); return; } let foundMalicious = false; patterns.forEach(pattern => { if (data.includes(pattern)) { console.log(`Malicious code found in file: ${filePath} - Pattern: ${pattern}`); foundMalicious = true; } }); if (foundMalicious) { runAIScan(filePath); } }); } async function runAIScan(filePath) { fs.readFile(filePath, 'utf8', async (err, data) => { if (err) { console.error(`Error reading file: ${err}`); return; } const apiUrl = `${llamaendpoint}`; let input = { "model": "llama3.2:1b", "messages": [ { "role": "system", "content": `${aiprompt}` }, { "role": "user", "content": `FILEPATH: ${filePath}, FILE: ${data}` } ], "stream": false, "raw": true } try { const response = await axios.post(apiUrl, input); console.log(`AI scan results for ${filePath}:\n${response.data.message.content}`); } catch (error) { console.log(error.response ? error.response.data : error.message) console.error(`Error running AI scan: ${error.response ? error.response.data : error.message}`); } }); } scanDirectory(pathtoscan);