Nmap Development mailing list archives

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


From: Sven Klemm <sven () c3d2 de>
Date: Mon, 04 Feb 2008 15:02:03 +0100

I've enhanced the patch slightly: documentation for the new field is
now included and the NSE HTTP library uses the targetname as hostname
if it is available.
I've attached the updated patch.

Cheers,
Sven

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

Index: targets.cc
===================================================================
--- targets.cc  (revision 6793)
+++ targets.cc  (working copy)
@@ -497,6 +497,13 @@
       hs->hostbatch[hidx] = new Target();
       hs->hostbatch[hidx]->setTargetSockAddr(&ss, sslen);
 
+      /* put target expression in target if we have a named host without netmask */
+      if ( hs->current_expression.get_targets_type() == TargetGroup::IPV4_NETMASK  &&
+         hs->current_expression.get_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 6793)
+++ 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() ) { // else nil
+               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 6793)
+++ 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: nselib/http.lua
===================================================================
--- nselib/http.lua     (revision 6793)
+++ nselib/http.lua     (working copy)
@@ -72,7 +72,7 @@
   options = options or {}
 
   if type(host) == 'table' then
-    host = ( host.name ~= '' and host.name ) or host.ip
+    host = host.targetname or ( host.name ~= '' and host.name ) or host.ip
   end
 
   local protocol = 'tcp'
Index: docs/scripting.xml
===================================================================
--- docs/scripting.xml  (revision 6793)
+++ docs/scripting.xml  (working copy)
@@ -1527,6 +1527,16 @@
            </listitem>
          </varlistentry>
          <varlistentry>
+           <term><option>host.targetname</option>
+             <indexterm><primary>host.targetname</primary></indexterm></term>
+           <listitem>
+             <para>Contains the name of the host as specified on the commandline.
+               If the target given on the commandline contains a netmask or is an IP
+               address the value of the field is <literal>nil</literal>.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
            <term><option>host.directly_connected</option>
              <indexterm><primary>host.directly_connected</primary></indexterm></term>
            <listitem>
Index: TargetGroup.cc
===================================================================
--- TargetGroup.cc      (revision 6793)
+++ 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 6793)
+++ 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 get_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; 
 
+  // is the current target expression a named host
+  int namedhost;
 };
 
 class HostGroupState {
Index: Target.cc
===================================================================
--- Target.cc   (revision 6793)
+++ 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: