function import_assets_csv($filename) { require_once 'classes/Util.inc'; $response = array(); $db = new ossim_db(); $conn = $db->connect(); if (($content = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)) == false) { $response['file_errors'] = "Failed to read file"; $response['status'] = false; return $response; } else { foreach ($content as $k => $v) { $data[] = explode(";", $v); } } $cont = 0; ini_set('max_execution_time', 180); ids_valid($data); if (count($data) <= 0) { $response['file_errors'] = _("Incompatible file format"); $response['status'] = false; return $response; } $allowed_sensors = Session::allowedSensors(); if (!empty($allowed_sensors)) { $my_allowed_sensors = explode(',', $allowed_sensors); } else { $response['file_errors'] = _("You need at least one sensor assigned"); $response['status'] = false; return $response; } foreach ($data as $k => $v) { $response['status'] = true; $response['read_line'] = $cont; $cont++; if (count($v) != 8) { $response['line_errors'][$cont][] = array("Line", _("Format not allowed")); $response['status'] = false; } $param = array(); foreach ($v as $i => $field) { $parameter = trim($field); $pattern = '/^\\"|\\"$|^\'|\'$/'; $param[] = preg_replace($pattern, '', $parameter); } //IP if (!ossim_valid($param[0], OSS_IP_ADDR, 'illegal:' . _("IP"))) { $response['line_errors'][$cont][] = array("IP", ossim_get_error_clean()); $response['status'] = false; } //Hostname if (empty($param[1])) { $param[1] = $param[0]; } else { if (!ossim_valid($param[1], OSS_SCORE, OSS_ALPHA, OSS_PUNC, 'illegal:' . _("Hostname"))) { $response['line_errors'][$cont][] = array("Hostname", ossim_get_error_clean()); $response['status'] = false; ossim_clean_error(); } } //FQDNs if (!empty($param[2])) { $fqdns_list = explode(",", $param[2]); foreach ($fqdns_list as $k => $fqdn) { if (!ossim_valid(trim($fqdn), OSS_NULLABLE, OSS_ALPHA, OSS_PUNC, 'illegal:' . _("FQDN/Aliases"))) { $response['line_errors'][$cont][] = array("FQDN/Aliases", ossim_get_error_clean()); $response['status'] = false; ossim_clean_error(); } } } //Description if (!ossim_valid($param[3], OSS_NULLABLE, OSS_SCORE, OSS_ALPHA, OSS_PUNC, OSS_AT, 'illegal:' . _("Description"))) { $response['line_errors'][$cont][] = array("Description", ossim_get_error_clean()); $response['status'] = false; ossim_clean_error(); } //Asset if ($param[4] == '') { $param[4] = 2; } else { if (!ossim_valid($param[4], OSS_NULLABLE, OSS_DIGIT, 'illegal:' . _("Asset value"))) { $response['line_errors'][$cont][] = array("Asset", ossim_get_error_clean()); $response['status'] = false; ossim_clean_error(); } } //NAT if (!ossim_valid($param[5], OSS_NULLABLE, OSS_IP_ADDR, 'illegal:' . _("NAT"))) { $response['line_errors'][$cont][] = array("NAT", ossim_get_error_clean()); $response['status'] = false; ossim_clean_error(); } //Sensors $sensors = array(); if (!empty($param[6])) { $sensor_name = array(); $list = explode(",", $param[6]); $sensors_list = array_intersect($list, $my_allowed_sensors); if (!empty($sensors_list)) { foreach ($sensors_list as $sensor) { $sensors[] = Sensor::get_sensor_name($conn, $sensor); } } else { $response['line_errors'][$cont][] = array("Sensors", _("You need at least one allowed Sensor")); $response['status'] = false; ossim_clean_error(); } } else { $response['line_errors'][$cont][] = array("Sensors", _("Column Sensors is empty")); $response['status'] = false; ossim_clean_error(); } $list_os = array("Windows", "Linux", "FreeBSD", "NetBSD", "OpenBSD", "MacOS", "Solaris", "Cisco", "AIX", "HP-UX", "Tru64", "IRIX", "BSD/OS", "SunOS", "Plan9", "IPhone"); //Operating System if (!empty($param[7]) && !in_array($param[7], $list_os)) { $param[7] = "Unknown"; } if ($response['status'] == true) { //Parameters $ip = $param[0]; $hostname = $param[1]; $asset = $param[4]; $threshold_c = 30; $threshold_a = 30; $rrd_profile = ""; $alert = 0; $persistence = 0; $nat = $param[5]; $descr = $param[3]; $os = $param[7]; $fqdns = $param[2]; $latitude = ''; $longitude = ''; $icon = 0; if (!Host::in_host($conn, $ip)) { Host::insert($conn, $ip, $hostname, $asset, $threshold_c, $threshold_a, $rrd_profile, $alert, $persistence, $nat, $sensors, $descr, $os, $mac, $mac_vendor, $latitude, $longitude, $fqdns, $icon); } else { Host::update($conn, $ip, $hostname, $asset, $threshold_c, $threshold_a, $rrd_profile, $alert, $persistence, $nat, $sensors, $descr, $os, $mac, $mac_vendor, $latitude, $longitude, $fqdns, $icon); } } } $response['read_line'] = $cont; return $response; }
$db = new ossim_db(); $conn = $db->connect(); $asset = 2; $conf = $GLOBALS["CONF"]; $threshold = $conf->get_conf("threshold"); $alert = 0; $persistence = 0; $nat = ""; $sensor_list = Sensor::get_all($conn, "ORDER BY name"); $nsens = count($sensor_list); $sensors = array(); $num_sens = 0; foreach ($sensor_list as $sensor) { $sensor_name = $sensor->get_name(); $num_sens++; $sensors[] = $sensor_name; } $descr = ""; $os = ""; $mac = ""; $mac_vendor = ""; $latitude = 0; $longitude = 0; if (!Host::in_host($conn, $ip)) { Host::insert($conn, $ip, $ip, $asset, $threshold, $threshold, "", $alert, $persistence, $nat, $sensors, $descr, $os, $mac, $mac_vendor, $latitude, $longitude); } else { echo _("Warning: the host inserted already exists, inventory insert skipped."); exit; } $db->close($conn); echo _("Host ") . $host . _(" Successfully inserted into inventory with default values.");
</a> </td> <?php } ?> <td class="nobborder">|</td> <td class="nobborder" nowrap> <a href="<?php echo Sensor::get_sensor_link($conn, $host) . "/{$host}.html"; ?> " target="report"> <?php echo gettext("Usage"); ?> </a> </td> <?php if (Host::in_host($conn, $host) || Net::isIpInAnyNet($conn, $host)) { $interface = Sensor::get_sensor_interface($conn, $host); ?> <td class="nobborder">|</td> <td class="nobborder" nowrap> <a href="<?php echo Sensor::get_sensor_link($conn, $host) . "/plugins/rrdPlugin?action=list&key=interfaces/{$interface}/hosts/{$ip_slashed}&title=host%20{$host}"; ?> " target="report"> <?php echo gettext("Anomalies"); ?> </a> </td> <?php } $db->close($conn); ?>
function update_db($global_info, $scan) { $db = new ossim_db(); $conn = $db->connect(); $array_os = array("win" => "1", "linux" => "2", "cisco" => "3", "freebsd" => "5", "netbsd" => "6", "openbsd" => "7", "hp-ux" => "8", "solaris" => "9", "macos" => "10", "plan9" => "11", "sco" => "12", "aix" => "13", "unix" => "14"); $ips = $global_info["ips"]; $sensors = $global_info["sboxs"]; $nagios = $global_info['nagios']; // load protocol ids $protocol_ids = array(); if ($protocol_list = Protocol::get_list($conn)) { foreach ($protocol_list as $protocol_data) { $protocol_ids[$protocol_data->get_name()] = $protocol_data->get_id(); } } for ($i = 0; $i < $ips; $i++) { $ip = $global_info["ip_{$i}"]; if (!empty($ip)) { $hosts[] = $ip; //gethostbyaddr($ip); $os = $scan[$ip]["os"]; $os_id = 0; foreach ($array_os as $k => $v) { if (preg_match("/{$k}/i", $os)) { $os_id = $v; break; } } if (Host::in_host($conn, $ip)) { echo "* " . gettext("Updating") . " {$ip}..<br/>"; Host::update($conn, $ip, gethostbyaddr($ip), $global_info["asset"], $global_info["threshold_c"], $global_info["threshold_a"], $global_info["rrd_profile"], 0, 0, $global_info["nat"], $sensors, $global_info["descr"], $scan["{$ip}"]["os"], $scan["{$ip}"]["mac"], $scan["{$ip}"]["mac_vendor"]); Host_scan::delete($conn, $ip, 3001); //if (isset($global_info["nessus"])) { Host_scan::insert($conn, $ip, 3001, 0); } } else { echo "<span style='color='blue'>\n"; echo "* " . gettext("Inserting") . " {$ip}..<br/>\n"; echo "</span>\n"; Host::insert($conn, $ip, gethostbyaddr($ip), $global_info["asset"], $global_info["threshold_c"], $global_info["threshold_a"], $global_info["rrd_profile"], 0, 0, $global_info["nat"], $sensors, $global_info["descr"], $scan[$ip]["os"], $scan[$ip]["mac"], $scan[$ip]["mac_vendor"]); // if (isset($global_info["nessus"])) { Host_scan::insert($conn, $ip, 3001, 0); } } if ($os_id != 0) { Host_plugin_sid::delete($conn, $ip, 5001); Host_plugin_sid::insert($conn, $ip, 5001, $os_id); } if (!empty($nagios)) { if (!Host_scan::in_host_scan($conn, $ip, 2007)) { Host_scan::insert($conn, $ip, 2007, "", $ip, $sensors, ""); } } else { if (Host_scan::in_host_scan($conn, $ip, 2007)) { Host_scan::delete($conn, $ip, 2007); } } /* Services */ Host_plugin_sid::delete($conn, $ip, 5002); foreach ($scan[$ip]["services"] as $port_proto => $service) { $service["proto"] = $protocol_ids[strtolower(trim($service["proto"]))]; Host_services::insert($conn, $ip, $service["port"], strftime("%Y-%m-%d %H:%M:%S"), $_SERVER["SERVER_ADDR"], $service["proto"], $service["service"], $service["service"], $service["version"], 1); Host_plugin_sid::insert($conn, $ip, 5002, $service["port"]); } flush(); } } // Insert group name $groupname = $global_info["groupname"]; if (!empty($groupname) && !empty($hosts)) { $exists_hosts = count(Host_group::get_list($conn, " AND g.name='{$groupname}'")) > 0; if ($exists_hosts) { echo "<br/>" . _("The group name already exists") . "<br/>"; } else { Host_group::insert($conn, $groupname, $global_info["threshold_c"], $global_info["threshold_a"], $global_info["rrd_profile"], $sensors, $hosts, $global_info["descr"]); } //if (isset($global_info["nessus"])) { Host_group_scan::insert($conn, $groupname, 3001, 0); } if (!empty($nagios)) { Host_group_scan::insert($conn, $groupname, 2007, 0); } } $db->close($conn); }