From 5078eecdfd867be58c8e9f38c3277bcadbd458e0 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 7 Feb 2022 12:31:31 -0500 Subject: [PATCH] Add in get_networks and get_subnets as utilities to parse devices and networks in preparation for scanning/probing --- web/includes/functions.php | 66 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/web/includes/functions.php b/web/includes/functions.php index e346ceb34..bfa4b9746 100644 --- a/web/includes/functions.php +++ b/web/includes/functions.php @@ -2414,4 +2414,70 @@ function i18n() { return implode('-', $string); } + +function get_networks() { + $interfaces = array(); + + exec('ip link', $output, $status); + if ( $status ) { + $html_output = implode('
', $output); + ZM\Error("Unable to list network interfaces, status is '$status'. Output was:

$html_output"); + } else { + foreach ( $output as $line ) { + if ( preg_match('/^\d+: ([[:alnum:]]+):/', $line, $matches ) ) { + if ( $matches[1] != 'lo' ) { + $interfaces[$matches[1]] = $matches[1]; + } else { + ZM\Debug("No match for $line"); + } + } + } + } + $routes = array(); + exec('ip route', $output, $status); + if ( $status ) { + $html_output = implode('
', $output); + ZM\Error("Unable to list network interfaces, status is '$status'. Output was:

$html_output"); + } else { + foreach ( $output as $line ) { + if ( preg_match('/^default via [.[:digit:]]+ dev ([[:alnum:]]+)/', $line, $matches) ) { + $interfaces['default'] = $matches[1]; + } else if ( preg_match('/^([.[:digit:]]+\/[[:digit:]]+) dev ([[:alnum:]]+)/', $line, $matches) ) { + $interfaces[$matches[2]] .= ' ' . $matches[1]; + ZM\Debug("Matched $line: $matches[2] .= $matches[1]"); + } else { + ZM\Debug("Didn't match $line"); + } + } # end foreach line of output + } + return $interfaces; +} + +# Returns an array of subnets like 192.168.1.0/24 for a given interface. +# Will ignore mdns networks. + +function get_subnets($interface) { + $subnets = array(); + exec('ip route', $output, $status); + if ( $status ) { + $html_output = implode('
', $output); + ZM\Error("Unable to list network interfaces, status is '$status'. Output was:

$html_output"); + } else { + foreach ($output as $line) { + if (preg_match('/^([.[:digit:]]+\/[[:digit:]]+) dev ([[:alnum:]]+)/', $line, $matches)) { + if ($matches[1] == '169.254.0.0/16') { + # Ignore mdns + } else if ($matches[2] == $interface) { + $subnets[] = $matches[1]; + } else { + ZM\Debug("Wrong interface $matches[1] != $interface"); + } + } else { + ZM\Debug("Didn't match $line"); + } + } # end foreach line of output + } + return $subnets; +} + ?>