Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
* security: Add a version-safe CSP nonce (`plugin_mactrack_csp_nonce()`) to every inline `<script>` tag
* security: Escape Cabletron SNMP command arguments and normalize numeric options
* security: Escape rescan executable and script paths before command execution
* security: Escape site and device names in the views, option lists, edit headers and MAC authorization emails
* security: Escape values taken from uploaded device and device type CSV files in the import results
* security: Neutralise spreadsheet formula prefixes and double embedded quotes in CSV exports
* security: Normalize administrative device list SQL filters before query construction
* security: Normalize device report SQL filters before query construction
* security: Parameterize stale-process site filtering and validate process IDs before termination
* security: Read the Cabletron port table through the Cacti SNMP API instead of building a command line
* security: Remove all inline JavaScript handlers like onClick, onChange
* security: Replace MAC action request deserialization with escaped JSON payloads
* security: Require POST for SNMP option item reorder and removal, and scope removal to its option set
* security: Require POST for the device rescan, site scan, enable and disable actions
* security: Restrict Net_DNS2 cache deserialization to DNS response classes
* security: Safely quote ignored-interface regexes and normalize interface report filters
Expand Down
18 changes: 18 additions & 0 deletions js/mactrack_snmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,21 @@
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/

/* Reorder and remove actions change data, so they are sent as POST with the CSRF token */
$(function() {
$(document).on('click', '.mactrackSnmpItemAction', function(event) {
event.preventDefault();

var action = $(this).attr('data-action');
var itemId = $(this).attr('data-item-id');
var snmpId = $(this).attr('data-snmp-id');
var base = urlPath + 'plugins/mactrack/mactrack_snmp.php';

$.post(base + '?action=' + encodeURIComponent(action) + '&item_id=' + encodeURIComponent(itemId) + '&id=' + encodeURIComponent(snmpId), { __csrf_magic: csrfMagicToken }).done(function() {
loadPageNoHeader(base + '?header=false&action=edit&id=' + encodeURIComponent(snmpId));
}).fail(function(data) {
getPresentHTTPError(data);
});
});
});
71 changes: 30 additions & 41 deletions lib/mactrack_cabletron.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,58 +303,47 @@ function get_repeater_rev4_ports($site, &$device, $lowPort, $highPort): array {
$device['ports_active'] = 0;
}

if ($device['snmp_version'] == 2) {
$snmp_version = '2c';
} else {
$snmp_version = $device['snmp_version'];
}

$port_keys = [];
$return_array = [];
$new_port_key_array = [];
$port_number = 0;
$nextOID = '.1.3.6.1.4.1.52.4.1.1.1.4.1.5.2.1.2';
$to = ceil($device['snmp_timeout'] / 1000);
$portOID = '.1.3.6.1.4.1.52.4.1.1.1.4.1.5.2.1.2';

// walk the port table through the Cacti SNMP API so no command line is built
$walk_array = cacti_snmp_walk($device['hostname'], $snmp_readstring,
$portOID, $device['snmp_version'], $device['snmp_username'],
$device['snmp_password'], $device['snmp_auth_protocol'],
$device['snmp_priv_passphrase'], $device['snmp_priv_protocol'],
$device['snmp_context'], $device['snmp_port'], $device['snmp_timeout'],
$device['snmp_retries'], $device['max_oids'] ?? 10);

$i = 0;
$previous_port = 0;

