scan_all in bash script

This commit is contained in:
Adrien MALINGREY 2023-04-13 15:46:47 +02:00
parent 6e0e3b60e3
commit ea98cd9903
7 changed files with 77 additions and 3 deletions

View File

@ -4,7 +4,7 @@ Scan hosts with nmap and display results in webpage.
* Create a configuration yaml file in confs/ subdirectory (see example below).
It may be generated by scanning a network with `init.sh`.
* Scan with `php scan_all.php` (use a cron task!).
* Scan with `./scan_all.sh` (use a cron task!).
* Open index.php to see results.
## Example

View File

@ -16,4 +16,4 @@ read network
nmap --script smb-enum-shares.nse -oX "scans/$filename.xml" $network
xsltproc --stringparam site "$site" --stringparam network $network toyaml.xsl "$DIR/scans/$filename.xml" > "$DIR/confs/$filename.yaml"
xsltproc --stringparam site "$site" --stringparam network $network to_yaml.xsl "$DIR/scans/$filename.xml" > "$DIR/confs/$filename.yaml"

26
nmap_cmd.php Normal file
View File

@ -0,0 +1,26 @@
<?php
$file = $argv[1];
$site = basename($file, ".yaml");
$__DIR__ = __DIR__;
$conf = yaml_parse_file($file);
$targets = [];
$services = [];
foreach ($conf as $key => $value) {
if ($key != "site") {
foreach($value as $hostaddress => $servicesList) {
$targets[$hostaddress] = true;
if ($servicesList) foreach ($servicesList as $service) {
$services[$service] = true;
}
}
}
}
$targets = join(array_keys($targets), " ");
$services = join(array_keys($services), ",");
echo ("nmap -v -Pn -p $services --script smb-enum-shares,$__DIR__/nmap -oX $__DIR__/scans/.~$site.xml $targets");
?>

View File

@ -1,4 +1,6 @@
<?php
set_time_limit(0);
if (! function_exists('str_ends_with')) {
function str_ends_with(string $haystack, string $needle): bool {
$needle_len = strlen($needle);
@ -49,7 +51,7 @@ XML
$targets = join(array_keys($targets), " ");
$services = join(array_keys($services), ",");
`nmap -v -Pn -p $services --script smb-enum-shares,'$__DIR__/nmap' -oX '$__DIR__/scans/.~$site.xml' $targets && mv '$__DIR__/scans/.~$site.xml' '$__DIR__/scans/$site.xml'`;
`nmap -v -Pn -p $services --script smb-enum-shares,"$__DIR__/nmap" -oX "$__DIR__/scans/.~$site.xml" $targets && mv "$__DIR__/scans/.~$site.xml" "$__DIR__/scans/$site.xml"`;
$xml->asXML("$__DIR__/site/$site.xml");
}

14
scan_all.sh Executable file
View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
DIR="$(dirname -- "$0")"
mkdir -p "$DIR/scans"
mkdir -p "$DIR/site"
for conf in confs/*.yaml
do
site="$(basename ${conf/.yaml/})"
php "$DIR/to_xml.php" $conf > "$DIR/site/$site.xml"
php "$DIR/nmap_cmd.php" $conf | sh
mv "$DIR/scans/.~$site.xml" "$DIR/scans/$site.xml"
done

32
to_xml.php Normal file
View File

@ -0,0 +1,32 @@
<?php
$file = $argv[1];
$site = basename($file, ".yaml");
$conf = yaml_parse_file($file);
$xml = new SimpleXMLElement(<<<XML
<?xml version="1.0"?>
<?xml-stylesheet href='../results.xsl' type='text/xsl'?>
<lanScanConf scanpath="scans/$site.xml"/>
XML
);
foreach ($conf as $key => $value) {
if ($key == "site") {
$xml->addAttribute("site", $value);
} else {
$xmlGroup = $xml->addChild("group");
$xmlGroup->addAttribute("name", $key);
foreach($value as $hostaddress => $servicesList) {
$xmlHost = $xmlGroup->addChild("host");
$xmlHost->addAttribute("address", $hostaddress);
if ($servicesList) foreach ($servicesList as $service) {
$xmlService = $xmlHost->addChild("service");
$xmlService->addAttribute("name", $service);
}
}
}
}
echo $xml->asXML();
?>