/* Urutau - CAMPAIGNS page: sets of addresses doing the same thing. List (cards, activity Gantt, similarity matrix) at #/campaigns and detail at #/campaigns/. Data: /api/v1/campaigns and /api/v1/campaigns/{id}. Hostile data: textContent only. */ (function () { 'use strict'; var NS = 'http://www.w3.org/2000/svg'; var PER = 12; var STAGES = ['recon', 'probe', 'exploit', 'access', 'banned']; var ICON = ''; var ID_OK = /^[A-Za-z0-9_-]{1,64}$/; function boot() { var U = window.Urutau; if (!U || !U.registerPage) return false; var t = U.t, nf = U.fmt.num; function h(tag, a) { if (typeof a === 'string') a = { class: a }; return U.el.apply(null, [tag, a].concat([].slice.call(arguments, 2))); } U.registerI18n({ en: { 'cp.title': 'Campaigns', 'cp.lead.one': '{n} campaign groups {k} IPs', 'cp.lead.other': '{n} campaigns group {k} IPs', 'cp.lead.body': 'Addresses that asked for the same things {range}.', 'cp.lead.empty': 'No campaign in this slice', 'cp.lead.emptyBody': 'No set of at least three addresses behaved alike {range}.', 'cp.detail.lead': 'Campaign {id} groups {k} IPs', 'cp.cards': 'Campaigns', 'cp.ips': 'IPs', 'cp.asns': 'ASNs', 'cp.ccs': 'Countries', 'cp.sessions': 'Sessions', 'cp.window': 'Window', 'cp.techs': 'Techniques', 'cp.sig': 'Signature', 'cp.tool': 'Tool', 'cp.open': 'Open', 'cp.gantt': 'Activity', 'cp.gantt.hint': 'When each campaign was active.', 'cp.matrix': 'Similarity', 'cp.matrix.hint': 'Share of addresses two campaigns have in common.', 'cp.members': 'Members', 'cp.timeline': 'Timeline', 'cp.timeline.hint': 'Sessions of each member.', 'cp.replay': 'Replay campaign', 'cp.back': 'All campaigns', 'cp.c.ip': 'Address', 'cp.c.first': 'First seen', 'cp.c.last': 'Last seen', 'cp.c.stage': 'Stage', 'cp.c.sessions': 'Sessions', 'cp.truncated': 'The study was cut short; the list may be incomplete.', 'cp.err': 'Could not load the campaigns.', 'cp.retry': 'Try again', 'cp.unknown': 'Campaign not found', 'cp.unknownBody': 'It is not in this slice. Widen the period or go back to the list.', 'cp.stage.recon': 'Recon', 'cp.stage.probe': 'Probe', 'cp.stage.exploit': 'Exploit', 'cp.stage.access': 'Access', 'cp.stage.banned': 'Banned', 'cp.n.one': '{n} campaign', 'cp.n.other': '{n} campaigns', 'cp.more': '+{n} more' }, 'pt-BR': { 'cp.title': 'Campanhas', 'cp.lead.one': '{n} campanha agrupa {k} IPs', 'cp.lead.other': '{n} campanhas agrupam {k} IPs', 'cp.lead.body': 'Endereços que pediram as mesmas coisas {range}.', 'cp.lead.empty': 'Nenhuma campanha neste recorte', 'cp.lead.emptyBody': 'Nenhum conjunto de três ou mais endereços agiu igual {range}.', 'cp.detail.lead': 'A campanha {id} agrupa {k} IPs', 'cp.cards': 'Campanhas', 'cp.ips': 'IPs', 'cp.asns': 'ASNs', 'cp.ccs': 'Países', 'cp.sessions': 'Sessões', 'cp.window': 'Janela', 'cp.techs': 'Técnicas', 'cp.sig': 'Assinatura', 'cp.tool': 'Ferramenta', 'cp.open': 'Abrir', 'cp.gantt': 'Atividade', 'cp.gantt.hint': 'Quando cada campanha esteve ativa.', 'cp.matrix': 'Semelhança', 'cp.matrix.hint': 'Fração de endereços que duas campanhas têm em comum.', 'cp.members': 'Membros', 'cp.timeline': 'Linha do tempo', 'cp.timeline.hint': 'Sessões de cada membro.', 'cp.replay': 'Reproduzir campanha', 'cp.back': 'Todas as campanhas', 'cp.c.ip': 'Endereço', 'cp.c.first': 'Primeira vez', 'cp.c.last': 'Última vez', 'cp.c.stage': 'Estágio', 'cp.c.sessions': 'Sessões', 'cp.truncated': 'O estudo foi interrompido; a lista pode estar incompleta.', 'cp.err': 'Não foi possível carregar as campanhas.', 'cp.retry': 'Tentar de novo', 'cp.unknown': 'Campanha não encontrada', 'cp.unknownBody': 'Ela não está neste recorte. Amplie o período ou volte à lista.', 'cp.stage.recon': 'Reconhecimento', 'cp.stage.probe': 'Sondagem', 'cp.stage.exploit': 'Exploração', 'cp.stage.access': 'Acesso', 'cp.stage.banned': 'Banido', 'cp.n.one': '{n} campanha', 'cp.n.other': '{n} campanhas', 'cp.more': '+{n} mais' } }); function sec(v) { var n = Date.parse(v); return isFinite(n) ? Math.floor(n / 1000) : 0; } function iso(s) { return new Date(s * 1000).toISOString(); } function stg(s) { return STAGES.indexOf(s) >= 0 ? s : 'recon'; } function short(id) { return String(id).slice(0, 12); } function replayHref(c) { return '#/replay?campaign=' + encodeURIComponent(c.id) + '&from=' + encodeURIComponent(iso(sec(c.first) - 300)) + '&to=' + encodeURIComponent(iso(sec(c.last) + 300)); } function ipLink(ip) { return h('a', { class: 'mono cp-ip', href: '#/ip/' + encodeURIComponent(ip), text: ip }); } function chips(list, max) { var box = h('span', 'cp-chips'); (list || []).slice(0, max).forEach(function (x) { box.appendChild(h('span', { class: 'cp-chip', text: x })); }); if ((list || []).length > max) box.appendChild(h('span', { class: 'cp-chip cp-more', text: t('cp.more', { n: list.length - max }) })); return box; } function svg(tag, a) { var n = document.createElementNS(NS, tag); Object.keys(a || {}).forEach(function (k) { n.setAttribute(k, String(a[k])); }); return n; } // rows: [{label, segs: [{a, b, cls}]}], span: [min, max]; one SVG, rows 22 px high function gantt(rows, a, b, aria) { var W = 720, L = 130, R = 8, RH = 22, H = rows.length * RH + 22, span = Math.max(1, b - a); var s = svg('svg', { viewBox: '0 0 ' + W + ' ' + H, role: 'img', 'aria-label': aria, class: 'cp-gantt' }); rows.forEach(function (r, i) { var y = i * RH; s.appendChild(svg('line', { x1: L, x2: W - R, y1: y + RH - 1, y2: y + RH - 1, class: 'cp-gl' })); var lab = svg('text', { x: 0, y: y + 15, class: 'cp-gt' }); lab.textContent = r.label.length > 18 ? r.label.slice(0, 17) + '…' : r.label; s.appendChild(lab); r.segs.forEach(function (g) { var x = L + (g.a - a) / span * (W - L - R), w = Math.max(3, (g.b - g.a) / span * (W - L - R)); var rc = svg('rect', { x: x, y: y + 4, width: w, height: RH - 9, rx: 3, class: g.cls || 'cp-bar' }); var ti = svg('title'); ti.textContent = U.fmt.date(g.a) + ' → ' + U.fmt.date(g.b); rc.appendChild(ti); s.appendChild(rc); }); }); [0, 1].forEach(function (k) { var x = svg('text', { x: k ? W - R : L, y: H - 6, 'text-anchor': k ? 'end' : 'start', class: 'cp-gt' }); x.textContent = U.fmt.date(k ? b : a); s.appendChild(x); }); return h('div', 'cp-gwrap', s); } function jaccard(x, y) { var m = {}, i = 0; x.forEach(function (v) { m[v] = 1; }); y.forEach(function (v) { if (m[v]) i++; }); var u = x.length + y.length - i; return u ? i / u : 0; } function matrix(cs) { var list = cs.slice(0, 10), box = h('div', 'cp-matrix'); box.style.setProperty('--n', String(list.length)); box.appendChild(h('span', 'cp-mc')); list.forEach(function (c) { box.appendChild(h('span', { class: 'cp-mh', text: short(c.id) })); }); list.forEach(function (a) { box.appendChild(h('span', { class: 'cp-mh', text: short(a.id) })); list.forEach(function (b) { var j = jaccard(a.ips || [], b.ips || []), lv = j >= .8 ? 4 : j >= .5 ? 3 : j >= .25 ? 2 : j > 0 ? 1 : 0; box.appendChild(h('span', { class: 'cp-mc cp-lv' + lv, title: short(a.id) + ' / ' + short(b.id) + ': ' + Math.round(j * 100) + '%', text: j > 0 ? String(Math.round(j * 100)) : '' })); }); }); return box; } function stat(k, v) { return h('div', 'cp-st', h('span', { class: 'cp-sk', text: k }), h('b', { class: 'num', text: v })); } var inst = null; function Page(root, ctx) { var self = this, seq = 0, alive = true, data = null, key = ''; this.ctx = ctx; var wrap = h('div', 'cp'); root.appendChild(wrap); function id() { var s = self.ctx.params && self.ctx.params.segments || []; return s[0] || ''; } function load() { var my = ++seq, cid = id(), path = cid ? '/api/v1/campaigns/' + encodeURIComponent(cid) : '/api/v1/campaigns'; if (cid && !ID_OK.test(cid)) { data = { missing: true }; paint(); return; } U.api(path, Object.assign({ host: self.ctx.host }, cid ? {} : { page: U.pageParam(), per_page: PER }, U.rangeParams())).then(function (d) { if (!alive || my !== seq) return; if (!cid && d.page > 1 && !(d.campaigns || []).length && d.total) { U.setParam('p', d.pages); load(); return; } data = d; key = cid; paint(); }).catch(function (e) { if (!alive || my !== seq) return; if (e && e.status === 404 && cid) { data = { missing: true }; key = cid; paint(); return; } if (data) return; var b = h('button', { type: 'button', class: 'btn', text: t('cp.retry') }); b.addEventListener('click', load); wrap.textContent = ''; wrap.appendChild(h('div', 'state-error', h('strong', { text: t('cp.err') }), b)); }); } function paint() { wrap.textContent = ''; if (!data) return; if (id()) return detail(); var cs = data.campaigns || [], range = U.rangeLabel({ inline: true }); wrap.appendChild(cs.length ? U.ui.lead(U.pl('cp.lead', cs.length, { n: nf(data.total || cs.length), k: nf(data.ips_n || 0) }), t('cp.lead.body', { range: range }), 'danger') : U.ui.lead(t('cp.lead.empty'), t('cp.lead.emptyBody', { range: range }), 'neutral')); if (data.truncated) wrap.appendChild(h('p', { class: 'note', text: t('cp.truncated') })); if (!cs.length) return; var grid = h('div', 'cp-cards'); cs.forEach(function (c) { var sg = c.signature || {}; grid.appendChild(h('article', 'cp-card', h('header', null, h('a', { class: 'cp-id mono', href: '#/campaigns/' + encodeURIComponent(c.id), text: short(c.id) }), h('span', { class: 'cp-win', text: U.fmt.date(sec(c.first)) + ' → ' + U.fmt.date(sec(c.last)) })), h('div', 'cp-stats', stat(t('cp.ips'), nf(c.ips_n || 0)), stat(t('cp.asns'), nf((c.asns || []).length)), stat(t('cp.ccs'), nf((c.ccs || []).length)), stat(t('cp.sessions'), nf(c.sessions_n || 0))), h('p', 'cp-sig', h('span', { class: 'cp-sk', text: t('cp.sig') }), chips(sg.paths, 3)), sg.tool ? h('p', 'cp-sig', h('span', { class: 'cp-sk', text: t('cp.tool') }), h('span', { class: 'cp-chip', text: sg.tool })) : null, (c.techs || []).length ? h('p', 'cp-sig', h('span', { class: 'cp-sk', text: t('cp.techs') }), chips(c.techs, 4)) : null, (c.ccs || []).length ? h('p', 'cp-sig', h('span', { class: 'cp-sk', text: t('cp.ccs') }), chips((c.ccs || []).map(function (x) { return (U.fmt.flag(x) + ' ' + x).trim(); }), 6)) : null, h('footer', null, h('a', { class: 'btn', href: '#/campaigns/' + encodeURIComponent(c.id), text: t('cp.open') }), h('a', { class: 'btn', href: replayHref(c), text: t('cp.replay') })))); }); wrap.appendChild(U.ui.panel({ title: t('cp.cards'), help: 'term.campaign' }, grid, U.ui.pager({ page: data.page, pages: data.pages, total: data.total, totalText: U.pl('cp.n', data.total, { n: nf(data.total) }), onPage: function (n) { U.setParam('p', n); load(); } }))); var top = cs.slice(0, 15), mn = Infinity, mx = 0; top.forEach(function (c) { mn = Math.min(mn, sec(c.first)); mx = Math.max(mx, sec(c.last)); }); wrap.appendChild(U.ui.panel({ title: t('cp.gantt'), hint: t('cp.gantt.hint') }, gantt(top.map(function (c) { return { label: short(c.id), segs: [{ a: sec(c.first), b: sec(c.last) }] }; }), mn, Math.max(mx, mn + 1), t('cp.gantt')))); if (cs.length > 1) wrap.appendChild(U.ui.panel({ title: t('cp.matrix'), help: 'term.campaign', hint: t('cp.matrix.hint') }, matrix(cs))); } function detail() { wrap.appendChild(h('a', { class: 'btn cp-back', href: U.href('campaigns'), text: '← ' + t('cp.back') })); if (data.missing || !data.campaign) { wrap.appendChild(U.ui.empty(t('cp.unknown'), t('cp.unknownBody'))); return; } var c = data.campaign, sg = c.signature || {}; wrap.appendChild(U.ui.lead(t('cp.detail.lead', { id: short(c.id), k: nf(c.ips_n || 0) }), U.fmt.date(sec(c.first)) + ' → ' + U.fmt.date(sec(c.last)), 'danger')); var head = h('div', 'cp-card cp-wide', h('div', 'cp-stats', stat(t('cp.ips'), nf(c.ips_n || 0)), stat(t('cp.asns'), nf((c.asns || []).length)), stat(t('cp.ccs'), nf((c.ccs || []).length)), stat(t('cp.sessions'), nf(c.sessions_n || 0))), h('p', 'cp-sig', h('span', { class: 'cp-sk', text: t('cp.sig') }), chips(sg.paths, 5)), sg.tool ? h('p', 'cp-sig', h('span', { class: 'cp-sk', text: t('cp.tool') }), h('span', { class: 'cp-chip', text: sg.tool })) : null, (c.techs || []).length ? h('p', 'cp-sig', h('span', { class: 'cp-sk', text: t('cp.techs') }), chips(c.techs, 8)) : null, h('footer', null, h('a', { class: 'btn primary', href: replayHref(c), text: t('cp.replay') }))); wrap.appendChild(head); var ms = data.members || [], tb = h('tbody'); ms.forEach(function (m) { function td(l, k) { return h('td', { 'data-label': l }, k); } tb.appendChild(h('tr', 'row', td(t('cp.c.ip'), ipLink(m.ip)), td(t('cp.c.first'), h('span', { text: m.first ? U.fmt.date(m.first) : '' })), td(t('cp.c.last'), h('span', { text: m.last ? U.fmt.date(m.last) : '' })), td(t('cp.c.stage'), m.stage ? h('span', { class: 'cp-badge cp-s-' + stg(m.stage), text: t('cp.stage.' + stg(m.stage)) }) : h('span')), td(t('cp.c.sessions'), h('span', { class: 'num', text: (m.sessions || []).length ? nf(m.sessions.length) : '\u2013' })))); }); var th = h('tr'); ['ip', 'first', 'last', 'stage', 'sessions'].forEach(function (k) { th.appendChild(h('th', { text: t('cp.c.' + k) })); }); wrap.appendChild(U.ui.panel({ title: t('cp.members') + ' (' + nf(ms.length) + ')' }, h('table', 'stack', h('thead', null, th), tb))); var rows = [], mn = Infinity, mx = 0; ms.slice(0, 40).forEach(function (m) { var segs = (m.sessions || []).map(function (s) { var a = sec(s.start), b = sec(s.end); mn = Math.min(mn, a); mx = Math.max(mx, b); return { a: a, b: b, cls: 'cp-bar cp-b-' + stg(s.stage) }; }); if (segs.length) rows.push({ label: m.ip, segs: segs }); }); if (rows.length) wrap.appendChild(U.ui.panel({ title: t('cp.timeline'), hint: t('cp.timeline.hint') }, gantt(rows, mn, Math.max(mx, mn + 1), t('cp.timeline')))); } this.update = function (c) { var lang = self.ctx.lang, prev = key; self.ctx = c; if (lang !== c.lang && data) paint(); if (id() !== prev) data = null; load(); }; this.refresh = function () { load(); }; this.destroy = function () { alive = false; seq++; root.textContent = ''; }; load(); } U.registerPage({ id: 'campaigns', group: 'investigate', icon: ICON, titleKey: 'cp.title', mount: function (el, ctx) { if (inst) inst.destroy(); inst = new Page(el, ctx); }, update: function (ctx) { if (inst) inst.update(ctx); }, refresh: function () { if (inst) inst.refresh(); }, unmount: function () { if (inst) { inst.destroy(); inst = null; } } }); return true; } if (!boot()) document.addEventListener('DOMContentLoaded', boot); })();