-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh.js
More file actions
391 lines (323 loc) · 14 KB
/
Copy pathssh.js
File metadata and controls
391 lines (323 loc) · 14 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* BBSFirewall - SSH server (terminate mode)
* Accepts any credentials and proxies the session to the backend telnet server.
* Note: Binary file transfers (Zmodem, etc.) are unreliable over SSH due to PTY
* processing — use SSH_MODE=passthrough with a backend that has its own SSH
* server if you need reliable transfers.
* https://github.com/SysopNetwork/BBSFirewall
*/
const ssh2 = require('ssh2');
const net = require('net');
const fs = require('fs');
const logger = require('./logger');
const metrics = require('./metrics');
const { getIPFilter } = require('./ipfilter');
const { getGeoIP } = require('./geoip');
const { detectFromSSHEnvironment, detectFromTerminalType, getBackendPortForEncoding } = require('./encoding-detector');
const { buildHeader: buildProxyHeader } = require('./proxy-protocol');
const log = logger.getLogger('ssh');
// Mirrors ProxyConnection.shouldBlockConnection in proxy.js — the telnet path's
// GeoIP country block was never ported over here, which let a caller from a
// blocked country simply connect via SSH_MODE=terminate instead of telnet to
// bypass BLOCKED_COUNTRIES entirely.
function shouldBlockByCountry(config, ipAddress) {
const geoip = getGeoIP();
if (!geoip || !geoip.isEnabled) return false;
const geoInfo = geoip.getCountryInfo(ipAddress);
if (!geoInfo || !geoInfo.countryCode) {
if (config.blockUnknownCountries) {
log.info(`Blocked unknown country for IP: ${ipAddress}`);
return true;
}
return false;
}
log.debug(`SSH connection from ${geoInfo.countryName} (${geoInfo.countryCode})`);
if (config.blockedCountries.length > 0) {
const isBlocked = config.blockedCountries.includes(geoInfo.countryCode.toUpperCase());
if (isBlocked) {
log.info(`Blocked ${geoInfo.countryName} (${geoInfo.countryCode})`);
}
return isBlocked;
}
return false;
}
function createSSHServer(config) {
if (config.sshMode !== 'terminate') {
return null;
}
let hostKey;
try {
hostKey = fs.readFileSync(config.sshHostKey, 'utf8');
} catch (err) {
log.error(`Failed to read SSH host key from ${config.sshHostKey}: ${err.message}`);
log.error('Generate a host key with: ssh-keygen -t rsa -b 4096 -f ssh_host_key -N "" -m PEM');
process.exit(1);
}
const server = new ssh2.Server(
{
hostKeys: [hostKey],
algorithms: {
cipher: config.sshCiphers,
},
},
(client) => {
const clientIP = client._sock?.remoteAddress;
const clientPort = client._sock?.remotePort || 0;
client.on('error', (err) => {
log.debug(`SSH client error: ${err.message}`);
});
if (!clientIP) {
log.blocked('SSH connection rejected: unable to determine client IP');
client.end();
return;
}
log.connection(`SSH client connected from ${clientIP}`);
const ipFilter = getIPFilter();
let sshConnectionTracked = false;
let sshWhitelisted = false;
if (ipFilter) {
const accessCheck = ipFilter.shouldAllowConnection(clientIP);
if (!accessCheck.allowed) {
log.blocked(`SSH connection blocked from ${clientIP}: ${accessCheck.reason}`);
client.end();
return;
}
sshWhitelisted = accessCheck.whitelisted || false;
// Check per-IP concurrent connection limit (whitelisted IPs are exempt)
if (!accessCheck.whitelisted && ipFilter.isConnectionLimitExceeded(clientIP)) {
log.blocked(`SSH connection rejected: per-IP limit reached for ${clientIP}`);
client.end();
return;
}
// Check country blocking (whitelisted IPs are exempt)
if (!accessCheck.whitelisted && shouldBlockByCountry(config, clientIP)) {
log.blocked(`SSH connection blocked by country filter: ${clientIP}`);
client.end();
return;
}
// Register this connection in the per-IP tracker
ipFilter.trackConnectionOpen(clientIP);
sshConnectionTracked = true;
}
client.on('authentication', (ctx) => {
log.info(`SSH auth from ${clientIP} (user: ${ctx.username})`);
if (ctx.method === 'password' || ctx.method === 'none') {
ctx.accept();
} else {
ctx.reject(['password', 'none']);
}
});
client.on('ready', () => {
log.info(`SSH client ${clientIP} authenticated`);
client.on('session', (accept, reject) => {
log.debug(`Session requested for ${clientIP}`);
if (typeof accept !== 'function') {
log.error(`Session accept is not a function for ${clientIP}`);
return;
}
const session = accept();
let detectedEncoding = 'cp437';
let sshEnv = {};
let termType = null;
session.on('env', (accept, reject, info) => {
log.debug(`SSH env from ${clientIP}: ${info.key}=${info.value}`);
sshEnv[info.key] = info.value;
if (config.encodingDetection) {
const envDetected = detectFromSSHEnvironment(sshEnv);
if (envDetected === 'utf8') {
detectedEncoding = 'utf8';
log.info(`Detected UTF-8 encoding from SSH environment for ${clientIP}`);
}
}
accept && accept();
});
session.on('pty', (accept, reject, info) => {
log.debug(`PTY requested for ${clientIP}, term: ${info.term}`);
if (info && info.term) {
termType = info.term;
if (config.encodingDetection && detectedEncoding === 'cp437') {
const termDetected = detectFromTerminalType(termType);
if (termDetected === 'utf8') {
detectedEncoding = 'utf8';
log.info(`Detected UTF-8 from terminal type '${termType}' for ${clientIP}`);
}
}
}
if (typeof accept === 'function') {
accept();
} else {
log.warn(`PTY accept is not a function for ${clientIP}`);
}
});
session.on('window-change', (info) => {
log.debug(`Window change for ${clientIP}: ${info.cols}x${info.rows}`);
});
session.on('shell', (accept, reject) => {
log.debug(`Shell requested for ${clientIP}`);
if (typeof accept !== 'function') {
log.error(`Shell accept is not a function for ${clientIP}`);
return;
}
const stream = accept();
log.connection(`SSH shell session started for ${clientIP}`);
stream.allowHalfOpen = true;
const actualBackendPort = config.encodingDetection
? getBackendPortForEncoding(detectedEncoding, config)
: config.backendPort;
if (config.encodingDetection) {
log.info(`SSH client ${clientIP} using backend port ${actualBackendPort} for encoding: ${detectedEncoding}`);
}
const backendSocket = new net.Socket();
backendSocket.setNoDelay(true);
backendSocket.setKeepAlive(true, 30000);
// Pause the SSH stream until the backend is connected and the
// PROXY header has been written, so it is always first in the
// backend stream.
stream.pause();
// Give up if the backend TCP connection does not establish in time.
let connectTimer = config.backendConnectTimeout > 0 ? setTimeout(() => {
log.blocked(`SSH client ${clientIP}: backend connect timed out after ` +
`${config.backendConnectTimeout}ms (${config.backendHost}:${actualBackendPort})`);
stream.end();
if (!backendSocket.destroyed) backendSocket.destroy();
}, config.backendConnectTimeout) : null;
const clearConnectTimer = () => {
if (connectTimer) { clearTimeout(connectTimer); connectTimer = null; }
};
backendSocket.once('close', clearConnectTimer);
backendSocket.connect(actualBackendPort, config.backendHost, () => {
clearConnectTimer();
log.connection(`SSH client ${clientIP} connected to backend ${config.backendHost}:${actualBackendPort}`);
backendSocket.setNoDelay(true);
// Send PROXY Protocol v1 header before any BBS data flows.
// The backend must support it — see PROXY_PROTOCOL_ENABLED in .env.
if (config.proxyProtocolEnabled) {
const header = buildProxyHeader(
clientIP,
backendSocket.localAddress,
clientPort,
backendSocket.localPort
);
backendSocket.write(header);
log.info(`SSH PROXY Protocol header sent for ${clientIP}: ${header.trim()}`);
}
stream.resume();
});
let bytesFromClient = 0;
let bytesFromBackend = 0;
let triggerScan = !!(config.triggerBlock && config.triggerBlock.enabled) && !sshWhitelisted;
let triggerBuf = null;
stream.on('data', (data) => {
if (triggerScan) {
const cap = config.triggerBlock.scanBytes;
triggerBuf = triggerBuf ? Buffer.concat([triggerBuf, data]) : Buffer.from(data);
if (triggerBuf.length > cap) triggerBuf = triggerBuf.subarray(0, cap);
const ipf = getIPFilter();
const hit = ipf && ipf.matchTrigger(triggerBuf.toString('latin1'));
if (hit) {
log.blocked(`Auto-block ${clientIP}: shell input matched trigger ${JSON.stringify(hit)}`);
if (ipf) ipf.autoBlockIP(clientIP, hit);
metrics.incTriggerBlock();
stream.end();
if (!backendSocket.destroyed) backendSocket.destroy();
return;
}
if (triggerBuf.length >= cap) { triggerScan = false; triggerBuf = null; }
}
bytesFromClient += data.length;
metrics.incBytes('fromClient', data.length);
if (!backendSocket.writable || backendSocket.destroyed) {
log.debug(`Backend not writable, dropping ${data.length} bytes`);
return;
}
if (!backendSocket.write(data)) {
log.debug('Backend buffer full, pausing SSH stream');
stream.pause();
backendSocket.once('drain', () => {
log.debug('Backend drained, resuming SSH stream');
if (!stream.destroyed) stream.resume();
});
}
});
backendSocket.on('data', (data) => {
bytesFromBackend += data.length;
metrics.incBytes('fromBackend', data.length);
if (!stream.writable || stream.destroyed) {
log.debug(`SSH stream not writable, dropping ${data.length} bytes`);
return;
}
if (!stream.write(data)) {
log.debug('SSH stream buffer full, pausing backend');
backendSocket.pause();
stream.once('drain', () => {
log.debug('SSH stream drained, resuming backend');
if (!backendSocket.destroyed) backendSocket.resume();
});
}
});
backendSocket.on('error', (err) => {
log.error(`Backend error for SSH client ${clientIP}: ${err.message}`);
stream.end();
});
backendSocket.on('close', () => {
log.connection(`Backend connection closed for SSH client ${clientIP}`);
stream.end();
});
stream.on('close', () => {
log.connection(`SSH stream closed for ${clientIP}. Bytes: client→backend=${bytesFromClient}, backend→client=${bytesFromBackend}`);
if (!backendSocket.destroyed) backendSocket.destroy();
});
stream.on('error', (err) => {
log.error(`SSH stream error for ${clientIP}: ${err.message}`);
if (!backendSocket.destroyed) backendSocket.destroy();
});
});
session.on('exec', (accept, reject, info) => {
log.debug(`Exec request from ${clientIP}: ${info.command}`);
// A remote command against a BBS gateway is always a bot. If it
// matches a trigger, blacklist the source.
if (config.triggerBlock && config.triggerBlock.enabled && !sshWhitelisted) {
const ipf = getIPFilter();
const hit = ipf && ipf.matchTrigger(String(info.command || ''));
if (hit) {
log.blocked(`Auto-block ${clientIP}: exec command matched trigger ${JSON.stringify(hit)}`);
if (ipf) ipf.autoBlockIP(clientIP, hit);
metrics.incTriggerBlock();
client.end();
return;
}
}
reject();
});
});
});
client.on('close', () => {
log.connection(`SSH client ${clientIP} disconnected`);
if (sshConnectionTracked) {
const ipFilter = getIPFilter();
if (ipFilter) ipFilter.trackConnectionClose(clientIP);
}
});
}
);
return server;
}
function startSSHServer(config, activeConnectionsTracker) {
const server = createSSHServer(config);
if (!server) {
return null;
}
server.on('error', (err) => {
log.error(`SSH server error: ${err.message}`);
if (err.code === 'EADDRINUSE') {
log.error(`SSH port ${config.sshListenPort} is already in use`);
process.exit(1);
}
});
server.listen(config.sshListenPort, () => {
log.info(`SSH server (terminate) listening on port ${config.sshListenPort}`);
log.info(`SSH connections forwarded to ${config.backendHost}:${config.backendPort}`);
});
return server;
}
module.exports = { createSSHServer, startSSHServer };