From 55f6be594a15e88d90c27319961a775830a65660 Mon Sep 17 00:00:00 2001
From: adrien <adrien@malingrey.fr>
Date: Tue, 4 Feb 2025 22:57:44 +0100
Subject: [PATCH] traceroute

---
 config.php               |   6 +-
 index.php                |   2 +-
 scan.php                 |  27 ++-------
 stylesheets/hostScan.xsl | 117 ++++++++++++++++++++++-----------------
 4 files changed, 73 insertions(+), 79 deletions(-)

diff --git a/config.php b/config.php
index 278b0c0..09957cb 100644
--- a/config.php
+++ b/config.php
@@ -5,7 +5,7 @@ $SCANSDIR       = "scans";
 $STYLESHEETSDIR = "stylesheets";
 
 $NMAP = "sudo nmap"; # nmap command, E.g. 'nmap', 'sudo nmap' for root privileges or '/usr/bin/nmap' if not in PATH
-$LANSCANOPTIONS  = "-PSmicrosoft-ds -F -T5 --datadir . --script http-info,smb-shares-size --script-args-file script-args.ini -oX - --stylesheet $BASEDIR/$STYLESHEETSDIR/lanScan.xsl";
-$HOSTSCANOPTIONS = "-A -T5 --datadir . --script http-info,smb-shares-size --script-args-file script-args.ini -oX - --stylesheet $BASEDIR/$STYLESHEETSDIR/hostScan.xsl";
 $DATADIR = ".";
-$SCRIPTARGSFILE = "script-args.ini";
\ No newline at end of file
+$SCRIPTARGSFILE = "script-args.ini";
+$LANSCANOPTIONS  = "-PSmicrosoft-ds -F -T5 --datadir '$DATADIR' --script http-info,smb-shares-size --script-args-file '$SCRIPTARGSFILE' -oX - --stylesheet $BASEDIR/$STYLESHEETSDIR/lanScan.xsl";
+$HOSTSCANOPTIONS = "-A -T5 --datadir '$DATADIR' --script http-info,smb-shares-size --script-args-file '$SCRIPTARGSFILE' -oX - --stylesheet $BASEDIR/$STYLESHEETSDIR/hostScan.xsl";
\ No newline at end of file
diff --git a/index.php b/index.php
index 0fce543..c48f8a8 100644
--- a/index.php
+++ b/index.php
@@ -70,7 +70,7 @@ if (file_exists($SCANSDIR)) {
   </div>
 
   <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
-  <script src="https://cdn.jsdelivr.net/npm/semantic-ui/dist/semantic.min.js"></script>
+  <script src="https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.9.2/semantic.min.js"></script>
   <script>
     $('#lanSelect').dropdown({allowAdditions: true, clearable: true})
 
diff --git a/scan.php b/scan.php
index b70fe1d..eb778d3 100644
--- a/scan.php
+++ b/scan.php
@@ -7,33 +7,13 @@ $targetsListRegex        = '/^[\da-zA-Z-. \/]+$/';
 $lan = filter_input(INPUT_GET, 'lan', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => $targetsListRegex], "flags" => FILTER_NULL_ON_FAILURE]);
 if ($lan) {
   $cmd = "$NMAP $LANSCANOPTIONS $lan";
-
-  if (!file_exists($SCANSDIR)) mkdir($SCANSDIR);
   $filename = str_replace("/", "!", $lan);
-  $path = "$SCANSDIR/$filename.xml";
-
-  if (!file_exists($path)) $cmd .= " | tee '$path'";
-
-  header('Content-type: text/xml');
-  system("$cmd", $retcode);
-
-  exit();
 }
 
 $host = filter_input(INPUT_GET, 'host', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => $targetsListRegex], "flags" => FILTER_NULL_ON_FAILURE]);
 if ($host) {
   $cmd = "$NMAP $HOSTSCANOPTIONS $host";
-
-  if (!file_exists($SCANSDIR)) mkdir($SCANSDIR);
   $filename = str_replace("/", "!", $host);
-  $path = "$SCANSDIR/$filename.xml";
-
-  if (!file_exists($path)) $cmd .= " | tee '$path'";
-
-  header('Content-type: text/xml');
-  system("$cmd", $retcode);
-
-  exit();
 }
 
 $targets = filter_input(INPUT_GET, 'targets', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => $targetsListRegex], "flags" => FILTER_NULL_ON_FAILURE]);
