honeyweb-core 2.0.2 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,186 +1,94 @@
1
- // honeyweb-core/detection/behavioral.js
2
- // Behavioral analysis to detect bot-like patterns
3
-
4
1
  class BehavioralAnalyzer {
5
2
  constructor(config) {
6
3
  this.enabled = config.enabled !== false;
7
4
  this.suspicionThreshold = config.suspicionThreshold || 50;
8
5
  this.trackTiming = config.trackTiming !== false;
9
6
  this.trackNavigation = config.trackNavigation !== false;
10
-
11
- // Track sessions per IP
12
- this.sessions = new Map(); // ip -> { requests: [], pages: [], firstSeen: timestamp }
13
-
14
- // Auto-cleanup old sessions every 5 minutes
15
- this.cleanupInterval = setInterval(() => {
16
- this._cleanupOldSessions();
17
- }, 300000);
7
+ this.sessions = new Map();
8
+ this.cleanupInterval = setInterval(() => this._cleanupOldSessions(), 300000);
18
9
  }
19
10
 
20
- /**
21
- * Analyze request for bot-like behavior
22
- * @param {Object} req - Express request object
23
- * @param {string} ip - Client IP address
24
- * @returns {Object} - { suspicious: boolean, suspicionScore: number, reasons: string[] }
25
- */
26
11
  analyze(req, ip) {
27
- if (!this.enabled) {
28
- return { suspicious: false, suspicionScore: 0, reasons: [] };
29
- }
12
+ if (!this.enabled) return { suspicious: false, suspicionScore: 0, reasons: [] };
30
13
 
31
14
  const now = Date.now();
32
15
  const reasons = [];
33
16
  let suspicionScore = 0;
34
17
 
35
- // Get or create session
36
18
  let session = this.sessions.get(ip);
37
19
  if (!session) {
38
- session = {
39
- requests: [],
40
- pages: [],
41
- firstSeen: now
42
- };
20
+ session = { requests: [], pages: [], firstSeen: now };
43
21
  this.sessions.set(ip, session);
44
22
  }
45
23
 
46
- // 1. TIMING ANALYSIS
24
+ // Timing analysis
47
25
  if (this.trackTiming && session.requests.length > 0) {
48
- const lastRequest = session.requests[session.requests.length - 1];
49
- const timeSinceLastRequest = now - lastRequest;
26
+ const timeSinceLast = now - session.requests[session.requests.length - 1];
50
27
 
51
- // Too fast (< 100ms between requests)
52
- if (timeSinceLastRequest < 100) {
28
+ if (timeSinceLast < 100) {
53
29
  reasons.push('Requests too fast (< 100ms interval)');
54
30
  suspicionScore += 30;
55
31
  }
56
32
 
57
- // Check for consistent timing (bot pattern)
58
33
  if (session.requests.length >= 5) {
59
34
  const intervals = [];
60
35
  for (let i = 1; i < session.requests.length; i++) {
61
36
  intervals.push(session.requests[i] - session.requests[i - 1]);
62
37
  }
38
+ const avg = intervals.reduce((a, b) => a + b, 0) / intervals.length;
39
+ const variance = intervals.reduce((s, v) => s + Math.pow(v - avg, 2), 0) / intervals.length;
63
40
 
64
- // Calculate variance
65
- const avgInterval = intervals.reduce((a, b) => a + b, 0) / intervals.length;
66
- const variance = intervals.reduce((sum, interval) => {
67
- return sum + Math.pow(interval - avgInterval, 2);
68
- }, 0) / intervals.length;
69
-
70
- // Low variance = consistent timing = bot
71
- if (variance < 1000 && avgInterval < 2000) {
41
+ if (variance < 1000 && avg < 2000) {
72
42
  reasons.push('Consistent request timing (bot pattern)');
73
43
  suspicionScore += 25;
74
44
  }
75
45
  }
76
46
  }
77
47
 
78
- // 2. NAVIGATION ANALYSIS
48
+ // Navigation analysis
79
49
  if (this.trackNavigation) {
80
- const path = req.path;
81
-
82
- // Detect direct deep link access (skipping homepage)
83
- if (session.pages.length === 0 && path !== '/' && !path.startsWith('/public')) {
50
+ if (session.pages.length === 0 && req.path !== '/' && !req.path.startsWith('/public')) {
84
51
  reasons.push('Direct deep link access (skipped homepage)');
85
52
  suspicionScore += 15;
86
53
  }
87
54
 
88
- // Detect breadth-first crawling (accessing many different paths quickly)
89
- const uniquePaths = new Set(session.pages);
90
- if (uniquePaths.size > 10 && (now - session.firstSeen) < 10000) {
55
+ if (new Set(session.pages).size > 10 && (now - session.firstSeen) < 10000) {
91
56
  reasons.push('Breadth-first crawling detected');
92
57
  suspicionScore += 20;
93
58
  }
94
59
 
95
- session.pages.push(path);
60
+ session.pages.push(req.path);
96
61
  }
97
62
 
98
- // 3. SESSION ANALYSIS
99
- const sessionDuration = now - session.firstSeen;
100
- const requestCount = session.requests.length;
101
-
102
- // Short session with many requests (scraping)
103
- if (sessionDuration < 5000 && requestCount > 10) {
63
+ // Session analysis
64
+ const duration = now - session.firstSeen;
65
+ if (duration < 5000 && session.requests.length > 10) {
104
66
  reasons.push('High request rate in short session');
105
67
  suspicionScore += 20;
106
68
  }
107
-
108
- // Very long session with consistent activity (persistent bot)
109
- if (sessionDuration > 300000 && requestCount > 100) {
69
+ if (duration > 300000 && session.requests.length > 100) {
110
70
  reasons.push('Persistent automated activity');
111
71
  suspicionScore += 15;
112
72
  }
113
73
 
114
- // Record this request
115
74
  session.requests.push(now);
75
+ if (session.requests.length > 20) session.requests = session.requests.slice(-20);
76
+ if (session.pages.length > 50) session.pages = session.pages.slice(-50);
116
77
 
117
- // Keep only last 20 requests to prevent memory bloat
118
- if (session.requests.length > 20) {
119
- session.requests = session.requests.slice(-20);
120
- }
121
- if (session.pages.length > 50) {
122
- session.pages = session.pages.slice(-50);
123
- }
124
-
125
- // Cap score at 100
126
- suspicionScore = Math.min(100, suspicionScore);
127
-
128
- return {
129
- suspicious: suspicionScore >= this.suspicionThreshold,
130
- suspicionScore,
131
- reasons
132
- };
78
+ return { suspicious: Math.min(100, suspicionScore) >= this.suspicionThreshold, suspicionScore: Math.min(100, suspicionScore), reasons };
133
79
  }
134
80
 
135
- /**
136
- * Clean up old sessions (> 1 hour)
137
- * @private
138
- */
139
81
  _cleanupOldSessions() {
140
82
  const now = Date.now();
141
- const maxAge = 3600000; // 1 hour
142
-
143
83
  for (const [ip, session] of this.sessions.entries()) {
144
- if (now - session.firstSeen > maxAge) {
145
- this.sessions.delete(ip);
146
- }
84
+ if (now - session.firstSeen > 3600000) this.sessions.delete(ip);
147
85
  }
148
86
  }
149
87
 
150
- /**
151
- * Get statistics
152
- * @returns {Object}
153
- */
154
- getStats() {
155
- return {
156
- activeSessions: this.sessions.size,
157
- enabled: this.enabled
158
- };
159
- }
160
-
161
- /**
162
- * Reset session for an IP
163
- * @param {string} ip
164
- */
165
- resetSession(ip) {
166
- this.sessions.delete(ip);
167
- }
168
-
169
- /**
170
- * Clear all sessions
171
- */
172
- clear() {
173
- this.sessions.clear();
174
- }
175
-
176
- /**
177
- * Cleanup and stop timers
178
- */
179
- destroy() {
180
- if (this.cleanupInterval) {
181
- clearInterval(this.cleanupInterval);
182
- }
183
- }
88
+ getStats() { return { activeSessions: this.sessions.size, enabled: this.enabled }; }
89
+ resetSession(ip) { this.sessions.delete(ip); }
90
+ clear() { this.sessions.clear(); }
91
+ destroy() { if (this.cleanupInterval) clearInterval(this.cleanupInterval); }
184
92
  }
185
93
 
186
94
  module.exports = BehavioralAnalyzer;
@@ -1,31 +1,17 @@
1
- // honeyweb-core/detection/bot-detector.js
2
- // Bot fingerprinting - detect automated tools and headless browsers
3
-
4
1
  class BotDetector {
5
2
  constructor(config = {}) {
6
3
  this.enabled = config.enabled !== false;
7
4
  }
8
5
 
9
- /**
10
- * Detect if request is from a bot
11
- * @param {Object} req - Express request object
12
- * @returns {Object} - { isBot: boolean, confidence: number, indicators: string[] }
13
- */
14
6
  detect(req) {
15
- if (!this.enabled) {
16
- return { isBot: false, confidence: 0, indicators: [] };
17
- }
7
+ if (!this.enabled) return { isBot: false, confidence: 0, indicators: [] };
18
8
 
19
9
  const indicators = [];
20
10
  let confidence = 0;
21
-
22
11
  const userAgent = req.headers['user-agent'] || '';
23
12
  const headers = req.headers;
24
13
 
25
- // 1. USER-AGENT ANALYSIS
26
-
27
- // Known bot patterns
28
- const knownBotPatterns = [
14
+ const knownBots = [
29
15
  { pattern: /curl/i, name: 'curl', confidence: 90 },
30
16
  { pattern: /wget/i, name: 'wget', confidence: 90 },
31
17
  { pattern: /python-requests/i, name: 'Python Requests', confidence: 85 },
@@ -42,99 +28,62 @@ class BotDetector {
42
28
  { pattern: /okhttp/i, name: 'OkHttp', confidence: 75 }
43
29
  ];
44
30
 
45
- for (const { pattern, name, confidence: conf } of knownBotPatterns) {
31
+ for (const { pattern, name, confidence: conf } of knownBots) {
46
32
  if (pattern.test(userAgent)) {
47
33
  indicators.push(`Known bot User-Agent: ${name}`);
48
34
  confidence = Math.max(confidence, conf);
49
35
  }
50
36
  }
51
37
 
52
- // Empty or missing User-Agent
53
38
  if (!userAgent || userAgent.trim() === '') {
54
39
  indicators.push('Missing User-Agent header');
55
40
  confidence = Math.max(confidence, 70);
56
41
  }
57
42
 
58
- // 2. HEADER ANALYSIS
59
-
60
- // Missing common browser headers
61
- const commonHeaders = ['accept', 'accept-language', 'accept-encoding'];
62
- const missingHeaders = commonHeaders.filter(h => !headers[h]);
63
-
43
+ const missingHeaders = ['accept', 'accept-language', 'accept-encoding'].filter(h => !headers[h]);
64
44
  if (missingHeaders.length > 0) {
65
45
  indicators.push(`Missing headers: ${missingHeaders.join(', ')}`);
66
46
  confidence = Math.max(confidence, 40 + (missingHeaders.length * 10));
67
47
  }
68
48
 
69
- // Suspicious header combinations
70
- if (headers['accept'] && headers['accept'] === '*/*') {
71
- indicators.push('Generic Accept header (*/*)')
49
+ if (headers['accept'] === '*/*') {
50
+ indicators.push('Generic Accept header (*/*)');
72
51
  confidence = Math.max(confidence, 30);
73
52
  }
74
53
 
75
- // No Accept-Language (browsers always send this)
76
54
  if (!headers['accept-language']) {
77
55
  indicators.push('Missing Accept-Language header');
78
56
  confidence = Math.max(confidence, 40);
79
57
  }
80
58
 
81
- // Connection: close (common in automated tools)
82
59
  if (headers['connection'] === 'close') {
83
60
  indicators.push('Connection: close (bot pattern)');
84
61
  confidence = Math.max(confidence, 20);
85
62
  }
86
63
 
87
- // 3. BROWSER VERSION ANALYSIS
88
-
89
- // Detect outdated browser versions (bots often use old UA strings)
90
64
  const chromeMatch = userAgent.match(/Chrome\/(\d+)/);
91
- if (chromeMatch) {
92
- const version = parseInt(chromeMatch[1]);
93
- if (version < 90) {
94
- indicators.push(`Outdated Chrome version: ${version}`);
95
- confidence = Math.max(confidence, 30);
96
- }
65
+ if (chromeMatch && parseInt(chromeMatch[1]) < 90) {
66
+ indicators.push(`Outdated Chrome version: ${chromeMatch[1]}`);
67
+ confidence = Math.max(confidence, 30);
97
68
  }
98
69
 
99
- // 4. SUSPICIOUS PATTERNS
100
-
101
- // User-Agent too short (< 20 chars)
102
70
  if (userAgent.length > 0 && userAgent.length < 20) {
103
71
  indicators.push('Suspiciously short User-Agent');
104
72
  confidence = Math.max(confidence, 50);
105
73
  }
106
-
107
- // User-Agent too long (> 500 chars) - sometimes bots add extra info
108
74
  if (userAgent.length > 500) {
109
75
  indicators.push('Suspiciously long User-Agent');
110
76
  confidence = Math.max(confidence, 30);
111
77
  }
112
-
113
- // Multiple spaces in User-Agent (malformed)
114
78
  if (/\s{2,}/.test(userAgent)) {
115
79
  indicators.push('Malformed User-Agent (multiple spaces)');
116
80
  confidence = Math.max(confidence, 40);
117
81
  }
118
82
 
119
- // Cap confidence at 100
120
- confidence = Math.min(100, confidence);
121
-
122
- return {
123
- isBot: confidence >= 60,
124
- confidence,
125
- indicators
126
- };
83
+ return { isBot: Math.min(100, confidence) >= 60, confidence: Math.min(100, confidence), indicators };
127
84
  }
128
85
 
129
- /**
130
- * Get statistics
131
- * @returns {Object}
132
- */
133
- getStats() {
134
- return {
135
- enabled: this.enabled
136
- };
137
- }
86
+ getStats() { return { enabled: this.enabled }; }
138
87
  }
139
88
 
140
89
  module.exports = BotDetector;
@@ -1,7 +1,5 @@
1
- // honeyweb-core/detection/index.js
2
- // Detection orchestrator - combines all detection modules
3
-
4
1
  const { detectMaliciousPatterns } = require('./patterns');
2
+ const { detectTraversal } = require('./traversal');
5
3
  const RateLimiter = require('./rate-limiter');
6
4
  const BotWhitelist = require('./whitelist');
7
5
  const BehavioralAnalyzer = require('./behavioral');
@@ -11,12 +9,10 @@ class DetectionEngine {
11
9
  constructor(config) {
12
10
  this.config = config;
13
11
 
14
- // Initialize rate limiter
15
12
  this.rateLimiter = config.rateLimit.enabled
16
13
  ? new RateLimiter(config.rateLimit)
17
14
  : null;
18
15
 
19
- // Initialize Phase 2 detection modules
20
16
  this.whitelist = config.detection.whitelist.enabled
21
17
  ? new BotWhitelist(config.detection.whitelist)
22
18
  : null;
@@ -28,22 +24,15 @@ class DetectionEngine {
28
24
  this.botDetector = new BotDetector({ enabled: true });
29
25
  }
30
26
 
31
- /**
32
- * Analyze request for threats
33
- * @param {Object} req - Express request object
34
- * @param {string} ip - Client IP address
35
- * @returns {Promise<Object>} - { detected: boolean, threats: string[], threatLevel: number, whitelist: Object, behavioral: Object, botDetection: Object }
36
- */
37
27
  async analyze(req, ip) {
38
28
  const threats = [];
39
29
  let threatLevel = 0;
40
30
 
41
- // 0. Check whitelist first (legitimate bots should skip other checks)
31
+ // Whitelist check first legitimate bots skip all checks
42
32
  let whitelistResult = null;
43
33
  if (this.whitelist) {
44
34
  whitelistResult = await this.whitelist.check(req, ip);
45
35
  if (whitelistResult.isLegitimate) {
46
- // Legitimate bot - skip all other checks
47
36
  return {
48
37
  detected: false,
49
38
  threats: [],
@@ -54,46 +43,52 @@ class DetectionEngine {
54
43
  }
55
44
  }
56
45
 
57
- // 1. Pattern detection (SQLi/XSS)
46
+ // Pattern detection (SQLi/XSS)
58
47
  if (this.config.detection.patterns.enabled) {
59
48
  const patternResult = detectMaliciousPatterns(req);
60
49
  if (patternResult.detected) {
61
50
  threats.push(...patternResult.threats);
62
- threatLevel += 50; // High threat
51
+ threatLevel += 50;
63
52
  }
64
53
  }
65
54
 
66
- // 2. Rate limiting
55
+ // Path traversal detection
56
+ const traversalResult = detectTraversal(req);
57
+ if (traversalResult.detected) {
58
+ threats.push(...traversalResult.threats);
59
+ threatLevel += 50;
60
+ }
61
+
62
+ // Rate limiting
67
63
  let rateLimitResult = null;
68
64
  if (this.rateLimiter) {
69
65
  rateLimitResult = this.rateLimiter.check(ip);
70
66
  if (rateLimitResult.limited) {
71
67
  threats.push(`Rate limit exceeded: ${rateLimitResult.count} requests in ${this.config.rateLimit.window}ms`);
72
- threatLevel += 30; // Medium threat
68
+ threatLevel += 30;
73
69
  }
74
70
  }
75
71
 
76
- // 3. Behavioral analysis (Phase 2)
72
+ // Behavioral analysis
77
73
  let behavioralResult = null;
78
74
  if (this.behavioral) {
79
75
  behavioralResult = this.behavioral.analyze(req, ip);
80
76
  if (behavioralResult.suspicious) {
81
77
  threats.push(...behavioralResult.reasons);
82
- threatLevel += behavioralResult.suspicionScore * 0.3; // Scale down behavioral score
78
+ threatLevel += behavioralResult.suspicionScore * 0.5;
83
79
  }
84
80
  }
85
81
 
86
- // 4. Bot detection (Phase 2)
82
+ // Bot fingerprinting
87
83
  let botDetectionResult = null;
88
84
  if (this.botDetector) {
89
85
  botDetectionResult = this.botDetector.detect(req);
90
86
  if (botDetectionResult.isBot) {
91
87
  threats.push(...botDetectionResult.indicators);
92
- threatLevel += botDetectionResult.confidence * 0.2; // Scale down bot detection score
88
+ threatLevel += botDetectionResult.confidence * 0.4;
93
89
  }
94
90
  }
95
91
 
96
- // Cap threat level at 100
97
92
  threatLevel = Math.min(100, threatLevel);
98
93
 
99
94
  return {
@@ -108,10 +103,6 @@ class DetectionEngine {
108
103
  };
109
104
  }
110
105
 
111
- /**
112
- * Get detection statistics
113
- * @returns {Object}
114
- */
115
106
  getStats() {
116
107
  return {
117
108
  rateLimiter: this.rateLimiter ? this.rateLimiter.getStats() : null,
@@ -120,16 +111,9 @@ class DetectionEngine {
120
111
  };
121
112
  }
122
113
 
123
- /**
124
- * Cleanup and stop timers
125
- */
126
114
  destroy() {
127
- if (this.rateLimiter) {
128
- this.rateLimiter.destroy();
129
- }
130
- if (this.behavioral) {
131
- this.behavioral.destroy();
132
- }
115
+ if (this.rateLimiter) this.rateLimiter.destroy();
116
+ if (this.behavioral) this.behavioral.destroy();
133
117
  }
134
118
  }
135
119
 
@@ -1,9 +1,5 @@
1
- // honeyweb-core/detection/patterns.js
2
- // SQLi and XSS pattern detection (extracted from main index.js)
3
-
4
- // Malicious patterns for SQLi and XSS detection
5
1
  const MALICIOUS_PATTERNS = [
6
- // SQL Injection patterns
2
+ // SQL Injection
7
3
  /(\bUNION\b.*\bSELECT\b)/i,
8
4
  /(\bOR\b\s+\d+\s*=\s*\d+)/i,
9
5
  /(\bAND\b\s+\d+\s*=\s*\d+)/i,
@@ -14,7 +10,7 @@ const MALICIOUS_PATTERNS = [
14
10
  /(--\s*$)/,
15
11
  /(';\s*--)/,
16
12
 
17
- // XSS patterns
13
+ // XSS
18
14
  /(<script[^>]*>.*?<\/script>)/i,
19
15
  /(<iframe[^>]*>)/i,
20
16
  /(<img[^>]*onerror\s*=)/i,
@@ -25,59 +21,26 @@ const MALICIOUS_PATTERNS = [
25
21
  /(<embed[^>]*>)/i
26
22
  ];
27
23
 
28
- /**
29
- * Check if request contains malicious patterns
30
- * @param {Object} req - Express request object
31
- * @returns {Object} - { detected: boolean, threats: string[] }
32
- */
33
24
  function detectMaliciousPatterns(req) {
34
25
  const threats = [];
26
+ const targets = [
27
+ { value: req.url || '', label: 'URL' },
28
+ { value: JSON.stringify(req.query || {}), label: 'query' },
29
+ { value: req.headers['user-agent'] || '', label: 'User-Agent' },
30
+ { value: req.headers['referer'] || '', label: 'Referer' },
31
+ ];
35
32
 
36
- // Check URL
37
- const url = req.url || '';
38
- for (const pattern of MALICIOUS_PATTERNS) {
39
- if (pattern.test(url)) {
40
- threats.push(`Malicious pattern in URL: ${pattern.source.substring(0, 50)}`);
41
- }
42
- }
43
-
44
- // Check query parameters
45
- const query = JSON.stringify(req.query || {});
46
- for (const pattern of MALICIOUS_PATTERNS) {
47
- if (pattern.test(query)) {
48
- threats.push(`Malicious pattern in query: ${pattern.source.substring(0, 50)}`);
49
- }
50
- }
33
+ if (req.body) targets.push({ value: JSON.stringify(req.body), label: 'body' });
51
34
 
52
- // Check body (if exists)
53
- if (req.body) {
54
- const body = JSON.stringify(req.body);
35
+ for (const { value, label } of targets) {
55
36
  for (const pattern of MALICIOUS_PATTERNS) {
56
- if (pattern.test(body)) {
57
- threats.push(`Malicious pattern in body: ${pattern.source.substring(0, 50)}`);
37
+ if (pattern.test(value)) {
38
+ threats.push(`Malicious pattern in ${label}: ${pattern.source.substring(0, 50)}`);
58
39
  }
59
40
  }
60
41
  }
61
42
 
62
- // Check headers (User-Agent, Referer, etc.)
63
- const userAgent = req.headers['user-agent'] || '';
64
- const referer = req.headers['referer'] || '';
65
- for (const pattern of MALICIOUS_PATTERNS) {
66
- if (pattern.test(userAgent)) {
67
- threats.push(`Malicious pattern in User-Agent`);
68
- }
69
- if (pattern.test(referer)) {
70
- threats.push(`Malicious pattern in Referer`);
71
- }
72
- }
73
-
74
- return {
75
- detected: threats.length > 0,
76
- threats
77
- };
43
+ return { detected: threats.length > 0, threats };
78
44
  }
79
45
 
80
- module.exports = {
81
- MALICIOUS_PATTERNS,
82
- detectMaliciousPatterns
83
- };
46
+ module.exports = { MALICIOUS_PATTERNS, detectMaliciousPatterns };