-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
186 lines (154 loc) · 5.59 KB
/
Copy pathserver.js
File metadata and controls
186 lines (154 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const path = require('path');
const { v4: uuidv4 } = require('uuid');
const os = require('os');
const config = require('./config');
const CommandProcessor = require('./modules/commandProcessor');
const SessionLogger = require('./modules/sessionLogger');
const WallMessageSystem = require('./modules/wallMessage');
const app = express();
const server = http.createServer(app);
const io = new Server(server, { cors: { origin: '*' } });
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
const logger = new SessionLogger();
const activeProcessors = new Map();
const terminalNs = io.of('/terminal');
terminalNs.on('connection', (socket) => {
const sessionId = uuidv4();
const ip = socket.handshake.headers['x-forwarded-for'] || socket.handshake.address || 'unknown';
const userAgent = socket.handshake.headers['user-agent'] || 'unknown';
const session = logger.createSession(sessionId, { ip, userAgent });
const processor = new CommandProcessor(logger, sessionId);
activeProcessors.set(sessionId, processor);
const wall = new WallMessageSystem();
wall.start((msg) => { socket.emit('output', msg); });
console.log(`[+] New attacker session: ${sessionId} from ${ip}`);
const banner = `Last login: ${new Date(Date.now() - 3600000).toUTCString()} from 10.0.3.1\n`;
socket.emit('output', banner);
socket.emit('prompt', processor.getPrompt());
socket.emit('session-id', sessionId);
let awaitingSudo = false;
let sudoCommand = '';
socket.on('command', async (cmdLine) => {
if (awaitingSudo) {
const result = processor.handleSudoPassword(cmdLine, sudoCommand);
if (result.awaitingPassword) {
socket.emit('output', result.output);
socket.emit('password-prompt', true);
} else {
awaitingSudo = false;
socket.emit('output', result.output + '\n');
socket.emit('prompt', processor.getPrompt());
}
return;
}
const result = await processor.process(cmdLine);
const delay = result.delay || 100;
setTimeout(() => {
if (result.output) {
socket.emit('output', result.output + '\n');
}
if (result.awaitingPassword) {
awaitingSudo = true;
sudoCommand = result.sudoCommand || '';
socket.emit('password-prompt', true);
return;
}
if (result.shouldDisconnect) {
wall.stop();
logger.endSession(sessionId);
activeProcessors.delete(sessionId);
socket.emit('disconnected', true);
return;
}
if (result.wallTrigger) {
setTimeout(() => {
wall.triggerReactive(result.wallTrigger);
}, 5000 + Math.random() * 10000);
}
socket.emit('prompt', processor.getPrompt());
}, delay);
});
socket.on('disconnect', () => {
wall.stop();
logger.endSession(sessionId);
activeProcessors.delete(sessionId);
console.log(`[-] Session ended: ${sessionId}`);
});
});
const dashNs = io.of('/dashboard');
dashNs.on('connection', (socket) => {
console.log('[Dashboard] Admin connected');
socket.emit('stats', logger.getSessionStats());
socket.emit('sessions', logger.getAllSessions().map(s => ({
...s,
commands: s.commands.slice(-50),
})));
const interval = setInterval(() => {
socket.emit('stats', logger.getSessionStats());
socket.emit('sessions', logger.getAllSessions().map(s => ({
...s,
commands: s.commands.slice(-50),
})));
socket.emit('active-count', logger.getActiveSessions().length);
}, config.dashboard.refreshInterval);
socket.on('disconnect', () => {
clearInterval(interval);
console.log('[Dashboard] Admin disconnected');
});
});
app.get('/api/stats', (req, res) => {
res.json(logger.getSessionStats());
});
app.get('/api/sessions', (req, res) => {
res.json(logger.getAllSessions().map(s => ({
id: s.id,
ip: s.ip,
startTime: s.startTime,
endTime: s.endTime,
active: s.active,
totalCommands: s.totalCommands,
riskScore: s.riskScore,
exploitAttempts: s.exploitAttempts.length,
credentialsCaptured: s.credentialsCaptured.length,
tags: s.tags,
})));
});
app.get('/api/sessions/:id', (req, res) => {
const session = logger.getSession(req.params.id);
if (!session) return res.status(404).json({ error: 'Session not found' });
res.json(session);
});
app.get('/api/sessions/:id/commands', (req, res) => {
const session = logger.getSession(req.params.id);
if (!session) return res.status(404).json({ error: 'Session not found' });
res.json(session.commands);
});
app.get('/api/sessions/:id/exploits', (req, res) => {
const session = logger.getSession(req.params.id);
if (!session) return res.status(404).json({ error: 'Session not found' });
res.json(session.exploitAttempts);
});
app.get('/api/sessions/:id/credentials', (req, res) => {
const session = logger.getSession(req.params.id);
if (!session) return res.status(404).json({ error: 'Session not found' });
res.json(session.credentialsCaptured);
});
app.get('/', (req, res) => res.redirect('/dashboard'));
app.get('/dashboard', (req, res) => res.sendFile(path.join(__dirname, 'public', 'dashboard.html')));
server.listen(config.server.port, config.server.host, () => {
const nets = os.networkInterfaces();
let localIP = 'localhost';
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
if (net.family === 'IPv4' && !net.internal) {
localIP = net.address;
break;
}
}
}
console.log(`Server listening on http://${localIP}:${config.server.port}`);
});