@@ -173,10 +153,11 @@ if ($targets) {
   }
   
   $cmd = "$NMAP$options $targets";
-  echo "<!--$cmd-->\n";
-
-  if (!file_exists($SCANSDIR)) mkdir($SCANSDIR);
   $filename = str_replace("/", "!", $targets);
+}
+
+if ($cmd) {
+  if (!file_exists($SCANSDIR)) mkdir($SCANSDIR);
   $path = "$SCANSDIR/$filename.xml";
 
   if (!file_exists($path)) $cmd .= " | tee '$path'";
diff --git a/stylesheets/hostScan.xsl b/stylesheets/hostScan.xsl
index 2ec64cd..3c747db 100644
--- a/stylesheets/hostScan.xsl
+++ b/stylesheets/hostScan.xsl
@@ -11,19 +11,15 @@
   <xsl:output indent="yes" />
   <xsl:strip-space elements='*' />
 
-  <xsl:variable name="stylesheetURL"
-    select="substring-before(substring-after(processing-instruction('xml-stylesheet'),'href=&quot;'), '&quot;')" />
+  <xsl:variable name="stylesheetURL" select="substring-before(substring-after(processing-instruction('xml-stylesheet'),'href=&quot;'), '&quot;')" />
   <xsl:variable name="base" select="concat($stylesheetURL, '/../../')" />
 
   <xsl:template match="nmaprun">
     <xsl:variable name="targets" select="substring-after(@args, '.xsl ')" />
-    <xsl:variable
-      name="current" select="." />
-    <xsl:variable name="init"
-      select="document(concat($base, 'scans/', translate($targets,'/', '!'), '.xml'))/nmaprun" />
+    <xsl:variable name="current" select="." />
+    <xsl:variable name="init" select="document(concat($base, 'scans/', translate($targets,'/', '!'), '.xml'))/nmaprun" />
 
-    <html
-      lang="fr">
+    <html lang="fr">
       <xsl:apply-templates select="." mode="head">
         <xsl:with-param name="base" select="$base" />
         <xsl:with-param name="targets" select="$targets" />
@@ -34,8 +30,7 @@
         </xsl:apply-templates>
 
         <main class="ui main container inverted segment">
-          <xsl:apply-templates
-            select="$current/host | $init/host[not(address/@addr=$current/host/address/@addr)][not(status/@state='down')]">
+          <xsl:apply-templates select="$current/host | $init/host[not(address/@addr=$current/host/address/@addr)][not(status/@state='down')]">
             <xsl:with-param name="init" select="$init" />
             <xsl:with-param name="current" select="$current" />
           </xsl:apply-templates>
@@ -57,12 +52,9 @@ $('.ui.dropdown').dropdown()
   <xsl:template match="host">
     <xsl:param name="init" />
     <xsl:param name="current" />
-    <xsl:variable name="addr"
-      select="address/@addr" />
-    <xsl:variable name="initHost"
-      select="$init/host[address/@addr=$addr]" />
-    <xsl:variable name="currentHost"
-      select="$current/host[address/@addr=$addr]" />
+    <xsl:variable name="addr" select="address/@addr" />
+    <xsl:variable name="initHost" select="$init/host[address/@addr=$addr]" />
+    <xsl:variable name="currentHost" select="$current/host[address/@addr=$addr]" />
     <xsl:variable name="hostAddress">
       <xsl:choose>
         <xsl:when test="hostnames/hostname/@name">
@@ -77,7 +69,7 @@ $('.ui.dropdown').dropdown()
     <h1>
       <xsl:attribute name="class">
         <xsl:text>ui inverted header </xsl:text>
-          <xsl:choose>
+        <xsl:choose>
           <xsl:when test="$currentHost/status/@state='up'">green</xsl:when>
           <xsl:otherwise>red</xsl:otherwise>
         </xsl:choose>
@@ -92,8 +84,7 @@ $('.ui.dropdown').dropdown()
       </xsl:choose>
     </h1>
 
-    <table
-      class="ui inverted table" style="width: max-content">
+    <table class="ui inverted table" style="width: max-content">
       <thead>
         <tr>
           <xsl:if test="address[@addrtype='ipv4']/@addr">
@@ -141,7 +132,7 @@ $('.ui.dropdown').dropdown()
           <xsl:if test="distance/@value">
             <td>
               <xsl:value-of select="distance/@value" />
-              <xsl:text> rebond(s)</xsl:text>
+              <xsl:text> étape(s)</xsl:text>
             </td>
           </xsl:if>
           <xsl:if test="uptime/@lastboot">
@@ -153,8 +144,7 @@ $('.ui.dropdown').dropdown()
       </tbody>
     </table>
 
-    <xsl:if
-      test="hostscript/script">
+    <xsl:if test="hostscript/script">
       <div class="ui inverted tree accordion">
         <div class="title">
           <i class="dropdown icon"></i> Informations supplémentaires </div>
@@ -164,30 +154,26 @@ $('.ui.dropdown').dropdown()
       </div>
     </xsl:if>
 
-    <h2
-      class="ui header">Services</h2>
+    <h2 class="ui header">Services</h2>
 
     <div class="ui cards">
-      <xsl:apply-templates
-        select="$currentHost/ports/port | $initHost/ports/port[not(@portid=$currentHost/ports/port/@portid)][not(state/@state='closed')]">
+      <xsl:apply-templates select="$currentHost/ports/port | $initHost/ports/port[not(@portid=$currentHost/ports/port/@portid)][not(state/@state='closed')]">
         <xsl:with-param name="initHost" select="$initHost" />
         <xsl:with-param name="currentHost" select="$currentHost" />
         <xsl:with-param name="hostAddress" select="$hostAddress" />
       </xsl:apply-templates>
     </div>
 
+    <xsl:apply-templates select="trace" />
   </xsl:template>
 
   <xsl:template match="port">
     <xsl:param name="hostAddress" />
     <xsl:param name="initHost" />
     <xsl:param name="currentHost" />
-    <xsl:variable
-      name="portid" select="@portid" />
-    <xsl:variable name="initPort"
-      select="$initHost/ports/port[@portid=$portid]" />
-    <xsl:variable name="currentPort"
-      select="$currentHost/ports/port[@portid=$portid]" />
+    <xsl:variable name="portid" select="@portid" />
+    <xsl:variable name="initPort" select="$initHost/ports/port[@portid=$portid]" />
+    <xsl:variable name="currentPort" select="$currentHost/ports/port[@portid=$portid]" />
     <xsl:variable name="color">
       <xsl:choose>
         <xsl:when test="$currentPort/script[@id='http-info']/elem[@key='status']>=500">red</xsl:when>
@@ -199,8 +185,7 @@ $('.ui.dropdown').dropdown()
       </xsl:choose>
     </xsl:variable>
 
-    <div
-      class="ui inverted card {$color}">
+    <div class="ui inverted card {$color}">
       <div class="content">
         <div class="header">
           <div class="ui {$color} ribbon label">
@@ -249,8 +234,7 @@ $('.ui.dropdown').dropdown()
           </div>
         </div>
       </div>
-      <xsl:if
-        test="service/@name='ftp' or service/@name='ssh' or service/@name='http' or service/@name='https' or service/@name='ms-wbt-server'">
+      <xsl:if test="service/@name='ftp' or service/@name='ssh' or service/@name='http' or service/@name='https' or service/@name='ms-wbt-server'">
         <a class="ui {$color} button" target="_blank">
           <xsl:attribute name="href">
             <xsl:choose>
@@ -258,8 +242,7 @@ $('.ui.dropdown').dropdown()
                 <xsl:text>rdp.php?v=</xsl:text>
                 <xsl:value-of select="$hostAddress" />
                 <xsl:text>&amp;p=</xsl:text>
-                <xsl:value-of
-                  select="@portid" />
+                <xsl:value-of select="@portid" />
               </xsl:when>
               <xsl:otherwise>
                 <xsl:choose>
@@ -271,8 +254,7 @@ $('.ui.dropdown').dropdown()
                   </xsl:otherwise>
                 </xsl:choose>
                 <xsl:text>://</xsl:text>
-                <xsl:value-of
-                  select="$hostAddress" />
+                <xsl:value-of select="$hostAddress" />
                 <xsl:text>:</xsl:text>
                 <xsl:value-of select="@portid" />
               </xsl:otherwise>
@@ -286,14 +268,12 @@ $('.ui.dropdown').dropdown()
         <div class="ui {$color} center aligned dropdown share-size button">
           <xsl:attribute name="style">
             <xsl:for-each select="$currentPort/script[@id='smb-shares-size']/table">
-              <xsl:sort select="elem[@key='FreeSize'] div elem[@key='TotalSize']"
-                order="ascending" />
-                        <xsl:if test="position()=1">
+              <xsl:sort select="elem[@key='FreeSize'] div elem[@key='TotalSize']" order="ascending" />
+              <xsl:if test="position()=1">
                 <xsl:text>--free: </xsl:text>
-                            <xsl:value-of select="elem[@key='FreeSize']" />
-                            <xsl:text>; --total: </xsl:text>
-                            <xsl:value-of
-                  select="elem[@key='TotalSize']" />
+                <xsl:value-of select="elem[@key='FreeSize']" />
+                <xsl:text>; --total: </xsl:text>
+                <xsl:value-of select="elem[@key='TotalSize']" />
               </xsl:if>
             </xsl:for-each>
           </xsl:attribute>
@@ -327,8 +307,7 @@ $('.ui.dropdown').dropdown()
                 </tbody>
               </table>
             </xsl:if>
-            <xsl:apply-templates
-              select="table" />
+            <xsl:apply-templates select="table" />
           </xsl:when>
           <xsl:otherwise>
             <xsl:value-of select="@output" />
@@ -381,11 +360,45 @@ $('.ui.dropdown').dropdown()
 
   <xsl:template match="table">
     <xsl:param name="hostAddress" />
-        <a class="item share-size"
-      href="file://///{$hostAddress}/{@key}" target="_blank" rel="noopener noreferrer"
-      style="--free: {elem[@key='FreeSize']}; --total: {elem[@key='TotalSize']}">
+    <a class="item share-size" href="file://///{$hostAddress}/{@key}" target="_blank" rel="noopener noreferrer" style="--free: {elem[@key='FreeSize']}; --total: {elem[@key='TotalSize']}">
       <xsl:value-of select="@key" />
     </a>
   </xsl:template>
 
+
+  <xsl:template match="trace">
+    <h2 class="ui header">Traceroute</h2>
+
+    <table class="ui inverted table">
+      <thead>
+        <tr>
+          <th>Étape</th>
+          <th>Adresse</th>
+          <th>Temps</th>
+        </tr>
+      </thead>
+      <tbody>
+        <xsl:apply-templates select="hop" />
+      </tbody>
+    </table>
+  </xsl:template>
+
+  <xsl:template match="hop">
+    <tr>
+      <td>
+        <xsl:value-of select="@ttl" />
+      </td>
+      <td>
+        <xsl:value-of select="@host" />
+        <xsl:text> (</xsl:text>
+        <xsl:value-of select="@ipaddr" />
+        <xsl:text>)</xsl:text>
+      </td>
+      <td>
+        <xsl:value-of select="@rtt" />
+        <xsl:text> ms</xsl:text>
+      </td>
+    </tr>
+  </xsl:template>
+
 </xsl:stylesheet>
\ No newline at end of file