@profullstack/threatcrush 0.13.35 → 0.13.36
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/dist/daemon.js +70 -1
- package/dist/daemon.js.map +1 -1
- package/dist/index.js +70 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -43267,6 +43267,36 @@ async function geolocateViaThreatcrush(ip, fetchImpl = fetch) {
|
|
|
43267
43267
|
const code = body.results?.[ip]?.countryCode;
|
|
43268
43268
|
return typeof code === "string" && /^[A-Z]{2}$/.test(code) ? { countryCode: code } : null;
|
|
43269
43269
|
}
|
|
43270
|
+
async function geolocateManyViaThreatcrush(ips, fetchImpl = fetch) {
|
|
43271
|
+
const out = /* @__PURE__ */ new Map();
|
|
43272
|
+
for (let i = 0; i < ips.length; i += 200) {
|
|
43273
|
+
const chunk2 = ips.slice(i, i + 200);
|
|
43274
|
+
const res = await fetchImpl("https://threatcrush.com/api/geo", {
|
|
43275
|
+
method: "POST",
|
|
43276
|
+
headers: { "Content-Type": "application/json" },
|
|
43277
|
+
body: JSON.stringify({ ips: chunk2 }),
|
|
43278
|
+
signal: AbortSignal.timeout(15e3)
|
|
43279
|
+
});
|
|
43280
|
+
if (!res.ok) throw new Error(`geo lookup failed: HTTP ${res.status}`);
|
|
43281
|
+
const body = await res.json();
|
|
43282
|
+
for (const ip of chunk2) {
|
|
43283
|
+
const code = body.results?.[ip]?.countryCode;
|
|
43284
|
+
if (typeof code === "string" && /^[A-Z]{2}$/.test(code)) out.set(ip, code);
|
|
43285
|
+
}
|
|
43286
|
+
}
|
|
43287
|
+
return out;
|
|
43288
|
+
}
|
|
43289
|
+
function parseAcceptedLogins(text) {
|
|
43290
|
+
const out = /* @__PURE__ */ new Map();
|
|
43291
|
+
for (const m of text.matchAll(/Accepted \S+ for (\S+) from (\S+) port/g)) {
|
|
43292
|
+
const [, user, ip] = m;
|
|
43293
|
+
if (isProtected(ip, DEFAULT_PROTECTED)) continue;
|
|
43294
|
+
const ips = out.get(user) ?? /* @__PURE__ */ new Set();
|
|
43295
|
+
ips.add(ip);
|
|
43296
|
+
out.set(user, ips);
|
|
43297
|
+
}
|
|
43298
|
+
return out;
|
|
43299
|
+
}
|
|
43270
43300
|
var PrivilegeWatch = class {
|
|
43271
43301
|
constructor(opts) {
|
|
43272
43302
|
this.opts = opts;
|
|
@@ -43290,7 +43320,10 @@ var PrivilegeWatch = class {
|
|
|
43290
43320
|
}
|
|
43291
43321
|
this.checkSnapshot();
|
|
43292
43322
|
this.readSudoLedger();
|
|
43293
|
-
if (this.opts.settings.geo)
|
|
43323
|
+
if (this.opts.settings.geo) {
|
|
43324
|
+
this.opts.bus.on("event", this.onEvent);
|
|
43325
|
+
void this.backfillLogins();
|
|
43326
|
+
}
|
|
43294
43327
|
const timer = setInterval(() => {
|
|
43295
43328
|
this.checkSnapshot();
|
|
43296
43329
|
this.readSudoLedger();
|
|
@@ -43365,6 +43398,42 @@ var PrivilegeWatch = class {
|
|
|
43365
43398
|
}
|
|
43366
43399
|
}
|
|
43367
43400
|
}
|
|
43401
|
+
/**
|
|
43402
|
+
* Seed the login history from the last 30 days of sshd logins, once, when
|
|
43403
|
+
* there is none. Learning only from logins after install meant regular
|
|
43404
|
+
* automation from another country (a backup box pulling every four hours)
|
|
43405
|
+
* raised a "new country" alert on its first run; on dev2 it did, at once.
|
|
43406
|
+
*/
|
|
43407
|
+
async backfillLogins() {
|
|
43408
|
+
if ((0, import_node_fs32.existsSync)(this.opts.loginsPath)) return;
|
|
43409
|
+
const out = this.run("journalctl", ["_COMM=sshd", "_COMM=sshd-session", "--since=-30d", "-o", "cat", "--no-pager"]);
|
|
43410
|
+
const byUser = parseAcceptedLogins(out ?? "");
|
|
43411
|
+
const ips = [...new Set([...byUser.values()].flatMap((s) => [...s]))];
|
|
43412
|
+
let codes = /* @__PURE__ */ new Map();
|
|
43413
|
+
if (ips.length > 0) {
|
|
43414
|
+
try {
|
|
43415
|
+
codes = await (this.opts.geolocateMany ?? geolocateManyViaThreatcrush)(ips);
|
|
43416
|
+
} catch (err) {
|
|
43417
|
+
this.opts.log(`[privileged] login backfill geolocation failed: ${err.message}`);
|
|
43418
|
+
return;
|
|
43419
|
+
}
|
|
43420
|
+
}
|
|
43421
|
+
if ((0, import_node_fs32.existsSync)(this.opts.loginsPath)) return;
|
|
43422
|
+
const at = this.now().toISOString();
|
|
43423
|
+
const history = {};
|
|
43424
|
+
for (const [user, userIps] of byUser) {
|
|
43425
|
+
for (const ip of userIps) {
|
|
43426
|
+
const code = codes.get(ip);
|
|
43427
|
+
if (code) (history[user] ??= {})[code] = { first: at, last: at };
|
|
43428
|
+
}
|
|
43429
|
+
}
|
|
43430
|
+
try {
|
|
43431
|
+
writeJson(this.opts.loginsPath, history);
|
|
43432
|
+
} catch {
|
|
43433
|
+
}
|
|
43434
|
+
const summary = Object.entries(history).map(([u, c]) => `${u}: ${Object.keys(c).join("/")}`).join(", ");
|
|
43435
|
+
this.opts.log(`[privileged] login baseline from the last 30 days: ${summary || "no public SSH logins"}`);
|
|
43436
|
+
}
|
|
43368
43437
|
/** An accepted SSH login from a country new for that user. */
|
|
43369
43438
|
async checkLogin(event) {
|
|
43370
43439
|
if (event.module !== "ssh-guard" || !event.source_ip) return;
|