Repository navigation
Expand file tree
/
Copy pathdeviceFingerprintService.js
More file actions
488 lines (418 loc) · 13.9 KB
/
Copy pathdeviceFingerprintService.js
File metadata and controls
488 lines (418 loc) · 13.9 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
/**
* Device Fingerprinting Service
*
* Generates unique device fingerprints based on browser headers, hardware specs,
* and canvas rendering to detect multi-accounting fraud.
*
* Features:
* - Canvas fingerprinting (rendering-based)
* - Hardware/consumer software analysis
* - Browser/header fingerprinting
* - IP address correlation
* - Sybil attack detection
*/
const crypto = require('crypto');
const { v4: uuidv4 } = require('uuid');
class DeviceFingerprintService {
constructor(redisClient) {
this.redis = redisClient;
// Keys for Redis storage
this.DEVICE_PREFIX = 'device:';
this.WALLET_DEVICE_MAP_PREFIX = 'wallet:device:';
this.SYBIL_FLAGGED_PREFIX = 'sybil:flagged:device:';
this.CANVAS_SAMPLES_KEY = 'device:canvas:samples';
}
/**
* Generate a comprehensive device fingerprint from multiple data points
* @param {Object} requestData - Request information including headers
* @param {Object} clientData - Client-provided fingerprinting data
* @returns {Promise<Object>} Device fingerprint details
*/
async generateFingerprint(requestData, clientData = {}) {
const {
userAgent,
acceptLanguage,
acceptEncoding,
platform,
screenResolution,
timezone,
webglVendor,
webglRenderer,
canvasHash,
audioContextHash,
fonts,
plugins,
touchSupport,
hardwareConcurrency,
deviceMemory,
} = clientData;
// Extract IP from request
const ipAddress = requestData.ip ||
requestData.headers?.['x-forwarded-for']?.split(',')[0] ||
requestData.headers?.['x-real-ip'] ||
'unknown';
// Build fingerprint components
const fingerprintComponents = {
// Browser/HTTP headers
ua: userAgent || '',
lang: acceptLanguage || '',
encoding: acceptEncoding || '',
platform: platform || '',
// Screen/Display
screen: screenResolution || '',
tz: timezone || '',
// WebGL (GPU identification)
webglV: webglVendor || '',
webglR: webglRenderer || '',
// Canvas rendering hash (if provided by client)
canvas: canvasHash || '',
// Audio context fingerprint (if provided)
audio: audioContextHash || '',
// System fonts (hash of font list)
fonts: fonts || '',
// Browser plugins
plugins: plugins || '',
// Touch support (mobile vs desktop)
touch: touchSupport || '',
// Hardware specs
cpuCores: hardwareConcurrency || '',
memory: deviceMemory || '',
// Network layer
ip: this._anonymizeIP(ipAddress),
};
// Generate composite hash
const fingerprintString = JSON.stringify(fingerprintComponents);
const deviceFingerprint = crypto
.createHash('sha256')
.update(fingerprintString)
.digest('hex');
// Generate stable device ID (persistent across sessions)
const deviceId = this._generateStableDeviceId(deviceFingerprint, ipAddress);
// Store device fingerprint data
await this._storeDeviceData(deviceId, {
fingerprint: deviceFingerprint,
components: fingerprintComponents,
firstSeen: new Date().toISOString(),
lastSeen: new Date().toISOString(),
associatedWallets: [],
riskScore: 0,
sybilFlags: [],
});
// Track wallet-to-device mapping if wallet provided
if (clientData.walletAddress) {
await this._linkWalletToDevice(clientData.walletAddress, deviceId);
}
return {
deviceId,
fingerprint: deviceFingerprint,
confidence: this._calculateConfidence(fingerprintComponents),
riskLevel: this._assessInitialRisk(fingerprintComponents),
components: fingerprintComponents,
};
}
/**
* Link a wallet address to a device ID
* @param {string} walletAddress - Stellar wallet address
* @param {string} deviceId - Unique device identifier
*/
async linkWalletToDevice(walletAddress, deviceId) {
const normalizedWallet = walletAddress.toLowerCase();
// Map wallet to device
await this.redis.hset(
`${this.WALLET_DEVICE_MAP_PREFIX}${normalizedWallet}`,
{
deviceId,
linkedAt: new Date().toISOString(),
}
);
// Add wallet to device's associated wallets list
const deviceKey = `${this.DEVICE_PREFIX}${deviceId}`;
await this.redis.sadd(`${deviceKey}:wallets`, normalizedWallet);
// Update device data
const deviceData = await this.getDeviceData(deviceId);
if (deviceData && !deviceData.associatedWallets.includes(normalizedWallet)) {
deviceData.associatedWallets.push(normalizedWallet);
deviceData.lastSeen = new Date().toISOString();
await this._storeDeviceData(deviceId, deviceData);
}
// Check for potential Sybil attack
const sybilRisk = await this._detectSybilFromWalletLink(deviceId, normalizedWallet);
return sybilRisk;
}
/**
* Get device ID for a wallet address
* @param {string} walletAddress - Wallet address
* @returns {Promise<string|null>} Device ID or null
*/
async getDeviceForWallet(walletAddress) {
const normalizedWallet = walletAddress.toLowerCase();
const deviceData = await this.redis.hgetall(
`${this.WALLET_DEVICE_MAP_PREFIX}${normalizedWallet}`
);
return deviceData?.deviceId || null;
}
/**
* Get all wallets associated with a device
* @param {string} deviceId - Device ID
* @returns {Promise<Array<string>>} List of wallet addresses
*/
async getWalletsForDevice(deviceId) {
const wallets = await this.redis.smembers(`${this.DEVICE_PREFIX}${deviceId}:wallets`);
return wallets || [];
}
/**
* Detect potential Sybil attacks based on device-wallet patterns
* @param {string} deviceId - Device ID to analyze
* @returns {Promise<Object>} Sybil analysis result
*/
async analyzeSybilRisk(deviceId) {
const deviceData = await this.getDeviceData(deviceId);
if (!deviceData) {
return { risk: 'unknown', wallets: 0, flagged: false };
}
const walletCount = deviceData.associatedWallets.length;
let riskLevel = 'low';
let flags = [];
// Multiple wallets from same device is primary Sybil indicator
if (walletCount >= 10) {
riskLevel = 'critical';
flags.push('CRITICAL_MULTI_WALLET');
} else if (walletCount >= 5) {
riskLevel = 'high';
flags.push('HIGH_MULTI_WALLET');
} else if (walletCount >= 3) {
riskLevel = 'medium';
flags.push('MEDIUM_MULTI_WALLET');
}
// Check for similar fingerprint patterns
const similarDevices = await this._findSimilarFingerprints(deviceData.fingerprint);
if (similarDevices.length > 0) {
flags.push('SIMILAR_FINGERPRINT_CLUSTER');
riskLevel = riskLevel === 'low' ? 'medium' : 'high';
}
// Update device risk score
const riskScore = this._calculateRiskScore(walletCount, flags);
await this._updateDeviceRisk(deviceId, { riskScore, flags });
// Flag as Sybil if critical risk
if (riskLevel === 'critical') {
await this._flagAsSybil(deviceId, deviceData.associatedWallets);
}
return {
deviceId,
riskLevel,
riskScore,
walletCount,
wallets: deviceData.associatedWallets,
flags,
flagged: riskLevel === 'critical' || riskLevel === 'high',
similarDevices,
};
}
/**
* Get or create device ID from request
* @param {Object} requestData - Request information
* @param {Object} clientData - Client fingerprint data
* @returns {Promise<Object>} Device identification result
*/
async identifyDevice(requestData, clientData = {}) {
// Try to identify from existing wallet
if (clientData.walletAddress) {
const existingDeviceId = await this.getDeviceForWallet(clientData.walletAddress);
if (existingDeviceId) {
// Update last seen
await this._updateDeviceLastSeen(existingDeviceId);
return {
deviceId: existingDeviceId,
isNew: false,
identified: true,
};
}
}
// Generate new fingerprint
const fingerprint = await this.generateFingerprint(requestData, clientData);
return {
deviceId: fingerprint.deviceId,
fingerprint: fingerprint.fingerprint,
isNew: true,
identified: false,
riskLevel: fingerprint.riskLevel,
};
}
/**
* Internal: Generate stable device ID using consistent hashing
* @private
*/
_generateStableDeviceId(fingerprint, ipAddress) {
// Use first 16 chars of fingerprint + anonymized IP for stability
const ipComponent = this._anonymizeIP(ipAddress).replace(/\./g, '');
return `dev_${fingerprint.substring(0, 16)}${ipComponent}`;
}
/**
* Internal: Anonymize IP address for privacy compliance
* @private
*/
_anonymizeIP(ip) {
if (!ip || ip === 'unknown') return '0.0.0.0';
// IPv4: Remove last octet
if (ip.match(/^\d+\.\d+\.\d+\.\d+$/)) {
return ip.replace(/\.\d+$/, '.0');
}
// IPv6: Truncate
if (ip.includes(':')) {
const parts = ip.split(':');
return parts.slice(0, 5).join(':') + '::0';
}
return ip;
}
/**
* Internal: Store device data in Redis
* @private
*/
async _storeDeviceData(deviceId, data) {
const deviceKey = `${this.DEVICE_PREFIX}${deviceId}`;
await this.redis.hmset(deviceKey, {
fingerprint: data.fingerprint,
firstSeen: data.firstSeen,
lastSeen: data.lastSeen,
riskScore: String(data.riskScore || 0),
sybilFlags: JSON.stringify(data.sybilFlags || []),
});
// Store associated wallets as a set
if (data.associatedWallets?.length > 0) {
await this.redis.del(`${deviceKey}:wallets`);
await this.redis.sadd(`${deviceKey}:wallets`, ...data.associatedWallets);
}
// Set TTL (90 days)
await this.redis.expire(deviceKey, 7776000);
}
/**
* Internal: Get device data from Redis
* @private
*/
async getDeviceData(deviceId) {
const deviceKey = `${this.DEVICE_PREFIX}${deviceId}`;
const data = await this.redis.hgetall(deviceKey);
if (!data || !data.fingerprint) return null;
// Parse stored values
const wallets = await this.redis.smembers(`${deviceKey}:wallets`);
return {
fingerprint: data.fingerprint,
firstSeen: data.firstSeen,
lastSeen: data.lastSeen,
riskScore: parseInt(data.riskScore, 10) || 0,
sybilFlags: JSON.parse(data.sybilFlags || '[]'),
associatedWallets: wallets,
};
}
/**
* Internal: Link wallet to device and check for Sybil patterns
* @private
*/
async _detectSybilFromWalletLink(deviceId, walletAddress) {
const analysis = await this.analyzeSybilRisk(deviceId);
if (analysis.flagged) {
console.warn(
`[DeviceFingerprint] Potential Sybil attack detected: Device ${deviceId} ` +
`has ${analysis.walletCount} wallets with risk level ${analysis.riskLevel}`
);
}
return analysis;
}
/**
* Internal: Find devices with similar fingerprints
* @private
*/
async _findSimilarFingerprints(fingerprint) {
// Compare first 8 characters (partial match)
const prefix = fingerprint.substring(0, 8);
const similarDevices = [];
// This is a simplified check - in production, use more sophisticated matching
// For now, just return count of similar prefixes
return similarDevices;
}
/**
* Internal: Calculate risk score based on various factors
* @private
*/
_calculateRiskScore(walletCount, flags) {
let score = 0;
// Base score from wallet count
score += walletCount * 10;
// Additional flags
score += flags.length * 20;
return Math.min(score, 100); // Cap at 100
}
/**
* Internal: Update device risk assessment
* @private
*/
async _updateDeviceRisk(deviceId, { riskScore, flags }) {
const deviceKey = `${this.DEVICE_PREFIX}${deviceId}`;
await this.redis.hmset(deviceKey, {
riskScore: String(riskScore),
sybilFlags: JSON.stringify(flags),
updatedAt: new Date().toISOString(),
});
}
/**
* Internal: Update device last seen timestamp
* @private
*/
async _updateDeviceLastSeen(deviceId) {
const deviceKey = `${this.DEVICE_PREFIX}${deviceId}`;
await this.redis.hmset(deviceKey, {
lastSeen: new Date().toISOString(),
});
}
/**
* Internal: Flag device as Sybil attack entity
* @private
*/
async _flagAsSybil(deviceId, wallets) {
const sybilKey = `${this.SYBIL_FLAGGED_PREFIX}${deviceId}`;
await this.redis.hmset(sybilKey, {
deviceId,
flaggedAt: new Date().toISOString(),
walletCount: String(wallets.length),
wallets: JSON.stringify(wallets),
});
// Also add to sorted set for ranking
await this.redis.zadd('sybil:devices', wallets.length, deviceId);
}
/**
* Internal: Calculate confidence score of fingerprint
* @private
*/
_calculateConfidence(components) {
let confidence = 0;
let totalFactors = 0;
// Count available fingerprinting factors
if (components.ua) totalFactors++;
if (components.canvas) totalFactors++;
if (components.webglV || components.webglR) totalFactors++;
if (components.audio) totalFactors++;
if (components.fonts) totalFactors++;
if (components.screen) totalFactors++;
if (components.cpuCores) totalFactors++;
// Higher confidence with more factors
if (totalFactors >= 5) confidence = 95;
else if (totalFactors >= 3) confidence = 75;
else if (totalFactors >= 2) confidence = 50;
else confidence = 25;
return confidence;
}
/**
* Internal: Assess initial risk level
* @private
*/
_assessInitialRisk(components) {
// Basic risk assessment on initial fingerprint
// More detailed analysis happens when linking wallets
// Tor/Proxy detection (simplified)
if (components.encoding?.includes('br') && !components.platform) {
return 'medium';
}
return 'low';
}
}
module.exports = { DeviceFingerprintService };