badmfck-api-server 5.0.13 → 5.0.15
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.
- package/bluegreen.help.md +116 -1
- package/dist/apiServer/APIService.d.ts +2 -0
- package/dist/apiServer/APIService.js +24 -0
- package/dist/apiServer/LogService.js +1 -0
- package/dist/apiServer/MonitorService.js +11 -0
- package/dist/apiServer/deployment/BackupManager.d.ts +33 -0
- package/dist/apiServer/deployment/BackupManager.js +174 -0
- package/dist/apiServer/deployment/Backups.d.ts +2 -0
- package/dist/apiServer/deployment/Backups.js +43 -0
- package/dist/apiServer/deployment/ConfigService.d.ts +6 -1
- package/dist/apiServer/deployment/ConfigService.js +3 -1
- package/dist/apiServer/deployment/Deployer.d.ts +2 -0
- package/dist/apiServer/deployment/Deployer.js +18 -0
- package/dist/apiServer/deployment/DeployerService.d.ts +73 -1
- package/dist/apiServer/deployment/DeployerService.js +293 -18
- package/dist/apiServer/deployment/Restore.d.ts +2 -0
- package/dist/apiServer/deployment/Restore.js +88 -0
- package/dist/apiServer/deployment/cli.js +38 -0
- package/dist/apiServer/deployment/cliCommon.d.ts +14 -0
- package/dist/apiServer/deployment/cliCommon.js +58 -0
- package/dist/apiServer/helper/ClientIp.d.ts +2 -0
- package/dist/apiServer/helper/ClientIp.js +28 -0
- package/dist/apiServer/monitor/Monitor.d.ts +1 -0
- package/dist/apiServer/monitor/Monitor.js +12 -2
- package/dist/apiServer/monitor/ThreatMonitor.d.ts +65 -0
- package/dist/apiServer/monitor/ThreatMonitor.js +153 -0
- package/dist/apiServer/monitor/index.html +105 -61
- package/dist/apiServer/structures/Interfaces.d.ts +1 -0
- package/monitoring.help.md +46 -0
- package/package.json +1 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.deployerUrl = deployerUrl;
|
|
7
|
+
exports.authHeader = authHeader;
|
|
8
|
+
exports.responseError = responseError;
|
|
9
|
+
exports.palette = palette;
|
|
10
|
+
exports.fmtDate = fmtDate;
|
|
11
|
+
exports.fmtSize = fmtSize;
|
|
12
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
13
|
+
const __1 = require("../..");
|
|
14
|
+
function deployerUrl(opt, endpoint) {
|
|
15
|
+
if (/\/add\/?$/.test(opt.host))
|
|
16
|
+
return opt.host.replace(/\/add\/?$/, "/" + endpoint);
|
|
17
|
+
if (opt.switch_host && /\/switch\/?$/.test(opt.switch_host))
|
|
18
|
+
return opt.switch_host.replace(/\/switch\/?$/, "/" + endpoint);
|
|
19
|
+
throw new Error(`Cannot derive the ${endpoint} URL from host "${opt.host}" — make sure "host" ends with /add.`);
|
|
20
|
+
}
|
|
21
|
+
function authHeader(opt) {
|
|
22
|
+
return "Bearer " + crypto_1.default.createHash("sha256").update(opt.username + ":" + opt.password).digest("hex");
|
|
23
|
+
}
|
|
24
|
+
function responseError(r) {
|
|
25
|
+
const body = r?.ok ? r?.data : r?.error;
|
|
26
|
+
const e = (body && typeof body === "object")
|
|
27
|
+
? (body.error ?? (__1.ErrorUtils.isError(body.data) ? body.data : null) ?? (__1.ErrorUtils.isError(body) ? body : null))
|
|
28
|
+
: null;
|
|
29
|
+
if (e && (e.message || e.details)) {
|
|
30
|
+
const text = [e.message, e.details].filter(Boolean).join(" — ");
|
|
31
|
+
return e.code !== undefined ? `${text} (code ${e.code})` : text;
|
|
32
|
+
}
|
|
33
|
+
if (!r?.ok) {
|
|
34
|
+
if (r?.status !== undefined)
|
|
35
|
+
return `HTTP ${r.status}`;
|
|
36
|
+
const raw = typeof r?.error === "string" ? r.error : JSON.stringify(r?.error);
|
|
37
|
+
return "network error: " + raw;
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
function palette() {
|
|
42
|
+
const on = !!process.stdout.isTTY && !process.env.NO_COLOR;
|
|
43
|
+
const c = (code) => (t) => on ? `\x1b[${code}m${t}\x1b[0m` : t;
|
|
44
|
+
return { red: c("31"), green: c("32"), yellow: c("33"), blue: c("34"), dim: c("2"), bold: c("1") };
|
|
45
|
+
}
|
|
46
|
+
function fmtDate(ms) {
|
|
47
|
+
const d = new Date(ms), p = (n) => String(n).padStart(2, "0");
|
|
48
|
+
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`;
|
|
49
|
+
}
|
|
50
|
+
function fmtSize(kb) {
|
|
51
|
+
if (kb === null || kb === undefined)
|
|
52
|
+
return "—";
|
|
53
|
+
if (kb < 1024)
|
|
54
|
+
return `${kb} KB`;
|
|
55
|
+
if (kb < 1024 * 1024)
|
|
56
|
+
return `${(kb / 1024).toFixed(1)} MB`;
|
|
57
|
+
return `${(kb / 1024 / 1024).toFixed(2)} GB`;
|
|
58
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_TRUSTED_PROXIES = void 0;
|
|
4
|
+
exports.clientIp = clientIp;
|
|
5
|
+
exports.DEFAULT_TRUSTED_PROXIES = ["127.0.0.1", "::1"];
|
|
6
|
+
const norm = (a) => {
|
|
7
|
+
let s = String(a ?? "").trim();
|
|
8
|
+
if (s.startsWith("::ffff:"))
|
|
9
|
+
s = s.slice(7);
|
|
10
|
+
return s;
|
|
11
|
+
};
|
|
12
|
+
function clientIp(req, trustedProxies = new Set(exports.DEFAULT_TRUSTED_PROXIES)) {
|
|
13
|
+
const peer = norm(req?.socket?.remoteAddress ?? req?.connection?.remoteAddress);
|
|
14
|
+
if (!peer)
|
|
15
|
+
return "unknown";
|
|
16
|
+
if (!trustedProxies.has(peer))
|
|
17
|
+
return peer;
|
|
18
|
+
const real = req?.headers?.["x-real-ip"];
|
|
19
|
+
if (typeof real === "string" && real.trim())
|
|
20
|
+
return norm(real.split(",")[0]);
|
|
21
|
+
const xff = req?.headers?.["x-forwarded-for"];
|
|
22
|
+
if (typeof xff === "string" && xff.trim()) {
|
|
23
|
+
const parts = xff.split(",").map(s => s.trim()).filter(Boolean);
|
|
24
|
+
if (parts.length)
|
|
25
|
+
return norm(parts[parts.length - 1]);
|
|
26
|
+
}
|
|
27
|
+
return peer;
|
|
28
|
+
}
|
|
@@ -38,6 +38,7 @@ export declare class Monitor extends BaseEndpoint {
|
|
|
38
38
|
httpstats(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
39
39
|
inventory(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
40
40
|
metrics(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
41
|
+
threats(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
41
42
|
logfiles(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
42
43
|
logtail(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
43
44
|
metricsWeeks(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
@@ -19,6 +19,7 @@ const SystemInfo_1 = require("./SystemInfo");
|
|
|
19
19
|
const ProcessMonitor_1 = require("./ProcessMonitor");
|
|
20
20
|
const LogFiles_1 = require("./LogFiles");
|
|
21
21
|
const MonitorAuth_1 = require("./MonitorAuth");
|
|
22
|
+
const ThreatMonitor_1 = require("./ThreatMonitor");
|
|
22
23
|
exports.S_MONITOR_REGISTRATE_ACTION = new badmfck_signal_1.Signal();
|
|
23
24
|
exports.S_MONITOR_RESTART_REQUESTED = new badmfck_signal_1.Signal();
|
|
24
25
|
const logMap = {
|
|
@@ -72,6 +73,10 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
|
72
73
|
range: "",
|
|
73
74
|
$__range_values: ["week", "day", "hour"]
|
|
74
75
|
} },
|
|
76
|
+
{ ignoreInterceptor: true, endpoint: "threats", handler: this.threats, validationModel: {
|
|
77
|
+
limit: 0,
|
|
78
|
+
$__limit_optional: true
|
|
79
|
+
} },
|
|
75
80
|
{ ignoreInterceptor: true, endpoint: "logfiles", handler: this.logfiles },
|
|
76
81
|
{ ignoreInterceptor: true, endpoint: "logtail", handler: this.logtail, validationModel: {
|
|
77
82
|
source: "",
|
|
@@ -186,7 +191,7 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
|
186
191
|
throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "No users found for monitor access, please set up users first." };
|
|
187
192
|
const { user, error, enrollmentRequired } = MonitorAuth_1.MonitorAuth.authenticate(users, req.data.login, req.data.password, req.data.totp);
|
|
188
193
|
if (!user || error) {
|
|
189
|
-
(0, LogService_1.logWarn)(`Monitor login failed for "${req.data.login}": ${error}`);
|
|
194
|
+
(0, LogService_1.logWarn)(`Monitor login failed for "${req.data.login}" from ${req.ip ?? "unknown"}: ${error}`);
|
|
190
195
|
throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: error ?? "Invalid user" };
|
|
191
196
|
}
|
|
192
197
|
if (enrollmentRequired) {
|
|
@@ -202,7 +207,7 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
|
202
207
|
} };
|
|
203
208
|
}
|
|
204
209
|
const session = MonitorAuth_1.MonitorAuth.createSession(user.login);
|
|
205
|
-
(0, LogService_1.logInfo)(`Monitor login: "${user.login}"`, "Monitor");
|
|
210
|
+
(0, LogService_1.logInfo)(`Monitor login: "${user.login}" from ${req.ip ?? "unknown"}`, "Monitor");
|
|
206
211
|
return { data: {
|
|
207
212
|
token: session.token,
|
|
208
213
|
login: user.login,
|
|
@@ -293,6 +298,11 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
|
293
298
|
const result = await MonitorService_1.REQ_STAT.request(req.data);
|
|
294
299
|
return { data: result };
|
|
295
300
|
}
|
|
301
|
+
async threats(req) {
|
|
302
|
+
await this.requireAccess(req, "logs");
|
|
303
|
+
const limit = Number(req.data.limit) || 50;
|
|
304
|
+
return { data: ThreatMonitor_1.ThreatMonitor.snapshot(limit) };
|
|
305
|
+
}
|
|
296
306
|
async logfiles(req) {
|
|
297
307
|
await this.requireAccess(req, "logs");
|
|
298
308
|
return { data: await (0, LogFiles_1.listLogSources)(this.apiServiceOptions.logFiles) };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export type TThreatCategory = "scanner" | "injection" | "enumeration" | "auth" | "method" | "not-found";
|
|
2
|
+
export type TThreatLevel = "none" | "suspicious" | "high";
|
|
3
|
+
interface ISample {
|
|
4
|
+
ts: number;
|
|
5
|
+
method: string;
|
|
6
|
+
url: string;
|
|
7
|
+
status: number;
|
|
8
|
+
categories: TThreatCategory[];
|
|
9
|
+
}
|
|
10
|
+
export interface IThreatObservation {
|
|
11
|
+
ip: string;
|
|
12
|
+
method: string;
|
|
13
|
+
url: string;
|
|
14
|
+
status: number;
|
|
15
|
+
ua?: string | null;
|
|
16
|
+
body?: unknown;
|
|
17
|
+
contentLength?: number;
|
|
18
|
+
}
|
|
19
|
+
export declare class ThreatMonitor {
|
|
20
|
+
static HALF_LIFE_MS: number;
|
|
21
|
+
static LEVEL_SUSPICIOUS: number;
|
|
22
|
+
static LEVEL_HIGH: number;
|
|
23
|
+
static ENUM_WINDOW_MS: number;
|
|
24
|
+
static ENUM_DISTINCT_PATHS: number;
|
|
25
|
+
static MAX_IPS: number;
|
|
26
|
+
static MAX_EVENTS: number;
|
|
27
|
+
static MAX_BODY_SCAN: number;
|
|
28
|
+
private static records;
|
|
29
|
+
private static events;
|
|
30
|
+
private static decayed;
|
|
31
|
+
static levelOf(score: number): TThreatLevel;
|
|
32
|
+
private static rank;
|
|
33
|
+
static classify(o: IThreatObservation): TThreatCategory[];
|
|
34
|
+
static observe(o: IThreatObservation): void;
|
|
35
|
+
static snapshot(limit?: number): {
|
|
36
|
+
generatedAt: number;
|
|
37
|
+
settings: {
|
|
38
|
+
halfLifeMinutes: number;
|
|
39
|
+
suspiciousAt: number;
|
|
40
|
+
highAt: number;
|
|
41
|
+
};
|
|
42
|
+
totals: {
|
|
43
|
+
tracked: number;
|
|
44
|
+
suspicious: number;
|
|
45
|
+
high: number;
|
|
46
|
+
};
|
|
47
|
+
offenders: {
|
|
48
|
+
ip: string;
|
|
49
|
+
score: number;
|
|
50
|
+
level: TThreatLevel;
|
|
51
|
+
firstSeen: number;
|
|
52
|
+
lastSeen: number;
|
|
53
|
+
hits: number;
|
|
54
|
+
categories: Partial<Record<TThreatCategory, number>>;
|
|
55
|
+
userAgent: string | null;
|
|
56
|
+
samples: ISample[];
|
|
57
|
+
}[];
|
|
58
|
+
recent: (ISample & {
|
|
59
|
+
ip: string;
|
|
60
|
+
score: number;
|
|
61
|
+
})[];
|
|
62
|
+
};
|
|
63
|
+
static reset(): void;
|
|
64
|
+
}
|
|
65
|
+
export {};
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ThreatMonitor = void 0;
|
|
4
|
+
const LogService_1 = require("../LogService");
|
|
5
|
+
const SCANNER_PATHS = [
|
|
6
|
+
/\/\.(?:env|git|svn|hg|aws|ssh|DS_Store|htaccess|htpasswd|npmrc|dockerenv)(?:[\/.]|$)/i,
|
|
7
|
+
/\/wp-(?:login|admin|content|includes|config|json)/i,
|
|
8
|
+
/\/(?:xmlrpc|phpinfo|phpmyadmin|adminer)\b/i,
|
|
9
|
+
/\/(?:cgi-bin|actuator|server-status|server-info|jmx-console|manager\/html)(?:\/|$)/i,
|
|
10
|
+
/\.(?:php\d?|aspx?|jsp|cgi|pl|cfm)(?:$|\?|\/)/i,
|
|
11
|
+
/(?:etc\/passwd|win\.ini|boot\.ini|id_rsa)/i,
|
|
12
|
+
];
|
|
13
|
+
const INJECTION = [
|
|
14
|
+
/\.\.(?:\/|\\|%2f|%5c)|%2e%2e(?:\/|%2f|%5c)/i,
|
|
15
|
+
/\bunion\b[\s\S]{0,40}\bselect\b|\bor\b\s+['"]?\d+['"]?\s*=\s*['"]?\d+|'\s*(?:or|and)\s+'|;\s*(?:drop|truncate)\s+table/i,
|
|
16
|
+
/<\s*script\b|javascript:|\bon(?:error|load)\s*=/i,
|
|
17
|
+
/\$\{\s*(?:jndi|env|sys|java):/i,
|
|
18
|
+
/(?:;|\|\||&&|\|)\s*(?:cat|wget|curl|bash|nc|whoami)\b|\$\((?:cat|id|whoami|curl|wget)\b/i,
|
|
19
|
+
];
|
|
20
|
+
const ODD_METHODS = new Set(["TRACE", "TRACK", "PROPFIND", "PROPPATCH", "MKCOL", "COPY", "MOVE", "LOCK", "UNLOCK", "CONNECT", "DEBUG", "SEARCH"]);
|
|
21
|
+
const WEIGHT = {
|
|
22
|
+
scanner: 10, injection: 8, enumeration: 10, method: 5, auth: 2, "not-found": 1,
|
|
23
|
+
};
|
|
24
|
+
class ThreatMonitor {
|
|
25
|
+
static HALF_LIFE_MS = 30 * 60 * 1000;
|
|
26
|
+
static LEVEL_SUSPICIOUS = 10;
|
|
27
|
+
static LEVEL_HIGH = 30;
|
|
28
|
+
static ENUM_WINDOW_MS = 5 * 60 * 1000;
|
|
29
|
+
static ENUM_DISTINCT_PATHS = 10;
|
|
30
|
+
static MAX_IPS = 5000;
|
|
31
|
+
static MAX_EVENTS = 500;
|
|
32
|
+
static MAX_BODY_SCAN = 8 * 1024;
|
|
33
|
+
static records = new Map();
|
|
34
|
+
static events = [];
|
|
35
|
+
static decayed(r, now) {
|
|
36
|
+
return r.score * Math.pow(0.5, (now - r.scoreAt) / ThreatMonitor.HALF_LIFE_MS);
|
|
37
|
+
}
|
|
38
|
+
static levelOf(score) {
|
|
39
|
+
return score >= ThreatMonitor.LEVEL_HIGH ? "high" : score >= ThreatMonitor.LEVEL_SUSPICIOUS ? "suspicious" : "none";
|
|
40
|
+
}
|
|
41
|
+
static rank(l) { return l === "high" ? 2 : l === "suspicious" ? 1 : 0; }
|
|
42
|
+
static classify(o) {
|
|
43
|
+
const cats = new Set();
|
|
44
|
+
let target = String(o.url ?? "");
|
|
45
|
+
try {
|
|
46
|
+
target = decodeURIComponent(target);
|
|
47
|
+
}
|
|
48
|
+
catch { }
|
|
49
|
+
if (SCANNER_PATHS.some(r => r.test(target)))
|
|
50
|
+
cats.add("scanner");
|
|
51
|
+
if (INJECTION.some(r => r.test(target)))
|
|
52
|
+
cats.add("injection");
|
|
53
|
+
if (o.status >= 400 && !cats.has("injection") && o.body !== undefined && o.body !== null
|
|
54
|
+
&& !(o.contentLength && o.contentLength > 64 * 1024)) {
|
|
55
|
+
let text = "";
|
|
56
|
+
try {
|
|
57
|
+
text = typeof o.body === "string" ? o.body : JSON.stringify(o.body);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
text = "";
|
|
61
|
+
}
|
|
62
|
+
if (text && INJECTION.some(r => r.test(text.slice(0, ThreatMonitor.MAX_BODY_SCAN))))
|
|
63
|
+
cats.add("injection");
|
|
64
|
+
}
|
|
65
|
+
if (ODD_METHODS.has(String(o.method).toUpperCase()))
|
|
66
|
+
cats.add("method");
|
|
67
|
+
if (o.status === 401 || o.status === 403)
|
|
68
|
+
cats.add("auth");
|
|
69
|
+
if (o.status === 404)
|
|
70
|
+
cats.add("not-found");
|
|
71
|
+
return Array.from(cats);
|
|
72
|
+
}
|
|
73
|
+
static observe(o) {
|
|
74
|
+
if (!o || !o.ip)
|
|
75
|
+
return;
|
|
76
|
+
const cats = ThreatMonitor.classify(o);
|
|
77
|
+
if (cats.length === 0)
|
|
78
|
+
return;
|
|
79
|
+
const now = Date.now();
|
|
80
|
+
let r = ThreatMonitor.records.get(o.ip);
|
|
81
|
+
if (r)
|
|
82
|
+
ThreatMonitor.records.delete(o.ip);
|
|
83
|
+
else
|
|
84
|
+
r = { ip: o.ip, score: 0, scoreAt: now, firstSeen: now, lastSeen: now, hits: 0,
|
|
85
|
+
categories: {}, ua: null, samples: [], notFound: new Map(), enumFlaggedAt: 0 };
|
|
86
|
+
const path = String(o.url ?? "").split("?")[0];
|
|
87
|
+
if (cats.includes("not-found")) {
|
|
88
|
+
r.notFound.set(path, now);
|
|
89
|
+
for (const [p, ts] of r.notFound)
|
|
90
|
+
if (now - ts > ThreatMonitor.ENUM_WINDOW_MS)
|
|
91
|
+
r.notFound.delete(p);
|
|
92
|
+
while (r.notFound.size > 50)
|
|
93
|
+
r.notFound.delete(r.notFound.keys().next().value);
|
|
94
|
+
if (r.notFound.size >= ThreatMonitor.ENUM_DISTINCT_PATHS && now - r.enumFlaggedAt > ThreatMonitor.ENUM_WINDOW_MS) {
|
|
95
|
+
cats.push("enumeration");
|
|
96
|
+
r.enumFlaggedAt = now;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const before = ThreatMonitor.decayed(r, now);
|
|
100
|
+
const add = cats.reduce((sum, c) => sum + WEIGHT[c], 0);
|
|
101
|
+
r.score = before + add;
|
|
102
|
+
r.scoreAt = now;
|
|
103
|
+
r.lastSeen = now;
|
|
104
|
+
r.hits++;
|
|
105
|
+
if (o.ua)
|
|
106
|
+
r.ua = String(o.ua).slice(0, 200);
|
|
107
|
+
for (const c of cats)
|
|
108
|
+
r.categories[c] = (r.categories[c] ?? 0) + 1;
|
|
109
|
+
const sample = { ts: now, method: String(o.method), url: LogService_1.LogService.sanitize(String(o.url ?? "")).slice(0, 200),
|
|
110
|
+
status: o.status, categories: cats };
|
|
111
|
+
r.samples.push(sample);
|
|
112
|
+
if (r.samples.length > 10)
|
|
113
|
+
r.samples.shift();
|
|
114
|
+
ThreatMonitor.records.set(o.ip, r);
|
|
115
|
+
while (ThreatMonitor.records.size > ThreatMonitor.MAX_IPS)
|
|
116
|
+
ThreatMonitor.records.delete(ThreatMonitor.records.keys().next().value);
|
|
117
|
+
if (add >= WEIGHT.auth) {
|
|
118
|
+
ThreatMonitor.events.push({ ...sample, ip: o.ip, score: Math.round(r.score) });
|
|
119
|
+
if (ThreatMonitor.events.length > ThreatMonitor.MAX_EVENTS)
|
|
120
|
+
ThreatMonitor.events.shift();
|
|
121
|
+
}
|
|
122
|
+
const prev = ThreatMonitor.levelOf(before), next = ThreatMonitor.levelOf(r.score);
|
|
123
|
+
if (ThreatMonitor.rank(next) > ThreatMonitor.rank(prev)) {
|
|
124
|
+
const allCats = Object.keys(r.categories).join(",");
|
|
125
|
+
(0, LogService_1.logWarn)(`THREAT ip=${o.ip} level=${next} score=${Math.round(r.score)} categories=${allCats} last=${o.method} ${sample.url}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
static snapshot(limit = 50) {
|
|
129
|
+
const now = Date.now();
|
|
130
|
+
const offenders = Array.from(ThreatMonitor.records.values())
|
|
131
|
+
.map(r => {
|
|
132
|
+
const score = ThreatMonitor.decayed(r, now);
|
|
133
|
+
return { ip: r.ip, score: Math.round(score * 10) / 10, level: ThreatMonitor.levelOf(score),
|
|
134
|
+
firstSeen: r.firstSeen, lastSeen: r.lastSeen, hits: r.hits,
|
|
135
|
+
categories: r.categories, userAgent: r.ua, samples: r.samples.slice().reverse() };
|
|
136
|
+
})
|
|
137
|
+
.filter(o => o.score >= 1)
|
|
138
|
+
.sort((a, b) => b.score - a.score);
|
|
139
|
+
return {
|
|
140
|
+
generatedAt: now,
|
|
141
|
+
settings: { halfLifeMinutes: ThreatMonitor.HALF_LIFE_MS / 60000, suspiciousAt: ThreatMonitor.LEVEL_SUSPICIOUS, highAt: ThreatMonitor.LEVEL_HIGH },
|
|
142
|
+
totals: {
|
|
143
|
+
tracked: offenders.length,
|
|
144
|
+
suspicious: offenders.filter(o => o.level === "suspicious").length,
|
|
145
|
+
high: offenders.filter(o => o.level === "high").length,
|
|
146
|
+
},
|
|
147
|
+
offenders: offenders.slice(0, Math.max(1, Math.min(limit, 500))),
|
|
148
|
+
recent: ThreatMonitor.events.slice(-Math.max(1, Math.min(limit, 500))).reverse(),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
static reset() { ThreatMonitor.records.clear(); ThreatMonitor.events = []; }
|
|
152
|
+
}
|
|
153
|
+
exports.ThreatMonitor = ThreatMonitor;
|