This commit is contained in:
Daniel Maya 2022-08-29 09:39:47 +02:00
parent 182bb2d2c4
commit 16554cfc7a
7 changed files with 320 additions and 67 deletions

View File

@ -0,0 +1,5 @@
START TRANSACTION;
ALTER TABLE `tmap` ADD COLUMN `refresh_time` INT UNSIGNED NOT NULL DEFAULT 0;
COMMIT;

View File

@ -2102,36 +2102,32 @@ class NetworkMap
$i++;
}
if (!$this->relations) {
// Search for relations.
foreach ($this->nodes as $k => $item) {
$target = $this->calculateRelations($k);
// Search for relations.
foreach ($this->nodes as $k => $item) {
$target = $this->calculateRelations($k);
// Adopt all orphan nodes but pandora one.
if (empty($target)) {
if (isset($this->noPandoraNode) === false
|| $this->noPandoraNode == false
) {
if ($item['id_node'] != 0) {
$rel = [];
$rel['id_parent'] = 0;
$rel['id_child'] = $item['id_node'];
$rel['parent_type'] = NODE_PANDORA;
$rel['child_type'] = $item['node_type'];
$rel['id_child_source_data'] = $item['id_source_data'];
// Adopt all orphan nodes but pandora one.
if (empty($target) === true) {
if (isset($this->noPandoraNode) === false
|| $this->noPandoraNode == false
) {
if ($item['id_node'] != 0) {
$rel = [];
$rel['id_parent'] = 0;
$rel['id_child'] = $item['id_node'];
$rel['parent_type'] = NODE_PANDORA;
$rel['child_type'] = $item['node_type'];
$rel['id_child_source_data'] = $item['id_source_data'];
$orphans[] = $rel;
}
}
} else {
// Flattern edges.
foreach ($target as $rel) {
$edges[] = $rel;
$orphans[] = $rel;
}
}
} else {
// Flattern edges.
foreach ($target as $rel) {
$edges[] = $rel;
}
}
} else {
$edges = $this->relations;
}
if (is_array($edges)) {
@ -2628,6 +2624,79 @@ class NetworkMap
}
/**
* Regenerates a nodes - relationships array using graphviz dot
* schema and stores nodes&relations into $this->graph.
*
* @return object
*/
public function recalculateCoords()
{
global $config;
include_once 'include/functions_os.php';
$map_filter = $this->mapOptions['map_filter'];
/*
* Let graphviz place the nodes.
*/
if ($map_filter['empty_map']) {
$this->generateEmptyDotGraph();
} else if (!isset($this->dotGraph)) {
$this->generateDotGraph();
}
$graph = $this->calculateCoords();
if (is_array($graph) === true) {
$nodes = $graph['nodes'];
} else {
return [];
}
$nodes_aux = [];
// Prepare graph nodes.
foreach ($nodes as $id => $coords) {
$node_tmp['id'] = $id;
$source = $this->getNodeData($id);
$node_tmp['type'] = $source['node_type'];
$node_tmp['x'] = $coords['x'];
$node_tmp['y'] = $coords['y'];
switch ($node_tmp['type']) {
case NODE_AGENT:
$node_tmp['source_data'] = $source['id_agente'];
break;
case NODE_MODULE:
$node_tmp['source_data'] = $source['id_agente_modulo'];
break;
case NODE_PANDORA:
$node_tmp['source_data'] = 0;
$node_center['x'] = ($coords['x'] - MAP_X_CORRECTION);
$node_center['y'] = ($coords['y'] - MAP_Y_CORRECTION);
break;
case NODE_GENERIC:
default:
$node_tmp['source_data'] = $source['id_source'];
break;
}
$nodes_aux[$index] = $node_tmp;
$index++;
}
return $nodes_aux;
}
/**
* Transform node information into JS data.
*
@ -3317,6 +3386,10 @@ class NetworkMap
*/
public function loadController(?bool $return=true)
{
if (isset($this->mapOptions['refresh_time']) === false) {
$this->mapOptions['refresh_time'] = 0;
}
$output = '';
if ($this->useTooltipster
@ -3364,7 +3437,27 @@ class NetworkMap
init_drag_and_drop();
init_minimap();
function_open_minimap();
if ('.$this->mapOptions['refresh_time'].' > 0) {
var startCountDown = function (duration, cb) {
$("div.vc-countdown").countdown("destroy");
if (!duration) return;
var t = new Date();
t.setTime(t.getTime() + duration * 1000);
$("div.vc-countdown").countdown({
until: t,
format: "MS",
layout: "(%M%nn%M:%S%nn%S '.__('Until refresh').') ",
alwaysExpire: true,
onExpiry: function () {
refresh();
}
});
}
startCountDown('.($this->mapOptions['refresh_time']).', false);
}
$(document.body).on("mouseleave",
".context-menu-list",
function(e) {

View File

@ -2702,7 +2702,7 @@ function networkmap_clean_duplicate_links($id)
{
global $config;
$sql_duplicate_links = 'SELECT id, id_parent, id_child
$sql_duplicate_links = 'SELECT *
FROM trel_item t1
WHERE t1.deleted = 0 AND t1.id_child IN (
SELECT t2.id_child
@ -2712,7 +2712,7 @@ function networkmap_clean_duplicate_links($id)
AND t1.id_parent = t2.id_parent
AND t2.id_map = '.$id.')
AND t1.id_map = '.$id.'
ORDER BY id_parent, id_child';
ORDER BY id_parent, id_child, id_parent_source_data desc, id_child_source_data desc';
$rows = db_get_all_rows_sql($sql_duplicate_links);
if (empty($rows) === true) {
@ -2721,28 +2721,49 @@ function networkmap_clean_duplicate_links($id)
$pre_parent = -1;
$pre_child = -1;
$pre_parent_source = -1;
$pre_child_source = -1;
foreach ($rows as $row) {
if (($pre_parent === (int) $row['id_parent'])
&& ($pre_child === (int) $row['id_child'])
) {
// Delete the duplicate row.
db_process_sql_delete(
'trel_item',
['id' => $row['id']]
);
// Agent <-> Agent.
if ((int) $row['parent_type'] === 0 && (int) $row['child_type'] === 0) {
// Delete the duplicate row.
db_process_sql_delete(
'trel_item',
['id' => $row['id']]
);
} else {
// Agent <-> Module or Module <-> Agent or Module <-> Module.
if ($pre_parent_source === (int) $row['id_parent_source_data']
&& $pre_child_source === (int) $row['id_child_source_data']
) {
// Delete the duplicate row.
db_process_sql_delete(
'trel_item',
['id' => $row['id']]
);
} else {
$pre_parent_source = (int) $row['id_parent_source_data'];
$pre_child_source = (int) $row['id_child_source_data'];
}
}
} else {
$pre_parent = $row['id_parent'];
$pre_child = $row['id_child'];
$pre_parent = (int) $row['id_parent'];
$pre_child = (int) $row['id_child'];
if ((int) $row['parent_type'] === 1 || (int) $row['child_type'] === 1) {
$pre_parent_source = (int) $row['id_parent_source_data'];
$pre_child_source = (int) $row['id_child_source_data'];
}
}
}
db_process_sql($sql_duplicate_links);
do {
db_clean_cache();
$sql_duplicate_links_parent_as_children = '
SELECT id, id_parent, id_child
SELECT *
FROM trel_item t1
WHERE t1.deleted = 0 AND t1.id_child IN (
SELECT t2.id_parent
@ -2765,14 +2786,51 @@ function networkmap_clean_duplicate_links($id)
if (($row['id'] != $row2['id'])
&& ($row['id_child'] == $row2['id_parent'])
&& ($row['id_parent'] == $row2['id_child'])
&& ($row['parent_type'] == $row2['child_type'])
&& ($row['child_type'] == $row2['parent_type'])
) {
db_process_sql_delete(
'trel_item',
['id' => $row2['id']]
);
// Agent <-> Agent.
if ((int) $row2['parent_type'] === 0 && (int) $row2['child_type'] === 0) {
db_process_sql_delete(
'trel_item',
['id' => $row2['id']]
);
$found = true;
break;
$found = true;
break;
} else {
// Agent <-> Module or Module <-> Agent or Module <-> Module.
if ((int) $row['id_child_source_data'] === (int) $row2['id_parent_source_data']
&& (int) $row['id_parent_source_data'] === (int) $row2['id_child_source_data']
) {
db_process_sql_delete(
'trel_item',
['id' => $row2['id']]
);
$found = true;
break;
}
}
} else {
// Si no son del mismo tipo pero hay un parent_type = 0 y child_type = 0 borrar.
if ((int) $row['parent_type'] === 0 && (int) $row['child_type'] === 0) {
db_process_sql_delete(
'trel_item',
['id' => $row['id']]
);
$found = true;
break;
} else if ((int) $row2['parent_type'] === 0 && (int) $row2['child_type'] === 0) {
db_process_sql_delete(
'trel_item',
['id' => $row2['id']]
);
$found = true;
break;
}
}
}
@ -3024,19 +3082,28 @@ function erase_node($id)
$return = db_process_sql_update(
'titem',
['deleted' => 1],
['id' => $node['id']]
[
'id' => (int) $node['id'],
'id_map' => (int) $node['id_map'],
]
);
db_process_sql_update(
'trel_item',
['deleted' => 1],
['id_parent' => (int) $node['id']]
[
'id_parent' => (int) $node['id'],
'id_map' => (int) $node['id_map'],
]
);
db_process_sql_update(
'trel_item',
['deleted' => 1],
['id_child' => (int) $node['id']]
[
'id_child' => (int) $node['id'],
'id_map' => (int) $node['id_map'],
]
);
$node_modules = db_get_all_rows_filter(
@ -3054,39 +3121,52 @@ function erase_node($id)
db_process_sql_update(
'titem',
['deleted' => 1],
['id' => $node_module['id']]
[
'id' => (int) $node_module['id'],
'id_map' => (int) $node_module['id_map'],
]
);
db_process_sql_update(
'trel_item',
['deleted' => 1],
['id_parent_source_data' => (int) $node_module['source_data']]
[
'id_parent_source_data' => (int) $node_module['source_data'],
'id_map' => (int) $node_module['id_map'],
]
);
db_process_sql_update(
'trel_item',
['deleted' => 1],
['id_child_source_data' => (int) $node_module['source_data']]
[
'id_child_source_data' => (int) $node_module['source_data'],
'id_map' => (int) $node_module['id_map'],
]
);
}
}
} else {
$return = db_process_sql_delete(
'titem',
['id' => $node['id']]
[
'id' => (int) $node['id'],
'id_map' => (int) $node['id_map'],
]
);
db_process_sql_delete(
'trel_item',
['id_parent' => 0]
[
'parent_type' => 2,
'id_map' => (int) $node['id_map'],
]
);
db_process_sql_delete(
'trel_item',
['id_parent' => (int) $node['id']]
);
db_process_sql_delete(
'trel_item',
['id_child' => (int) $node['id']]
[
'child_type' => 2,
'id_map' => (int) $node['id_map'],
]
);
}
@ -4070,6 +4150,7 @@ function add_agent_node_in_option($id_networkmap, $id_agent, $x, $y)
function networkmap_get_new_nodes_and_links($networkmap, $x, $y)
{
$id_networkmap = $networkmap['id'];
$id_recon = $networkmap['source_data'];
$map_filter = $networkmap['filter'];
if (is_array($map_filter) === false) {
@ -4077,11 +4158,11 @@ function networkmap_get_new_nodes_and_links($networkmap, $x, $y)
}
if ((int) $networkmap['source'] === SOURCE_TASK) {
$agents = get_discovery_agents($id_networkmap, true);
$agents = get_discovery_agents($id_recon, true);
} else if ((int) $networkmap['source'] === SOURCE_NETWORK) {
// Network map, based on direct network.
$agents = networkmap_get_nodes_from_ip_mask(
$networkmap['source_data'],
$id_recon,
true
);
} else {

View File

@ -2099,8 +2099,6 @@ function show_menu(item, data) {
disabled: false,
callback: function(key, options) {
refresh();
// refresh_holding_area();
// update_networkmap();
}
};
items_list["restart_map"] = {
@ -2497,12 +2495,37 @@ function refresh() {
}
});
let location = "";
if ($("#main").height()) {
location = `index.php?sec=network&sec2=operation/agentes/pandora_networkmap&tab=view&id_networkmap=${networkmap_id}`;
} else {
location = `index.php?sec=network&sec2=operation/agentes/pandora_networkmap&tab=view&pure=1&id_networkmap=${networkmap_id}`;
}
if (array_nodes.length === 0 && array_links.length === 0) {
update_networkmap();
$("#spinner_networkmap").css("display", "none");
// window.location = location;
} else {
console.log("hay algun nodo o link nuevo, toca repintar mapa");
// crear una funcion donde se llame a graphviz y se reciban las nuevas posiciones, pero sin borrar links.
if (array_nodes.length > 0) {
$.ajax({
data: {
page: "operation/agentes/pandora_networkmap.view",
refresh_map: 1,
id: networkmap_id
},
dataType: "json",
type: "POST",
url: window.base_url_homedir + "/ajax.php",
success: function(data) {
$("#spinner_networkmap").css("display", "none");
window.location = location;
}
});
} else if (array_links.length > 0) {
$("#spinner_networkmap").css("display", "none");
window.location = location;
}
}
}
},

View File

@ -646,7 +646,7 @@ $(document).ready(function() {
});
$("#refresh_units").trigger("change");
$("#refresh_time_units").trigger("change");
});

View File

@ -78,6 +78,42 @@ if (is_ajax() === true) {
$reset_map = (bool) get_parameter('reset_map', false);
$refresh_map = (bool) get_parameter('refresh_map', false);
if ($refresh_map) {
$id_map = get_parameter('id');
include_once $config['homedir'].'/include/class/NetworkMap.class.php';
$map_manager = new NetworkMap(
['id_map' => $id_map]
);
$filter = json_decode($map_manager->map['filter'], true);
$z_dash = $filter['z_dash'];
$nodes = $map_manager->recalculateCoords();
foreach ($nodes as $key => $value) {
if ($value['type'] == 0 || $value['type'] == 2) {
$node['x'] = ($value['x'] + ($map_manager->map['center_x'] / 2) / $z_dash);
$node['y'] = ($value['y'] + ($map_manager->map['center_y'] / 2) / $z_dash);
$node['refresh'] = 0;
db_process_sql_update(
'titem',
$node,
[
'source_data' => $value['source_data'],
'id_map' => $id_map,
]
);
}
}
echo $id_map;
return;
}
if ($get_reset_map_form) {
$map_id = get_parameter('map_id');
@ -2271,6 +2307,10 @@ if ($networkmap === false) {
]
).'</a>',
];
$buttons['test'] = [
'active' => false,
'text' => '<div style="width:100%;height:54px;display:flex;align-items:center"><div class="vc-countdown"></div></div>',
];
} else {
if (!$dash_mode) {
$buttons['screen'] = [
@ -2306,6 +2346,10 @@ if ($networkmap === false) {
]
).'</a>',
];
$buttons['test'] = [
'active' => false,
'text' => '<div style="width:100%;height:54px;display:flex;align-items:center"><div class="vc-countdown"></div></div>',
];
}
}
@ -2325,8 +2369,15 @@ if ($networkmap === false) {
include_once $config['homedir'].'/include/class/NetworkMap.class.php';
$filter = json_decode($networkmap['filter'], true);
$zoom = $filter['z_dash'];
$map_manager = new NetworkMap(
[ 'id_map' => $networkmap['id']]
[
'id_map' => $networkmap['id'],
'center_x' => $networkmap['center_x'],
'center_y' => $networkmap['center_y'],
]
);
$map_manager->printMap();

View File

@ -2317,7 +2317,7 @@ CREATE TABLE IF NOT EXISTS `tmap` (
`generated` INT UNSIGNED NOT NULL DEFAULT 0,
`filter` TEXT,
`id_group_map` INT UNSIGNED NOT NULL DEFAULT 0,
`refresh_time` INT UNSIGNED NOT NULL DEFAULT 300,
`refresh_time` INT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4;