lookyloo/website/web/static/tree.js

561 lines
20 KiB
JavaScript
Raw Normal View History

2020-08-11 19:17:39 +02:00
"use strict";
2017-09-22 00:26:38 +02:00
// From : https://bl.ocks.org/d3noob/43a860bc0024792f8803bba8ca0d5ecd
// Set the dimensions and margins of the diagram
2020-08-11 19:17:39 +02:00
let margin = {
top: 20,
right: 200,
bottom: 30,
left: 90
};
let width = 960 - margin.left - margin.right;
let height = 1000 - margin.top - margin.bottom;
2017-09-22 00:26:38 +02:00
2020-08-11 19:17:39 +02:00
let node_width = 0;
let node_height = 55;
2017-10-04 15:13:42 +02:00
2020-08-11 19:17:39 +02:00
let main_svg = d3.select("body").append("svg")
2017-09-25 11:23:32 +02:00
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
// dummy container for tooltip
d3.select('body')
.append('div')
.attr('id', 'tooltip')
.attr('class', 'tooltip')
.attr('style', 'position: absolute; opacity: 0;');
2020-08-11 19:17:39 +02:00
// Define SVGs
let defs = main_svg.append("defs");
2018-06-29 08:03:52 +02:00
2017-09-25 11:23:32 +02:00
// Add background pattern
2020-08-11 19:17:39 +02:00
let pattern = defs.append('pattern')
2017-09-25 11:23:32 +02:00
.attr('id', 'backstripes')
.attr('x', margin.left)
2017-09-29 14:43:40 +02:00
.attr("width", node_width * 2)
2017-09-25 11:23:32 +02:00
.attr("height", 10)
.attr('patternUnits', "userSpaceOnUse" )
pattern.append('rect')
2017-09-29 14:43:40 +02:00
.attr('width', node_width)
2017-09-25 11:23:32 +02:00
.attr('height', height)
.attr("fill", "#EEEEEE");
2017-09-22 00:26:38 +02:00
// append the svg object to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
2020-08-11 19:17:39 +02:00
let node_container = main_svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
2017-09-22 00:26:38 +02:00
// Assigns parent, children, height, depth
2020-08-11 19:17:39 +02:00
let root = d3.hierarchy(treeData);
root.x0 = height / 2; // middle of the page
2017-09-22 00:26:38 +02:00
root.y0 = 0;
// declares a tree layout
2020-08-11 19:17:39 +02:00
let tree = d3.tree();
2017-09-22 00:26:38 +02:00
update(root);
function openTreeInNewTab(capture_uuid, hostnode_uuid=null) {
2020-08-11 19:17:39 +02:00
let url = `/tree/${capture_uuid}`;
if (hostnode_uuid != null) {
url += `/${hostnode_uuid}`;
}
let win = window.open(url, '_blank');
if (win == null) {
alert("The browser didn't allow Lookyloo to open a new tab. There should be an icon on the right of your URL bar to allow it.");
}
2020-08-07 18:01:06 +02:00
win.focus();
}
2020-08-11 19:17:39 +02:00
function open_hostnode_popup(d) {
let win = window.open(`/tree/${treeUUID}/hostname_popup/${d.data.uuid}`, '_blank', 'width=1024,height=768,left=200,top=100');
if (win == null) {
alert("The browser didn't allow Lookyloo to open a pop-up. There should be an icon on the right of your URL bar to allow it.");
}
win.focus();
}
2020-05-18 18:35:20 +02:00
2020-08-11 19:17:39 +02:00
function LocateNode(hostnode_uuid) {
let element = document.getElementById(`node_${hostnode_uuid}`);
2020-08-04 17:27:33 +02:00
element.scrollIntoView({behavior: "smooth", block: "center", inline: "center"});
2020-05-18 18:35:20 +02:00
2020-08-11 19:17:39 +02:00
let line_arrow = d3.select(`#node_${hostnode_uuid}`)
.append('g')
.attr('cursor', 'pointer')
.on('click', function() {
this.remove();
});
2020-08-11 19:17:39 +02:00
let line = d3.line()
// Other options: http://bl.ocks.org/d3indepth/raw/b6d4845973089bc1012dec1674d3aff8/
//.curve(d3.curveCardinal)
.curve(d3.curveBundle)
2020-08-11 19:17:39 +02:00
.x(point => point.lx)
.y(point => point.ly);
2020-08-11 19:17:39 +02:00
let line_tip = d3.symbol()
.type(d3.symbolTriangle)
.size(200);
2020-08-11 19:17:39 +02:00
let path = line_arrow
.append("path")
.attr("stroke-width", "3")
.attr("stroke", "black")
.attr("fill", "none")
2020-08-11 19:17:39 +02:00
.data([{
source: {x: node_width/2, y: -100},
target: {x: node_width/4, y: -node_height/2}
}])
.attr("class", "line")
2020-08-11 19:17:39 +02:00
.attr("d", d => line(
[{lx: d.source.x, ly: d.source.y},
{lx: d.target.x, ly: d.source.y},
{lx: d.target.x, ly: d.target.y}
])
);
let arrow = line_arrow
.append("path")
.attr("d", line_tip)
.attr("stroke", 'black')
.style('stroke-width', '3')
.attr("fill", 'white')
2020-08-11 19:17:39 +02:00
.attr("transform", `translate(${node_width / 4}, ${-node_height / 1.5}) rotate(60)`);
2020-08-11 19:17:39 +02:00
let glow = () => {
line_arrow.selectAll('path')
.transition().duration(1000) //Set transition
.style('stroke-width', '7')
.style('stroke', 'red')
.transition().duration(1000) //Set transition
.style('stroke-width', '3')
.style('stroke', 'black')
2020-08-11 19:17:39 +02:00
.on("end", () => {
if (++i > 15) {
line_arrow.remove();
} else {
glow();
}
});
};
2020-08-11 19:17:39 +02:00
let i = 0;
glow();
2020-05-18 18:35:20 +02:00
};
2020-07-28 18:26:07 +02:00
function UnflagAllNodes() {
d3.selectAll('.node_data').select('rect').style('fill', 'white');
d3.selectAll('.node_data').select('text').style('fill', 'black');
d3.selectAll('.node_data').select("#flag")
.text("🏁")
2020-08-11 19:17:39 +02:00
.on('click', d => NodeHighlight(d.data.uuid))
.on('mouseover',() => {
2020-08-06 17:47:39 +02:00
d3.select('#tooltip')
.style('opacity', 1)
2020-08-11 19:17:39 +02:00
.style('left', `${d3.event.pageX + 10}px`)
.style('top', `${d3.event.pageY + 10}px`)
.text('Flag this node');
2020-08-06 17:47:39 +02:00
})
2020-08-11 19:17:39 +02:00
.on('mouseout', () => d3.select('#tooltip').style('opacity', 0));
2020-07-28 18:26:07 +02:00
};
2020-07-28 18:26:07 +02:00
function UnflagHostNode(hostnode_uuid) {
2020-08-11 19:17:39 +02:00
d3.select(`#node_${hostnode_uuid}`).select('rect').style('fill', 'white');
d3.select(`#node_${hostnode_uuid}`).select('text').style('fill', 'black');
d3.select(`#node_${hostnode_uuid}`).select("#flag")
.text("🏁")
2020-08-11 19:17:39 +02:00
.on('click', d => NodeHighlight(d.data.uuid))
.on('mouseover', () => {
2020-08-06 17:47:39 +02:00
d3.select('#tooltip')
.style('opacity', 1)
2020-08-11 19:17:39 +02:00
.style('left', `${d3.event.pageX + 10}px`)
.style('top', `${d3.event.pageY + 10}px`)
.text('Flag this node');
2020-08-06 17:47:39 +02:00
})
2020-08-11 19:17:39 +02:00
.on('mouseout', () => d3.select('#tooltip').style('opacity', 0));
};
2020-08-11 19:17:39 +02:00
function NodeHighlight(hostnode_uuid) {
let element = document.getElementById(`node_${hostnode_uuid}`);
element.scrollIntoView({behavior: "smooth", block: "center", inline: "nearest"});
2020-08-11 19:17:39 +02:00
d3.select(`#node_${hostnode_uuid}`).select('rect').style('fill', 'black');
d3.select(`#node_${hostnode_uuid}`).select('text').style('fill', 'white');
d3.select(`#node_${hostnode_uuid}`).select("#flag")
.text('❌')
2020-08-11 19:17:39 +02:00
.on('click', d => UnflagHostNode(d.data.uuid))
.on('mouseover', () => {
2020-08-06 17:47:39 +02:00
d3.select('#tooltip')
.style('opacity', 1)
2020-08-11 19:17:39 +02:00
.style('left', `${d3.event.pageX + 10}px`)
.style('top', `${d3.event.pageY + 10}px`)
2020-08-06 17:47:39 +02:00
.text('Remove flag on this node');
})
2020-08-11 19:17:39 +02:00
.on('mouseout', () => d3.select('#tooltip').style('opacity', 0));
2018-02-01 23:58:26 +01:00
};
2020-08-11 19:17:39 +02:00
function icon_list(relative_x_pos, relative_y_pos, d) {
const icon_size = 16;
let icon_options = new Map([
2019-05-15 18:10:07 +02:00
['js', "/static/javascript.png"],
['exe', "/static/exe.png"],
['css', "/static/css.png"],
['font', "/static/font.png"],
['html', "/static/html.png"],
['json', "/static/json.png"],
['iframe', "/static/ifr.png"],
['image', "/static/img.png"],
['unknown_mimetype', "/static/wtf.png"],
['video', "/static/video.png"],
['request_cookie', "/static/cookie_read.png"],
['response_cookie', "/static/cookie_received.png"],
['redirect', "/static/redirect.png"],
['redirect_to_nothing', "/static/cookie_in_url.png"]
2020-08-11 19:17:39 +02:00
]);
2019-05-15 18:10:07 +02:00
2020-08-11 19:17:39 +02:00
// Put all the icone in one sub svg document
let icons = d3.create("svg")
.attr('x', relative_x_pos)
.attr('y', relative_y_pos);
2020-08-12 13:59:40 +02:00
icon_options.forEach(function(icon_path, key) {
2020-08-11 19:17:39 +02:00
icons
.datum(d)
.filter(d => {
let has_icon = false;
if (typeof d.data[key] === 'boolean') {
has_icon = d.data[key];
} else if (typeof d.data[key] === 'number') {
has_icon = d.data[key] > 0;
} else if (d.data[key] instanceof Array) {
has_icon = d.data[key].length > 0;
}
return has_icon;
})
.append("svg")
.attr('class', 'icon')
.attr('id', key)
.append('image')
.attr("width", icon_size)
.attr("height", icon_size)
2020-08-12 13:59:40 +02:00
.attr("xlink:href", icon_path);
2020-08-11 19:17:39 +02:00
});
icons.selectAll('.icon')
.datum(d)
.filter(function(d) {
if (typeof d.data[this.id] === 'boolean') {
return false;
} else if (typeof d.data[this.id] === 'number') {
d.to_print = d.data[this.id]
return d.data[this.id] > 0;
} else if (d.data[this.id] instanceof Array) {
d.to_print = d.data[this.id].length
return d.data[this.id].length > 0;
}
return false;
}).append('text')
.attr("dy", 8)
.style("font-size", "10px")
.attr('x', icon_size + 1)
.text(d => d.to_print);
2019-05-15 18:10:07 +02:00
return icons.node();
2018-02-01 01:16:20 +01:00
}
function text_entry(relative_x_pos, relative_y_pos, onclick_callback, d) {
2018-02-01 01:16:20 +01:00
// Avoid hiding the content after the circle
2020-08-11 19:17:39 +02:00
let nodeContent = d3.create("svg") // WARNING: svg is required there, "g" doesn't have getBBox
2018-04-05 22:59:45 +02:00
.attr('height', node_height)
2018-02-01 01:16:20 +01:00
.attr('x', relative_x_pos)
.attr('y', relative_y_pos)
2019-05-15 18:10:07 +02:00
.datum(d);
2018-02-01 01:16:20 +01:00
// Add labels for the nodes
2020-08-11 19:17:39 +02:00
let text_nodes = nodeContent.append("text")
2018-02-01 01:16:20 +01:00
.attr('dy', '.9em')
.attr("stroke", "white")
.style("font-size", "16px")
.attr("stroke-width", ".2px")
.style("opacity", .9)
2020-08-12 13:59:40 +02:00
.attr('cursor', 'pointer')
.on('click', onclick_callback)
.on('mouseover', () => {
d3.select('#tooltip')
.style('opacity', 1)
.style('left', `${d3.event.pageX + 10}px`)
.style('top', `${d3.event.pageY + 10}px`)
.text('Open investigation pop-up.');
})
.on('mouseout', () => d3.select('#tooltip').style('opacity', 0))
2019-05-17 16:59:56 +02:00
.text(d => {
2020-08-11 19:17:39 +02:00
let to_print;
if (d.data.name.length > 50) {
to_print = `[...] ${d.data.name.substring(d.data.name.length - 50, d.data.name.length)}`;
} else {
to_print = d.data.name
};
if (d.data.urls_count > 1) {
return `${to_print} (${d.data.urls_count})`;
2019-05-17 16:59:56 +02:00
}
2020-08-11 19:17:39 +02:00
return to_print;
});
return nodeContent.node();
2018-02-01 01:16:20 +01:00
}
// Recursively generate the tree
function update(root, computed_node_width=0) {
2017-09-22 00:26:38 +02:00
// Current height of the tree (cannot use height because it isn't recomputed when we rename children -> _children)
2020-08-11 19:17:39 +02:00
let max_depth = 1
root.each(d => {
if (d.children){
max_depth = d.depth > max_depth ? d.depth : max_depth;
}
});
2017-09-29 14:43:40 +02:00
if (computed_node_width != 0) {
computed_node_width += 30;
// Re-compute SVG size depending on the generated tree
2020-08-11 19:17:39 +02:00
let newWidth = Math.max((max_depth + 1) * computed_node_width, node_width);
// Update height
2020-08-04 17:27:33 +02:00
// node_height is the height of a node, node_height * 25 is the minimum so the root node isn't behind the menu
2020-08-11 19:17:39 +02:00
let newHeight = Math.max(root.descendants().reverse().length * node_height, 25 * node_height);
tree.size([newHeight, newWidth])
// Set background based on the computed width and height
2020-08-11 19:17:39 +02:00
let background = main_svg.insert('rect', ':first-child')
.attr('y', 0)
2019-05-15 18:10:07 +02:00
// FIXME: + 200 doesn't make much sense...
.attr('width', newWidth + margin.right + margin.left + 200)
.attr('height', newHeight + margin.top + margin.bottom)
.style('fill', "url(#backstripes)");
2019-05-15 18:10:07 +02:00
// Update size
d3.select("body svg")
2019-05-15 18:10:07 +02:00
// FIXME: + 200 doesn't make much sense...
.attr("width", newWidth + margin.right + margin.left + 200)
.attr("height", newHeight + margin.top + margin.bottom)
// Update pattern
main_svg.selectAll('pattern')
2020-08-12 13:59:40 +02:00
.attr('width', `${computed_node_width * 2}px`)
pattern.selectAll('rect')
2020-08-12 13:59:40 +02:00
.attr('width', `${computed_node_width}px`)
2017-09-29 14:43:40 +02:00
}
2017-09-22 00:26:38 +02:00
// Assigns the x and y position for the nodes
2020-08-11 19:17:39 +02:00
let treemap = tree(root);
2017-09-22 00:26:38 +02:00
// Compute the new tree layout. => Note: Need d.x & d.y
2020-08-11 19:17:39 +02:00
let nodes = treemap.descendants(),
links = treemap.descendants().slice(1);
2017-09-22 00:26:38 +02:00
// ****************** Nodes section ***************************
2020-08-11 19:17:39 +02:00
// Toggle children on click.
let toggle_children_collapse = (d) => {
if (d.children) {
d._children = d.children;
d.children = null;
}
else {
d.children = d._children;
d._children = null;
}
// Call update on the whole Tree
update(d.ancestors().reverse()[0]);
};
2017-09-22 00:26:38 +02:00
// Update the nodes...
const tree_nodes = node_container.selectAll('g.node')
.data(nodes, node => node.data.uuid);
tree_nodes.join(
// Enter any new modes at the parent's previous position.
enter => {
2020-08-11 19:17:39 +02:00
let node_group = enter.append('g')
.attr('class', 'node')
2020-08-11 19:17:39 +02:00
.attr("id", d => `node_${d.data.uuid}`)
.attr("transform", `translate(${root.y0}, ${root.x0})`);
2019-05-15 18:10:07 +02:00
node_group
// Add Circle for the nodes
.append('circle')
.attr('class', 'node')
.attr('r', 1e-6)
.style("fill", d => d._children ? "lightsteelblue" : "#fff")
2020-08-11 19:17:39 +02:00
.on('click', toggle_children_collapse);
2019-05-15 18:10:07 +02:00
2020-08-11 19:17:39 +02:00
let node_data = node_group
2019-05-15 18:10:07 +02:00
.append('svg')
.attr('class', 'node_data')
.attr('x', 0)
2019-05-15 18:10:07 +02:00
.attr('y', -30);
node_data.append('rect')
.attr("rx", 6)
.attr("ry", 6)
.attr('x', 12)
2019-05-15 18:10:07 +02:00
.attr('y', 0)
2020-08-12 13:59:40 +02:00
.attr('width', 0)
2019-05-15 18:10:07 +02:00
.style("opacity", "0.5")
.attr("stroke", 'black')
2019-05-15 18:10:07 +02:00
.attr('stroke-opacity', "0.8")
.attr("stroke-width", "2")
2019-05-15 18:10:07 +02:00
.attr("stroke-linecap", "round")
.attr("fill", "white");
// Set Hostname text
2019-05-15 18:10:07 +02:00
node_data
2020-08-11 19:17:39 +02:00
.append(d => text_entry(15, 5, open_hostnode_popup, d)); // Popup
// Set list of icons
2019-05-15 18:10:07 +02:00
node_data
.append(d => icon_list(17, 35, d));
2019-05-15 18:10:07 +02:00
2020-08-11 19:17:39 +02:00
node_group.select('.node_data').each(function(d){
2019-05-22 12:27:20 +02:00
// set position of icons based of their length
2020-08-11 19:17:39 +02:00
let cur_icon_list_len = 0;
d3.select(this).selectAll('.icon').each(function(){
2019-05-22 12:27:20 +02:00
d3.select(this).attr('x', cur_icon_list_len);
cur_icon_list_len += d3.select(this).node().getBBox().width;
});
2019-05-15 18:10:07 +02:00
2019-05-22 12:27:20 +02:00
// Rectangle around the domain name & icons
2020-08-12 13:59:40 +02:00
let selected_node_bbox_init = d3.select(this).select('text').node().getBoundingClientRect();
2019-05-22 12:27:20 +02:00
d3.select(this).select('rect')
2020-08-12 13:59:40 +02:00
.attr('height', node_height + 5)
2020-08-11 19:17:39 +02:00
.attr('width', selected_node_bbox_init.width + 50);
2019-05-22 12:27:20 +02:00
// Set the width for all the nodes
2020-08-12 13:59:40 +02:00
let selected_node_bbox = d3.select(this).select('rect').node().getBoundingClientRect(); // Required, as the node width need to include the rectangle
node_width = node_width > selected_node_bbox.width ? node_width : selected_node_bbox.width;
// Set Flag
d3.select(this).append("text")
2020-08-12 13:59:40 +02:00
.attr('x', `${selected_node_bbox.width - 12}px`)
.attr('y', '20px')
.style("font-size", "16px")
.attr("id", "flag")
.text("🏁")
.attr('cursor', 'pointer')
2020-08-11 19:17:39 +02:00
.on('click', d => NodeHighlight(d.data.uuid))
2020-08-12 13:59:40 +02:00
.on('mouseover', () => {
2020-08-06 17:47:39 +02:00
d3.select('#tooltip')
.style('opacity', 1)
2020-08-11 19:17:39 +02:00
.style('left', `${d3.event.pageX + 10}px`)
.style('top', `${d3.event.pageY + 10}px`)
.text('Flag this node');
2020-08-06 17:47:39 +02:00
})
2020-08-11 19:17:39 +02:00
.on('mouseout', () => d3.select('#tooltip').style('opacity', 0));
2020-08-11 19:17:39 +02:00
if (d.data.http_content) {
const icon_size = 24;
// set lock insecure connection
d3.select(this).append("svg").append('rect')
.attr('x', selected_node_bbox.width - 22)
.attr('y', selected_node_bbox.height - 13)
.attr('width', icon_size)
.attr('height', icon_size)
.attr('fill', 'white')
.attr('stroke', 'black');
2020-08-11 19:17:39 +02:00
d3.select(this).append('image')
.attr('x', selected_node_bbox.width - 22)
.attr('y', selected_node_bbox.height - 13)
.attr('id', 'insecure_image')
.attr("width", icon_size)
.attr("height", icon_size)
.attr("xlink:href", '/static/insecure.svg')
2020-08-12 13:59:40 +02:00
.on('mouseover', () => {
d3.select('#tooltip')
.style('opacity', 1)
2020-08-11 19:17:39 +02:00
.style('left', `${d3.event.pageX + 10}px`)
.style('top', `${d3.event.pageY + 10}px`)
.text('This node containts insecure requests');
})
2020-08-11 19:17:39 +02:00
.on('mouseout', () => d3.select('#tooltip').style('opacity', 0));
};
});
return node_group;
},
update => update,
exit => exit
2020-08-11 19:17:39 +02:00
.transition()
2019-05-17 16:59:56 +02:00
// Remove any exiting nodes
2020-08-11 19:17:39 +02:00
.attr("transform", node => `translate(${node.y0}, ${node.x0})`)
2019-05-17 16:59:56 +02:00
// On exit reduce the node circles size to 0
.attr('r', 1e-6)
// On exit reduce the opacity of text labels
.style('fill-opacity', 1e-6)
.remove()
).call(node => {
2019-05-17 16:59:56 +02:00
node
// Transition to the proper position for the node
2020-08-11 19:17:39 +02:00
.attr("transform", node => `translate(${node.y}, ${node.x})`)
2019-05-17 16:59:56 +02:00
// Update the node attributes and style
.select('circle.node')
.attr('r', 10)
.style("fill", node => node._children ? "lightsteelblue" : "#fff")
.attr('cursor', 'pointer');
2019-05-15 18:10:07 +02:00
2017-10-04 15:13:42 +02:00
});
2017-09-22 00:26:38 +02:00
nodes.forEach(d => {
// Store the old positions for transition.
d.x0 = d.x;
d.y0 = d.y;
2017-09-29 14:43:40 +02:00
});
2018-02-01 01:16:20 +01:00
2017-09-22 00:26:38 +02:00
// ****************** links section ***************************
// Update the links...
2020-08-11 19:17:39 +02:00
const link = node_container.selectAll('path.link').data(links, d => d.id);
// Creates a curved (diagonal) path from parent to the child nodes
let diagonal = (s, d) => {
return `M ${s.y} ${s.x}
C ${(s.y + d.y) / 2} ${s.x},
${(s.y + d.y) / 2} ${d.x},
${d.y} ${d.x}`
};
link.join(
enter => enter
// Enter any new links at the parent's previous position.
.insert('path', "g")
.attr("class", "link")
.attr('d', d => {
2020-08-11 19:17:39 +02:00
let o = {
x: d.x0,
y: d.y0
};
return diagonal(o, o)
}),
update => update,
exit => exit
.call(exit => exit
.attr('d', d => {
2020-08-11 19:17:39 +02:00
let o = {
x: d.x0,
y: d.y0
};
return diagonal(o, o)
})
.remove()
)
).call(link => link
.attr('d', d => diagonal(d, d.parent))
);
2017-09-22 00:26:38 +02:00
if (computed_node_width === 0) {
update(root, node_width)
2017-09-22 00:26:38 +02:00
}
}