Skip to content

Commit

Permalink
New formatting from MiuGIo.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frankccv committed Sep 11, 2023
1 parent ee38319 commit e9f76ac
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 62 deletions.
191 changes: 183 additions & 8 deletions www/public/js/chart/tab.remediation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var arr = [
{
id: "security",

title: "Remediation",
title: "Remediation Attacks",

x: 0,

Expand All @@ -17,7 +17,18 @@ var arr = [
userData: {
fn: "createRemediationReport",
},
},
},{
id: "vulnerabilities",
title: "Remediation to Vulnerabilities",
x: 0,
y: 0,
width: 12,
height: 5,
type: "info",
userData: {
fn: "createVulnerabilityReport"
}
}
];


Expand Down Expand Up @@ -65,7 +76,7 @@ ReportFactory.createRemediationReport = function (fPeriod) {


const database = new MMTDrop.Database({
collection: "remediation",
collection: "remediationAttack",

query: [],

Expand Down Expand Up @@ -121,7 +132,7 @@ ReportFactory.createRemediationReport = function (fPeriod) {
// data[i]["button"]="<a class='sancus-button' href='/sancus/remediation?cid=" + data[i]["CID"] + "'>button</a>"

//data[i]["status"]= `<button id="sancus-buttton" style="background-color: #f1f1f1; border: none; padding: 0; cursor: pointer;"> <img id="red" src="../img/red_button.jpg" alt="Image" style="width: 20px; height: 20px;">`
data[i]["status"]= `<button id="sancus-buttton`+i+`" style="background-color: #f1f1f1; border: none; padding: 0; cursor: pointer;"> <img id=red_`+i+` src="../img/red_button.jpg" alt="Image" style="width: 20px; height: 20px;">`
data[i]["status"]= `<button id="sancus-buttton`+i+`" style="background-color: #f1f1f1; border: none; padding: 0; cursor: pointer;"> <img id=redAtt_`+i+` src="../img/red_button.jpg" alt="Image" style="width: 20px; height: 20px;"> <span id = spanAtt_`+ i +`>Apply Remediation</span> `;
data[i]["Row"]=i+1; //initialize column of rows with correct number

// data[i]["status"]=`<button class="btn-primary" type="object" style="backgound-color:red"/>`;
Expand All @@ -138,7 +149,7 @@ ReportFactory.createRemediationReport = function (fPeriod) {
{id : "Row",label:"Row"},
{id : "CID", label: "CID" },
{id : "description", label: "description"} ,
{id : "attack", label: "Attack/Vulnerability"} ,
{id : "attack", label: "Attack"} ,
{id : "timestamp", label: "Timestamp"} ,
{id : "ipAttack", label: "IP attacker"} ,
{id : "status", label: "status"}
Expand Down Expand Up @@ -168,7 +179,7 @@ ReportFactory.createRemediationReport = function (fPeriod) {
if( row_data == undefined )
return;
//console.log(row_data[0]);//Access to first element of the row array
const url = "/sancus/remediation?CID=" + row_data[1] + "&IP=" + row_data[4];//Access to 2 column of CID
const url = "/sancus/remediation?CID=" + row_data[1] + "&IP=" + row_data[5];//Access to 2 column of CID

MMTDrop.tools.ajax(url, {}, "POST", {
error : function(){
Expand All @@ -178,9 +189,11 @@ ReportFactory.createRemediationReport = function (fPeriod) {
MMTDrop.alert.success("Remediation successfully sent ", 10*1000);
// var button = document.getElementById("sancus-buttton");
//console.log(row_data[4]);
var image = document.getElementById("red_"+index);
var image = document.getElementById("redAtt_"+index);
image.src = "../img/green_button.png";
image.style= "width: 20px; height: 20px";
image.style = "width: 20px; height: 20px";
var text = document.getElementById("spanAtt_"+index);
text.textContent = "Remediation applied" ;
// button.style.backgroundColor = "green";
// console.log(row_data[3])

Expand Down Expand Up @@ -226,3 +239,165 @@ ReportFactory.createRemediationReport = function (fPeriod) {
return report;
};

ReportFactory.createVulnerabilityReport = function( fperiod) {

const database = new MMTDrop.Database({
collection: "remediationVuln",

query: [],

action: "aggregate",//aggregate

raw: true,

no_group: true,

no_override_when_reload: true,
}, function( data ){
//got data from DB
console.log("Data from db"+ data)
//do any processing here if need
return data;
}, false);
database.updateParameter = function (_old_param) {
fProbe.hide()
console.log( fPeriod.selectedOption().id )
console.log( fProbe.selectedOption().id )

const $match = {
//"value" : {"$gt": 1}

};

//$match[ 1 ] = URL_PARAM.app_id(); //app id

//$match["2"] = URL_PARAM.probe_id;

//s$match["3"] = URL_PARAM.metric_id;

return { query: [{ $match: $match }] };
};

var cTable = MMTDrop.chartFactory.createTable({
getData: {
getDataFn: function (db) {
const data = db.data();

console.log("Database data: " + data);


for (var i = 0; i < data.length; i++){
//data[i]["button"] ='<button type="button" onclick="f()">Click me!</button>';
//data[i]["button"] = "<a href='/sancus/remediation?cid=" + data[i]["CID"] + "'>Send orchestrator</a>"
//data[i]["button"] = '<form action="/sancus/remediation?CID='+data[i]["CID"]+'" method="POST"> <button type="submit">Send POST Request</button>'
//data[i]["button"] = "<a href='/sancus/remediation?value=" + data[i]["value"] + "&description="+data[i]["description"]+"'>button</a>"
// data[i]["button"]="<a class='sancus-button' href='/sancus/remediation?cid=" + data[i]["CID"] + "'>button</a>"

//data[i]["status"]= `<button id="sancus-buttton" style="background-color: #f1f1f1; border: none; padding: 0; cursor: pointer;"> <img id="red" src="../img/red_button.jpg" alt="Image" style="width: 20px; height: 20px;">`
data[i]["status"]= `<button id="sancus-buttton`+i+`" style="background-color: #f1f1f1; border: none; padding: 0; cursor: pointer;"> <img id=red_`+i+` src="../img/red_button.jpg" alt="Image" style="width: 20px; height: 20px;"> <span id= span_`+ i +`>Apply Remediation</span> `;
data[i]["Row"]=i+1; //initialize column of rows with correct number

// data[i]["status"]=`<button class="btn-primary" type="object" style="backgound-color:red"/>`;


//data[i]["button"]= "<a href='/sancus/remediation'>bottone</a>"
// data[i]["button"]='<button type="button" class="sancus-button" >Send to Orchestrator</button>';
// <a>http:/ /localhost:8080/sancus/remediation</a>
}

return {
columns: [
//{id: 1, label: "App ID"},
{id : "Row",label:"Row"},
{id : "CID", label: "CID" },
{id : "description", label: "description"} ,
{id : "attack", label: "Vulnerability id"} ,
{id : "timestamp", label: "Timestamp"} ,
{id : "ipAttack", label: "IP attacker"} ,
{id : "status", label: "status"}
],

data: data,
};
},
},

chart: {
//order: [[1, "asc"]],

dom: "<'row'<'col-sm-5'l><'col-sm-7'f>><'row-cursor-pointer't><'row'<'col-sm-3'i><'col-sm-9'p>>",

deferRender: true,
},

afterEachRender: function( _chart ){
// Add event listener for opening and closing details
_chart.chart.on('click', 'tr[role=row]', function (){
var tr = $(this);
var row = _chart.chart.api().row(tr);
var index = row.data()[0] - 1;

var row_data = row.data();
if( row_data == undefined )
return;
//console.log(row_data[0]);//Access to first element of the row array
const url = "/sancus/remediation?CID=" + row_data[1] + "&IP=" + row_data[5];//Access to 2 column of CID

MMTDrop.tools.ajax(url, {}, "POST", {
error : function(){
MMTDrop.alert.error("Cannot send remediation", 10*1000);
},
success: function(){
MMTDrop.alert.success("Remediation successfully sent ", 10*1000);
// var button = document.getElementById("sancus-buttton");
//console.log(row_data[4]);
var image = document.getElementById("red_"+index);
image.src = "../img/green_button.png";
image.style= "width: 20px; height: 20px";
var text = document.getElementById("span_"+index);
text.textContent= "Remediation applied" ;
// button.style.backgroundColor = "green";
// console.log(row_data[3])

}
})


});

}
});



var report = new MMTDrop.Report(
// title

null,

// database

database,

// filers

[],

// charts

[
{
charts: [cTable],

width: 12,
},
],

//order of data flux

[{ object: cTable }]
);

return report;

};
69 changes: 33 additions & 36 deletions www/reportReader/busReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ default:
async function queryIpMongo( attackId ) {
const url = `mongodb://` + config.database_server.host+ `:` + config.database_server.port;
const dbName = 'mmt-data'; // Replace with your database name
var ipAttacker='';
var ipAttacker='10.10.10.2';
// MongoDB aggregation pipeline
const pipeline = [
{ $match: { "4": attackId } },
Expand All @@ -57,8 +57,7 @@ async function queryIpMongo( attackId ) {
ipAttacker = result[0].ipSrcValue;

}
else
ipAttacker="10.10.10.2";

} catch (err) {
console.error('Error:', err);
} finally {
Expand All @@ -70,32 +69,22 @@ async function queryIpMongo( attackId ) {
}
async function extractDescriptions(json1, json2) {
// Initialize an empty array to store the output JSON objects
const output = [];
var ipAttacker = "10.2.2.3" ;

// Loop through the input array
for (let i = 0; i < json1.length; i++) {
// Find the corresponding description from json2
const [CID, attack] = json1[i];
const currentTimestampInSeconds = Math.floor(new Date().getTime() / 1000);

const descriptionObj = json2.find( (item) => item.CID === CID);
ipAttacker = await queryIpMongo( attack[0][0] );
// Create the output JSON object
const jsonObj = {
CID:CID,
attack: attack[0],
timestamp: currentTimestampInSeconds,
ipAttack : ipAttacker,
description: descriptionObj ? descriptionObj.description : "" ,//if there is no description this value will be empty string
};
// Push the jsonObj into the output array

output.push(jsonObj);
}

return output;

const outputJson = {};
var ipAttacker = "10.2.2.3" ;

if( typeof json1[0] === 'string'){
ipAttacker = await queryIpMongo( json1[0] );
const currentTimestampInSeconds = Math.floor(new Date().getTime() / 1000);
const descriptionObj = json2.find( (item) => item.CID === json1[0]);

outputJson.CID = json1[0];
outputJson.attack = json1[1][0]
outputJson.description = descriptionObj ? descriptionObj.description : "";
outputJson.ipAttack = ipAttacker ;
outputJson.timestamp = currentTimestampInSeconds ;

}
return outputJson;
}

function receiveMessage (channel, message) {
Expand All @@ -120,22 +109,30 @@ report_client_miugio.on('message', async function ( channel,message) {
//message is a string {id:0, []}
//Process message: it should have a particular structure
try{
console.log("Received message on Miugio topic "+message+" "+config.databaseName);
console.log("Received message on Miugio topic " + message + " "+config.databaseName);
//Insert to Databases
const json1 = JSON.parse( message);//Convert MIU?GIO json
const json1 = JSON.parse( message );//Convert MIU?GIO json

const json2 = require('../countermeasures.json');
//console.log("json2")

const jsonAttacks = await extractDescriptions( json1 [0] , json2);
const jsonRemediation = await extractDescriptions( json1 [1] , json2);

const [jsonOutput1, jsonOutput2] = await extractDescriptions(json1, json2);
//json.description = 'ciao';
// Print the JSON objects

//console.log("jsonOutput1 =", JSON.stringify(jsonOutput1, null, 4));
//console.log("jsonOutput2 =", JSON.stringify(jsonOutput2, null, 4));
console.log(jsonAttacks)
console.log(jsonRemediation)

//process message: insert into "sancus_report" collection
sancus_db.add("remediation",[jsonOutput1]);
sancus_db.add("remediation",[jsonOutput2]);
if( JSON.stringify(jsonAttacks) != '{}')

sancus_db.add("remediationAttack",[jsonAttacks]);

if( JSON.stringify(jsonRemediation) != '{}')

sancus_db.add("remediationVuln", [ jsonRemediation]);

}
catch (error) {
Expand Down
18 changes: 0 additions & 18 deletions www/reportReader/maintainDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,7 @@ const WRITE_CONCERN = {
single: false, //delete all documents that satisfy the filter
};

function removeOldRemediation(db, collectionName, timestamp){
try{
queryCount ++;
// Construct the query to remove records older than the threshold timestamp
const query = { timestamp: { $lt: thresholdTimestampInSeconds } };

// Remove the matching records
const result = db.collectionName.deleteMany(query);

console.log(collectionName + `${result.deletedCount} records deleted`);

}catch( e ){
queryCount --;
console.error("Error while deleting old records in collection " + collectionName );
console.error( e );
}


}

let queryCount = 0; //number of queries being executing
/**
Expand Down

0 comments on commit e9f76ac

Please sign in to comment.