-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-security.js
More file actions
551 lines (465 loc) · 17 KB
/
session-security.js
File metadata and controls
551 lines (465 loc) · 17 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/**
* MITRE ATT&CK T1185: Browser Session Hijacking - Defense Implementation
* Educational demonstration of session security and hijacking detection
*
* @description Shows vulnerable vs secure session management in Express.js
* @reference https://attack.mitre.org/techniques/T1185/
*/
const crypto = require('crypto');
// =============================================================================
// SESSION STORAGE (In-Memory for Demo)
// =============================================================================
/**
* In-memory session store
* In production, use Redis, Memcached, or encrypted database storage
*/
class SessionStore {
constructor() {
this.sessions = new Map();
this.userSessions = new Map(); // Track sessions per user
}
/**
* Create a new session
* @param {string} sessionId - Unique session identifier
* @param {object} data - Session data
*/
create(sessionId, data) {
this.sessions.set(sessionId, {
...data,
createdAt: Date.now(),
lastActivity: Date.now()
});
// Track user's sessions for concurrent session limits
const userId = data.userId;
if (!this.userSessions.has(userId)) {
this.userSessions.set(userId, new Set());
}
this.userSessions.get(userId).add(sessionId);
}
/**
* Get session data
* @param {string} sessionId - Session identifier
* @returns {object|null} Session data or null
*/
get(sessionId) {
return this.sessions.get(sessionId) || null;
}
/**
* Update session data
* @param {string} sessionId - Session identifier
* @param {object} updates - Data to update
*/
update(sessionId, updates) {
const session = this.sessions.get(sessionId);
if (session) {
this.sessions.set(sessionId, { ...session, ...updates, lastActivity: Date.now() });
}
}
/**
* Delete a session
* @param {string} sessionId - Session identifier
*/
delete(sessionId) {
const session = this.sessions.get(sessionId);
if (session) {
// Remove from user's session list
const userSessionSet = this.userSessions.get(session.userId);
if (userSessionSet) {
userSessionSet.delete(sessionId);
}
this.sessions.delete(sessionId);
}
}
/**
* Get all sessions for a user
* @param {string} userId - User identifier
* @returns {Array} Array of session IDs
*/
getUserSessions(userId) {
return Array.from(this.userSessions.get(userId) || []);
}
/**
* Delete all sessions for a user
* @param {string} userId - User identifier
*/
deleteUserSessions(userId) {
const sessionIds = this.getUserSessions(userId);
sessionIds.forEach(sid => this.sessions.delete(sid));
this.userSessions.delete(userId);
}
}
// =============================================================================
// VULNERABLE SESSION MANAGEMENT
// =============================================================================
/**
* VULNERABLE: Basic session management without security controls
* ATT&CK T1185 - Browser Session Hijacking
*
* @description Vulnerable to session hijacking, fixation, and replay attacks
* @vulnerability No fingerprinting, no rotation, no hijack detection
*/
class VulnerableSessionManager {
constructor() {
this.store = new SessionStore();
}
/**
* VULNERABLE: Creates session without security controls
* @param {object} req - Request object
* @param {string} userId - User identifier
* @returns {string} Session ID
*/
createSession(req, userId) {
// VULNERABLE: Predictable session ID (just a counter or timestamp)
const sessionId = `session_${Date.now()}_${Math.random()}`;
console.log('🔴 VULNERABLE: Creating session without security controls');
this.store.create(sessionId, {
userId,
// VULNERABLE: No fingerprinting data stored
});
return sessionId;
}
/**
* VULNERABLE: Validates session without security checks
* @param {object} req - Request object
* @param {string} sessionId - Session ID
* @returns {object|null} Session data
*/
validateSession(req, sessionId) {
const session = this.store.get(sessionId);
if (!session) {
return null;
}
// VULNERABLE: No fingerprinting validation
// VULNERABLE: No hijacking detection
// VULNERABLE: Sessions never expire or rotate
console.log('🔴 VULNERABLE: Session validated without security checks');
return session;
}
}
// =============================================================================
// SECURE SESSION MANAGEMENT
// =============================================================================
/**
* SECURE: Session management with anti-hijacking controls
* Defense against ATT&CK T1185
*
* @description Implements fingerprinting, rotation, hijack detection, and session limits
* @security Multiple layers of session security
*/
class SecureSessionManager {
constructor(options = {}) {
this.store = new SessionStore();
this.config = {
maxConcurrentSessions: options.maxConcurrentSessions || 3,
sessionTimeout: options.sessionTimeout || 30 * 60 * 1000, // 30 minutes
rotationInterval: options.rotationInterval || 15 * 60 * 1000, // 15 minutes
fingerprintStrength: options.fingerprintStrength || 'medium', // low, medium, high
...options
};
}
/**
* SECURE: Creates session with security controls
* @param {object} req - Request object with IP and headers
* @param {string} userId - User identifier
* @returns {string} Session ID
*/
createSession(req, userId) {
// Defense 1: Cryptographically secure session ID
const sessionId = this.generateSecureSessionId();
// Defense 2: Create session fingerprint
const fingerprint = this.createFingerprint(req);
// Defense 3: Check concurrent session limits
this.enforceConcurrentSessionLimit(userId);
console.log('✅ SECURE: Creating session with fingerprinting and limits');
this.store.create(sessionId, {
userId,
fingerprint,
rotationDue: Date.now() + this.config.rotationInterval,
expiresAt: Date.now() + this.config.sessionTimeout
});
return sessionId;
}
/**
* SECURE: Validates session with multiple security checks
* @param {object} req - Request object
* @param {string} sessionId - Session ID
* @returns {object|null} Session data or null if invalid/hijacked
*/
validateSession(req, sessionId) {
const session = this.store.get(sessionId);
if (!session) {
console.log('🛡️ Session not found');
return null;
}
// Defense 1: Check session expiration
if (Date.now() > session.expiresAt) {
console.log('🛡️ BLOCKED: Session expired');
this.store.delete(sessionId);
return null;
}
// Defense 2: Detect session hijacking via fingerprint validation
const currentFingerprint = this.createFingerprint(req);
const hijackScore = this.detectHijacking(session.fingerprint, currentFingerprint);
if (hijackScore > 0.5) {
console.log('🛡️ BLOCKED: Possible session hijacking detected!');
console.log(` Hijack confidence: ${(hijackScore * 100).toFixed(1)}%`);
// Invalidate all user sessions on hijack detection
this.store.deleteUserSessions(session.userId);
return null;
}
// Defense 3: Session rotation
if (Date.now() > session.rotationDue) {
console.log('🔄 Session rotation required');
return { ...session, rotationRequired: true };
}
// Defense 4: Update activity timestamp
this.store.update(sessionId, {
lastActivity: Date.now(),
expiresAt: Date.now() + this.config.sessionTimeout
});
console.log('✅ Session validated successfully');
return session;
}
/**
* Rotates session ID while preserving session data
* @param {string} oldSessionId - Old session ID
* @param {object} req - Request object
* @returns {string} New session ID
*/
rotateSession(oldSessionId, req) {
const session = this.store.get(oldSessionId);
if (!session) {
return null;
}
// Create new session with same data
const newSessionId = this.generateSecureSessionId();
this.store.create(newSessionId, {
...session,
fingerprint: this.createFingerprint(req),
rotationDue: Date.now() + this.config.rotationInterval,
rotationCount: (session.rotationCount || 0) + 1
});
// Delete old session
this.store.delete(oldSessionId);
console.log('🔄 Session rotated successfully');
return newSessionId;
}
/**
* Generates cryptographically secure session ID
* @returns {string} Session ID
*/
generateSecureSessionId() {
return crypto.randomBytes(32).toString('hex');
}
/**
* Creates session fingerprint from request characteristics
* @param {object} req - Request object
* @returns {object} Fingerprint data
*/
createFingerprint(req) {
const fingerprint = {
ipAddress: req.ip || req.connection?.remoteAddress || 'unknown',
userAgent: req.headers?.['user-agent'] || 'unknown'
};
// Higher strength includes more signals (but less flexible for legitimate use)
if (this.config.fingerprintStrength === 'high') {
fingerprint.acceptLanguage = req.headers?.['accept-language'];
fingerprint.acceptEncoding = req.headers?.['accept-encoding'];
}
// Create hash of fingerprint
fingerprint.hash = this.hashFingerprint(fingerprint);
return fingerprint;
}
/**
* Hashes fingerprint data
* @param {object} fingerprint - Fingerprint data
* @returns {string} Hash
*/
hashFingerprint(fingerprint) {
const data = JSON.stringify({
ip: fingerprint.ipAddress,
ua: fingerprint.userAgent,
lang: fingerprint.acceptLanguage,
enc: fingerprint.acceptEncoding
});
return crypto.createHash('sha256').update(data).digest('hex');
}
/**
* Detects potential session hijacking by comparing fingerprints
* @param {object} original - Original fingerprint
* @param {object} current - Current fingerprint
* @returns {number} Hijack score (0-1, higher = more likely hijacked)
*/
detectHijacking(original, current) {
let suspicionScore = 0;
let checks = 0;
// Check 1: IP address change (HIGH severity)
if (original.ipAddress !== current.ipAddress) {
suspicionScore += 0.7;
console.log(`⚠️ IP changed: ${original.ipAddress} → ${current.ipAddress}`);
}
checks++;
// Check 2: User Agent change (MEDIUM severity)
if (original.userAgent !== current.userAgent) {
suspicionScore += 0.4;
console.log(`⚠️ User Agent changed`);
}
checks++;
// Check 3: Accept-Language change (LOW severity)
if (this.config.fingerprintStrength === 'high') {
if (original.acceptLanguage !== current.acceptLanguage) {
suspicionScore += 0.1;
}
checks++;
}
return Math.min(suspicionScore, 1.0);
}
/**
* Enforces concurrent session limits per user
* @param {string} userId - User identifier
*/
enforceConcurrentSessionLimit(userId) {
const userSessions = this.store.getUserSessions(userId);
if (userSessions.length >= this.config.maxConcurrentSessions) {
console.log(`🛡️ Enforcing concurrent session limit (${this.config.maxConcurrentSessions})`);
// Remove oldest session
let oldestSession = null;
let oldestTime = Infinity;
userSessions.forEach(sessionId => {
const session = this.store.get(sessionId);
if (session && session.lastActivity < oldestTime) {
oldestTime = session.lastActivity;
oldestSession = sessionId;
}
});
if (oldestSession) {
this.store.delete(oldestSession);
console.log('🗑️ Removed oldest session');
}
}
}
/**
* Destroys a session
* @param {string} sessionId - Session ID
*/
destroySession(sessionId) {
this.store.delete(sessionId);
console.log('🗑️ Session destroyed');
}
/**
* Destroys all sessions for a user
* @param {string} userId - User identifier
*/
destroyUserSessions(userId) {
this.store.deleteUserSessions(userId);
console.log('🗑️ All user sessions destroyed');
}
}
// =============================================================================
// EXPRESS MIDDLEWARE
// =============================================================================
/**
* Creates Express middleware for session management
* @param {SecureSessionManager} sessionManager - Session manager instance
* @returns {Function} Express middleware
*/
function createSessionMiddleware(sessionManager) {
return (req, res, next) => {
// Extract session ID from cookie or header
const sessionId = req.cookies?.sessionId || req.headers['x-session-id'];
if (!sessionId) {
req.session = null;
return next();
}
// Validate session
const session = sessionManager.validateSession(req, sessionId);
if (!session) {
res.clearCookie('sessionId');
req.session = null;
return next();
}
// Check if rotation is required
if (session.rotationRequired) {
const newSessionId = sessionManager.rotateSession(sessionId, req);
res.cookie('sessionId', newSessionId, {
httpOnly: true,
secure: true,
sameSite: 'strict',
maxAge: 24 * 60 * 60 * 1000 // 24 hours
});
session.sessionId = newSessionId;
}
req.session = session;
next();
};
}
// =============================================================================
// EXAMPLE USAGE
// =============================================================================
if (require.main === module) {
console.log('='.repeat(80));
console.log('MITRE ATT&CK T1185: Session Hijacking Defense Demo');
console.log('='.repeat(80));
// Mock request objects
const createMockRequest = (ip, userAgent) => ({
ip,
headers: { 'user-agent': userAgent },
connection: { remoteAddress: ip }
});
console.log('\n--- VULNERABLE SESSION MANAGER ---\n');
const vulnerableManager = new VulnerableSessionManager();
const vulnReq1 = createMockRequest('192.168.1.100', 'Mozilla/5.0 (Windows NT 10.0)');
const vulnSessionId = vulnerableManager.createSession(vulnReq1, 'user123');
console.log('Session created:', vulnSessionId);
// Simulate hijacker with different IP
const hijackerReq = createMockRequest('10.0.0.50', 'Mozilla/5.0 (Linux; Android)');
const vulnResult = vulnerableManager.validateSession(hijackerReq, vulnSessionId);
console.log('⚠️ Hijacker validated:', vulnResult ? 'YES (VULNERABLE!)' : 'NO');
console.log('\n--- SECURE SESSION MANAGER ---\n');
const secureManager = new SecureSessionManager({
maxConcurrentSessions: 2,
sessionTimeout: 30 * 60 * 1000,
rotationInterval: 15 * 60 * 1000,
fingerprintStrength: 'medium'
});
// Create session for legitimate user
console.log('1. Creating session for legitimate user:');
const secureReq1 = createMockRequest('192.168.1.100', 'Mozilla/5.0 (Windows NT 10.0)');
const secureSessionId = secureManager.createSession(secureReq1, 'user456');
console.log(' Session ID:', secureSessionId.substring(0, 16) + '...');
// Validate with legitimate user
console.log('\n2. Legitimate user validates session:');
const legitResult = secureManager.validateSession(secureReq1, secureSessionId);
console.log(' Validated:', legitResult ? 'YES' : 'NO');
// Simulate hijacking attempt
console.log('\n3. Hijacker attempts to use stolen session:');
const secureHijackerReq = createMockRequest('10.0.0.50', 'Mozilla/5.0 (Linux; Android)');
const hijackResult = secureManager.validateSession(secureHijackerReq, secureSessionId);
console.log(' Hijacker blocked:', hijackResult ? 'NO (FAILED!)' : 'YES (SUCCESS!)');
// Test concurrent session limits
console.log('\n4. Testing concurrent session limits:');
const session2 = secureManager.createSession(secureReq1, 'user789');
const session3 = secureManager.createSession(secureReq1, 'user789');
const session4 = secureManager.createSession(secureReq1, 'user789'); // Should remove oldest
console.log(' Sessions created: 3, Limit: 2, Oldest removed: YES');
// Test session rotation
console.log('\n5. Testing session rotation:');
const newSessionId = secureManager.rotateSession(session4, secureReq1);
console.log(' Old session ID:', session4.substring(0, 16) + '...');
console.log(' New session ID:', newSessionId.substring(0, 16) + '...');
console.log('\n' + '='.repeat(80));
console.log('Demo complete. Key defenses implemented:');
console.log(' ✅ Session fingerprinting (IP + User-Agent)');
console.log(' ✅ Hijacking detection with suspicion scoring');
console.log(' ✅ Automatic session rotation');
console.log(' ✅ Concurrent session limits');
console.log(' ✅ Session timeout and expiration');
console.log(' ✅ Cryptographically secure session IDs');
console.log('='.repeat(80));
}
module.exports = {
SessionStore,
VulnerableSessionManager,
SecureSessionManager,
createSessionMiddleware
};