while (1) {
$exec_string = trim(cacti_escapeshellcmd(read_config_option('path_snmpgetnext')) .
' -c ' . cacti_escapeshellarg($snmp_readstring) .
' -OnUQ -v ' . cacti_escapeshellarg($snmp_version) .
' -r ' . intval($device['snmp_retries']) .
' -t ' . intval($to) . ' ' .
cacti_escapeshellarg($device['hostname'] . ':' . intval($device['snmp_port'])) . ' ' .
cacti_escapeshellarg($nextOID));

exec($exec_string, $return_array, $return_code);

[$nextOID, $port_number] = explode('=', $return_array[$i]);
if (cacti_sizeof($walk_array)) {
foreach ($walk_array as $walk_item) {
$port_number = trim(strtr($walk_item['value'], ' ', ''));

if ($port_number < $previous_port) {
break;
}

if (($port_number <= $highPort) && ($port_number >= $lowPort)) {
if (!in_array($port_number, $ignore_ports, true)) {
// set defaults for devices in case they don't have/support vlans
$new_port_key_array[$i]['vlan_id'] = 'N/A';
$new_port_key_array[$i]['vlan_name'] = 'N/A';
$new_port_key_array[$i]['port_name'] = 'N/A';
if ($port_number < $previous_port) {
break;
}

$new_port_key_array[$i]['key'] = trim(substr($nextOID,36));
$new_port_key_array[$i]['port_number'] = trim(strtr($port_number,' ',''));
if (($port_number <= $highPort) && ($port_number >= $lowPort)) {
if (!in_array($port_number, $ignore_ports, true)) {
// set defaults for devices in case they don't have/support vlans
$new_port_key_array[$i]['vlan_id'] = 'N/A';
$new_port_key_array[$i]['vlan_name'] = 'N/A';
$new_port_key_array[$i]['port_name'] = 'N/A';
$new_port_key_array[$i]['key'] = trim(mactrack_oid_suffix($walk_item['oid'], $portOID));
$new_port_key_array[$i]['port_number'] = $port_number;
}

$previous_port = $port_number;
} else {
break;
}

$previous_port = trim(strtr($port_number,' ',''));
} else {
break;
mactrack_debug('OID: ' . $walk_item['oid'] . ', PORT: ' . $port_number);
$i++;
}

mactrack_debug('CMD: ' . $exec_string . ', PORT: ' . $port_number);
$i++;
$port_number = '';
}

if (cacti_sizeof($new_port_key_array) > 0) {
Expand Down
40 changes: 36 additions & 4 deletions lib/mactrack_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4306,7 +4306,7 @@ function mactrack_format_interface_row($stat): string {
ob_start();

form_selectable_cell(mactrack_interface_actions($stat['device_id'], $stat['ifIndex']), $stat['device_id']);
form_selectable_cell($stat['device_name'], $stat['device_id']);
form_selectable_ecell($stat['device_name'], $stat['device_id']);
form_selectable_cell(strtoupper($stat['device_type']), $stat['device_id']);
form_selectable_cell($stat['ifName'], $stat['device_id']);
form_selectable_cell($stat['ifDescr'], $stat['device_id']);
Expand Down Expand Up @@ -4665,7 +4665,7 @@ function mactrack_format_dot1x_row($port_result): string {
}

$row .= "<td class='nowrap'>" . mactrack_interface_actions($port_result['device_id'], $port_result['port_number']) . '</td>';
$row .= '<td><b>' . $port_result['device_name'] . '</b></td>';
$row .= '<td><b>' . html_escape($port_result['device_name']) . '</b></td>';
$row .= '<td>' . $port_result['hostname'] . '</td>';
$row .= '<td><b>' . $port_result['username'] . '</b></td>';
$row .= '<td>' . mactrack_dot1x_status_pill($port_result) . '</td>';
Expand Down Expand Up @@ -4729,6 +4729,38 @@ function mactrack_display_Octets($octets): string {
return $octets . ' ' . $suffix;
}

/**
* Prepares one value for a double-quoted CSV cell. A text value that starts
* with a character a spreadsheet treats as the start of a formula (= + - @,
* tab or carriage return) gets a leading apostrophe so it is read as text,
* and embedded double quotes are doubled. Numeric values are left as they
* are, so negative and signed numbers keep their meaning.
*
* @param mixed $value The cell value.
*
* @return string The value to place between the cell's double quotes.
*/
function mactrack_csv_cell($value): string {
$value = (string) $value;

if ($value !== '' && !is_numeric($value) && strpos("=+-@\t\r", $value[0]) !== false) {
$value = "'" . $value;
}

return str_replace('"', '""', $value);
}

/**
* Format ordered values as one quoted CSV record.
*/
function mactrack_csv_row(array $fields, array $columns = []): string {
if ($columns) {
$fields = array_map(static fn ($column) => $fields[$column], $columns);
}

return '"' . implode('","', array_map('mactrack_csv_cell', $fields)) . '"';
}

/**
* Rejects an admin AJAX action that did not arrive as a POST.
*
Expand Down Expand Up @@ -5332,7 +5364,7 @@ function mactrack_format_device_row($device, $actions = false): void {
}

form_selectable_cell(filter_value($device['device_name'], get_request_var('filter'), 'mactrack_devices.php?action=edit&device_id=' . $device['device_id']), $device['device_id']);
form_selectable_cell($device['site_name'], $device['device_id']);
form_selectable_ecell($device['site_name'], $device['device_id']);
form_selectable_cell(get_colored_device_status(($device['disabled'] == 'on' ? true : false), $device['snmp_status']), $device['device_id']);
form_selectable_cell(filter_value($device['hostname'], get_request_var('filter')), $device['device_id']);
form_selectable_cell(($device['device_type'] == '' ? __('Not Detected', 'mactrack') : $device['device_type']), $device['device_id']);
Expand Down Expand Up @@ -5572,7 +5604,7 @@ function mactrack_site_filter($page = 'mactrack_sites.php'): void {

if (get_request_var('site_id') == $site['site_id']) {
print ' selected';
} print '>' . $site['site_name'] . '</option>';
} print '>' . html_escape($site['site_name']) . '</option>';
}
}
?>
Expand Down
33 changes: 14 additions & 19 deletions mactrack_device_types.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,18 +533,13 @@ function mactrack_device_type_export(): void {

if (cacti_sizeof($device_types)) {
foreach ($device_types as $device_type) {
array_push($xport_array,'"' . $device_type['vendor'] . '","' .
$device_type['description'] . '","' .
$device_type['device_type'] . '","' .
$device_type['sysDescr_match'] . '","' .
$device_type['sysObjectID_match'] . '","' .
$device_type['scanning_function'] . '","' .
$device_type['ip_scanning_function'] . '","' .
$device_type['dot1x_scanning_function'] . '","' .
$device_type['serial_number_oid'] . '","' .
$device_type['lowPort'] . '","' .
$device_type['highPort'] . '","' .
$device_type['disabled'] . '"');
$xport_array[] = mactrack_csv_row([
$device_type['vendor'], $device_type['description'], $device_type['device_type'],
$device_type['sysDescr_match'], $device_type['sysObjectID_match'],
$device_type['scanning_function'], $device_type['ip_scanning_function'],
$device_type['dot1x_scanning_function'], $device_type['serial_number_oid'],
$device_type['lowPort'], $device_type['highPort'], $device_type['disabled'],
]);
}
}

Expand Down Expand Up @@ -913,9 +908,9 @@ function mactrack_device_type_import_processor(&$device_types): array {
$save_order .= ')';

if ($required >= 3) {
array_push($return_array, __('HEADER LINE PROCESSED OK: <br>Columns found where: %s', $save_order, 'mactrack') . '<br>');
array_push($return_array, __('HEADER LINE PROCESSED OK: <br>Columns found where: %s', html_escape($save_order), 'mactrack') . '<br>');
} else {
array_push($return_array, __('HEADER LINE PROCESSING ERROR: Missing required field <br>Columns found where: %s', $save_order, 'mactrack') . '<br>');
array_push($return_array, __('HEADER LINE PROCESSING ERROR: Missing required field <br>Columns found where: %s', html_escape($save_order), 'mactrack') . '<br>');

break;
}
Expand Down Expand Up @@ -1017,24 +1012,24 @@ function mactrack_device_type_import_processor(&$device_types): array {
' VALUES' . $save_value . $update_suffix;

if (db_execute($sql_execute)) {
array_push($return_array, __('INSERT SUCCEEDED: Vendor: %s, Description: %s, Type: %s, sysDescr: %s, sysObjectID: %s', $vendor, $description, $device_type, $sysDescr_match, $sysObjectID_match, 'mactrack'));
array_push($return_array, __('INSERT SUCCEEDED: Vendor: %s, Description: %s, Type: %s, sysDescr: %s, sysObjectID: %s', html_escape($vendor), html_escape($description), html_escape($device_type), html_escape($sysDescr_match), html_escape($sysObjectID_match), 'mactrack'));
} else {
array_push($return_array, __('INSERT FAILED: Vendor: %s, Description: %s, Type: %s, sysDescr: %s, sysObjectID: %s', $vendor, $description, $device_type, $sysDescr_match, $sysObjectID_match, 'mactrack'));
array_push($return_array, __('INSERT FAILED: Vendor: %s, Description: %s, Type: %s, sysDescr: %s, sysObjectID: %s', html_escape($vendor), html_escape($description), html_escape($device_type), html_escape($sysDescr_match), html_escape($sysObjectID_match), 'mactrack'));
}
} else {
// perform check to see if the row exists
$existing_row = db_fetch_row("SELECT * FROM mac_track_device_types $sql_where");

if (cacti_sizeof($existing_row)) {
array_push($return_array, __('INSERT SKIPPED, EXISTING: Vendor: %s, Description: %s, Type: %s, sysDescr: %s, sysObjectID: %s', $vendor, $description, $device_type, $sysDescr_match, $sysObjectID_match, 'mactrack'));
array_push($return_array, __('INSERT SKIPPED, EXISTING: Vendor: %s, Description: %s, Type: %s, sysDescr: %s, sysObjectID: %s', html_escape($vendor), html_escape($description), html_escape($device_type), html_escape($sysDescr_match), html_escape($sysObjectID_match), 'mactrack'));
} else {
$sql_execute = 'INSERT INTO mac_track_device_types ' . $save_order .
' VALUES' . $save_value;

if (db_execute($sql_execute)) {
array_push($return_array, __('INSERT SUCCEEDED: Vendor: %s, Description: %s, Type: %s, sysDescr: %s, sysObjectID: %s', $vendor, $description, $device_type, $sysDescr_match, $sysObjectID_match, 'mactrack'));
array_push($return_array, __('INSERT SUCCEEDED: Vendor: %s, Description: %s, Type: %s, sysDescr: %s, sysObjectID: %s', html_escape($vendor), html_escape($description), html_escape($device_type), html_escape($sysDescr_match), html_escape($sysObjectID_match), 'mactrack'));
} else {
array_push($return_array, __('INSERT FAILED: Vendor: %s, Description: %s, Type: %s, sysDescr: %s, sysObjectID: %s', $vendor, $description, $device_type, $sysDescr_match, $sysObjectID_match, 'mactrack'));
array_push($return_array, __('INSERT FAILED: Vendor: %s, Description: %s, Type: %s, sysDescr: %s, sysObjectID: %s', html_escape($vendor), html_escape($description), html_escape($device_type), html_escape($sysDescr_match), html_escape($sysObjectID_match), 'mactrack'));
}
}
}
Expand Down
Loading
Loading