· 4 years ago · Apr 04, 2021, 05:20 AM
1var endpoint = "wss://open-data.api.satori.com";
2var appkey = "Fd1A23752B4eCbD8Ec8E131171CdD0a8"; // check weekly for new public api key
3var channelName = "live-cyber-attack-threat-map";
4
5var client = new RTM(endpoint, appkey);
6var satoriData = [];
7
8function showText(text) {
9 var view = document.getElementById("output");
10 var record = "<div>" + text + "</div>";
11 view.innerHTML = record + view.innerHTML;
12}
13
14function updateTally(msg, table, dimension) {
15 var sumTable = document.getElementById(table);
16 let numRows = sumTable.rows.length;
17 let incremented = false;
18 let newDimension = "";
19 if (msg[dimension] === "") {
20 newDimension = "Unknown";
21 } else {
22 newDimension = msg[dimension];
23 }
24
25 for (let i = 0; i < numRows; i++) {
26 let currentDimension = sumTable.rows[i].cells[0].innerHTML;
27 let dimensionCount = parseInt(sumTable.rows[i].cells[1].innerHTML);
28 if (currentDimension === newDimension) {
29 sumTable.rows[i].cells[1].innerHTML = (dimensionCount + 1);
30 incremented = true;
31 }
32 }
33 if (incremented === false) {
34 let addDimension = sumTable.insertRow(-1);
35 let cell1 = addDimension.insertCell(0);
36 let cell2 = addDimension.insertCell(1);
37 if (msg[dimension] === "") {
38 cell1.innerHTML = "Unknown";
39 } else {
40 cell1.innerHTML = msg[dimension];
41 }
42 cell2.innerHTML = 1;
43 }
44}
45
46// https://www.w3schools.com/howto/howto_js_sort_table.asp
47function sortTable(tableName) {
48 let table, rows, switching, i, countOne, countTwo, shouldSwitch;
49 table = document.getElementById(tableName);
50 switching = true;
51
52 while (switching) {
53
54 switching = false;
55 rows = table.getElementsByTagName("TR");
56
57 for (i = 1; i < (rows.length - 1); i++) {
58 shouldSwitch = false;
59
60 countOne = rows[i].getElementsByTagName("TD")[1];
61 countTwo = rows[i + 1].getElementsByTagName("TD")[1];
62
63 if (parseInt(countOne.innerHTML) < parseInt(countTwo.innerHTML)) {
64 shouldSwitch = true;
65 break;
66 }
67 }
68 if (shouldSwitch) {
69 rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
70 switching = true;
71 }
72 }
73}
74
75
76// client.on("enter-connected", function () {
77// showText("Connected to Satori RTM!");
78// });
79client.on("leave-connected", function () {
80 showText("Disconnected from Satori RTM");
81});
82client.on("error", function (error) {
83 var reason;
84 if (error.body) {
85 // reason = error.body.error + " - " + error.body.reason;
86 showText("As of October 2017, Satori has disabled this feed.");
87 } else {
88 reason = "As of October 2017, Satori has disabled this feed.";
89 }
90 // showText("RTM client failed: " + reason);
91 showText("As of October 2017, Satori has disabled this feed.");
92});
93
94var subscription = client.subscribe(channelName, RTM.SubscriptionMode.SIMPLE, {
95 filter: 'select * from `live-cyber-attack-threat-map` where country_origin="US"'
96});
97
98subscription.on("enter-subscribed", function() {
99 showText("Subscribed to the channel: " + channelName);
100});
101subscription.on("rtm/subscribe/error", function(pdu) {
102 // showText("Failed to subscribe: " + pdu.body.error + " - " + pdu.body.reason);
103 showText("As of October 2017, Satori has disabled this feed.");
104});
105subscription.on("rtm/subscription/data", function(pdu) {
106 // Messages arrive in an array.
107 pdu.body.messages.forEach(function(msg) {
108 satoriData = satoriData.concat(msg);
109 showText("Data is received: " + JSON.stringify(msg));
110 updateTally(msg, 'attack-tally', 'attacker');
111 updateTally(msg, 'malware-tally', 'attack_type');
112 updateTally(msg, 'target-tally', 'country_target');
113 sortTable('attack-tally');
114 sortTable('malware-tally');
115 sortTable('target-tally');
116 });
117});
118
119client.start();
120