home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / nmap254b.zip / output.c < prev    next >
C/C++ Source or Header  |  2001-10-14  |  32KB  |  744 lines

  1.  
  2. /***********************************************************************/
  3. /* output.c -- Handles the Nmap output system.  This currently         */
  4. /* involves console-style human readable output, XML output,           */
  5. /* Script |<iddi3 output, and the legacy greppable output (used to be  */
  6. /* called "machine readable").  I expect that future output forms      */
  7. /* (such as HTML) may be created by a different program, library, or   */
  8. /* script using the XML output.                                        */
  9. /*                                                                     */
  10. /***********************************************************************/
  11. /*  The Nmap Security Scanner is (C) 1995-2001 Insecure.Com LLC. This  */
  12. /*  program is free software; you can redistribute it and/or modify    */
  13. /*  it under the terms of the GNU General Public License as published  */
  14. /*  by the Free Software Foundation; Version 2.  This guarantees your  */
  15. /*  right to use, modify, and redistribute this software under certain */
  16. /*  conditions.  If this license is unacceptable to you, we may be     */
  17. /*  willing to sell alternative licenses (contact sales@insecure.com). */
  18. /*                                                                     */
  19. /*  If you received these files with a written license agreement       */
  20. /*  stating terms other than the (GPL) terms above, then that          */
  21. /*  alternative license agreement takes precendence over this comment. */
  22. /*                                                                     */
  23. /*  Source is provided to this software because we believe users have  */
  24. /*  a right to know exactly what a program is going to do before they  */
  25. /*  run it.  This also allows you to audit the software for security   */
  26. /*  holes (none have been found so far).                               */
  27. /*                                                                     */
  28. /*  Source code also allows you to port Nmap to new platforms, fix     */
  29. /*  bugs, and add new features.  You are highly encouraged to send     */
  30. /*  your changes to fyodor@insecure.org for possible incorporation     */
  31. /*  into the main distribution.  By sending these changes to Fyodor or */
  32. /*  one the insecure.org development mailing lists, it is assumed that */
  33. /*  you are offering Fyodor the unlimited, non-exclusive right to      */
  34. /*  reuse, modify, and relicense the code.  This is important because  */
  35. /*  the inability to relicense code has caused devastating problems    */
  36. /*  for other Free Software projects (such as KDE and NASM).  Nmap     */
  37. /*  will always be available Open Source.  If you wish to specify      */
  38. /*  special license conditions of your contributions, just say so      */
  39. /*  when you send them.                                                */
  40. /*                                                                     */
  41. /*  This program is distributed in the hope that it will be useful,    */
  42. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of     */
  43. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  */
  44. /*  General Public License for more details (                          */
  45. /*  http://www.gnu.org/copyleft/gpl.html ).                            */
  46. /*                                                                     */
  47. /***********************************************************************/
  48.  
  49. /* $Id: output.c,v 1.11 2001/10/14 06:46:09 fyodor Exp $ */
  50.  
  51. #include "output.h"
  52. #include "osscan.h"
  53.  
  54. extern struct ops o;
  55. char *logtypes[LOG_TYPES]=LOG_NAMES;
  56.  
  57. /* Prints the familiar Nmap tabular output showing the "interesting"
  58.    ports found on the machine.  It also handles the Machine/Greppable
  59.    output and the XML output.  It is pretty ugly -- in particular I
  60.    should write helper functions to handle the table creation */
  61. void printportoutput(struct hoststruct *currenths, portlist *plist) {
  62.   char protocol[4];
  63.   char rpcinfo[64];
  64.   char rpcmachineinfo[64];
  65.   char portinfo[64];
  66.   char tmpbuf[64];
  67.   char *state;
  68.   char serviceinfo[64];
  69.   char *name=NULL;
  70.   int first = 1;
  71.   struct servent *service;
  72.   struct protoent *proto;
  73.   struct port *current;
  74.   int numignoredports;
  75.   int portno, protocount;
  76.   struct port **protoarrays[2];
  77.  
  78.   numignoredports = plist->state_counts[plist->ignored_port_state];
  79.  
  80.   assert(numignoredports <= plist->numports);
  81.  
  82.  
  83.   log_write(LOG_XML, "<ports><extraports state=\"%s\" count=\"%d\" />\n", 
  84.         statenum2str(currenths->ports.ignored_port_state), 
  85.         numignoredports);
  86.  
  87.   if (numignoredports == plist->numports) {
  88.     log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,
  89.               "%s %d scanned %s on %s (%s) %s: %s\n",
  90.           (numignoredports == 1)? "The" : "All", numignoredports,
  91.           (numignoredports == 1)? "port" : "ports", currenths->name, 
  92.           inet_ntoa(currenths->host), 
  93.           (numignoredports == 1)? "is" : "are", 
  94.           statenum2str(currenths->ports.ignored_port_state));
  95.     log_write(LOG_MACHINE,"Host: %s (%s)\tStatus: Up", 
  96.           inet_ntoa(currenths->host), currenths->name);
  97.     log_write(LOG_XML, "</ports>\n");
  98.     return;
  99.   }
  100.  
  101.   log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"Interesting %s on %s (%s):\n",
  102.         (o.ipprotscan)? "protocols" : "ports", currenths->name, 
  103.         inet_ntoa(currenths->host));
  104.   log_write(LOG_MACHINE,"Host: %s (%s)", inet_ntoa(currenths->host), 
  105.         currenths->name);
  106.   
  107.   if (numignoredports > 0) {
  108.     log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"(The %d %s%s scanned but not shown below %s in state: %s)\n", numignoredports, o.ipprotscan?"protocol":"port", (numignoredports == 1)? "" : "s", (numignoredports == 1)? "is" : "are", statenum2str(plist->ignored_port_state));
  109.   }
  110.  
  111.   if (o.ipprotscan) {
  112.     log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"Protocol   State       Name");
  113.   } else if (!o.rpcscan) {  
  114.     log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"Port       State       Service");
  115.   } else {
  116.     log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"Port       State       Service (RPC)");
  117.   }
  118.   log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"%s", (o.identscan)? ((o.rpcscan)? "           Owner\n" : "                 Owner\n") :"\n");
  119.   log_write(LOG_MACHINE,"\t%s: ", (o.ipprotscan)? "Protocols" : "Ports" );
  120.   
  121.   protoarrays[0] = plist->tcp_ports;
  122.   protoarrays[1] = plist->udp_ports;
  123.   current = NULL;
  124.   if (o.ipprotscan) {
  125.     for (portno = 1; portno < 256; portno++) {
  126.       if (!plist->ip_prots[portno]) continue;
  127.       current = plist->ip_prots[portno];
  128.       if (current->state != plist->ignored_port_state) {
  129.     if (!first) log_write(LOG_MACHINE,", ");
  130.     else first = 0;
  131.     state = statenum2str(current->state);
  132.     proto = nmap_getprotbynum(htons(current->portno));
  133.     snprintf(portinfo, sizeof(portinfo), "%-24s",
  134.          proto?proto->p_name: "unknown");
  135.     log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"%-11d%-12s%-24s\n", portno, state, portinfo);
  136.     log_write(LOG_MACHINE,"%d/%s/%s/", current->portno, state, 
  137.           (proto)? proto->p_name : "");
  138.     log_write(LOG_XML, "<port protocol=\"ip\" portid=\"%d\"><state state=\"%s\" />", current->portno, state);
  139.     if (proto && proto->p_name && *proto->p_name)
  140.       log_write(LOG_XML, "\n<service name=\"%s\" conf=\"3\" method=\"table\" />", proto->p_name);
  141.     log_write(LOG_XML, "</port>\n");
  142.       }
  143.     }
  144.   } else {
  145.    for(portno = 1; portno < 65536; portno++) {
  146.     for(protocount = 0; protocount < 2; protocount++) {
  147.       if (protoarrays[protocount] && protoarrays[protocount][portno]) 
  148.     current = protoarrays[protocount][portno];
  149.       else continue;
  150.       
  151.       if (current->state != plist->ignored_port_state) {    
  152.     if (!first) log_write(LOG_MACHINE,", ");
  153.     else first = 0;
  154.     strcpy(protocol,(current->proto == IPPROTO_TCP)? "tcp": "udp");
  155.     snprintf(portinfo, sizeof(portinfo), "%d/%s", current->portno, protocol);
  156.     state = statenum2str(current->state);
  157.     service = nmap_getservbyport(htons(current->portno), protocol);
  158.     
  159.     if (o.rpcscan) {
  160.       switch(current->rpc_status) {
  161.       case RPC_STATUS_UNTESTED:
  162.         rpcinfo[0] = '\0';
  163.         strcpy(rpcmachineinfo, "");
  164.         break;
  165.       case RPC_STATUS_UNKNOWN:
  166.         strcpy(rpcinfo, "(RPC (Unknown Prog #))");
  167.         strcpy(rpcmachineinfo, "R");
  168.         break;
  169.       case RPC_STATUS_NOT_RPC:
  170.         rpcinfo[0] = '\0';
  171.         strcpy(rpcmachineinfo, "N");
  172.         break;
  173.       case RPC_STATUS_GOOD_PROG:
  174.         name = nmap_getrpcnamebynum(current->rpc_program);
  175.         snprintf(rpcmachineinfo, sizeof(rpcmachineinfo), "(%s:%li*%i-%i)", (name)? name : "", current->rpc_program, current->rpc_lowver, current->rpc_highver);
  176.         if (!name) {
  177.           snprintf(rpcinfo, sizeof(rpcinfo), "(#%li (unknown) V%i-%i)", current->rpc_program, current->rpc_lowver, current->rpc_highver);
  178.         } else {
  179.           if (current->rpc_lowver == current->rpc_highver) {
  180.         snprintf(rpcinfo, sizeof(rpcinfo), "(%s V%i)", name, current->rpc_lowver);
  181.           } else 
  182.         snprintf(rpcinfo, sizeof(rpcinfo), "(%s V%i-%i)", name, current->rpc_lowver, current->rpc_highver);
  183.         }
  184.         break;
  185.       default:
  186.         fatal("Unknown rpc_status %d", current->rpc_status);
  187.         break;
  188.       }
  189.       snprintf(serviceinfo, sizeof(serviceinfo), "%s%s%s", (service)? service->s_name : ((*rpcinfo)? "" : "unknown"), (service)? " " : "",  rpcinfo);
  190.     } else {
  191.       Strncpy(serviceinfo, (service)? service->s_name : "unknown" , sizeof(serviceinfo));
  192.       strcpy(rpcmachineinfo, "");
  193.     }
  194.     log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"%-11s%-12s%-24s", portinfo, state, serviceinfo);
  195.     log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"%s\n", (current->owner)? current->owner : "");
  196.     
  197.     log_write(LOG_MACHINE,"%d/%s/%s/%s/%s/%s//", current->portno, state, 
  198.           protocol, (current->owner)? current->owner : "",
  199.           (service)? service->s_name: "", rpcmachineinfo);    
  200.     
  201.     log_write(LOG_XML, "<port protocol=\"%s\" portid=\"%d\">", protocol, current->portno);
  202.     log_write(LOG_XML, "<state state=\"%s\" />", state);
  203.     if (current->owner && *current->owner) {
  204.       log_write(LOG_XML, "<owner name=\"%s\" />", current->owner);
  205.     }
  206.     if (o.rpcscan && current->rpc_status == RPC_STATUS_GOOD_PROG) {
  207.       if (name) Strncpy(tmpbuf, name, sizeof(tmpbuf));
  208.       else snprintf(tmpbuf, sizeof(tmpbuf), "#%li", current->rpc_program);
  209.       log_write(LOG_XML, "<service name=\"%s\" proto=\"rpc\" rpcnum=\"%li\" lowver=\"%i\" highver=\"%i\" method=\"detection\" conf=\"5\" />\n", tmpbuf, current->rpc_program, current->rpc_lowver, current->rpc_highver);
  210.     } else if (service) {
  211.       log_write(LOG_XML, "<service name=\"%s\" method=\"table\" conf=\"3\"%s />\n", service->s_name, (o.rpcscan && current->rpc_status == RPC_STATUS_UNKNOWN)? "proto=\"rpc\"" : ""); 
  212.     }
  213.     log_write(LOG_XML, "</port>\n");
  214.       }
  215.     }
  216.    }
  217.   }
  218.   log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"\n");
  219.   log_write(LOG_MACHINE, "\tIgnored State: %s (%d)", statenum2str(plist->ignored_port_state), plist->state_counts[plist->ignored_port_state]);
  220.   log_write(LOG_XML, "</ports>\n");
  221. }
  222.  
  223. char* xml_convert (const char* str) {
  224.   unsigned int i;
  225.   char *temp, ch, prevch = 0, *p;
  226.   temp = malloc(strlen(str)*6+1);
  227.   for (p = temp;(prevch = ch, ch = *str);str++) {
  228.     char *a;
  229.     switch (ch) {
  230.     case '<':
  231.       a = "<";
  232.       break;
  233.     case '>':
  234.       a = ">";
  235.       break;
  236.     case '&':
  237.       a =  "&";
  238.       break;
  239.     case '"':
  240.       a = """;
  241.       break;
  242.     case '\'':
  243.       a = "'";
  244.       break;
  245.     case '-': 
  246.       if (prevch == '-') { /* Must escape -- for comments */
  247.         a =  "-";
  248.         break;
  249.       }
  250.     default:
  251.       *p++ = ch;
  252.       continue;
  253.     }
  254.     strcpy(p,a); p += strlen(a);
  255.   }
  256.   *p = 0;
  257.   temp = realloc(temp,strlen(temp)+1);
  258.   return temp;
  259. }
  260.  
  261. /* Write some information (printf style args) to the given log stream(s) */
  262. void log_write(int logt, const char *fmt, ...)
  263. {
  264.   va_list  ap;
  265.   int i,l=logt,skid=1;
  266.   char buffer[1000];
  267.  
  268.   va_start(ap, fmt);
  269.   if (l & LOG_STDOUT) {
  270.     vfprintf(o.nmap_stdout, fmt, ap);
  271.     l-=LOG_STDOUT;
  272.   }
  273.   if (l & LOG_SKID_NOXLT) { skid=0; l -= LOG_SKID_NOXLT; l |= LOG_SKID; }
  274.   if (l<0 || l>LOG_MASK) return;
  275.   for (i=0;l;l>>=1,i++)
  276.     {
  277.       if (!o.logfd[i] || !(l&1)) continue;
  278.       vsnprintf(buffer,sizeof(buffer)-1,fmt,ap);
  279.       if (skid && ((1<<i)&LOG_SKID)) skid_output(buffer);
  280.       fwrite(buffer,1,strlen(buffer),o.logfd[i]);
  281.     }
  282.   va_end(ap);
  283. }
  284.  
  285. /* Close the given log stream(s) */
  286. void log_close(int logt)
  287. {
  288.   int i;
  289.   if (logt<0 || logt>LOG_MASK) return;
  290.   for (i=0;logt;logt>>=1,i++) if (o.logfd[i] && (logt&1)) fclose(o.logfd[i]);
  291. }
  292.  
  293. /* Flush the given log stream(s).  In other words, all buffered output
  294.    is written to the log immediately */
  295. void log_flush(int logt) {
  296.   int i;
  297.  
  298.   if (logt & LOG_STDOUT) {
  299.     fflush(o.nmap_stdout);
  300.     logt -= LOG_STDOUT;
  301.   }
  302.   if (logt & LOG_SKID_NOXLT)
  303.     fatal("You are not allowed to log_flush() with LOG_SKID_NOXLT");
  304.  
  305.   if (logt<0 || logt>LOG_MASK) return;
  306.  
  307.   for (i=0;logt;logt>>=1,i++)
  308.     {
  309.       if (!o.logfd[i] || !(logt&1)) continue;
  310.       fflush(o.logfd[i]);
  311.     }
  312.  
  313. }
  314.  
  315. /* Flush every single log stream -- all buffered output is written to the
  316.    corresponding logs immediately */
  317. void log_flush_all() {
  318.   int fileno;
  319.  
  320.   for(fileno = 0; fileno < LOG_TYPES; fileno++) {
  321.     if (o.logfd[fileno]) fflush(o.logfd[fileno]);
  322.   }
  323.   fflush(stdout);
  324.   fflush(stderr);
  325. }
  326.  
  327. /* Open a log descriptor of the type given to the filename given.  If 
  328.    append is nonzero, the file will be appended instead of clobbered if
  329.    it already exists.  If the file does not exist, it will be created */
  330. int log_open(int logt, int append, char *filename)
  331. {
  332.   int i=0;
  333.   if (logt<=0 || logt>LOG_MASK) return -1;
  334.   while ((logt&1)==0) { i++; logt>>=1; }
  335.   if (o.logfd[i]) fatal("Only one %s output filename allowed",logtypes[i]);
  336.   if (*filename == '-' && *(filename + 1) == '\0')
  337.     {
  338.       o.logfd[i]=stdout;
  339.       o.nmap_stdout = fopen("/dev/null", "w");
  340.       if (!o.nmap_stdout)
  341.     fatal("Could not assign /dev/null to stdout for writing");
  342.   }
  343.   else
  344.     {
  345.       if (o.append_output)
  346.     o.logfd[i] = fopen(filename, "a");
  347.       else
  348.     o.logfd[i] = fopen(filename, "w");
  349.       if (!o.logfd[i])
  350.     fatal("Failed to open %s output file %s for writing", logtypes[i], filename);
  351.     }
  352.   return 1;
  353. }
  354.  
  355. /* Used in creating skript kiddie style output.  |<-R4d! */
  356. void skid_output(char *s)
  357. {
  358.   int i;
  359.   for (i=0;s[i];i++)
  360.     if (rand()%2==0)
  361.       /* Substitutions commented out are not known to me, but maybe look nice */
  362.       switch(s[i])
  363.     {
  364.     case 'A': s[i]='4'; break;
  365.       /*    case 'B': s[i]='8'; break;
  366.          case 'b': s[i]='6'; break;
  367.             case 'c': s[i]='k'; break;
  368.             case 'C': s[i]='K'; break; */
  369.     case 'e':
  370.     case 'E': s[i]='3'; break;
  371.     case 'i':
  372.     case 'I': s[i]="!|1"[rand()%3]; break;
  373.       /*      case 'k': s[i]='c'; break;
  374.             case 'K': s[i]='C'; break;*/
  375.     case 'o':
  376.     case 'O': s[i]='0'; break;
  377.     case 's':
  378.     case 'S': 
  379.       if (s[i+1] && !isalnum((int) s[i+1])) 
  380.         s[i] = 'z';
  381.       else s[i] = '$';
  382.       break;
  383.     case 'z': s[i]='s'; break;
  384.     case 'Z': s[i]='S'; break;
  385.     }  
  386.     else
  387.       {
  388.     if (s[i]>='A' && s[i]<='Z' && (rand()%3==0)) s[i]+='a'-'A';
  389.     else if (s[i]>='a' && s[i]<='z' && (rand()%3==0)) s[i]-='a'-'A';
  390.       }
  391. }
  392.  
  393. /* The items in ports should be
  394.    in sequential order for space savings and easier to read output.  Outputs
  395.    the rangelist to the log stream given (such as LOG_MACHINE or LOG_XML) */
  396. void output_rangelist_given_ports(int logt, unsigned short *ports,
  397.                             int numports) {
  398. int i, previous_port = -2, range_start = -2, port;
  399. char outpbuf[128];
  400.  
  401.  for(i=0; i <= numports; i++) {
  402.    port = (i < numports)? ports[i] : 0xABCDE;
  403.    if (port != previous_port + 1) {
  404.      outpbuf[0] = '\0';
  405.      if (range_start != previous_port && range_start != -2)
  406.        sprintf(outpbuf, "-%hu", previous_port);
  407.      if (port != 0xABCDE) {
  408.        if (range_start != -2)
  409.      strcat(outpbuf, ",");
  410.        sprintf(outpbuf + strlen(outpbuf), "%hu", port);
  411.      }
  412.      log_write(logt, "%s", outpbuf);
  413.      range_start = port;
  414.    }
  415.    previous_port = port;
  416.  }
  417. }
  418.  
  419. /* Output the list of ports scanned to the top of machine parseable
  420.    logs (in a comment, unfortunately).  The items in ports should be
  421.    in sequential order for space savings and easier to read output */
  422. void output_ports_to_machine_parseable_output(struct scan_lists *ports, 
  423.                           int tcpscan, int udpscan,
  424.                           int protscan) {
  425.   int tcpportsscanned = ports->tcp_count;
  426.   int udpportsscanned = ports->udp_count;
  427.   int protsscanned = ports->prot_count;
  428.  log_write(LOG_MACHINE, "# Ports scanned: TCP(%d;", tcpportsscanned);
  429.  if (tcpportsscanned)
  430.    output_rangelist_given_ports(LOG_MACHINE, ports->tcp_ports, tcpportsscanned);
  431.  log_write(LOG_MACHINE, ") UDP(%d;", udpportsscanned);
  432.  if (udpportsscanned)
  433.    output_rangelist_given_ports(LOG_MACHINE, ports->udp_ports, udpportsscanned);
  434.  log_write(LOG_MACHINE, ") PROTOCOLS(%d;", protsscanned);
  435.  if (protsscanned)
  436.    output_rangelist_given_ports(LOG_MACHINE, ports->prots, protsscanned);
  437.  log_write(LOG_MACHINE, ")\n");
  438. }
  439.  
  440. /* Simple helper function for output_xml_scaninfo_records */
  441. static void doscaninfo(char *type, char *proto, unsigned short *ports, 
  442.           int numports) {
  443.   log_write(LOG_XML, "<scaninfo type=\"%s\" protocol=\"%s\" numservices=\"%d\" services=\"", type, proto, numports);
  444.   output_rangelist_given_ports(LOG_XML, ports, numports);
  445.   log_write(LOG_XML, "\" />\n");
  446. }
  447.  
  448. /* Similar to output_ports_to_machine_parseable_output, this function
  449.    outputs the XML version, which is scaninfo records of each scan
  450.    requested and the ports which it will scan for */
  451. void output_xml_scaninfo_records(struct scan_lists *scanlist) {
  452.   if (o.synscan) 
  453.     doscaninfo("syn", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  454.   if (o.ackscan) 
  455.     doscaninfo("ack", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  456.   if (o.bouncescan) 
  457.     doscaninfo("bounce", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  458.   if (o.connectscan)
  459.     doscaninfo("connect", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  460.   if (o.nullscan)
  461.     doscaninfo("null", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  462.   if (o.xmasscan)
  463.     doscaninfo("xmas", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  464.   if (o.windowscan)
  465.     doscaninfo("window", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  466.   if (o.maimonscan) 
  467.     doscaninfo("maimon", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  468.   if (o.finscan) 
  469.     doscaninfo("fin", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  470.   if (o.udpscan) 
  471.     doscaninfo("udp", "udp", scanlist->udp_ports, scanlist->udp_count);
  472.   if (o.ipprotscan) 
  473.     doscaninfo("ipproto", "ip", scanlist->prots, scanlist->prot_count); 
  474. }
  475.  
  476. /* Helper function to write the status and address/hostname info of a host 
  477.    into the XML log */
  478. static void write_xml_initial_hostinfo(struct hoststruct *currenths,
  479.                   char *status) {
  480.   log_write(LOG_XML, "<status state=\"%s\" />\n<address addr=\"%s\" addrtype=\"ipv4\" />\n", status,inet_ntoa(currenths->host));
  481.   if (currenths->name && *currenths->name) {
  482.     log_write(LOG_XML, "<hostnames><hostname name=\"%s\" type=\"PTR\" /></hostnames>\n", currenths->name);
  483.   } else /* If machine is up, put blank hostname so front ends know that
  484.         no name resolution is forthcoming */
  485.     if (strcmp(status, "up") == 0) log_write(LOG_XML, "<hostnames />\n");
  486. }
  487.  
  488. /* Writes host status info to the log streams (including STDOUT).  An
  489.    example is "Host: 10.11.12.13 (foo.bar.example.com)\tStatus: Up\n" to 
  490.    machine log.  resolve_all should be passed nonzero if the user asked
  491.    for all hosts (even down ones) to be resolved */
  492. void write_host_status(struct hoststruct *currenths, int resolve_all) {
  493.  
  494.   if (o.listscan) {
  495.     /* write "unknown" to stdout, machine, and xml */
  496.     log_write(LOG_STDOUT|LOG_NORMAL|LOG_SKID, "Host %s (%s) not scanned\n", currenths->name, inet_ntoa(currenths->host));
  497.     log_write(LOG_MACHINE, "Host: %s (%s)\tStatus: Unknown\n", inet_ntoa(currenths->host), currenths->name);
  498.     write_xml_initial_hostinfo(currenths, "unknown");
  499.   } 
  500.  
  501.   else if (currenths->wierd_responses) { /* SMURF ADDRESS */
  502.     /* Write xml "down" or "up" based on flags and the smurf info */
  503.     write_xml_initial_hostinfo(currenths, 
  504.                    (currenths->flags & HOST_UP)? "up" : "down");
  505.     log_write(LOG_XML, "<smurf responses=\"%d\" />\n", 
  506.           currenths->wierd_responses);
  507.     log_write(LOG_MACHINE,"Host: %s (%s)\tStatus: Smurf (%d responses)\n",  inet_ntoa(currenths->host), currenths->name, currenths->wierd_responses);
  508.     
  509.     if (o.pingscan)
  510.       log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"Host  %s (%s) seems to be a subnet broadcast address (returned %d extra pings).%s\n",  currenths->name, inet_ntoa(currenths->host), currenths->wierd_responses, 
  511.         (currenths->flags & HOST_UP)? " Note -- the actual IP also responded." : "");
  512.     else {
  513.       log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"Host  %s (%s) seems to be a subnet broadcast address (returned %d extra pings). %s.\n",  currenths->name, 
  514.         inet_ntoa(currenths->host), currenths->wierd_responses,
  515.         (currenths->flags & HOST_UP)? 
  516.         " Still scanning it due to ping response from its own IP" 
  517.         : "Skipping host");
  518.     }
  519.   } 
  520.  
  521.   else if (o.pingscan) {
  522.     write_xml_initial_hostinfo(currenths, 
  523.                    (currenths->flags & HOST_UP)? "up" : "down");
  524.     if (currenths->flags & HOST_UP) {
  525.       log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"Host %s (%s) appears to be up.\n", currenths->name, inet_ntoa(currenths->host));
  526.       log_write(LOG_MACHINE,"Host: %s (%s)\tStatus: Up\n", inet_ntoa(currenths->host), currenths->name);
  527.     } else if (o.verbose || resolve_all) {
  528.       if (resolve_all)
  529.     log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"Host %s (%s) appears to be down.\n", currenths->name, inet_ntoa(currenths->host));
  530.       else log_write(LOG_STDOUT,"Host %s (%s) appears to be down.\n", currenths->name, inet_ntoa(currenths->host));
  531.       log_write(LOG_MACHINE, "Host: %s (%s)\tStatus: Down\n", inet_ntoa(currenths->host), currenths->name);
  532.     }
  533.   } 
  534.  
  535.   else {   /* Normal case (non ping/list scan or smurf address) */
  536.     write_xml_initial_hostinfo(currenths, 
  537.                    (currenths->flags & HOST_UP)? "up" : "down");
  538.     if (o.verbose) {
  539.       if (currenths->flags & HOST_UP) {
  540.     log_write(LOG_STDOUT, "Host %s (%s) appears to be up ... good.\n", 
  541.           currenths->name, inet_ntoa(currenths->host));
  542.       } else {
  543.  
  544.     if (resolve_all) {   
  545.       log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"Host %s (%s) appears to be down, skipping it.\n", currenths->name, inet_ntoa(currenths->host));
  546.     }
  547.     else {
  548.       log_write(LOG_STDOUT,"Host %s (%s) appears to be down, skipping it.\n", currenths->name, inet_ntoa(currenths->host));
  549.     }
  550.     log_write(LOG_MACHINE, "Host: %s (%s)\tStatus: Down\n", inet_ntoa(currenths->host), currenths->name);
  551.       }
  552.     }
  553.   }
  554. }
  555.  
  556.  
  557. /* Prints the formatted OS Scan output to stdout, logfiles, etc (but only
  558.    if an OS Scan was performed */
  559. void printosscanoutput(struct hoststruct *currenths) {
  560.   int i;
  561.   char numlst[512]; /* For creating lists of numbers */
  562.   char *p; /* Used in manipulating numlst above */
  563.  
  564.   if (currenths->osscan_performed) {
  565.     log_write(LOG_XML, "<os>");
  566.     if (currenths->osscan_openport > 0) {
  567.       log_write(LOG_XML, 
  568.         "<portused state=\"open\" proto=\"tcp\" portid=\"%hu\" />\n",
  569.         currenths->osscan_openport);
  570.     }
  571.     if (currenths->osscan_closedport > 0) {
  572.       log_write(LOG_XML, 
  573.         "<portused state=\"closed\" proto=\"tcp\" portid=\"%hu\" />\n",
  574.         currenths->osscan_closedport);
  575.     }
  576.     
  577.     if (currenths->FPR.overall_results == OSSCAN_SUCCESS) {
  578.       if (currenths->FPR.num_perfect_matches > 0) {
  579.         char *p;
  580.     log_write(LOG_MACHINE,"\tOS: %s",  currenths->FPR.prints[0]->OS_name);
  581.     log_write(LOG_XML, "<osmatch name=\"%s\" accuracy=\"100\" />\n", 
  582.           p = xml_convert(currenths->FPR.prints[0]->OS_name));
  583.         free(p);
  584.     i = 1;
  585.     while(currenths->FPR.accuracy[i] == 1 ) {
  586.       log_write(LOG_MACHINE,"|%s", currenths->FPR.prints[i]->OS_name);
  587.       log_write(LOG_XML, "<osmatch name=\"%s\" accuracy=\"100\" />\n", 
  588.             p = xml_convert(currenths->FPR.prints[i]->OS_name));
  589.           free(p);
  590.       i++;
  591.     }
  592.     
  593.     if (currenths->FPR.num_perfect_matches == 1)
  594.       log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,
  595.             "Remote operating system guess: %s", 
  596.             currenths->FPR.prints[0]->OS_name);
  597.     
  598.     else {
  599.       log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,
  600.             "Remote OS guesses: %s", 
  601.             currenths->FPR.prints[0]->OS_name);
  602.       i = 1;
  603.       while(currenths->FPR.accuracy[i] == 1) {
  604.         log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,", %s", 
  605.               currenths->FPR.prints[i]->OS_name);
  606.         i++;
  607.       }
  608.     }
  609.       } else {
  610.     if (o.osscan_guess && currenths->FPR.num_matches > 0) {
  611.       /* Print the best guesses available */
  612.       log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"Aggressive OS guesses: %s (%d%%)", currenths->FPR.prints[0]->OS_name, (int) (currenths->FPR.accuracy[0] * 100));
  613.       for(i=1; i < 10 && currenths->FPR.num_matches > i &&
  614.         currenths->FPR.accuracy[i] > 
  615.         currenths->FPR.accuracy[0] - 0.10; i++) {
  616.             char *p;
  617.         log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,", %s (%d%%)", currenths->FPR.prints[i]->OS_name, (int) (currenths->FPR.accuracy[i] * 100));
  618.         log_write(LOG_XML, "<osmatch name=\"%s\" accuracy=\"%d\" />\n", 
  619.               p = xml_convert(currenths->FPR.prints[i]->OS_name),  
  620.               (int) (currenths->FPR.accuracy[i] * 100));
  621.             free(p);
  622.       }
  623.       log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT, "\n");
  624.     }
  625.     if (o.scan_delay < 500 && currenths->osscan_openport > 0 &&
  626.         currenths->osscan_closedport > 0 ) {
  627.       log_write(LOG_NORMAL|LOG_SKID_NOXLT|LOG_STDOUT,"No exact OS matches for host (If you know what OS is running on it, see http://www.insecure.org/cgi-bin/nmap-submit.cgi).\nTCP/IP fingerprint:\n%s\n\n", mergeFPs(currenths->FPs, currenths->numFPs, currenths->osscan_openport, currenths->osscan_closedport));
  628.     } else {
  629.       log_write(LOG_NORMAL|LOG_SKID_NOXLT|LOG_STDOUT,"No exact OS matches for host (test conditions non-ideal).\nTCP/IP fingerprint:\n%s\n\n", mergeFPs(currenths->FPs, currenths->numFPs, currenths->osscan_openport, currenths->osscan_closedport));
  630.     }
  631.       }
  632.       
  633.       log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"\n");      
  634.       if (currenths->goodFP >= 0 && (o.debugging || o.verbose > 1) && currenths->FPR.num_perfect_matches > 0 ) {
  635.     log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"OS Fingerprint:\n%s\n", fp2ascii(currenths->FPs[currenths->goodFP]));
  636.       }
  637.     } else if (currenths->FPR.overall_results == OSSCAN_NOMATCHES) {
  638.       if (o.scan_delay < 500  && currenths->osscan_openport > 0 &&
  639.       currenths->osscan_closedport > 0 ) {
  640.     log_write(LOG_NORMAL|LOG_SKID_NOXLT|LOG_STDOUT,"No OS matches for host (If you know what OS is running on it, see http://www.insecure.org/cgi-bin/nmap-submit.cgi).\nTCP/IP fingerprint:\n%s\n\n", mergeFPs(currenths->FPs, currenths->numFPs, currenths->osscan_openport, currenths->osscan_closedport));
  641.       } else {
  642.     log_write(LOG_NORMAL|LOG_SKID_NOXLT|LOG_STDOUT,"No OS matches for host (test conditions non-ideal).\nTCP/IP fingerprint:\n%s\n\n", mergeFPs(currenths->FPs, currenths->numFPs, currenths->osscan_openport, currenths->osscan_closedport));
  643.       }
  644.     } else if (currenths->FPR.overall_results == OSSCAN_TOOMANYMATCHES)
  645.       {
  646.     log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"Too many fingerprints match this host for me to give an accurate OS guess\n");
  647.     if (o.debugging || o.verbose) {
  648.       log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"TCP/IP fingerprint:\n%s\n\n",  mergeFPs(currenths->FPs, currenths->numFPs, currenths->osscan_openport, currenths->osscan_closedport));
  649.     }
  650.       } else { assert(0); }
  651.      log_write(LOG_XML, "</os>\n");
  652.  
  653.      if (currenths->seq.lastboot) {
  654.        char tmbuf[128];
  655.        struct timeval tv;
  656.        gettimeofday(&tv, NULL);
  657.        strncpy(tmbuf, ctime(&(currenths->seq.lastboot)), sizeof(tmbuf));
  658.        chomp(tmbuf);
  659.        log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"Uptime %.3f days (since %s)\n", (double) (tv.tv_sec - currenths->seq.lastboot) / 86400, tmbuf);
  660.        log_write(LOG_XML, "<uptime seconds=\"%li\" lastboot=\"%s\" />\n", tv.tv_sec - currenths->seq.lastboot, tmbuf);
  661.      }
  662.  
  663.      if (currenths->seq.responses > 3) {
  664.        p=numlst;
  665.        for(i=0; i < currenths->seq.responses; i++) {
  666.      if (p - numlst > (sizeof(numlst) - 15)) 
  667.        fatal("STRANGE ERROR #3877 -- please report to fyodor@insecure.org\n");
  668.      if (p != numlst) *p++=',';
  669.      sprintf(p, "%X", currenths->seq.seqs[i]);
  670.      while(*p) p++;
  671.        }
  672.  
  673.        log_write(LOG_XML, "<tcpsequence index=\"%li\" class=\"%s\" difficulty=\"%s\" values=\"%s\" />\n", currenths->seq.index, seqclass2ascii(currenths->seq.seqclass), seqidx2difficultystr(currenths->seq.index), numlst); 
  674.        if (o.verbose)
  675.      log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"%s", seqreport(&(currenths->seq)));
  676.        log_write(LOG_MACHINE,"\tSeq Index: %d", currenths->seq.index);
  677.      }
  678.  
  679.      if (currenths->seq.responses > 2) {
  680.        p=numlst;
  681.        for(i=0; i < currenths->seq.responses; i++) {
  682.      if (p - numlst > (sizeof(numlst) - 15)) 
  683.        fatal("STRANGE ERROR #3876 -- please report to fyodor@insecure.org\n");
  684.      if (p != numlst) *p++=',';
  685.      sprintf(p, "%hX", currenths->seq.ipids[i]);
  686.      while(*p) p++;
  687.        }
  688.        log_write(LOG_XML, "<ipidsequence class=\"%s\" values=\"%s\"/>\n", ipidclass2ascii(currenths->seq.ipid_seqclass), numlst);
  689.        if (o.verbose)
  690.      log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"IPID Sequence Generation: %s\n", ipidclass2ascii(currenths->seq.ipid_seqclass));
  691.        log_write(LOG_MACHINE,"\tSeq Index: %d", currenths->seq.index);
  692.  
  693.        p=numlst;
  694.        for(i=0; i < currenths->seq.responses; i++) {
  695.      if (p - numlst > (sizeof(numlst) - 15)) 
  696.        fatal("STRANGE ERROR #3877 -- please report to fyodor@insecure.org\n");
  697.      if (p != numlst) *p++=',';
  698.      sprintf(p, "%X", currenths->seq.timestamps[i]);
  699.      while(*p) p++;
  700.        }
  701.        
  702.        log_write(LOG_XML, "<tcptssequence class=\"%s\"", tsseqclass2ascii(currenths->seq.ts_seqclass));
  703.        if (currenths->seq.ts_seqclass != TS_SEQ_UNSUPPORTED) {
  704.      log_write(LOG_XML, " values=\"%s\"", numlst);
  705.        }
  706.        log_write(LOG_XML, "/>\n");
  707.      }
  708.   }
  709. }
  710.  
  711. /* Prints the statistics and other information that goes at the very end
  712.    of an Nmap run */
  713. void printfinaloutput(int numhosts_scanned, int numhosts_up, 
  714.               time_t starttime) {
  715.   time_t timep;
  716.   int i;
  717.   char mytime[128];
  718.  
  719.   timep = time(NULL);
  720.   i = timep - starttime;
  721.   
  722.   if (numhosts_scanned == 0)
  723.     fprintf(stderr, "WARNING: No targets were specified, so 0 hosts scanned.\n");
  724.   if (numhosts_scanned == 1 && numhosts_up == 0 && !o.listscan)
  725.     log_write(LOG_STDOUT, "Note: Host seems down. If it is really up, but blocking our ping probes, try -P0\n");
  726.   log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"\n");
  727.   log_write(LOG_STDOUT|LOG_SKID, "Nmap run completed -- %d %s (%d %s up) scanned in %d %s\n", numhosts_scanned, (numhosts_scanned == 1)? "IP address" : "IP addresses", numhosts_up, (numhosts_up == 1)? "host" : "hosts",  i, (i == 1)? "second": "seconds");
  728.  
  729.  
  730.   Strncpy(mytime, ctime(&timep), sizeof(mytime));
  731.   chomp(mytime);
  732.   
  733.   log_write(LOG_XML, "<runstats><finished time=\"%d\" /><hosts up=\"%d\" down=\"%d\" total=\"%d\" />\n", timep, numhosts_up, numhosts_scanned - numhosts_up, numhosts_scanned);
  734.  
  735.   log_write(LOG_XML, "<!-- Nmap run completed at %s; %d %s (%d %s up) scanned in %d %s -->\n", mytime, numhosts_scanned, (numhosts_scanned == 1)? "IP address" : "IP addresses", numhosts_up, (numhosts_up == 1)? "host" : "hosts",  i, (i == 1)? "second": "seconds");
  736.   log_write(LOG_NORMAL|LOG_MACHINE, "# Nmap run completed at %s -- %d %s (%d %s up) scanned in %d %s\n", mytime, numhosts_scanned, (numhosts_scanned == 1)? "IP address" : "IP addresses", numhosts_up, (numhosts_up == 1)? "host" : "hosts",  i, (i == 1)? "second": "seconds");
  737.  
  738.   log_write(LOG_XML, "</runstats></nmaprun>\n");
  739.  
  740. }
  741.  
  742.  
  743.  
  744.