/* Urutau - MITRE ATT&CK page: tactics x techniques matrix, radar per tactic and ranking with trend. Data: /api/v1/techniques. Drill-down reads /api/v1/events and keeps the events that carry the technique. Log data is hostile: dynamic text goes through textContent only. */ (function () { 'use strict'; var TECH_RE = /^T\d{4}(\.\d{3})?$/; var DRILL_LIMIT = 5000; function boot() { var U = window.Urutau; if (!U || !U.registerPage) return false; U.registerI18n({ en: { 'mt.title': 'MITRE ATT&CK', 'mt.lead.some': '{n} techniques in {m} tactics {range}', 'mt.lead.sub': '{n} events matched a known technique.', 'mt.lead.none': 'No technique observed {range}', 'mt.lead.none.sub': 'Nothing matched the ATT&CK mapping in this window.', 'mt.matrix': 'Which tactics and techniques hit us?', 'mt.matrix.hint': 'Columns are tactics, cells are techniques; the darker, the more events. Click a cell to see who used it.', 'mt.radar': 'Pressure per tactic', 'mt.rank': 'Techniques by volume', 'mt.col.tech': 'Technique', 'mt.col.events': 'Events', 'mt.col.ips': 'IPs', 'mt.col.trend': 'Trend', 'mt.trend.new': 'new', 'mt.trend.vs': 'against the previous period of the same length', 'mt.cell': '{name}: {events} events, {ips} IPs', 'mt.drill.title': 'Who used {id}', 'mt.drill.loading': 'Loading events...', 'mt.drill.none': 'No recent event with this technique in the loaded sample.', 'mt.drill.note': 'Showing {n} of the most recent events ({ips} IPs).', 'mt.drill.ips': 'IPs', 'mt.drill.events': 'Latest events', 'mt.drill.open': 'Open on attack.mitre.org', 'mt.drill.close': 'Close', 'mt.error': 'Could not load the techniques.', 'mt.disabled': 'The event registry is off', 'mt.disabled.sub': 'Techniques are read from the event registry; enable it to see this page.', 'mt.empty': 'No technique in this window', 'mt.partial': 'Partial result: the study hit its time or size limit.' }, 'pt-BR': { 'mt.title': 'MITRE ATT&CK', 'mt.lead.some': '{n} técnicas em {m} táticas {range}', 'mt.lead.sub': '{n} eventos corresponderam a uma técnica conhecida.', 'mt.lead.none': 'Nenhuma técnica observada {range}', 'mt.lead.none.sub': 'Nada correspondeu ao mapeamento ATT&CK nesta janela.', 'mt.matrix': 'Que táticas e técnicas nos atingem?', 'mt.matrix.hint': 'As colunas são táticas e as células, técnicas; quanto mais escuro, mais eventos. Clique numa célula para ver quem a usou.', 'mt.radar': 'Pressão por tática', 'mt.rank': 'Técnicas por volume', 'mt.col.tech': 'Técnica', 'mt.col.events': 'Eventos', 'mt.col.ips': 'IPs', 'mt.col.trend': 'Tendência', 'mt.trend.new': 'nova', 'mt.trend.vs': 'em relação ao período anterior de mesma duração', 'mt.cell': '{name}: {events} eventos, {ips} IPs', 'mt.drill.title': 'Quem usou {id}', 'mt.drill.loading': 'Carregando eventos...', 'mt.drill.none': 'Nenhum evento recente com esta técnica na amostra carregada.', 'mt.drill.note': 'Mostrando {n} dos eventos mais recentes ({ips} IPs).', 'mt.drill.ips': 'IPs', 'mt.drill.events': 'Últimos eventos', 'mt.drill.open': 'Abrir em attack.mitre.org', 'mt.drill.close': 'Fechar', 'mt.error': 'Não foi possível carregar as técnicas.', 'mt.disabled': 'O registro de eventos está desligado', 'mt.disabled.sub': 'As técnicas vêm do registro de eventos; ative-o para ver esta página.', 'mt.empty': 'Nenhuma técnica nesta janela', 'mt.partial': 'Resultado parcial: o estudo atingiu o limite de tempo ou de tamanho.' } }); var el = U.el, t = U.t, nf = U.fmt.num; var root = null, seq = 0, alive = false, ctx = null, data = null, errBox = null, sel = null, drillSeq = 0; function clear(n) { while (n.firstChild) n.removeChild(n.firstChild); } function when(s) { return s ? U.fmt.ago(s) : ''; } function techURL(id) { if (!TECH_RE.test(id)) return 'https://attack.mitre.org/techniques/'; return 'https://attack.mitre.org/techniques/' + id.replace('.', '/') + '/'; } function trendNode(row) { if (row.trend_pct === null || row.trend_pct === undefined) return el('span', { class: 'mt-trend mt-new', title: t('mt.trend.vs') }, t('mt.trend.new')); var v = row.trend_pct, up = v > 0.5, down = v < -0.5; var txt = (up ? '▲ ' : down ? '▼ ' : '■ ') + Math.abs(Math.round(v)) + '%'; return el('span', { class: 'mt-trend ' + (up ? 'mt-up' : down ? 'mt-down' : 'mt-flat'), title: t('mt.trend.vs') }, txt); } function renderMatrix(tactics, max) { var grid = el('div', { class: 'mt-matrix' }); tactics.forEach(function (ta) { var col = el('div', { class: 'mt-col' }); var total = ta.techniques.reduce(function (a, x) { return a + x.events; }, 0); col.appendChild(el('div', { class: 'mt-th' }, el('strong', null, ta.name), el('span', { class: 'mt-th-sub' }, (ta.id || '-') + ' · ' + nf(total)))); ta.techniques.forEach(function (tc) { var ratio = Math.max(0.08, Math.min(1, tc.events / max)); var b = el('button', { type: 'button', class: 'mt-cell' + (sel === tc.id ? ' on' : ''), style: { '--i': ratio.toFixed(3) }, title: t('mt.cell', { name: tc.name, events: nf(tc.events), ips: nf(tc.ips) }) }, el('span', { class: 'mt-id' }, tc.id), el('span', { class: 'mt-name' }, tc.name), el('span', { class: 'mt-n num' }, nf(tc.events))); b.addEventListener('click', function () { sel = sel === tc.id ? null : tc.id; paint(); }); col.appendChild(b); }); grid.appendChild(col); }); return grid; } function renderRank(rows) { var tb = el('tbody'); rows.slice(0, 15).forEach(function (r) { tb.appendChild(el('tr', null, el('td', null, el('a', { class: 'mt-link', href: techURL(r.id), target: '_blank', rel: 'noopener noreferrer' }, r.id), ' ', r.name), el('td', { class: 'num' }, nf(r.events)), el('td', { class: 'num' }, nf(r.ips)), el('td', null, trendNode(r)))); }); return el('div', { class: 'mt-tablewrap' }, el('table', { class: 'mt-table' }, el('thead', null, el('tr', null, el('th', null, t('mt.col.tech')), el('th', { class: 'num' }, t('mt.col.events')), el('th', { class: 'num' }, t('mt.col.ips')), el('th', null, t('mt.col.trend')))), tb)); } var drillBox = null; function drill(tc) { var my = ++drillSeq; if (!drillBox) return; clear(drillBox); drillBox.appendChild(el('p', { class: 'hint' }, t('mt.drill.loading'))); var p = ctx && ctx.from ? { from: ctx.from, to: ctx.to } : U.rangeParams(); var q = {}; Object.keys(p).forEach(function (k) { q[k] = p[k]; }); if (ctx && ctx.host && ctx.host !== 'all') q.host = ctx.host; q.limit = DRILL_LIMIT; U.api('/api/v1/events', q).then(function (d) { if (!alive || my !== drillSeq) return; var evs = (d.events || []).filter(function (e) { return Array.isArray(e.tech) && e.tech.indexOf(tc.id) >= 0; }); var byIP = {}; evs.forEach(function (e) { byIP[e.ip] = (byIP[e.ip] || 0) + 1; }); var ips = Object.keys(byIP).sort(function (a, b) { return byIP[b] - byIP[a]; }); clear(drillBox); drillBox.appendChild(el('div', { class: 'mt-drill-h' }, el('h3', null, t('mt.drill.title', { id: tc.id }) + ' · ' + tc.name), el('a', { class: 'btn', href: techURL(tc.id), target: '_blank', rel: 'noopener noreferrer' }, t('mt.drill.open')), (function () { var b = el('button', { type: 'button', class: 'btn' }, t('mt.drill.close')); b.addEventListener('click', function () { sel = null; paint(); }); return b; })())); if (!evs.length) { drillBox.appendChild(el('p', { class: 'hint' }, t('mt.drill.none'))); return; } drillBox.appendChild(el('p', { class: 'hint' }, t('mt.drill.note', { n: nf(evs.length), ips: nf(ips.length) }))); var chips = el('div', { class: 'mt-ips' }); ips.slice(0, 24).forEach(function (ip) { var b = el('button', { type: 'button', class: 'chip' }, ip + ' ×' + nf(byIP[ip])); b.addEventListener('click', function () { U.openIP(ip); }); chips.appendChild(b); }); var tb = el('tbody'); evs.slice(0, 20).forEach(function (e) { tb.appendChild(el('tr', null, el('td', null, e.t ? U.fmt.clock(e.t) : ''), el('td', null, String(e.ip || '')), el('td', null, String(e.cat || '')), el('td', { class: 'mt-path' }, String(e.path || e.user || e.jail || '')))); }); drillBox.appendChild(el('h4', null, t('mt.drill.ips'))); drillBox.appendChild(chips); drillBox.appendChild(el('h4', null, t('mt.drill.events'))); drillBox.appendChild(el('div', { class: 'mt-tablewrap' }, el('table', { class: 'mt-table' }, tb))); }).catch(function () { if (!alive || my !== drillSeq) return; clear(drillBox); drillBox.appendChild(el('p', { class: 'hint' }, t('mt.error'))); }); } function paint() { if (!root) return; clear(root); drillBox = null; if (!data) { root.appendChild(el('div', { class: 'skeleton', style: { height: '8rem' } })); return; } if (!data.enabled) { root.appendChild(U.ui.empty(t('mt.disabled'), t('mt.disabled.sub'))); return; } var tactics = data.tactics || [], all = []; tactics.forEach(function (ta) { ta.techniques.forEach(function (x) { all.push(x); }); }); var range = U.rangeLabel({ inline: true }); if (!all.length) { root.appendChild(U.ui.lead(t('mt.lead.none', { range: range }), t('mt.lead.none.sub'), 'ok')); root.appendChild(U.ui.empty(t('mt.empty'))); return; } root.appendChild(U.ui.lead(t('mt.lead.some', { n: nf(all.length), m: nf(tactics.length), range: range }), t('mt.lead.sub', { n: nf(data.total) }), 'danger')); if (data.truncated) root.appendChild(el('p', { class: 'hint' }, t('mt.partial'))); var max = Math.max.apply(null, all.map(function (x) { return x.events; })); var grid = el('div', { class: 'grid-12' }); var mp = U.ui.panel({ title: t('mt.matrix'), hint: t('mt.matrix.hint'), cls: 'span-12' }, renderMatrix(tactics, max)); drillBox = el('div', { class: 'mt-drill' }); if (sel) { mp.body.appendChild(drillBox); } else drillBox = null; grid.appendChild(mp); var axes = tactics.map(function (ta) { return { key: ta.id || 'other', label: ta.name }; }); var vals = tactics.map(function (ta) { return ta.techniques.reduce(function (a, x) { return a + x.events; }, 0); }); var rp = U.ui.panel({ title: t('mt.radar'), cls: 'span-4' }); if (U.charts && U.charts.radar && axes.length >= 3) rp.body.appendChild(U.charts.radar(axes, [{ label: t('mt.col.events'), values: vals }], { title: t('mt.radar'), format: nf })); else if (U.charts && U.charts.bars) rp.body.appendChild(U.charts.bars(tactics.map(function (ta, i) { return { key: ta.id || 'other', label: ta.name, value: vals[i] }; }), { title: t('mt.radar'), format: nf })); grid.appendChild(rp); grid.appendChild(U.ui.panel({ title: t('mt.rank'), cls: 'span-8' }, renderRank(all.slice().sort(function (a, b) { return b.events - a.events; })))); root.appendChild(grid); if (sel) { var tc = all.filter(function (x) { return x.id === sel; })[0]; if (tc) drill(tc); else { sel = null; } } } function load() { var my = ++seq; U.api('/api/v1/techniques', Object.assign(U.rangeParams(), ctx && ctx.host && ctx.host !== 'all' ? { host: ctx.host } : {})).then(function (d) { if (!alive || my !== seq) return; data = d; paint(); }).catch(function () { if (!alive || my !== seq || data) return; clear(root); root.appendChild(el('div', { class: 'state-error' }, el('strong', null, t('mt.error')))); }); } U.registerPage({ id: 'mitre', group: 'analyze', titleKey: 'mt.title', icon: '', mount: function (node, cx) { root = el('div', { class: 'mt-page' }); node.appendChild(root); ctx = cx; alive = true; data = null; sel = null; paint(); load(); }, update: function (cx) { ctx = cx; load(); }, refresh: function () { if (!sel) load(); }, unmount: function () { alive = false; seq++; drillSeq++; if (root && root.parentNode) root.parentNode.removeChild(root); root = null; data = null; } }); return true; } if (!boot()) { var tries = 0, iv = setInterval(function () { if (boot() || ++tries > 50) clearInterval(iv); }, 100); } })();