From ea98cd9903254e7636b79f9278302a5241f4214d Mon Sep 17 00:00:00 2001
From: adrien <adrien@malingrey.fr>
Date: Thu, 13 Apr 2023 15:46:47 +0200
Subject: [PATCH] scan_all in bash script

---
 README.md                 |  2 +-
 init.sh                   |  2 +-
 nmap_cmd.php              | 26 ++++++++++++++++++++++++++
 scan_all.php              |  4 +++-
 scan_all.sh               | 14 ++++++++++++++
 to_xml.php                | 32 ++++++++++++++++++++++++++++++++
 toyaml.xsl => to_yaml.xsl |  0
 7 files changed, 77 insertions(+), 3 deletions(-)
 create mode 100644 nmap_cmd.php
 create mode 100755 scan_all.sh
 create mode 100644 to_xml.php
 rename toyaml.xsl => to_yaml.xsl (100%)

diff --git a/README.md b/README.md
index 7c21adb..a699f2d 100644
--- a/README.md
+++ b/README.md
@@ -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 
diff --git a/init.sh b/init.sh
index a2c068f..925d627 100755
--- a/init.sh
+++ b/init.sh
@@ -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"
diff --git a/nmap_cmd.php b/nmap_cmd.php
new file mode 100644
index 0000000..116aded
--- /dev/null
+++ b/nmap_cmd.php
@@ -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");
+?>
diff --git a/scan_all.php b/scan_all.php
index 07a011e..9ef5459 100644
--- a/scan_all.php
+++ b/scan_all.php
@@ -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");
     }
diff --git a/scan_all.sh b/scan_all.sh
new file mode 100755
index 0000000..0c7f2bd
--- /dev/null
+++ b/scan_all.sh
@@ -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
diff --git a/to_xml.php b/to_xml.php
new file mode 100644
index 0000000..0d4fc4a
--- /dev/null
+++ b/to_xml.php
@@ -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();
+?>
diff --git a/toyaml.xsl b/to_yaml.xsl
similarity index 100%
rename from toyaml.xsl
rename to to_yaml.xsl