@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 CHANGED
@@ -17100,6 +17100,36 @@ async function geolocateViaThreatcrush(ip, fetchImpl = fetch) {
17100
17100
  const code = body.results?.[ip]?.countryCode;
17101
17101
  return typeof code === "string" && /^[A-Z]{2}$/.test(code) ? { countryCode: code } : null;
17102
17102
  }
17103
+ async function geolocateManyViaThreatcrush(ips, fetchImpl = fetch) {
17104
+ const out = /* @__PURE__ */ new Map();
17105
+ for (let i = 0; i < ips.length; i += 200) {
17106
+ const chunk = ips.slice(i, i + 200);
17107
+ const res = await fetchImpl("https://threatcrush.com/api/geo", {
17108
+ method: "POST",
17109
+ headers: { "Content-Type": "application/json" },
17110
+ body: JSON.stringify({ ips: chunk }),
17111
+ signal: AbortSignal.timeout(15e3)
17112
+ });
17113
+ if (!res.ok) throw new Error(`geo lookup failed: HTTP ${res.status}`);
17114
+ const body = await res.json();
17115
+ for (const ip of chunk) {
17116
+ const code = body.results?.[ip]?.countryCode;
17117
+ if (typeof code === "string" && /^[A-Z]{2}$/.test(code)) out.set(ip, code);
17118
+ }
17119
+ }
17120
+ return out;
17121
+ }
17122
+ function parseAcceptedLogins(text) {
17123
+ const out = /* @__PURE__ */ new Map();
17124
+ for (const m of text.matchAll(/Accepted \S+ for (\S+) from (\S+) port/g)) {
17125
+ const [, user, ip] = m;
17126
+ if (isProtected(ip, DEFAULT_PROTECTED)) continue;
17127
+ const ips = out.get(user) ?? /* @__PURE__ */ new Set();
17128
+ ips.add(ip);
17129
+ out.set(user, ips);
17130
+ }
17131
+ return out;
17132
+ }
17103
17133
  var PrivilegeWatch = class {
17104
17134
  constructor(opts) {
17105
17135
  this.opts = opts;
@@ -17123,7 +17153,10 @@ var PrivilegeWatch = class {
17123
17153
  }
17124
17154
  this.checkSnapshot();
17125
17155
  this.readSudoLedger();
17126
- if (this.opts.settings.geo) this.opts.bus.on("event", this.onEvent);
17156
+ if (this.opts.settings.geo) {
17157
+ this.opts.bus.on("event", this.onEvent);
17158
+ void this.backfillLogins();
17159
+ }
17127
17160
  const timer = setInterval(() => {
17128
17161
  this.checkSnapshot();
17129
17162
  this.readSudoLedger();
@@ -17198,6 +17231,42 @@ var PrivilegeWatch = class {
17198
17231
  }
17199
17232
  }
17200
17233
  }
17234
+ /**
17235
+ * Seed the login history from the last 30 days of sshd logins, once, when
17236
+ * there is none. Learning only from logins after install meant regular
17237
+ * automation from another country (a backup box pulling every four hours)
17238
+ * raised a "new country" alert on its first run; on dev2 it did, at once.
17239
+ */
17240
+ async backfillLogins() {
17241
+ if ((0, import_node_fs21.existsSync)(this.opts.loginsPath)) return;
17242
+ const out = this.run("journalctl", ["_COMM=sshd", "_COMM=sshd-session", "--since=-30d", "-o", "cat", "--no-pager"]);
17243
+ const byUser = parseAcceptedLogins(out ?? "");
17244
+ const ips = [...new Set([...byUser.values()].flatMap((s) => [...s]))];
17245
+ let codes = /* @__PURE__ */ new Map();
17246
+ if (ips.length > 0) {
17247
+ try {
17248
+ codes = await (this.opts.geolocateMany ?? geolocateManyViaThreatcrush)(ips);
17249
+ } catch (err) {
17250
+ this.opts.log(`[privileged] login backfill geolocation failed: ${err.message}`);
17251
+ return;
17252
+ }
17253
+ }
17254
+ if ((0, import_node_fs21.existsSync)(this.opts.loginsPath)) return;
17255
+ const at = this.now().toISOString();
17256
+ const history = {};
17257
+ for (const [user, userIps] of byUser) {
17258
+ for (const ip of userIps) {
17259
+ const code = codes.get(ip);
17260
+ if (code) (history[user] ??= {})[code] = { first: at, last: at };
17261
+ }
17262
+ }
17263
+ try {
17264
+ writeJson(this.opts.loginsPath, history);
17265
+ } catch {
17266
+ }
17267
+ const summary = Object.entries(history).map(([u, c]) => `${u}: ${Object.keys(c).join("/")}`).join(", ");
17268
+ this.opts.log(`[privileged] login baseline from the last 30 days: ${summary || "no public SSH logins"}`);
17269
+ }
17201
17270
  /** An accepted SSH login from a country new for that user. */
17202
17271
  async checkLogin(event) {
17203
17272
  if (event.module !== "ssh-guard" || !event.source_ip) return;