Nmap Development mailing list archives

[NSE] make target name given on commandline accessible to nse scripts


From: Sven Klemm <sven () c3d2 de>
Date: Sun, 27 Jan 2008 20:04:28 +0100

Hi,

I've attached a patch that keeps the name given on the commandline for
named hosts and makes it available to nse scripts as targetname in the
host table. This is extremely useful for protocols with virtual host
support.

Cheers,
Sven

-- 
Sven Klemm
http://cthulhu.c3d2.de/~sven/

Index: targets.cc
===================================================================
--- targets.cc  (revision 6760)
+++ targets.cc  (working copy)
@@ -497,6 +497,13 @@
       hs->hostbatch[hidx] = new Target();
       hs->hostbatch[hidx]->setTargetSockAddr(&ss, sslen);
 
+      /* put host expression in target if we have a named host */
+      if ( hs->current_expression.get_targets_type() == TargetGroup::IPV4_NETMASK  &&
+           hs->current_expression.is_namedhost() &&
+           !strchr( hs->target_expressions[hs->next_expression-1], '/' ) ) {
+        hs->hostbatch[hidx]->setTargetName(hs->target_expressions[hs->next_expression-1]);
+      }
+
       /* We figure out the source IP/device IFF
         1) We are r00t AND
         2) We are doing tcp or udp pingscan OR
Index: nse_nmaplib.cc
===================================================================
--- nse_nmaplib.cc      (revision 6760)
+++ nse_nmaplib.cc      (working copy)
@@ -180,7 +180,7 @@
        }
 }
 
-/* set host ip and host name onto the
+/* set host ip, host name and target name onto the
  * table which is currently on the stack
  * set name of the os run by the host onto the
  * table which is currently on the stack
@@ -199,6 +199,11 @@
        lua_pushstring(l, strncpy(hostname, currenths->HostName(), 1024));
        lua_setfield(l, -2, "name");
 
+       if ( currenths->TargetName() ) {
+               lua_pushstring(l, strncpy(hostname, currenths->TargetName(), 1024));
+               lua_setfield(l, -2, "targetname");
+       }
+
        if(currenths->directlyConnectedOrUnset() != -1){
            lua_pushboolean(l, currenths->directlyConnected());
            lua_setfield(l, -2, "directly_connected");
Index: Target.h
===================================================================
--- Target.h    (revision 6760)
+++ Target.h    (working copy)
@@ -176,6 +176,16 @@
   /* This next version returns a STATIC buffer -- so no concurrency */
   const char *NameIP();
 
+  /* Give the name from the last setTargetName() call, which is the 
+   name of the target given on the command line if it's a named
+   host. */
+  const char *TargetName() { return targetname; }
+  /* You can set to NULL to erase a name.  The targetname is blown
+     away when you setTargetSockAddr(), so make sure you do these in proper
+     order
+  */
+  void setTargetName(char *name);
+
   /* If the host is directly connected on a network, set and retrieve
      that information here.  directlyConnected() will abort if it hasn't
      been set yet.  */
@@ -246,6 +256,7 @@
   unsigned int flags; /* HOST_UP, HOST_DOWN, HOST_FIREWALLED, HOST_BROADCAST (instead of HOST_BROADCAST use 
wierd_responses */
   struct timeout_info to;
   char *hostname; // Null if unable to resolve or unset
+  char * targetname; // The name of the target host given on the commmand line if it is a named host
 
 #ifndef NOLUA
   ScriptResults scriptResults;
Index: TargetGroup.cc
===================================================================
--- TargetGroup.cc      (revision 6760)
+++ TargetGroup.cc      (working copy)
@@ -168,7 +168,7 @@
   char *hostexp = strdup(target_expr);
   struct hostent *target;
   unsigned long longtmp;
-  int namedhost = 0;
+  namedhost = 0;
 
   if (targets_type != TYPE_NONE)
     Initialize();
Index: TargetGroup.h
===================================================================
--- TargetGroup.h       (revision 6760)
+++ TargetGroup.h       (working copy)
@@ -134,6 +134,8 @@
   char get_targets_type() {return targets_type;};
   /* get the netmask */
   int get_mask() {return netmask;};
+  /* is the current expression a named host */
+  int is_namedhost() {return namedhost;};
   /* Skip an octet in the range array */
   int skip_range(_octet_nums octet);
  private:
@@ -160,6 +162,8 @@
                  the fields are not valid */
   unsigned long ipsleft; 
 
+  // whether the current target expression is a named host
+  int namedhost;
 };
 
 class HostGroupState {
Index: Target.cc
===================================================================
--- Target.cc   (revision 6760)
+++ Target.cc   (working copy)
@@ -119,6 +119,7 @@
 
 void Target::Initialize() {
   hostname = NULL;
+  targetname = NULL;
   memset(&seq, 0, sizeof(seq));
   distance = -1;
   FPR1 = NULL;
@@ -169,6 +170,9 @@
   if (hostname)
     free(hostname);
 
+  if (targetname)
+    free(targetname);
+
   if (nameIPBuf) {
     free(nameIPBuf);
     nameIPBuf = NULL;
@@ -221,6 +225,7 @@
     /* We had an old target sock, so we better blow away the hostname as
        this one may be new. */
     setHostName(NULL);
+    setTargetName(NULL);
   }
   memcpy(&targetsock, ss, ss_len);
   targetsocklen = ss_len;
@@ -308,6 +313,16 @@
   }
 }
 
+void Target::setTargetName(char *name) {
+  if (targetname) {
+    free(targetname);
+    targetname = NULL;
+  }
+  if (name) {
+    targetname = strdup(name);
+  }
+}
+
  /* Generates a printable string consisting of the host's IP
      address and hostname (if available).  Eg "www.insecure.org
      (64.71.184.53)" or "fe80::202:e3ff:fe14:1102".  The name is

Attachment: signature.asc
Description: OpenPGP digital signature


_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://SecLists.Org

Current thread: