PageRenderTime 72ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/output.cc

https://gitlab.com/g10h4ck/nmap-gsoc2015
C++ | 2703 lines | 2081 code | 290 blank | 332 comment | 687 complexity | cd625942f2c7ce02d1bc93430316e5e7 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-2.0, LGPL-2.1, MIT
  1. /***************************************************************************
  2. * output.cc -- Handles the Nmap output system. This currently involves *
  3. * console-style human readable output, XML output, Script |<iddi3 *
  4. * output, and the legacy grepable output (used to be called "machine *
  5. * readable"). I expect that future output forms (such as HTML) may be *
  6. * created by a different program, library, or script using the XML *
  7. * output. *
  8. * *
  9. ***********************IMPORTANT NMAP LICENSE TERMS************************
  10. * *
  11. * The Nmap Security Scanner is (C) 1996-2015 Insecure.Com LLC. Nmap is *
  12. * also a registered trademark of Insecure.Com LLC. This program is free *
  13. * software; you may redistribute and/or modify it under the terms of the *
  14. * GNU General Public License as published by the Free Software *
  15. * Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE CLARIFICATIONS *
  16. * AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your right to use, *
  17. * modify, and redistribute this software under certain conditions. If *
  18. * you wish to embed Nmap technology into proprietary software, we sell *
  19. * alternative licenses (contact sales@nmap.com). Dozens of software *
  20. * vendors already license Nmap technology such as host discovery, port *
  21. * scanning, OS detection, version detection, and the Nmap Scripting *
  22. * Engine. *
  23. * *
  24. * Note that the GPL places important restrictions on "derivative works", *
  25. * yet it does not provide a detailed definition of that term. To avoid *
  26. * misunderstandings, we interpret that term as broadly as copyright law *
  27. * allows. For example, we consider an application to constitute a *
  28. * derivative work for the purpose of this license if it does any of the *
  29. * following with any software or content covered by this license *
  30. * ("Covered Software"): *
  31. * *
  32. * o Integrates source code from Covered Software. *
  33. * *
  34. * o Reads or includes copyrighted data files, such as Nmap's nmap-os-db *
  35. * or nmap-service-probes. *
  36. * *
  37. * o Is designed specifically to execute Covered Software and parse the *
  38. * results (as opposed to typical shell or execution-menu apps, which will *
  39. * execute anything you tell them to). *
  40. * *
  41. * o Includes Covered Software in a proprietary executable installer. The *
  42. * installers produced by InstallShield are an example of this. Including *
  43. * Nmap with other software in compressed or archival form does not *
  44. * trigger this provision, provided appropriate open source decompression *
  45. * or de-archiving software is widely available for no charge. For the *
  46. * purposes of this license, an installer is considered to include Covered *
  47. * Software even if it actually retrieves a copy of Covered Software from *
  48. * another source during runtime (such as by downloading it from the *
  49. * Internet). *
  50. * *
  51. * o Links (statically or dynamically) to a library which does any of the *
  52. * above. *
  53. * *
  54. * o Executes a helper program, module, or script to do any of the above. *
  55. * *
  56. * This list is not exclusive, but is meant to clarify our interpretation *
  57. * of derived works with some common examples. Other people may interpret *
  58. * the plain GPL differently, so we consider this a special exception to *
  59. * the GPL that we apply to Covered Software. Works which meet any of *
  60. * these conditions must conform to all of the terms of this license, *
  61. * particularly including the GPL Section 3 requirements of providing *
  62. * source code and allowing free redistribution of the work as a whole. *
  63. * *
  64. * As another special exception to the GPL terms, Insecure.Com LLC grants *
  65. * permission to link the code of this program with any version of the *
  66. * OpenSSL library which is distributed under a license identical to that *
  67. * listed in the included docs/licenses/OpenSSL.txt file, and distribute *
  68. * linked combinations including the two. *
  69. * *
  70. * Any redistribution of Covered Software, including any derived works, *
  71. * must obey and carry forward all of the terms of this license, including *
  72. * obeying all GPL rules and restrictions. For example, source code of *
  73. * the whole work must be provided and free redistribution must be *
  74. * allowed. All GPL references to "this License", are to be treated as *
  75. * including the terms and conditions of this license text as well. *
  76. * *
  77. * Because this license imposes special exceptions to the GPL, Covered *
  78. * Work may not be combined (even as part of a larger work) with plain GPL *
  79. * software. The terms, conditions, and exceptions of this license must *
  80. * be included as well. This license is incompatible with some other open *
  81. * source licenses as well. In some cases we can relicense portions of *
  82. * Nmap or grant special permissions to use it in other open source *
  83. * software. Please contact fyodor@nmap.org with any such requests. *
  84. * Similarly, we don't incorporate incompatible open source software into *
  85. * Covered Software without special permission from the copyright holders. *
  86. * *
  87. * If you have any questions about the licensing restrictions on using *
  88. * Nmap in other works, are happy to help. As mentioned above, we also *
  89. * offer alternative license to integrate Nmap into proprietary *
  90. * applications and appliances. These contracts have been sold to dozens *
  91. * of software vendors, and generally include a perpetual license as well *
  92. * as providing for priority support and updates. They also fund the *
  93. * continued development of Nmap. Please email sales@nmap.com for further *
  94. * information. *
  95. * *
  96. * If you have received a written license agreement or contract for *
  97. * Covered Software stating terms other than these, you may choose to use *
  98. * and redistribute Covered Software under those terms instead of these. *
  99. * *
  100. * Source is provided to this software because we believe users have a *
  101. * right to know exactly what a program is going to do before they run it. *
  102. * This also allows you to audit the software for security holes. *
  103. * *
  104. * Source code also allows you to port Nmap to new platforms, fix bugs, *
  105. * and add new features. You are highly encouraged to send your changes *
  106. * to the dev@nmap.org mailing list for possible incorporation into the *
  107. * main distribution. By sending these changes to Fyodor or one of the *
  108. * Insecure.Org development mailing lists, or checking them into the Nmap *
  109. * source code repository, it is understood (unless you specify otherwise) *
  110. * that you are offering the Nmap Project (Insecure.Com LLC) the *
  111. * unlimited, non-exclusive right to reuse, modify, and relicense the *
  112. * code. Nmap will always be available Open Source, but this is important *
  113. * because the inability to relicense code has caused devastating problems *
  114. * for other Free Software projects (such as KDE and NASM). We also *
  115. * occasionally relicense the code to third parties as discussed above. *
  116. * If you wish to specify special license conditions of your *
  117. * contributions, just say so when you send them. *
  118. * *
  119. * This program is distributed in the hope that it will be useful, but *
  120. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  121. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Nmap *
  122. * license file for more details (it's in a COPYING file included with *
  123. * Nmap, and also available from https://svn.nmap.org/nmap/COPYING) *
  124. * *
  125. ***************************************************************************/
  126. /* $Id$ */
  127. #include "nmap.h"
  128. #include "output.h"
  129. #include "osscan.h"
  130. #include "osscan2.h"
  131. #include "NmapOps.h"
  132. #include "NmapOutputTable.h"
  133. #include "MACLookup.h"
  134. #include "portreasons.h"
  135. #include "protocols.h"
  136. #include "FingerPrintResults.h"
  137. #include "Target.h"
  138. #include "utils.h"
  139. #include "xml.h"
  140. #include "nbase.h"
  141. #include "libnetutil/netutil.h"
  142. #include <math.h>
  143. #include <set>
  144. #include <vector>
  145. #include <list>
  146. #include <sstream>
  147. extern NmapOps o;
  148. static const char *logtypes[LOG_NUM_FILES] = LOG_NAMES;
  149. /* Used in creating skript kiddie style output. |<-R4d! */
  150. static void skid_output(char *s) {
  151. int i;
  152. for (i = 0; s[i]; i++)
  153. /* We need a 50/50 chance here, use a random number */
  154. if ((get_random_u8() & 0x01) == 0)
  155. /* Substitutions commented out are not known to me, but maybe look nice */
  156. switch (s[i]) {
  157. case 'A':
  158. s[i] = '4';
  159. break;
  160. /* case 'B': s[i]='8'; break;
  161. case 'b': s[i]='6'; break;
  162. case 'c': s[i]='k'; break;
  163. case 'C': s[i]='K'; break; */
  164. case 'e':
  165. case 'E':
  166. s[i] = '3';
  167. break;
  168. case 'i':
  169. case 'I':
  170. s[i] = "!|1"[get_random_u8() % 3];
  171. break;
  172. /* case 'k': s[i]='c'; break;
  173. case 'K': s[i]='C'; break; */
  174. case 'o':
  175. case 'O':
  176. s[i] = '0';
  177. break;
  178. case 's':
  179. case 'S':
  180. if (s[i + 1] && !isalnum((int) (unsigned char) s[i + 1]))
  181. s[i] = 'z';
  182. else
  183. s[i] = '$';
  184. break;
  185. case 'z':
  186. s[i] = 's';
  187. break;
  188. case 'Z':
  189. s[i] = 'S';
  190. break;
  191. } else {
  192. if (s[i] >= 'A' && s[i] <= 'Z' && (get_random_u8() % 3 == 0)) {
  193. s[i] += 'a' - 'A'; /* 1/3 chance of lower-case */
  194. } else if (s[i] >= 'a' && s[i] <= 'z' && (get_random_u8() % 3 == 0)) {
  195. s[i] -= 'a' - 'A'; /* 1/3 chance of upper-case */
  196. }
  197. }
  198. }
  199. /* Remove all "\nSF:" from fingerprints */
  200. static char *servicefp_sf_remove(const char *str) {
  201. char *temp = (char *) safe_malloc(strlen(str) + 1);
  202. char *dst = temp, *src = (char *) str;
  203. char *ampptr = 0;
  204. while (*src) {
  205. if (strncmp(src, "\nSF:", 4) == 0) {
  206. src += 4;
  207. continue;
  208. }
  209. /* Needed so "&something;" is not truncated midway */
  210. if (*src == '&') {
  211. ampptr = dst;
  212. } else if (*src == ';') {
  213. ampptr = 0;
  214. }
  215. *dst++ = *src++;
  216. }
  217. if (ampptr != 0) {
  218. *ampptr = '\0';
  219. } else {
  220. *dst = '\0';
  221. }
  222. return temp;
  223. }
  224. // Prints an XML <service> element for the information given in
  225. // serviceDeduction. This function should only be called if ether
  226. // the service name or the service fingerprint is non-null.
  227. static void print_xml_service(const struct serviceDeductions *sd) {
  228. xml_open_start_tag("service");
  229. xml_attribute("name", "%s", sd->name ? sd->name : "unknown");
  230. if (sd->product)
  231. xml_attribute("product", "%s", sd->product);
  232. if (sd->version)
  233. xml_attribute("version", "%s", sd->version);
  234. if (sd->extrainfo)
  235. xml_attribute("extrainfo", "%s", sd->extrainfo);
  236. if (sd->hostname)
  237. xml_attribute("hostname", "%s", sd->hostname);
  238. if (sd->ostype)
  239. xml_attribute("ostype", "%s", sd->ostype);
  240. if (sd->devicetype)
  241. xml_attribute("devicetype", "%s", sd->devicetype);
  242. if (sd->service_fp) {
  243. char *servicefp = servicefp_sf_remove(sd->service_fp);
  244. xml_attribute("servicefp", "%s", servicefp);
  245. free(servicefp);
  246. }
  247. if (sd->service_tunnel == SERVICE_TUNNEL_SSL)
  248. xml_attribute("tunnel", "ssl");
  249. xml_attribute("method", "%s", (sd->dtype == SERVICE_DETECTION_TABLE) ? "table" : "probed");
  250. xml_attribute("conf", "%i", sd->name_confidence);
  251. if (sd->cpe.empty()) {
  252. xml_close_empty_tag();
  253. } else {
  254. unsigned int i;
  255. xml_close_start_tag();
  256. for (i = 0; i < sd->cpe.size(); i++) {
  257. xml_start_tag("cpe");
  258. xml_write_escaped("%s", sd->cpe[i]);
  259. xml_end_tag();
  260. }
  261. xml_end_tag();
  262. }
  263. }
  264. #ifdef WIN32
  265. /* Show a fatal error explaining that an interface is not Ethernet and won't
  266. work on Windows. Do nothing if --send-ip (PACKET_SEND_IP_STRONG) was used. */
  267. void win32_fatal_raw_sockets(const char *devname) {
  268. if ((o.sendpref & PACKET_SEND_IP_STRONG) != 0)
  269. return;
  270. if (devname != NULL) {
  271. fatal("Only ethernet devices can be used for raw scans on Windows, and\n"
  272. "\"%s\" is not an ethernet device. Use the --unprivileged option\n"
  273. "for this scan.", devname);
  274. } else {
  275. fatal("Only ethernet devices can be used for raw scans on Windows. Use\n"
  276. "the --unprivileged option for this scan.");
  277. }
  278. }
  279. /* Display the mapping from libdnet interface names (like "eth0") to WinPcap
  280. interface names (like "\Device\NPF_{...}"). This is the same mapping used by
  281. eth_open and so can help diagnose connection problems. Additionally display
  282. WinPcap interface names that are not mapped to by any libdnet name, in other
  283. words the names of interfaces Nmap has no way of using.*/
  284. static void print_iflist_pcap_mapping(const struct interface_info *iflist,
  285. int numifs) {
  286. pcap_if_t *pcap_ifs;
  287. std::list<const pcap_if_t *> leftover_pcap_ifs;
  288. std::list<const pcap_if_t *>::iterator leftover_p;
  289. int i;
  290. /* Build a list of "leftover" libpcap interfaces. Initially it contains all
  291. the interfaces. */
  292. pcap_ifs = getpcapinterfaces();
  293. for (const pcap_if_t *p = pcap_ifs; p != NULL; p = p->next)
  294. leftover_pcap_ifs.push_front(p);
  295. if (numifs > 0 || !leftover_pcap_ifs.empty()) {
  296. NmapOutputTable Tbl(1 + numifs + leftover_pcap_ifs.size(), 2);
  297. Tbl.addItem(0, 0, false, "DEV");
  298. Tbl.addItem(0, 1, false, "WINDEVICE");
  299. /* Show the libdnet names and what they map to. */
  300. for (i = 0; i < numifs; i++) {
  301. char pcap_name[1024];
  302. if (DnetName2PcapName(iflist[i].devname, pcap_name, sizeof(pcap_name))) {
  303. /* We got a name. Remove it from the list of leftovers. */
  304. std::list<const pcap_if_t *>::iterator next;
  305. for (leftover_p = leftover_pcap_ifs.begin();
  306. leftover_p != leftover_pcap_ifs.end(); leftover_p = next) {
  307. next = leftover_p;
  308. next++;
  309. if (strcmp((*leftover_p)->name, pcap_name) == 0)
  310. leftover_pcap_ifs.erase(leftover_p);
  311. }
  312. } else {
  313. Strncpy(pcap_name, "<none>", sizeof(pcap_name));
  314. }
  315. Tbl.addItem(i + 1, 0, false, iflist[i].devname);
  316. Tbl.addItem(i + 1, 1, true, pcap_name);
  317. }
  318. /* Show the "leftover" libpcap interface names (those without a libdnet
  319. name that maps to them). */
  320. for (leftover_p = leftover_pcap_ifs.begin();
  321. leftover_p != leftover_pcap_ifs.end();
  322. leftover_p++) {
  323. Tbl.addItem(i + 1, 0, false, "<none>");
  324. Tbl.addItem(i + 1, 1, false, (*leftover_p)->name);
  325. i++;
  326. }
  327. log_write(LOG_PLAIN, "%s\n", Tbl.printableTable(NULL));
  328. log_flush_all();
  329. }
  330. pcap_freealldevs(pcap_ifs);
  331. }
  332. #endif
  333. /* Print a detailed list of Nmap interfaces and routes to
  334. normal/skiddy/stdout output */
  335. int print_iflist(void) {
  336. int numifs = 0, numroutes = 0;
  337. struct interface_info *iflist;
  338. struct sys_route *routes;
  339. NmapOutputTable *Tbl = NULL;
  340. char errstr[256];
  341. const char *address = NULL;
  342. errstr[0]='\0';
  343. iflist = getinterfaces(&numifs, errstr, sizeof(errstr));
  344. int i;
  345. /* First let's handle interfaces ... */
  346. if (iflist==NULL || numifs<=0) {
  347. log_write(LOG_PLAIN, "INTERFACES: NONE FOUND(!)\n");
  348. if (o.debugging)
  349. log_write(LOG_STDOUT, "Reason: %s\n", errstr);
  350. } else {
  351. int devcol = 0, shortdevcol = 1, ipcol = 2, typecol = 3, upcol = 4, mtucol = 5, maccol = 6;
  352. Tbl = new NmapOutputTable(numifs + 1, 7);
  353. Tbl->addItem(0, devcol, false, "DEV", 3);
  354. Tbl->addItem(0, shortdevcol, false, "(SHORT)", 7);
  355. Tbl->addItem(0, ipcol, false, "IP/MASK", 7);
  356. Tbl->addItem(0, typecol, false, "TYPE", 4);
  357. Tbl->addItem(0, upcol, false, "UP", 2);
  358. Tbl->addItem(0, mtucol, false, "MTU", 3);
  359. Tbl->addItem(0, maccol, false, "MAC", 3);
  360. for (i = 0; i < numifs; i++) {
  361. Tbl->addItem(i + 1, devcol, false, iflist[i].devfullname);
  362. Tbl->addItemFormatted(i + 1, shortdevcol, false, "(%s)",
  363. iflist[i].devname);
  364. address = inet_ntop_ez(&(iflist[i].addr), sizeof(iflist[i].addr));
  365. Tbl->addItemFormatted(i + 1, ipcol, false, "%s/%d",
  366. address == NULL ? "(none)" : address,
  367. iflist[i].netmask_bits);
  368. if (iflist[i].device_type == devt_ethernet) {
  369. Tbl->addItem(i + 1, typecol, false, "ethernet");
  370. Tbl->addItemFormatted(i + 1, maccol, false,
  371. "%02X:%02X:%02X:%02X:%02X:%02X",
  372. iflist[i].mac[0], iflist[i].mac[1],
  373. iflist[i].mac[2], iflist[i].mac[3],
  374. iflist[i].mac[4], iflist[i].mac[5]);
  375. } else if (iflist[i].device_type == devt_loopback)
  376. Tbl->addItem(i + 1, typecol, false, "loopback");
  377. else if (iflist[i].device_type == devt_p2p)
  378. Tbl->addItem(i + 1, typecol, false, "point2point");
  379. else
  380. Tbl->addItem(i + 1, typecol, false, "other");
  381. Tbl->addItem(i + 1, upcol, false,
  382. (iflist[i].device_up ? "up" : "down"));
  383. Tbl->addItemFormatted(i + 1, mtucol, false, "%d", iflist[i].mtu);
  384. }
  385. log_write(LOG_PLAIN, "************************INTERFACES************************\n");
  386. log_write(LOG_PLAIN, "%s\n", Tbl->printableTable(NULL));
  387. log_flush_all();
  388. delete Tbl;
  389. }
  390. #ifdef WIN32
  391. /* Print the libdnet->libpcap interface name mapping. */
  392. print_iflist_pcap_mapping(iflist, numifs);
  393. #endif
  394. /* OK -- time to handle routes */
  395. errstr[0]='\0';
  396. routes = getsysroutes(&numroutes, errstr, sizeof(errstr));
  397. u16 nbits;
  398. if (routes==NULL || numroutes<= 0) {
  399. log_write(LOG_PLAIN, "ROUTES: NONE FOUND(!)\n");
  400. if (o.debugging)
  401. log_write(LOG_STDOUT, "Reason: %s\n", errstr);
  402. } else {
  403. int dstcol = 0, devcol = 1, metcol = 2, gwcol = 3;
  404. Tbl = new NmapOutputTable(numroutes + 1, 4);
  405. Tbl->addItem(0, dstcol, false, "DST/MASK", 8);
  406. Tbl->addItem(0, devcol, false, "DEV", 3);
  407. Tbl->addItem(0, metcol, false, "METRIC", 6);
  408. Tbl->addItem(0, gwcol, false, "GATEWAY", 7);
  409. for (i = 0; i < numroutes; i++) {
  410. nbits = routes[i].netmask_bits;
  411. Tbl->addItemFormatted(i + 1, dstcol, false, "%s/%d",
  412. inet_ntop_ez(&routes[i].dest, sizeof(routes[i].dest)), nbits);
  413. Tbl->addItem(i + 1, devcol, false, routes[i].device->devfullname);
  414. Tbl->addItemFormatted(i + 1, metcol, false, "%d", routes[i].metric);
  415. if (!sockaddr_equal_zero(&routes[i].gw))
  416. Tbl->addItem(i + 1, gwcol, true, inet_ntop_ez(&routes[i].gw, sizeof(routes[i].gw)));
  417. }
  418. log_write(LOG_PLAIN, "**************************ROUTES**************************\n");
  419. log_write(LOG_PLAIN, "%s\n", Tbl->printableTable(NULL));
  420. log_flush_all();
  421. delete Tbl;
  422. }
  423. return 0;
  424. }
  425. #ifndef NOLUA
  426. /* Escape control characters to make a string safe to display on a terminal. */
  427. static std::string escape_for_screen(const std::string s) {
  428. std::string r;
  429. for (unsigned int i = 0; i < s.size(); i++) {
  430. char buf[5];
  431. unsigned char c = s[i];
  432. if (c == '\t' || c == '\r' || c == '\n' || (0x20 <= c && c <= 0x7e)) {
  433. r += c;
  434. } else {
  435. Snprintf(buf, sizeof(buf), "\\x%02X", c);
  436. r += buf;
  437. }
  438. }
  439. return r;
  440. }
  441. /* Do something to protect characters that can't appear in XML. This is not a
  442. reversible transform, more a last-ditch effort to write readable XML with
  443. characters that shouldn't be part of regular output anyway. The escaping that
  444. xml_write_escaped is not enough; some characters are not allowed to appear in
  445. XML, not even escaped. */
  446. std::string protect_xml(const std::string s) {
  447. /* escape_for_screen is good enough. */
  448. return escape_for_screen(s);
  449. }
  450. /* This is a helper function to determine the ordering of the script results
  451. based on their id. */
  452. static bool scriptid_lessthan(ScriptResult a, ScriptResult b) {
  453. return strcmp(a.get_id(), b.get_id()) < 0;
  454. }
  455. static char *formatScriptOutput(ScriptResult sr) {
  456. std::vector<std::string> lines;
  457. std::string c_output;
  458. const char *p, *q;
  459. std::string result;
  460. unsigned int i;
  461. c_output = escape_for_screen(sr.get_output_str());
  462. if (c_output.empty())
  463. return NULL;
  464. p = c_output.c_str();
  465. while (*p != '\0') {
  466. q = strchr(p, '\n');
  467. if (q == NULL) {
  468. lines.push_back(std::string(p));
  469. break;
  470. } else {
  471. lines.push_back(std::string(p, q - p));
  472. p = q + 1;
  473. }
  474. }
  475. if (lines.empty())
  476. lines.push_back("");
  477. for (i = 0; i < lines.size(); i++) {
  478. if (i < lines.size() - 1)
  479. result += "| ";
  480. else
  481. result += "|_";
  482. if (i == 0)
  483. result += std::string(sr.get_id()) + ": ";
  484. result += lines[i];
  485. if (i < lines.size() - 1)
  486. result += "\n";
  487. }
  488. return strdup(result.c_str());
  489. }
  490. #endif /* NOLUA */
  491. /* Prints the familiar Nmap tabular output showing the "interesting"
  492. ports found on the machine. It also handles the Machine/Grepable
  493. output and the XML output. It is pretty ugly -- in particular I
  494. should write helper functions to handle the table creation */
  495. void printportoutput(Target *currenths, PortList *plist) {
  496. char protocol[MAX_IPPROTOSTRLEN + 1];
  497. char portinfo[64];
  498. char grepvers[256];
  499. char *p;
  500. const char *state;
  501. char serviceinfo[64];
  502. int i;
  503. int first = 1;
  504. struct protoent *proto;
  505. Port *current;
  506. Port port;
  507. char hostname[1200];
  508. struct serviceDeductions sd;
  509. NmapOutputTable *Tbl = NULL;
  510. int portcol = -1; // port or IP protocol #
  511. int statecol = -1; // port/protocol state
  512. int servicecol = -1; // service or protocol name
  513. int versioncol = -1;
  514. int reasoncol = -1;
  515. int colno = 0;
  516. unsigned int rowno;
  517. int numrows;
  518. int numignoredports = plist->numIgnoredPorts();
  519. int numports = plist->numPorts();
  520. std::vector<const char *> saved_servicefps;
  521. if (o.noportscan)
  522. return;
  523. xml_start_tag("ports");
  524. int prevstate = PORT_UNKNOWN;
  525. int istate;
  526. while ((istate = plist->nextIgnoredState(prevstate)) != PORT_UNKNOWN) {
  527. xml_open_start_tag("extraports");
  528. xml_attribute("state", "%s", statenum2str(istate));
  529. xml_attribute("count", "%d", plist->getStateCounts(istate));
  530. xml_close_start_tag();
  531. xml_newline();
  532. print_xml_state_summary(plist, istate);
  533. xml_end_tag();
  534. xml_newline();
  535. prevstate = istate;
  536. }
  537. if (numignoredports == numports) {
  538. if (numignoredports == 0) {
  539. log_write(LOG_PLAIN, "0 ports scanned on %s\n",
  540. currenths->NameIP(hostname, sizeof(hostname)));
  541. } else {
  542. log_write(LOG_PLAIN, "%s %d scanned %s on %s %s ",
  543. (numignoredports == 1) ? "The" : "All", numignoredports,
  544. (numignoredports == 1) ? "port" : "ports",
  545. currenths->NameIP(hostname, sizeof(hostname)),
  546. (numignoredports == 1) ? "is" : "are");
  547. if (plist->numIgnoredStates() == 1) {
  548. log_write(LOG_PLAIN, "%s", statenum2str(plist->nextIgnoredState(PORT_UNKNOWN)));
  549. } else {
  550. prevstate = PORT_UNKNOWN;
  551. while ((istate = plist->nextIgnoredState(prevstate)) != PORT_UNKNOWN) {
  552. if (prevstate != PORT_UNKNOWN)
  553. log_write(LOG_PLAIN, " or ");
  554. log_write(LOG_PLAIN, "%s (%d)", statenum2str(istate),
  555. plist->getStateCounts(istate));
  556. prevstate = istate;
  557. }
  558. }
  559. if (o.reason)
  560. print_state_summary(plist, STATE_REASON_EMPTY);
  561. log_write(LOG_PLAIN, "\n");
  562. }
  563. log_write(LOG_MACHINE, "Host: %s (%s)\tStatus: Up",
  564. currenths->targetipstr(), currenths->HostName());
  565. xml_end_tag(); /* ports */
  566. xml_newline();
  567. return;
  568. }
  569. if (o.verbose > 1 || o.debugging) {
  570. time_t tm_secs, tm_sece;
  571. struct tm *tm;
  572. char tbufs[128];
  573. tm_secs = currenths->StartTime();
  574. tm_sece = currenths->EndTime();
  575. tm = localtime(&tm_secs);
  576. if (strftime(tbufs, sizeof(tbufs), "%Y-%m-%d %H:%M:%S %Z", tm) <= 0)
  577. fatal("Unable to properly format host start time");
  578. log_write(LOG_PLAIN, "Scanned at %s for %lds\n",
  579. tbufs, (long) (tm_sece - tm_secs));
  580. }
  581. log_write(LOG_MACHINE, "Host: %s (%s)", currenths->targetipstr(),
  582. currenths->HostName());
  583. /* Show line like:
  584. Not shown: 3995 closed ports, 514 filtered ports
  585. if appropriate (note that states are reverse-sorted by # of ports) */
  586. prevstate = PORT_UNKNOWN;
  587. while ((istate = plist->nextIgnoredState(prevstate)) != PORT_UNKNOWN) {
  588. if (prevstate == PORT_UNKNOWN)
  589. log_write(LOG_PLAIN, "Not shown: ");
  590. else
  591. log_write(LOG_PLAIN, ", ");
  592. char desc[32];
  593. if (o.ipprotscan)
  594. Snprintf(desc, sizeof(desc),
  595. (plist->getStateCounts(istate) ==
  596. 1) ? "protocol" : "protocols");
  597. else
  598. Snprintf(desc, sizeof(desc),
  599. (plist->getStateCounts(istate) == 1) ? "port" : "ports");
  600. log_write(LOG_PLAIN, "%d %s %s", plist->getStateCounts(istate),
  601. statenum2str(istate), desc);
  602. prevstate = istate;
  603. }
  604. if (prevstate != PORT_UNKNOWN)
  605. log_write(LOG_PLAIN, "\n");
  606. if (o.reason)
  607. print_state_summary(plist, STATE_REASON_FULL);
  608. /* OK, now it is time to deal with the service table ... */
  609. colno = 0;
  610. portcol = colno++;
  611. statecol = colno++;
  612. servicecol = colno++;
  613. if (o.reason)
  614. reasoncol = colno++;
  615. if (o.servicescan)
  616. versioncol = colno++;
  617. numrows = numports - numignoredports;
  618. #ifndef NOLUA
  619. int scriptrows = 0;
  620. if (plist->numscriptresults > 0)
  621. scriptrows = plist->numscriptresults;
  622. numrows += scriptrows;
  623. #endif
  624. assert(numrows > 0);
  625. numrows++; // The header counts as a row
  626. Tbl = new NmapOutputTable(numrows, colno);
  627. // Lets start with the headers
  628. if (o.ipprotscan)
  629. Tbl->addItem(0, portcol, false, "PROTOCOL", 8);
  630. else
  631. Tbl->addItem(0, portcol, false, "PORT", 4);
  632. Tbl->addItem(0, statecol, false, "STATE", 5);
  633. Tbl->addItem(0, servicecol, false, "SERVICE", 7);
  634. if (versioncol > 0)
  635. Tbl->addItem(0, versioncol, false, "VERSION", 7);
  636. if (reasoncol > 0)
  637. Tbl->addItem(0, reasoncol, false, "REASON", 6);
  638. log_write(LOG_MACHINE, "\t%s: ", (o.ipprotscan) ? "Protocols" : "Ports");
  639. rowno = 1;
  640. if (o.ipprotscan) {
  641. current = NULL;
  642. while ((current = plist->nextPort(current, &port, IPPROTO_IP, 0)) != NULL) {
  643. if (!plist->isIgnoredState(current->state)) {
  644. if (!first)
  645. log_write(LOG_MACHINE, ", ");
  646. else
  647. first = 0;
  648. if (o.reason) {
  649. if (current->reason.ttl)
  650. Tbl->addItemFormatted(rowno, reasoncol, false, "%s ttl %d",
  651. port_reason_str(current->reason), current->reason.ttl);
  652. else
  653. Tbl->addItem(rowno, reasoncol, true, port_reason_str(current->reason));
  654. }
  655. state = statenum2str(current->state);
  656. proto = nmap_getprotbynum(current->portno);
  657. Snprintf(portinfo, sizeof(portinfo), "%s", proto ? proto->p_name : "unknown");
  658. Tbl->addItemFormatted(rowno, portcol, false, "%d", current->portno);
  659. Tbl->addItem(rowno, statecol, true, state);
  660. Tbl->addItem(rowno, servicecol, true, portinfo);
  661. log_write(LOG_MACHINE, "%d/%s/%s/", current->portno, state,
  662. (proto) ? proto->p_name : "");
  663. xml_open_start_tag("port");
  664. xml_attribute("protocol", "ip");
  665. xml_attribute("portid", "%d", current->portno);
  666. xml_close_start_tag();
  667. xml_open_start_tag("state");
  668. xml_attribute("state", "%s", state);
  669. xml_attribute("reason", "%s", reason_str(current->reason.reason_id, SINGULAR));
  670. xml_attribute("reason_ttl", "%d", current->reason.ttl);
  671. if (current->reason.ip_addr.sockaddr.sa_family != AF_UNSPEC) {
  672. struct sockaddr_storage ss;
  673. memcpy(&ss, &current->reason.ip_addr, sizeof(current->reason.ip_addr));
  674. xml_attribute("reason_ip", "%s", inet_ntop_ez(&ss, sizeof(ss)));
  675. }
  676. xml_close_empty_tag();
  677. if (proto && proto->p_name && *proto->p_name) {
  678. xml_newline();
  679. xml_open_start_tag("service");
  680. xml_attribute("name", "%s", proto->p_name);
  681. xml_attribute("conf", "8");
  682. xml_attribute("method", "table");
  683. xml_close_empty_tag();
  684. }
  685. xml_end_tag(); /* port */
  686. xml_newline();
  687. rowno++;
  688. }
  689. }
  690. } else {
  691. char fullversion[160];
  692. current = NULL;
  693. while ((current = plist->nextPort(current, &port, TCPANDUDPANDSCTP, 0)) != NULL) {
  694. if (!plist->isIgnoredState(current->state)) {
  695. if (!first)
  696. log_write(LOG_MACHINE, ", ");
  697. else
  698. first = 0;
  699. strcpy(protocol, IPPROTO2STR(current->proto));
  700. Snprintf(portinfo, sizeof(portinfo), "%d/%s", current->portno, protocol);
  701. state = statenum2str(current->state);
  702. plist->getServiceDeductions(current->portno, current->proto, &sd);
  703. if (sd.service_fp && saved_servicefps.size() <= 8)
  704. saved_servicefps.push_back(sd.service_fp);
  705. current->getNmapServiceName(serviceinfo, sizeof(serviceinfo));
  706. Tbl->addItem(rowno, portcol, true, portinfo);
  707. Tbl->addItem(rowno, statecol, false, state);
  708. Tbl->addItem(rowno, servicecol, true, serviceinfo);
  709. if (o.reason) {
  710. if (current->reason.ttl)
  711. Tbl->addItemFormatted(rowno, reasoncol, false, "%s ttl %d",
  712. port_reason_str(current->reason), current->reason.ttl);
  713. else
  714. Tbl->addItem(rowno, reasoncol, true, port_reason_str(current->reason));
  715. }
  716. sd.populateFullVersionString(fullversion, sizeof(fullversion));
  717. if (*fullversion && versioncol > 0)
  718. Tbl->addItem(rowno, versioncol, true, fullversion);
  719. // How should we escape illegal chars in grepable output?
  720. // Well, a reasonably clean way would be backslash escapes
  721. // such as \/ and \\ . // But that makes it harder to pick
  722. // out fields with awk, cut, and such. So I'm gonna use the
  723. // ugly hack (fitting to grepable output) of replacing the '/'
  724. // character with '|' in the version field.
  725. Strncpy(grepvers, fullversion, sizeof(grepvers) / sizeof(*grepvers));
  726. p = grepvers;
  727. while ((p = strchr(p, '/'))) {
  728. *p = '|';
  729. p++;
  730. }
  731. if (!sd.name)
  732. serviceinfo[0] = '\0';
  733. else {
  734. p = serviceinfo;
  735. while ((p = strchr(p, '/'))) {
  736. *p = '|';
  737. p++;
  738. }
  739. }
  740. log_write(LOG_MACHINE, "%d/%s/%s//%s//%s/", current->portno,
  741. state, protocol, serviceinfo, grepvers);
  742. xml_open_start_tag("port");
  743. xml_attribute("protocol", "%s", protocol);
  744. xml_attribute("portid", "%d", current->portno);
  745. xml_close_start_tag();
  746. xml_open_start_tag("state");
  747. xml_attribute("state", "%s", state);
  748. xml_attribute("reason", "%s", reason_str(current->reason.reason_id, SINGULAR));
  749. xml_attribute("reason_ttl", "%d", current->reason.ttl);
  750. if (current->reason.ip_addr.sockaddr.sa_family != AF_UNSPEC) {
  751. struct sockaddr_storage ss;
  752. memcpy(&ss, &current->reason.ip_addr, sizeof(current->reason.ip_addr));
  753. xml_attribute("reason_ip", "%s", inet_ntop_ez(&ss, sizeof(ss)));
  754. }
  755. xml_close_empty_tag();
  756. if (sd.name || sd.service_fp || sd.service_tunnel != SERVICE_TUNNEL_NONE)
  757. print_xml_service(&sd);
  758. rowno++;
  759. #ifndef NOLUA
  760. if (o.script) {
  761. ScriptResults::const_iterator ssr_iter;
  762. //Sort the results before outputting them on the screen
  763. current->scriptResults.sort(scriptid_lessthan);
  764. for (ssr_iter = current->scriptResults.begin();
  765. ssr_iter != current->scriptResults.end(); ssr_iter++) {
  766. ssr_iter->write_xml();
  767. char *script_output = formatScriptOutput((*ssr_iter));
  768. if (script_output != NULL) {
  769. Tbl->addItem(rowno, 0, true, true, script_output);
  770. free(script_output);
  771. }
  772. rowno++;
  773. }
  774. }
  775. #endif
  776. xml_end_tag(); /* port */
  777. xml_newline();
  778. }
  779. }
  780. }
  781. /* log_write(LOG_PLAIN,"\n"); */
  782. /* Grepable output supports only one ignored state. */
  783. if (plist->numIgnoredStates() == 1) {
  784. istate = plist->nextIgnoredState(PORT_UNKNOWN);
  785. if (plist->getStateCounts(istate) > 0)
  786. log_write(LOG_MACHINE, "\tIgnored State: %s (%d)",
  787. statenum2str(istate), plist->getStateCounts(istate));
  788. }
  789. xml_end_tag(); /* ports */
  790. xml_newline();
  791. // Now we write the table for the user
  792. log_write(LOG_PLAIN, "%s", Tbl->printableTable(NULL));
  793. delete Tbl;
  794. // There may be service fingerprints I would like the user to submit
  795. if (saved_servicefps.size() > 0) {
  796. int numfps = saved_servicefps.size();
  797. log_write(LOG_PLAIN, "%d service%s unrecognized despite returning data."
  798. " If you know the service/version, please submit the following"
  799. " fingerprint%s at"
  800. " https://nmap.org/cgi-bin/submit.cgi?new-service :\n",
  801. numfps, (numfps > 1) ? "s" : "", (numfps > 1) ? "s" : "");
  802. for (i = 0; i < numfps; i++) {
  803. if (numfps > 1)
  804. log_write(LOG_PLAIN, "==============NEXT SERVICE FINGERPRINT (SUBMIT INDIVIDUALLY)==============\n");
  805. log_write(LOG_PLAIN, "%s\n", saved_servicefps[i]);
  806. }
  807. }
  808. log_flush_all();
  809. }
  810. char *logfilename(const char *str, struct tm *tm) {
  811. char *ret, *end, *p;
  812. char tbuf[10];
  813. int retlen = strlen(str) * 6 + 1;
  814. ret = (char *) safe_malloc(retlen);
  815. end = ret + retlen;
  816. for (p = ret; *str; str++) {
  817. if (*str == '%') {
  818. str++;
  819. if (!*str)
  820. break;
  821. switch (*str) {
  822. case 'H':
  823. strftime(tbuf, sizeof tbuf, "%H", tm);
  824. break;
  825. case 'M':
  826. strftime(tbuf, sizeof tbuf, "%M", tm);
  827. break;
  828. case 'S':
  829. strftime(tbuf, sizeof tbuf, "%S", tm);
  830. break;
  831. case 'T':
  832. strftime(tbuf, sizeof tbuf, "%H%M%S", tm);
  833. break;
  834. case 'R':
  835. strftime(tbuf, sizeof tbuf, "%H%M", tm);
  836. break;
  837. case 'm':
  838. strftime(tbuf, sizeof tbuf, "%m", tm);
  839. break;
  840. case 'd':
  841. strftime(tbuf, sizeof tbuf, "%d", tm);
  842. break;
  843. case 'y':
  844. strftime(tbuf, sizeof tbuf, "%y", tm);
  845. break;
  846. case 'Y':
  847. strftime(tbuf, sizeof tbuf, "%Y", tm);
  848. break;
  849. case 'D':
  850. strftime(tbuf, sizeof tbuf, "%m%d%y", tm);
  851. break;
  852. default:
  853. *p++ = *str;
  854. continue;
  855. }
  856. assert(end - p > 1);
  857. Strncpy(p, tbuf, end - p - 1);
  858. p += strlen(tbuf);
  859. } else {
  860. *p++ = *str;
  861. }
  862. }
  863. *p = 0;
  864. return (char *) safe_realloc(ret, strlen(ret) + 1);
  865. }
  866. /* This is the workhorse of the logging functions. Usually it is
  867. called through log_write(), but it can be called directly if you are dealing
  868. with a vfprintf-style va_list. YOU MUST SANDWICH EACH EXECUTION OF THIS CALL
  869. BETWEEN va_start() AND va_end() calls. */
  870. void log_vwrite(int logt, const char *fmt, va_list ap) {
  871. char *writebuf;
  872. bool skid_noxlate = false;
  873. int rc = 0;
  874. int len;
  875. int fileidx = 0;
  876. int l;
  877. int logtype;
  878. va_list apcopy;
  879. for (logtype = 1; logtype <= LOG_MAX; logtype <<= 1) {
  880. if (!(logt & logtype))
  881. continue;
  882. switch (logtype) {
  883. case LOG_STDOUT:
  884. vfprintf(o.nmap_stdout, fmt, ap);
  885. break;
  886. case LOG_STDERR:
  887. fflush(stdout); // Otherwise some systems will print stderr out of order
  888. vfprintf(stderr, fmt, ap);
  889. break;
  890. case LOG_SKID_NOXLT:
  891. skid_noxlate = true;
  892. /* no break */
  893. case LOG_NORMAL:
  894. case LOG_MACHINE:
  895. case LOG_SKID:
  896. case LOG_XML:
  897. len = alloc_vsprintf(&writebuf, fmt, ap);
  898. if (writebuf == NULL)
  899. fatal("%s: alloc_vsprintf failed.", __func__);
  900. if (logtype == LOG_SKID_NOXLT)
  901. l = LOG_SKID;
  902. else
  903. l = logtype;
  904. fileidx = 0;
  905. while ((l & 1) == 0) {
  906. fileidx++;
  907. l >>= 1;
  908. }
  909. assert(fileidx < LOG_NUM_FILES);
  910. if (o.logfd[fileidx] && len) {
  911. if ((logtype & (LOG_SKID|LOG_SKID_NOXLT)) && !skid_noxlate)
  912. skid_output(writebuf);
  913. rc = fwrite(writebuf, len, 1, o.logfd[fileidx]);
  914. if (rc != 1) {
  915. fatal("Failed to write %d bytes of data to (logt==%d) stream. fwrite returned %d. Quitting.", len, logtype, rc);
  916. }
  917. va_end(apcopy);
  918. }
  919. free(writebuf);
  920. break;
  921. default:
  922. /* Unknown log type.
  923. * ---
  924. * Note that we're not calling fatal() here to avoid infinite call loop
  925. * between fatal() and this log_vwrite() function. */
  926. assert(0); /* We want people to report it. */
  927. }
  928. }
  929. return;
  930. }
  931. /* Write some information (printf style args) to the given log stream(s).
  932. Remember to watch out for format string bugs. */
  933. void log_write(int logt, const char *fmt, ...) {
  934. va_list ap;
  935. assert(logt > 0);
  936. if (!fmt || !*fmt)
  937. return;
  938. for (int l = 1; l <= LOG_MAX; l <<= 1) {
  939. if (logt & l) {
  940. va_start(ap, fmt);
  941. log_vwrite(l, fmt, ap);
  942. va_end(ap);
  943. }
  944. }
  945. return;
  946. }
  947. /* Close the given log stream(s) */
  948. void log_close(int logt) {
  949. int i;
  950. if (logt < 0 || logt > LOG_FILE_MASK)
  951. return;
  952. for (i = 0; logt; logt >>= 1, i++)
  953. if (o.logfd[i] && (logt & 1))
  954. fclose(o.logfd[i]);
  955. }
  956. /* Flush the given log stream(s). In other words, all buffered output
  957. is written to the log immediately */
  958. void log_flush(int logt) {
  959. int i;
  960. if (logt & LOG_STDOUT) {
  961. fflush(o.nmap_stdout);
  962. logt -= LOG_STDOUT;
  963. }
  964. if (logt & LOG_STDERR) {
  965. fflush(stderr);
  966. logt -= LOG_STDERR;
  967. }
  968. if (logt & LOG_SKID_NOXLT)
  969. fatal("You are not allowed to %s() with LOG_SKID_NOXLT", __func__);
  970. if (logt < 0 || logt > LOG_FILE_MASK)
  971. return;
  972. for (i = 0; logt; logt >>= 1, i++) {
  973. if (!o.logfd[i] || !(logt & 1))
  974. continue;
  975. fflush(o.logfd[i]);
  976. }
  977. }
  978. /* Flush every single log stream -- all buffered output is written to the
  979. corresponding logs immediately */
  980. void log_flush_all() {
  981. int fileno;
  982. for (fileno = 0; fileno < LOG_NUM_FILES; fileno++) {
  983. if (o.logfd[fileno])
  984. fflush(o.logfd[fileno]);
  985. }
  986. fflush(stdout);
  987. fflush(stderr);
  988. }
  989. /* Open a log descriptor of the type given to the filename given. If
  990. append is nonzero, the file will be appended instead of clobbered if
  991. it already exists. If the file does not exist, it will be created */
  992. int log_open(int logt, int append, char *filename) {
  993. int i = 0;
  994. if (logt <= 0 || logt > LOG_FILE_MASK)
  995. return -1;
  996. while ((logt & 1) == 0) {
  997. i++;
  998. logt >>= 1;
  999. }
  1000. if (o.logfd[i])
  1001. fatal("Only one %s output filename allowed", logtypes[i]);
  1002. if (*filename == '-' && *(filename + 1) == '\0') {
  1003. o.logfd[i] = stdout;
  1004. o.nmap_stdout = fopen(DEVNULL, "w");
  1005. if (!o.nmap_stdout)
  1006. fatal("Could not assign %s to stdout for writing", DEVNULL);
  1007. } else {
  1008. if (append)
  1009. o.logfd[i] = fopen(filename, "a");
  1010. else
  1011. o.logfd[i] = fopen(filename, "w");
  1012. if (!o.logfd[i])
  1013. fatal("Failed to open %s output file %s for writing", logtypes[i],
  1014. filename);
  1015. }
  1016. return 1;
  1017. }
  1018. /* The items in ports should be
  1019. in sequential order for space savings and easier to read output. Outputs the
  1020. rangelist to the log stream given (such as LOG_MACHINE or LOG_XML) */
  1021. static void output_rangelist_given_ports(int logt, unsigned short *ports,
  1022. int numports) {
  1023. int start, end;
  1024. start = 0;
  1025. while (start < numports) {
  1026. end = start;
  1027. while (end + 1 < numports && ports[end + 1] == ports[end] + 1)
  1028. end++;
  1029. if (start > 0)
  1030. log_write(logt, ",");
  1031. if (start == end)
  1032. log_write(logt, "%hu", ports[start]);
  1033. else
  1034. log_write(logt, "%hu-%hu", ports[start], ports[end]);
  1035. start = end + 1;
  1036. }
  1037. }
  1038. /* Output the list of ports scanned to the top of machine parseable
  1039. logs (in a comment, unfortunately). The items in ports should be
  1040. in sequential order for space savings and easier to read output */
  1041. void output_ports_to_machine_parseable_output(struct scan_lists *ports) {
  1042. int tcpportsscanned = ports->tcp_count;
  1043. int udpportsscanned = ports->udp_count;
  1044. int sctpportsscanned = ports->sctp_count;
  1045. int protsscanned = ports->prot_count;
  1046. log_write(LOG_MACHINE, "# Ports scanned: TCP(%d;", tcpportsscanned);
  1047. if (tcpportsscanned)
  1048. output_rangelist_given_ports(LOG_MACHINE, ports->tcp_ports, tcpportsscanned);
  1049. log_write(LOG_MACHINE, ") UDP(%d;", udpportsscanned);
  1050. if (udpportsscanned)
  1051. output_rangelist_given_ports(LOG_MACHINE, ports->udp_ports, udpportsscanned);
  1052. log_write(LOG_MACHINE, ") SCTP(%d;", sctpportsscanned);
  1053. if (sctpportsscanned)
  1054. output_rangelist_given_ports(LOG_MACHINE, ports->sctp_ports, sctpportsscanned);
  1055. log_write(LOG_MACHINE, ") PROTOCOLS(%d;", protsscanned);
  1056. if (protsscanned)
  1057. output_rangelist_given_ports(LOG_MACHINE, ports->prots, protsscanned);
  1058. log_write(LOG_MACHINE, ")\n");
  1059. log_flush_all();
  1060. }
  1061. // A simple helper function for doscaninfo handles the c14n of o.scanflags
  1062. static void doscanflags() {
  1063. struct {
  1064. unsigned char flag;
  1065. const char *name;
  1066. } flags[] = {
  1067. { TH_FIN, "FIN" },
  1068. { TH_SYN, "SYN" },
  1069. { TH_RST, "RST" },
  1070. { TH_PUSH, "PSH" },
  1071. { TH_ACK, "ACK" },
  1072. { TH_URG, "URG" },
  1073. { TH_ECE, "ECE" },
  1074. { TH_CWR, "CWR" }
  1075. };
  1076. if (o.scanflags != -1) {
  1077. std::string flagstring;
  1078. for (unsigned int i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  1079. if (o.scanflags & flags[i].flag)
  1080. flagstring += flags[i].name;
  1081. }
  1082. xml_attribute("scanflags", "%s", flagstring.c_str());
  1083. }
  1084. }
  1085. /* Simple helper function for output_xml_scaninfo_records */
  1086. static void doscaninfo(const char *type, const char *proto,
  1087. unsigned short *ports, int numports) {
  1088. xml_open_start_tag("scaninfo");
  1089. xml_attribute("type", "%s", type);
  1090. if (strncmp(proto, "tcp", 3) == 0) {
  1091. doscanflags();
  1092. }
  1093. xml_attribute("protocol", "%s", proto);
  1094. xml_attribute("numservices", "%d", numports);
  1095. xml_write_raw(" services=\"");
  1096. output_rangelist_given_ports(LOG_XML, ports, numports);
  1097. xml_write_raw("\"");
  1098. xml_close_empty_tag();
  1099. xml_newline();
  1100. }
  1101. static std::string quote(const char *s) {
  1102. std::string result("");
  1103. const char *p;
  1104. bool space;
  1105. space = false;
  1106. for (p = s; *p != '\0'; p++) {
  1107. if (isspace(*p))
  1108. space = true;
  1109. if (*p == '"' || *p == '\\')
  1110. result += "\\";
  1111. result += *p;
  1112. }
  1113. if (space)
  1114. result = "\"" + result + "\"";
  1115. return result;
  1116. }
  1117. /* Return a std::string containing all n strings separated by whitespace, and
  1118. individually quoted if needed. */
  1119. std::string join_quoted(const char * const strings[], unsigned int n) {
  1120. std::string result("");
  1121. unsigned int i;
  1122. for (i = 0; i < n; i++) {
  1123. if (i > 0)
  1124. result += " ";
  1125. result += quote(strings[i]);
  1126. }
  1127. return result;
  1128. }
  1129. /* Similar to output_ports_to_machine_parseable_output, this function
  1130. outputs the XML version, which is scaninfo records of each scan
  1131. requested and the ports which it will scan for */
  1132. void output_xml_scaninfo_records(struct scan_lists *scanlist) {
  1133. if (o.synscan)
  1134. doscaninfo("syn", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  1135. if (o.ackscan)
  1136. doscaninfo("ack", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  1137. if (o.bouncescan)
  1138. doscaninfo("bounce", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  1139. if (o.connectscan)
  1140. doscaninfo("connect", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  1141. if (o.nullscan)
  1142. doscaninfo("null", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  1143. if (o.xmasscan)
  1144. doscaninfo("xmas", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  1145. if (o.windowscan)
  1146. doscaninfo("window", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  1147. if (o.maimonscan)
  1148. doscaninfo("maimon", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  1149. if (o.finscan)
  1150. doscaninfo("fin", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
  1151. if (o.udpscan)
  1152. doscaninfo("udp", "udp", scanlist->udp_ports, scanlist->udp_count);
  1153. if (o.sctpinitscan)
  1154. doscaninfo("sctpinit", "sctp", scanlist->sctp_ports, scanlist->sctp_count);
  1155. if (o.sctpcookieechoscan)
  1156. doscaninfo("sctpcookieecho", "sctp", scanlist->sctp_ports, scanlist->sctp_count);
  1157. if (o.ipprotscan)
  1158. doscaninfo("ipproto", "ip", scanlist->prots, scanlist->prot_count);
  1159. log_flush_all();
  1160. }
  1161. /* Prints the MAC address (if discovered) to XML output */
  1162. static void print_MAC_XML_Info(Target *currenths) {
  1163. const u8 *mac = currenths->MACAddress();
  1164. char macascii[32];
  1165. if (mac) {
  1166. const char *macvendor = MACPrefix2Corp(mac);
  1167. Snprintf(macascii, sizeof(macascii), "%02X:%02X:%02X:%02X:%02X:%02X",
  1168. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  1169. xml_open_start_tag("address");
  1170. xml_attribute("addr", "%s", macascii);
  1171. xml_attribute("addrtype", "mac");
  1172. if (macvendor)
  1173. xml_attribute("vendor", "%s", macvendor);
  1174. xml_close_empty_tag();
  1175. xml_newline();
  1176. }
  1177. }
  1178. /* Helper function to write the status and address/hostname info of a host
  1179. into the XML log */
  1180. static void write_xml_initial_hostinfo(Target *currenths,
  1181. const char *status) {
  1182. xml_open_start_tag("status");
  1183. xml_attribute("state", "%s", status);
  1184. xml_attribute("reason", "%s", reason_str(currenths->reason.reason_id, SINGULAR));
  1185. xml_attribute("reason_ttl", "%d", currenths->reason.ttl);
  1186. xml_close_empty_tag();
  1187. xml_newline();
  1188. xml_open_start_tag("address");
  1189. xml_attribute("addr", "%s", currenths->targetipstr());
  1190. xml_attribute("addrtype", "%s", (o.af() == AF_INET) ? "ipv4" : "ipv6");
  1191. xml_close_empty_tag();
  1192. xml_newline();
  1193. print_MAC_XML_Info(currenths);
  1194. /* Output a hostnames element whenever we have a name to write or the target
  1195. is up. */
  1196. if (currenths->TargetName() != NULL || *currenths->HostName() || strcmp(status, "up") == 0) {
  1197. xml_start_tag("hostnames");
  1198. xml_newline();
  1199. if (currenths->TargetName() != NULL) {
  1200. xml_open_start_tag("hostname");
  1201. xml_attribute("name", "%s", currenths->TargetName());
  1202. xml_attribute("type", "user");
  1203. xml_close_empty_tag();
  1204. xml_newline();
  1205. }
  1206. if (*currenths->HostName()) {
  1207. xml_open_start_tag("hostname");
  1208. xml_attribute("name", "%s", currenths->HostName());
  1209. xml_attribute("type", "PTR");
  1210. xml_close_empty_tag();
  1211. xml_newline();
  1212. }
  1213. xml_end_tag();
  1214. xml_newline();
  1215. }
  1216. log_flush_all();
  1217. }
  1218. static void write_xml_osclass(const OS_Classification *osclass, double accuracy) {
  1219. xml_open_start_tag("osclass");
  1220. xml_attribute("type", "%s", osclass->Device_Type);
  1221. xml_attribute("vendor", "%s", osclass->OS_Vendor);
  1222. xml_attribute("osfamily", "%s", osclass->OS_Family);
  1223. // Because the OS_Generation field is optional.
  1224. if (osclass->OS_Generation)
  1225. xml_attribute("osgen", "%s", osclass->OS_Generation);
  1226. xml_attribute("accuracy", "%d", (int) (accuracy * 100));
  1227. if (osclass->cpe.empty()) {
  1228. xml_close_empty_tag();
  1229. } else {
  1230. unsigned int i;
  1231. xml_close_start_tag();
  1232. for (i = 0; i < osclass->cpe.size(); i++) {
  1233. xml_start_tag("cpe");
  1234. xml_write_escaped("%s", osclass->cpe[i]);
  1235. xml_end_tag();
  1236. }
  1237. xml_end_tag();
  1238. }
  1239. xml_newline();
  1240. }
  1241. static void write_xml_osmatch(const FingerMatch *match, double accuracy) {
  1242. xml_open_start_tag("osmatch");
  1243. xml_attribute("name", "%s", match->OS_name);
  1244. xml_attribute("accuracy", "%d", (int) (accuracy * 100));
  1245. xml_attribute("line", "%d", match->line);
  1246. /* When o.deprecated_xml_osclass is true, we don't write osclass elements as
  1247. children of osmatch but rather as unrelated siblings. */
  1248. if (match->OS_class.empty() || o.deprecated_xml_osclass) {
  1249. xml_close_empty_tag();
  1250. } else {
  1251. unsigned int i;
  1252. xml_close_start_tag();
  1253. xml_newline();
  1254. for (i = 0; i < match->OS_class.size(); i++)
  1255. write_xml_osclass(&match->OS_class[i], accuracy);
  1256. xml_end_tag();
  1257. }
  1258. xml_newline();
  1259. }
  1260. /* Convert a number to a string, keeping the given number of significant digits.
  1261. The result is returned in a static buffer. */
  1262. static char *num_to_string_sigdigits(double d, int digits) {
  1263. static char buf[32];
  1264. int shift;
  1265. int n;
  1266. assert(digits >= 0);
  1267. if (d == 0.0) {
  1268. shift = -digits;
  1269. } else {
  1270. shift = (int) floor(log10(fabs(d))) - digits + 1;
  1271. d = floor(d / pow(10.0, shift) + 0.5);
  1272. d = d * pow(10.0, shift);
  1273. }
  1274. n = Snprintf(buf, sizeof(buf), "%.*f", MAX(0, -shift), d);
  1275. assert(n > 0 && n < (int) sizeof(buf));
  1276. return buf;
  1277. }
  1278. /* Writes a heading for a full scan report ("Nmap scan report for..."),
  1279. including host status and DNS records. */
  1280. void write_host_header(Target *currenths) {
  1281. if ((currenths->flags & HOST_UP) || o.verbose || o.resolve_all) {
  1282. if (currenths->flags & HOST_UP) {
  1283. log_write(LOG_PLAIN, "Nmap scan report for %s\n", currenths->NameIP());
  1284. } else if (currenths->flags & HOST_DOWN) {
  1285. log_write(LOG_PLAIN, "Nmap scan report for %s [host down", currenths->NameIP());
  1286. if (o.reason)
  1287. log_write(LOG_PLAIN, ", %s", target_reason_str(currenths));
  1288. log_write(LOG_PLAIN, "]\n");
  1289. }
  1290. }
  1291. write_host_status(currenths);
  1292. if (currenths->TargetName() != NULL
  1293. && currenths->resolved_addrs.size() > 1) {
  1294. const struct sockaddr_storage *hs_ss = currenths->TargetSockAddr();
  1295. log_write(LOG_PLAIN, "Other addresses for %s (not scanned):",
  1296. currenths->TargetName());
  1297. for (std::list<struct sockaddr_storage>::const_iterator it = currenths->resolved_addrs.begin(), end = currenths->resolved_addrs.end();
  1298. it != end; it++) {
  1299. struct sockaddr_storage ss = *it;
  1300. if (!sockaddr_storage_equal(&ss, hs_ss)) {
  1301. log_write(LOG_PLAIN, " %s", inet_ntop_ez(&ss, sizeof(ss)));
  1302. }
  1303. }
  1304. log_write(LOG_PLAIN, "\n");
  1305. }
  1306. /* Print reverse DNS if it differs. */
  1307. if (currenths->TargetName() != NULL
  1308. && currenths->HostName() != NULL && currenths->HostName()[0] != '\0'
  1309. && strcmp(currenths->TargetName(), currenths->HostName()) != 0) {
  1310. log_write(LOG_PLAIN, "rDNS record for %s: %s\n",
  1311. currenths->targetipstr(), currenths->HostName());
  1312. }
  1313. }
  1314. /* Writes host status info to the log streams (including STDOUT). An
  1315. example is "Host: 10.11.12.13 (foo.bar.example.com)\tStatus: Up\n" to
  1316. machine log. */
  1317. void write_host_status(Target *currenths) {
  1318. if (o.listscan) {
  1319. /* write "unknown" to machine and xml */
  1320. log_write(LOG_MACHINE, "Host: %s (%s)\tStatus: Unknown\n",
  1321. currenths->targetipstr(), currenths->HostName());
  1322. write_xml_initial_hostinfo(currenths, "unknown");
  1323. } else if (currenths->weird_responses) {
  1324. /* SMURF ADDRESS */
  1325. /* Write xml "down" or "up" based on flags and the smurf info */
  1326. write_xml_initial_hostinfo(currenths,
  1327. (currenths->
  1328. flags & HOST_UP) ? "up" : "down");
  1329. xml_open_start_tag("smurf");
  1330. xml_attribute("responses", "%d", currenths->weird_responses);
  1331. xml_close_empty_tag();
  1332. xml_newline();
  1333. log_write(LOG_MACHINE, "Host: %s (%s)\tStatus: Smurf (%d responses)\n",
  1334. currenths->targetipstr(), currenths->HostName(),
  1335. currenths->weird_responses);
  1336. if (o.noportscan) {
  1337. log_write(LOG_PLAIN, "Host seems to be a subnet broadcast address (returned %d extra pings).%s\n",
  1338. currenths->weird_responses,
  1339. (currenths->flags & HOST_UP) ? " Note -- the actual IP also responded." : "");
  1340. } else {
  1341. log_write(LOG_PLAIN, "Host seems to be a subnet broadcast address (returned %d extra pings). %s.\n",
  1342. currenths->weird_responses,
  1343. (currenths->flags & HOST_UP) ? " Still scanning it due to ping response from its own IP" : "Skipping host");
  1344. }
  1345. } else {
  1346. /* Ping scan / port scan. */
  1347. write_xml_initial_hostinfo(currenths, (currenths->flags & HOST_UP) ? "up" : "down");
  1348. if (currenths->flags & HOST_UP) {
  1349. log_write(LOG_PLAIN, "Host is up");
  1350. if (o.reason)
  1351. log_write(LOG_PLAIN, ", %s", target_reason_str(currenths));
  1352. if (o.reason && currenths->reason.ttl)
  1353. log_write(LOG_PLAIN, " ttl %d", currenths->reason.ttl);
  1354. if (currenths->to.srtt != -1)
  1355. log_write(LOG_PLAIN, " (%ss latency)",
  1356. num_to_string_sigdigits(currenths->to.srtt / 1000000.0, 2));
  1357. log_write(LOG_PLAIN, ".\n");
  1358. log_write(LOG_MACHINE, "Host: %s (%s)\tStatus: Up\n",
  1359. currenths->targetipstr(), currenths->HostName());
  1360. } else if (currenths->flags & HOST_DOWN) {
  1361. log_write(LOG_MACHINE, "Host: %s (%s)\tStatus: Down\n",
  1362. currenths->targetipstr(), currenths->HostName());
  1363. }
  1364. }
  1365. }
  1366. /* Returns -1 if adding the entry is not possible because it would
  1367. overflow. Otherwise it returns the new number of entries. Note
  1368. that only unique entries are added. Also note that *numentries is
  1369. incremented if the candidate is added. arrsize is the number of
  1370. char * members that fit into arr */
  1371. static int addtochararrayifnew(const char *arr[], int *numentries, int arrsize,
  1372. const char *candidate) {
  1373. int i;
  1374. // First lets see if the member already exists
  1375. for (i = 0; i < *numentries; i++) {
  1376. if (strcmp(arr[i], candidate) == 0)
  1377. return *numentries;
  1378. }
  1379. // Not already there... do we have room for a new one?
  1380. if (*numentries >= arrsize)
  1381. return -1;
  1382. // OK, not already there and we have room, so we'll add it.
  1383. arr[*numentries] = candidate;
  1384. (*numentries)++;
  1385. return *numentries;
  1386. }
  1387. /* guess is true if we should print guesses */
  1388. #define MAX_OS_CLASSMEMBERS 8
  1389. static void printosclassificationoutput(const struct
  1390. OS_Classification_Results *OSR,
  1391. bool guess) {
  1392. int classno, cpeno, familyno;
  1393. unsigned int i;
  1394. int overflow = 0; /* Whether we have too many devices to list */
  1395. const char *types[MAX_OS_CLASSMEMBERS];
  1396. const char *cpes[MAX_OS_CLASSMEMBERS];
  1397. char fullfamily[MAX_OS_CLASSMEMBERS][128]; // "[vendor] [os family]"
  1398. double familyaccuracy[MAX_OS_CLASSMEMBERS]; // highest accuracy for this fullfamily
  1399. char familygenerations[MAX_OS_CLASSMEMBERS][96]; // example: "4.X|5.X|6.X"
  1400. int numtypes = 0, numcpes = 0, numfamilies = 0;
  1401. char tmpbuf[1024];
  1402. for (i = 0; i < MAX_OS_CLASSMEMBERS; i++) {
  1403. familygenerations[i][0] = '\0';
  1404. familyaccuracy[i] = 0.0;
  1405. }
  1406. if (OSR->overall_results == OSSCAN_SUCCESS) {
  1407. if (o.deprecated_xml_osclass) {
  1408. for (classno = 0; classno < OSR->OSC_num_matches; classno++)
  1409. write_xml_osclass(OSR->OSC[classno], OSR->OSC_Accuracy[classno]);
  1410. }
  1411. // Now to create the fodder for normal output
  1412. for (classno = 0; classno < OSR->OSC_num_matches; classno++) {
  1413. /* We have processed enough if any of the following are true */
  1414. if ((!guess && classno >= OSR->OSC_num_perfect_matches) ||
  1415. OSR->OSC_Accuracy[classno] <= OSR->OSC_Accuracy[0] - 0.1 ||
  1416. (OSR->OSC_Accuracy[classno] < 1.0 && classno > 9))
  1417. break;
  1418. if (addtochararrayifnew(types, &numtypes, MAX_OS_CLASSMEMBERS,
  1419. OSR->OSC[classno]->Device_Type) == -1) {
  1420. overflow = 1;
  1421. }
  1422. for (i = 0; i < OSR->OSC[classno]->cpe.size(); i++) {
  1423. if (addtochararrayifnew(cpes, &numcpes, MAX_OS_CLASSMEMBERS,
  1424. OSR->OSC[classno]->cpe[i]) == -1) {
  1425. overflow = 1;
  1426. }
  1427. }
  1428. // If family and vendor names are the same, no point being redundant
  1429. if (strcmp(OSR->OSC[classno]->OS_Vendor, OSR->OSC[classno]->OS_Family) == 0)
  1430. Strncpy(tmpbuf, OSR->OSC[classno]->OS_Family, sizeof(tmpbuf));
  1431. else
  1432. Snprintf(tmpbuf, sizeof(tmpbuf), "%s %s", OSR->OSC[classno]->OS_Vendor, OSR->OSC[classno]->OS_Family);
  1433. // Let's see if it is already in the array
  1434. for (familyno = 0; familyno < numfamilies; familyno++) {
  1435. if (strcmp(fullfamily[familyno], tmpbuf) == 0) {
  1436. // got a match ... do we need to add the generation?
  1437. if (OSR->OSC[classno]->OS_Generation
  1438. && !strstr(familygenerations[familyno],
  1439. OSR->OSC[classno]->OS_Generation)) {
  1440. int flen = strlen(familygenerations[familyno]);
  1441. // We add it, preceded by | if something is already there
  1442. if (flen + 2 + strlen(OSR->OSC[classno]->OS_Generation) >=
  1443. sizeof(familygenerations[familyno]))
  1444. fatal("buffer 0verfl0w of familygenerations");
  1445. if (*familygenerations[familyno])
  1446. strcat(familygenerations[familyno], "|");
  1447. strncat(familygenerations[familyno],
  1448. OSR->OSC[classno]->OS_Generation,
  1449. sizeof(familygenerations[familyno]) - flen - 1);
  1450. }
  1451. break;
  1452. }
  1453. }
  1454. if (familyno == numfamilies) {
  1455. // Looks like the new family is not in the list yet. Do we have room to add it?
  1456. if (numfamilies >= MAX_OS_CLASSMEMBERS) {
  1457. overflow = 1;
  1458. break;
  1459. }
  1460. // Have space, time to add...
  1461. Strncpy(fullfamily[numfamilies], tmpbuf, 128);
  1462. if (OSR->OSC[classno]->OS_Generation)
  1463. Strncpy(familygenerations[numfamilies],
  1464. OSR->OSC[classno]->OS_Generation, 48);
  1465. familyaccuracy[numfamilies] = OSR->OSC_Accuracy[classno];
  1466. numfamilies++;
  1467. }
  1468. }
  1469. if (!overflow && numfamilies >= 1) {
  1470. log_write(LOG_PLAIN, "Device type: ");
  1471. for (classno = 0; classno < numtypes; classno++)
  1472. log_write(LOG_PLAIN, "%s%s", types[classno], (classno < numtypes - 1) ? "|" : "");
  1473. log_write(LOG_PLAIN, "\nRunning%s: ", OSR->OSC_num_perfect_matches == 0 ? " (JUST GUESSING)" : "");
  1474. for (familyno = 0; familyno < numfamilies; familyno++) {
  1475. if (familyno > 0)
  1476. log_write(LOG_PLAIN, ", ");
  1477. log_write(LOG_PLAIN, "%s", fullfamily[familyno]);
  1478. if (*familygenerations[familyno])
  1479. log_write(LOG_PLAIN, " %s", familygenerations[familyno]);
  1480. if (familyno >= OSR->OSC_num_perfect_matches)
  1481. log_write(LOG_PLAIN, " (%.f%%)",
  1482. floor(familyaccuracy[familyno] * 100));
  1483. }
  1484. log_write(LOG_PLAIN, "\n");
  1485. if (numcpes > 0) {
  1486. log_write(LOG_PLAIN, "OS CPE:");
  1487. for (cpeno = 0; cpeno < numcpes; cpeno++)
  1488. log_write(LOG_PLAIN, " %s", cpes[cpeno]);
  1489. log_write(LOG_PLAIN, "\n");
  1490. }
  1491. }
  1492. }
  1493. log_flush_all();
  1494. return;
  1495. }
  1496. /* Prints the MAC address if one was found for the target (generally
  1497. this means that the target is directly connected on an ethernet
  1498. network. This only prints to human output -- XML is handled by a
  1499. separate call ( print_MAC_XML_Info ) because it needs to be printed
  1500. in a certain place to conform to DTD. */
  1501. void printmacinfo(Target *currenths) {
  1502. const u8 *mac = currenths->MACAddress();
  1503. char macascii[32];
  1504. if (mac) {
  1505. const char *macvendor = MACPrefix2Corp(mac);
  1506. Snprintf(macascii, sizeof(macascii), "%02X:%02X:%02X:%02X:%02X:%02X",
  1507. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  1508. log_write(LOG_PLAIN, "MAC Address: %s (%s)\n", macascii,
  1509. macvendor ? macvendor : "Unknown");
  1510. }
  1511. }
  1512. /* A convenience wrapper around mergeFPs. */
  1513. const char *FingerPrintResultsIPv4::merge_fpr(const Target *currenths,
  1514. bool isGoodFP, bool wrapit) const {
  1515. return mergeFPs(this->FPs, this->numFPs, isGoodFP, currenths->TargetSockAddr(),
  1516. currenths->distance,
  1517. currenths->distance_calculation_method,
  1518. currenths->MACAddress(), this->osscan_opentcpport,
  1519. this->osscan_closedtcpport, this->osscan_closedudpport,
  1520. wrapit);
  1521. }
  1522. /* Run-length encode a string in chunks of two bytes. The output sequence
  1523. AA{n} means to repeat AA n times. The input must not contain '{' or '}'
  1524. characters. */
  1525. static std::string run_length_encode(const std::string &s) {
  1526. std::ostringstream result;
  1527. const char *p, *q;
  1528. unsigned int reps;
  1529. p = s.c_str();
  1530. while (*p != '\0' && *(p + 1) != '\0') {
  1531. for (q = p + 2; *q == *p && *(q + 1) == *(p + 1); q += 2)
  1532. ;
  1533. reps = (q - p) / 2;
  1534. if (reps < 3)
  1535. result << std::string(p, q);
  1536. else
  1537. result << std::string(p, 2) << "{" << reps << "}";
  1538. p = q;
  1539. }
  1540. if (*p != '\0')
  1541. result << std::string(p);
  1542. return result.str();
  1543. }
  1544. static std::string wrap(const std::string &s) {
  1545. const static char *prefix = "OS:";
  1546. std::string t, buf;
  1547. int i, len, prefixlen;
  1548. size_t p;
  1549. t = s;
  1550. /* Remove newlines. */
  1551. p = 0;
  1552. while ((p = t.find("\n", p)) != std::string::npos)
  1553. t.erase(p, 1);
  1554. len = t.size();
  1555. prefixlen = strlen(prefix);
  1556. assert(FP_RESULT_WRAP_LINE_LEN > prefixlen);
  1557. for (i = 0; i < len; i += FP_RESULT_WRAP_LINE_LEN - prefixlen) {
  1558. buf.append(prefix);
  1559. buf.append(t, i, FP_RESULT_WRAP_LINE_LEN - prefixlen);
  1560. buf.append("\n");
  1561. }
  1562. return buf;
  1563. }
  1564. static void scrub_packet(PacketElement *pe, unsigned char fill) {
  1565. unsigned char fillbuf[16];
  1566. memset(fillbuf, fill, sizeof(fillbuf));
  1567. for (; pe != NULL; pe = pe->getNextElement()) {
  1568. if (pe->protocol_id() == HEADER_TYPE_IPv6) {
  1569. IPv6Header *ipv6 = (IPv6Header *) pe;
  1570. ipv6->setSourceAddress(fillbuf);
  1571. ipv6->setDestinationAddress(fillbuf);
  1572. } else if (pe->protocol_id() == HEADER_TYPE_ICMPv6) {
  1573. ICMPv6Header *icmpv6 = (ICMPv6Header *) pe;
  1574. in6_addr *addr = (in6_addr *) fillbuf;
  1575. if (icmpv6->getType() == ICMPV6_NEIGHBOR_ADVERTISEMENT)
  1576. icmpv6->setTargetAddress(*addr);
  1577. }
  1578. }
  1579. }
  1580. static std::string get_scrubbed_buffer(const FPResponse *resp) {
  1581. std::ostringstream result;
  1582. PacketElement *scrub1, *scrub2;
  1583. u8 *buf1, *buf2;
  1584. int len1, len2;
  1585. unsigned int i;
  1586. scrub1 = PacketParser::split(resp->buf, resp->len);
  1587. assert(scrub1 != NULL);
  1588. scrub_packet(scrub1, 0x00);
  1589. scrub2 = PacketParser::split(resp->buf, resp->len);
  1590. assert(scrub2 != NULL);
  1591. scrub_packet(scrub2, 0xFF);
  1592. buf1 = scrub1->getBinaryBuffer(&len1);
  1593. buf2 = scrub2->getBinaryBuffer(&len2);
  1594. assert(resp->len == (unsigned int) len1);
  1595. assert(resp->len == (unsigned int) len2);
  1596. result.fill('0');
  1597. result << std::hex;
  1598. for (i = 0; i < resp->len; i++) {
  1599. if (resp->buf[i] == buf1[i] && resp->buf[i] == buf2[i]) {
  1600. result.width(2);
  1601. result << (unsigned int) resp->buf[i];
  1602. } else {
  1603. result << "XX";
  1604. }
  1605. }
  1606. free(buf1);
  1607. free(buf2);
  1608. PacketParser::freePacketChain(scrub1);
  1609. PacketParser::freePacketChain(scrub2);
  1610. return result.str();
  1611. }
  1612. const char *FingerPrintResultsIPv6::merge_fpr(const Target *currenths,
  1613. bool isGoodFP, bool wrapit) const {
  1614. static char str[10240];
  1615. const FingerPrintResultsIPv6 *FPR;
  1616. std::ostringstream result;
  1617. std::string output;
  1618. unsigned int i;
  1619. /* Write the SCAN line. */
  1620. WriteSInfo(str, sizeof(str), isGoodFP, "6", currenths->TargetSockAddr(),
  1621. currenths->distance, currenths->distance_calculation_method,
  1622. currenths->MACAddress(), this->osscan_opentcpport,
  1623. this->osscan_closedtcpport, this->osscan_closedudpport);
  1624. result << str << "\n";
  1625. FPR = (FingerPrintResultsIPv6 *) currenths->FPR;
  1626. assert(FPR->begin_time.tv_sec != 0);
  1627. for (i = 0; i < sizeof(FPR->fp_responses) / sizeof(FPR->fp_responses[0]); i++) {
  1628. const FPResponse *resp;
  1629. std::string scrubbed;
  1630. resp = this->fp_responses[i];
  1631. if (resp == NULL)
  1632. continue;
  1633. scrubbed = get_scrubbed_buffer(resp);
  1634. if (wrapit)
  1635. scrubbed = run_length_encode(scrubbed);
  1636. result << resp->probe_id << "(P=" << scrubbed;
  1637. assert(resp->senttime.tv_sec != 0);
  1638. result << "%ST=" << TIMEVAL_FSEC_SUBTRACT(resp->senttime, FPR->begin_time);
  1639. assert(resp->rcvdtime.tv_sec != 0);
  1640. result << "%RT=" << TIMEVAL_FSEC_SUBTRACT(resp->rcvdtime, FPR->begin_time);
  1641. result << ")\n";
  1642. }
  1643. result << "EXTRA(";
  1644. result << "FL=";
  1645. result.fill('0');
  1646. result << std::hex;
  1647. result.width(5);
  1648. result << FPR->flow_label;
  1649. result << ")\n";
  1650. output = result.str();
  1651. if (wrapit) {
  1652. output = wrap(output);
  1653. }
  1654. Strncpy(str, output.c_str(), sizeof(str));
  1655. return str;
  1656. }
  1657. static void write_merged_fpr(const FingerPrintResults *FPR,
  1658. const Target *currenths,
  1659. bool isGoodFP, bool wrapit) {
  1660. log_write(LOG_NORMAL | LOG_SKID_NOXLT | LOG_STDOUT,
  1661. "TCP/IP fingerprint:\n%s\n",
  1662. FPR->merge_fpr(currenths, isGoodFP, wrapit));
  1663. /* Added code here to print fingerprint to XML file any time it would be
  1664. printed to any other output format */
  1665. xml_open_start_tag("osfingerprint");
  1666. xml_attribute("fingerprint", "%s", FPR->merge_fpr(currenths, isGoodFP, wrapit));
  1667. xml_close_empty_tag();
  1668. xml_newline();
  1669. }
  1670. /* Prints the formatted OS Scan output to stdout, logfiles, etc (but only
  1671. if an OS Scan was performed).*/
  1672. void printosscanoutput(Target *currenths) {
  1673. int i;
  1674. char numlst[512]; /* For creating lists of numbers */
  1675. char *p; /* Used in manipulating numlst above */
  1676. FingerPrintResults *FPR;
  1677. int osscan_flag;
  1678. if (!(osscan_flag = currenths->osscanPerformed()))
  1679. return;
  1680. if (currenths->FPR == NULL)
  1681. return;
  1682. FPR = currenths->FPR;
  1683. xml_start_tag("os");
  1684. if (FPR->osscan_opentcpport > 0) {
  1685. xml_open_start_tag("portused");
  1686. xml_attribute("state", "open");
  1687. xml_attribute("proto", "tcp");
  1688. xml_attribute("portid", "%hu", FPR->osscan_opentcpport);
  1689. xml_close_empty_tag();
  1690. xml_newline();
  1691. }
  1692. if (FPR->osscan_closedtcpport > 0) {
  1693. xml_open_start_tag("portused");
  1694. xml_attribute("state", "closed");
  1695. xml_attribute("proto", "tcp");
  1696. xml_attribute("portid", "%hu", FPR->osscan_closedtcpport);
  1697. xml_close_empty_tag();
  1698. xml_newline();
  1699. }
  1700. if (FPR->osscan_closedudpport > 0) {
  1701. xml_open_start_tag("portused");
  1702. xml_attribute("state", "closed");
  1703. xml_attribute("proto", "udp");
  1704. xml_attribute("portid", "%hu", FPR->osscan_closedudpport);
  1705. xml_close_empty_tag();
  1706. xml_newline();
  1707. }
  1708. if (osscan_flag == OS_PERF_UNREL &&
  1709. !(FPR->overall_results == OSSCAN_TOOMANYMATCHES ||
  1710. (FPR->num_perfect_matches > 8 && !o.debugging)))
  1711. log_write(LOG_PLAIN, "Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port\n");
  1712. // If the FP can't be submitted anyway, might as well make a guess.
  1713. const char *reason = FPR->OmitSubmissionFP();
  1714. printosclassificationoutput(FPR->getOSClassification(), o.osscan_guess || reason);
  1715. if (FPR->overall_results == OSSCAN_SUCCESS &&
  1716. (FPR->num_perfect_matches <= 8 || o.debugging)) {
  1717. /* Success, not too many perfect matches. */
  1718. if (FPR->num_perfect_matches > 0) {
  1719. /* Some perfect matches. */
  1720. for (i = 0; i < FPR->num_perfect_matches; i++)
  1721. write_xml_osmatch(FPR->matches[i], FPR->accuracy[i]);
  1722. log_write(LOG_MACHINE, "\tOS: %s", FPR->matches[0]->OS_name);
  1723. for (i = 1; i < FPR->num_perfect_matches; i++)
  1724. log_write(LOG_MACHINE, "|%s", FPR->matches[i]->OS_name);
  1725. log_write(LOG_PLAIN, "OS details: %s", FPR->matches[0]->OS_name);
  1726. for (i = 1; i < FPR->num_perfect_matches; i++)
  1727. log_write(LOG_PLAIN, ", %s", FPR->matches[i]->OS_name);
  1728. log_write(LOG_PLAIN, "\n");
  1729. if (o.debugging || o.verbose > 1)
  1730. write_merged_fpr(FPR, currenths, reason == NULL, true);
  1731. } else {
  1732. /* No perfect matches. */
  1733. if ((o.verbose > 1 || o.debugging) && reason)
  1734. log_write(LOG_NORMAL | LOG_SKID_NOXLT | LOG_STDOUT,
  1735. "OS fingerprint not ideal because: %s\n", reason);
  1736. for (i = 0; i < 10 && i < FPR->num_matches && FPR->accuracy[i] > FPR->accuracy[0] - 0.10; i++)
  1737. write_xml_osmatch(FPR->matches[i], FPR->accuracy[i]);
  1738. if ((o.osscan_guess || reason) && FPR->num_matches > 0) {
  1739. /* Print the best guesses available */
  1740. log_write(LOG_PLAIN, "Aggressive OS guesses: %s (%.f%%)",
  1741. FPR->matches[0]->OS_name, floor(FPR->accuracy[0] * 100));
  1742. for (i = 1; i < 10 && FPR->num_matches > i && FPR->accuracy[i] > FPR->accuracy[0] - 0.10; i++)
  1743. log_write(LOG_PLAIN, ", %s (%.f%%)", FPR->matches[i]->OS_name, floor(FPR->accuracy[i] * 100));
  1744. log_write(LOG_PLAIN, "\n");
  1745. }
  1746. if (!reason) {
  1747. log_write(LOG_NORMAL | LOG_SKID_NOXLT | LOG_STDOUT,
  1748. "No exact OS matches for host (If you know what OS is running on it, see https://nmap.org/submit/ ).\n");
  1749. write_merged_fpr(FPR, currenths, true, true);
  1750. } else {
  1751. log_write(LOG_NORMAL | LOG_SKID_NOXLT | LOG_STDOUT,
  1752. "No exact OS matches for host (test conditions non-ideal).\n");
  1753. if (o.verbose > 1 || o.debugging)
  1754. write_merged_fpr(FPR, currenths, false, false);
  1755. }
  1756. }
  1757. } else if (FPR->overall_results == OSSCAN_NOMATCHES) {
  1758. /* No matches at all. */
  1759. if (!reason) {
  1760. log_write(LOG_NORMAL | LOG_SKID_NOXLT | LOG_STDOUT,
  1761. "No OS matches for host (If you know what OS is running on it, see https://nmap.org/submit/ ).\n");
  1762. write_merged_fpr(FPR, currenths, true, true);
  1763. } else {
  1764. log_write(LOG_NORMAL | LOG_SKID_NOXLT | LOG_STDOUT,
  1765. "OS fingerprint not ideal because: %s\n", reason);
  1766. log_write(LOG_NORMAL | LOG_SKID_NOXLT | LOG_STDOUT,
  1767. "No OS matches for host\n");
  1768. if (o.debugging || o.verbose > 1)
  1769. write_merged_fpr(FPR, currenths, false, false);
  1770. }
  1771. } else if (FPR->overall_results == OSSCAN_TOOMANYMATCHES
  1772. || (FPR->num_perfect_matches > 8 && !o.debugging)) {
  1773. /* Too many perfect matches. */
  1774. log_write(LOG_NORMAL | LOG_SKID_NOXLT | LOG_STDOUT,
  1775. "Too many fingerprints match this host to give specific OS details\n");
  1776. if (o.debugging || o.verbose > 1)
  1777. write_merged_fpr(FPR, currenths, false, false);
  1778. } else {
  1779. assert(0);
  1780. }
  1781. xml_end_tag(); /* os */
  1782. xml_newline();
  1783. if (currenths->seq.lastboot) {
  1784. char tmbuf[128];
  1785. struct timeval tv;
  1786. time_t lastboot;
  1787. lastboot = (time_t) currenths->seq.lastboot;
  1788. strncpy(tmbuf, ctime(&lastboot), sizeof(tmbuf));
  1789. chomp(tmbuf);
  1790. gettimeofday(&tv, NULL);
  1791. if (o.verbose)
  1792. log_write(LOG_PLAIN, "Uptime guess: %.3f days (since %s)\n",
  1793. (double) (tv.tv_sec - currenths->seq.lastboot) / 86400,
  1794. tmbuf);
  1795. xml_open_start_tag("uptime");
  1796. xml_attribute("seconds", "%li", tv.tv_sec - currenths->seq.lastboot);
  1797. xml_attribute("lastboot", "%s", tmbuf);
  1798. xml_close_empty_tag();
  1799. xml_newline();
  1800. }
  1801. if (currenths->distance != -1) {
  1802. log_write(LOG_PLAIN, "Network Distance: %d hop%s\n",
  1803. currenths->distance, (currenths->distance == 1) ? "" : "s");
  1804. xml_open_start_tag("distance");
  1805. xml_attribute("value", "%d", currenths->distance);
  1806. xml_close_empty_tag();
  1807. xml_newline();
  1808. }
  1809. if (currenths->seq.responses > 3) {
  1810. p = numlst;
  1811. for (i = 0; i < currenths->seq.responses; i++) {
  1812. if (p - numlst > (int) (sizeof(numlst) - 15))
  1813. fatal("STRANGE ERROR #3877 -- please report to fyodor@nmap.org\n");
  1814. if (p != numlst)
  1815. *p++ = ',';
  1816. sprintf(p, "%X", currenths->seq.seqs[i]);
  1817. while (*p)
  1818. p++;
  1819. }
  1820. xml_open_start_tag("tcpsequence");
  1821. xml_attribute("index", "%li", (long) currenths->seq.index);
  1822. xml_attribute("difficulty", "%s", seqidx2difficultystr(currenths->seq.index));
  1823. xml_attribute("values", "%s", numlst);
  1824. xml_close_empty_tag();
  1825. xml_newline();
  1826. if (o.verbose)
  1827. log_write(LOG_PLAIN, "TCP Sequence Prediction: Difficulty=%d (%s)\n", currenths->seq.index, seqidx2difficultystr(currenths->seq.index));
  1828. log_write(LOG_MACHINE, "\tSeq Index: %d", currenths->seq.index);
  1829. }
  1830. if (currenths->seq.responses > 2) {
  1831. p = numlst;
  1832. for (i = 0; i < currenths->seq.responses; i++) {
  1833. if (p - numlst > (int) (sizeof(numlst) - 15))
  1834. fatal("STRANGE ERROR #3876 -- please report to fyodor@nmap.org\n");
  1835. if (p != numlst)
  1836. *p++ = ',';
  1837. sprintf(p, "%hX", currenths->seq.ipids[i]);
  1838. while (*p)
  1839. p++;
  1840. }
  1841. xml_open_start_tag("ipidsequence");
  1842. xml_attribute("class", "%s", ipidclass2ascii(currenths->seq.ipid_seqclass));
  1843. xml_attribute("values", "%s", numlst);
  1844. xml_close_empty_tag();
  1845. xml_newline();
  1846. if (o.verbose)
  1847. log_write(LOG_PLAIN, "IP ID Sequence Generation: %s\n",
  1848. ipidclass2ascii(currenths->seq.ipid_seqclass));
  1849. log_write(LOG_MACHINE, "\tIP ID Seq: %s",
  1850. ipidclass2ascii(currenths->seq.ipid_seqclass));
  1851. p = numlst;
  1852. for (i = 0; i < currenths->seq.responses; i++) {
  1853. if (p - numlst > (int) (sizeof(numlst) - 15))
  1854. fatal("STRANGE ERROR #3878 -- please report to fyodor@nmap.org\n");
  1855. if (p != numlst)
  1856. *p++ = ',';
  1857. sprintf(p, "%X", currenths->seq.timestamps[i]);
  1858. while (*p)
  1859. p++;
  1860. }
  1861. xml_open_start_tag("tcptssequence");
  1862. xml_attribute("class", "%s", tsseqclass2ascii(currenths->seq.ts_seqclass));
  1863. if (currenths->seq.ts_seqclass != TS_SEQ_UNSUPPORTED) {
  1864. xml_attribute("values", "%s", numlst);
  1865. }
  1866. xml_close_empty_tag();
  1867. xml_newline();
  1868. }
  1869. log_flush_all();
  1870. }
  1871. /* An auxillary function for printserviceinfooutput(). Returns
  1872. non-zero if a and b are considered the same hostnames. */
  1873. static int hostcmp(const char *a, const char *b) {
  1874. return strcasecmp(a, b) == 0;
  1875. }
  1876. /* Prints the alternate hostname/OS/device information we got from the service
  1877. scan (if it was performed) */
  1878. void printserviceinfooutput(Target *currenths) {
  1879. Port *p = NULL;
  1880. Port port;
  1881. struct serviceDeductions sd;
  1882. int i, numhostnames = 0, numostypes = 0, numdevicetypes = 0, numcpes = 0;
  1883. char hostname_tbl[MAX_SERVICE_INFO_FIELDS][MAXHOSTNAMELEN];
  1884. char ostype_tbl[MAX_SERVICE_INFO_FIELDS][64];
  1885. char devicetype_tbl[MAX_SERVICE_INFO_FIELDS][64];
  1886. char cpe_tbl[MAX_SERVICE_INFO_FIELDS][80];
  1887. const char *delim;
  1888. for (i = 0; i < MAX_SERVICE_INFO_FIELDS; i++)
  1889. hostname_tbl[i][0] = ostype_tbl[i][0] = devicetype_tbl[i][0] = cpe_tbl[i][0] = '\0';
  1890. while ((p = currenths->ports.nextPort(p, &port, TCPANDUDPANDSCTP, PORT_OPEN))) {
  1891. std::vector<char *>::iterator it;
  1892. // The following 2 lines (from portlist.h) tell us that we don't need to
  1893. // worry about free()ing anything in the serviceDeductions struct. pass in
  1894. // an allocated struct serviceDeductions (don't worry about initializing, and
  1895. // you don't have to free any internal ptrs.
  1896. currenths->ports.getServiceDeductions(p->portno, p->proto, &sd);
  1897. if (sd.hostname && !hostcmp(currenths->HostName(), sd.hostname)) {
  1898. for (i = 0; i < MAX_SERVICE_INFO_FIELDS; i++) {
  1899. if (hostname_tbl[i][0] && hostcmp(&hostname_tbl[i][0], sd.hostname))
  1900. break;
  1901. if (!hostname_tbl[i][0]) {
  1902. numhostnames++;
  1903. strncpy(&hostname_tbl[i][0], sd.hostname, sizeof(hostname_tbl[i]));
  1904. break;
  1905. }
  1906. }
  1907. }
  1908. if (sd.ostype) {
  1909. for (i = 0; i < MAX_SERVICE_INFO_FIELDS; i++) {
  1910. if (ostype_tbl[i][0] && !strcmp(&ostype_tbl[i][0], sd.ostype))
  1911. break;
  1912. if (!ostype_tbl[i][0]) {
  1913. numostypes++;
  1914. strncpy(&ostype_tbl[i][0], sd.ostype, sizeof(ostype_tbl[i]));
  1915. break;
  1916. }
  1917. }
  1918. }
  1919. if (sd.devicetype) {
  1920. for (i = 0; i < MAX_SERVICE_INFO_FIELDS; i++) {
  1921. if (devicetype_tbl[i][0] && !strcmp(&devicetype_tbl[i][0], sd.devicetype))
  1922. break;
  1923. if (!devicetype_tbl[i][0]) {
  1924. numdevicetypes++;
  1925. strncpy(&devicetype_tbl[i][0], sd.devicetype, sizeof(devicetype_tbl[i]));
  1926. break;
  1927. }
  1928. }
  1929. }
  1930. for (it = sd.cpe.begin(); it != sd.cpe.end(); it++) {
  1931. for (i = 0; i < MAX_SERVICE_INFO_FIELDS; i++) {
  1932. if (cpe_tbl[i][0] && !strcmp(&cpe_tbl[i][0], *it))
  1933. break;
  1934. /* Applications (CPE part "a") aren't shown in this summary list in
  1935. normal output. "a" classifications belong to an individual port, not
  1936. the entire host, unlike "h" (hardware) and "o" (operating system).
  1937. There isn't a good place to put the "a" classifications, so they are
  1938. written to XML only. */
  1939. if (cpe_get_part(*it) == 'a')
  1940. break;
  1941. if (!cpe_tbl[i][0]) {
  1942. numcpes++;
  1943. strncpy(&cpe_tbl[i][0], *it, sizeof(cpe_tbl[i]));
  1944. break;
  1945. }
  1946. }
  1947. }
  1948. }
  1949. if (!numhostnames && !numostypes && !numdevicetypes && !numcpes)
  1950. return;
  1951. log_write(LOG_PLAIN, "Service Info:");
  1952. delim = " ";
  1953. if (numhostnames) {
  1954. log_write(LOG_PLAIN, "%sHost%s: %s", delim, numhostnames == 1 ? "" : "s", &hostname_tbl[0][0]);
  1955. for (i = 1; i < MAX_SERVICE_INFO_FIELDS; i++) {
  1956. if (hostname_tbl[i][0])
  1957. log_write(LOG_PLAIN, ", %s", &hostname_tbl[i][0]);
  1958. }
  1959. delim = "; ";
  1960. }
  1961. if (numostypes) {
  1962. log_write(LOG_PLAIN, "%sOS%s: %s", delim, numostypes == 1 ? "" : "s",
  1963. &ostype_tbl[0][0]);
  1964. for (i = 1; i < MAX_SERVICE_INFO_FIELDS; i++) {
  1965. if (ostype_tbl[i][0])
  1966. log_write(LOG_PLAIN, ", %s", &ostype_tbl[i][0]);
  1967. }
  1968. delim = "; ";
  1969. }
  1970. if (numdevicetypes) {
  1971. log_write(LOG_PLAIN, "%sDevice%s: %s", delim,
  1972. numdevicetypes == 1 ? "" : "s", &devicetype_tbl[0][0]);
  1973. for (i = 1; i < MAX_SERVICE_INFO_FIELDS; i++) {
  1974. if (devicetype_tbl[i][0])
  1975. log_write(LOG_PLAIN, ", %s", &devicetype_tbl[i][0]);
  1976. }
  1977. delim = "; ";
  1978. }
  1979. if (numcpes > 0) {
  1980. log_write(LOG_PLAIN, "%sCPE: %s", delim, &cpe_tbl[0][0]);
  1981. for (i = 1; i < MAX_SERVICE_INFO_FIELDS; i++) {
  1982. if (cpe_tbl[i][0])
  1983. log_write(LOG_PLAIN, ", %s", &cpe_tbl[i][0]);
  1984. }
  1985. delim = "; ";
  1986. }
  1987. log_write(LOG_PLAIN, "\n");
  1988. log_flush_all();
  1989. }
  1990. #ifndef NOLUA
  1991. void printscriptresults(ScriptResults *scriptResults, stype scantype) {
  1992. ScriptResults::iterator iter;
  1993. char *script_output;
  1994. if (scriptResults->size() > 0) {
  1995. scriptResults->sort(scriptid_lessthan);
  1996. if (scantype == SCRIPT_PRE_SCAN) {
  1997. xml_start_tag("prescript");
  1998. log_write(LOG_PLAIN, "Pre-scan script results:\n");
  1999. } else {
  2000. xml_start_tag("postscript");
  2001. log_write(LOG_PLAIN, "Post-scan script results:\n");
  2002. }
  2003. for (iter = scriptResults->begin(); iter != scriptResults->end(); iter++) {
  2004. iter->write_xml();
  2005. script_output = formatScriptOutput((*iter));
  2006. if (script_output != NULL) {
  2007. log_write(LOG_PLAIN, "%s\n", script_output);
  2008. free(script_output);
  2009. }
  2010. }
  2011. xml_end_tag();
  2012. }
  2013. }
  2014. void printhostscriptresults(Target *currenths) {
  2015. ScriptResults::iterator iter;
  2016. char *script_output;
  2017. if (currenths->scriptResults.size() > 0) {
  2018. currenths->scriptResults.sort(scriptid_lessthan);
  2019. xml_start_tag("hostscript");
  2020. log_write(LOG_PLAIN, "\nHost script results:\n");
  2021. for (iter = currenths->scriptResults.begin();
  2022. iter != currenths->scriptResults.end();
  2023. iter++) {
  2024. iter->write_xml();
  2025. script_output = formatScriptOutput((*iter));
  2026. if (script_output != NULL) {
  2027. log_write(LOG_PLAIN, "%s\n", script_output);
  2028. free(script_output);
  2029. }
  2030. }
  2031. xml_end_tag();
  2032. }
  2033. }
  2034. #endif
  2035. /* Print a table with traceroute hops. */
  2036. static void printtraceroute_normal(Target *currenths) {
  2037. static const int HOP_COL = 0, RTT_COL = 1, HOST_COL = 2;
  2038. NmapOutputTable Tbl(currenths->traceroute_hops.size() + 1, 3);
  2039. struct probespec probe;
  2040. std::list<TracerouteHop>::iterator it;
  2041. int row;
  2042. /* No trace, must be localhost. */
  2043. if (currenths->traceroute_hops.size() == 0)
  2044. return;
  2045. /* Print header. */
  2046. log_write(LOG_PLAIN, "\n");
  2047. probe = currenths->traceroute_probespec;
  2048. if (probe.type == PS_TCP) {
  2049. log_write(LOG_PLAIN, "TRACEROUTE (using port %d/%s)\n",
  2050. probe.pd.tcp.dport, proto2ascii_lowercase(probe.proto));
  2051. } else if (probe.type == PS_UDP) {
  2052. log_write(LOG_PLAIN, "TRACEROUTE (using port %d/%s)\n",
  2053. probe.pd.udp.dport, proto2ascii_lowercase(probe.proto));
  2054. } else if (probe.type == PS_SCTP) {
  2055. log_write(LOG_PLAIN, "TRACEROUTE (using port %d/%s)\n",
  2056. probe.pd.sctp.dport, proto2ascii_lowercase(probe.proto));
  2057. } else if (probe.type == PS_ICMP || probe.type == PS_ICMPV6 || probe.type == PS_PROTO) {
  2058. struct protoent *proto = nmap_getprotbynum(probe.proto);
  2059. log_write(LOG_PLAIN, "TRACEROUTE (using proto %d/%s)\n",
  2060. probe.proto, proto ? proto->p_name : "unknown");
  2061. } else if (probe.type == PS_NONE) {
  2062. /* "Traces" of directly connected targets don't send any packets. */
  2063. log_write(LOG_PLAIN, "TRACEROUTE\n");
  2064. } else {
  2065. fatal("Unknown probe type %d.", probe.type);
  2066. }
  2067. row = 0;
  2068. Tbl.addItem(row, HOP_COL, false, "HOP");
  2069. Tbl.addItem(row, RTT_COL, false, "RTT");
  2070. Tbl.addItem(row, HOST_COL, false, "ADDRESS");
  2071. row++;
  2072. it = currenths->traceroute_hops.begin();
  2073. if (!o.debugging) {
  2074. /* Consolidate shared hops. */
  2075. TracerouteHop *shared_hop = NULL;
  2076. struct sockaddr_storage addr;
  2077. size_t sslen;
  2078. sslen = sizeof(addr);
  2079. currenths->TargetSockAddr(&addr, &sslen);
  2080. while (it != currenths->traceroute_hops.end()
  2081. && !sockaddr_storage_equal(&it->tag, &addr)) {
  2082. shared_hop = &*it;
  2083. it++;
  2084. }
  2085. if (shared_hop != NULL) {
  2086. Tbl.addItem(row, HOP_COL, false, "-");
  2087. if (shared_hop->ttl == 1) {
  2088. Tbl.addItemFormatted(row, RTT_COL, true,
  2089. "Hop 1 is the same as for %s",
  2090. inet_ntop_ez(&shared_hop->tag, sizeof(shared_hop->tag)));
  2091. } else if (shared_hop->ttl > 1) {
  2092. Tbl.addItemFormatted(row, RTT_COL, true,
  2093. "Hops 1-%d are the same as for %s", shared_hop->ttl,
  2094. inet_ntop_ez(&shared_hop->tag, sizeof(shared_hop->tag)));
  2095. }
  2096. row++;
  2097. }
  2098. }
  2099. while (it != currenths->traceroute_hops.end()) {
  2100. Tbl.addItemFormatted(row, HOP_COL, false, "%d", it->ttl);
  2101. if (it->timedout) {
  2102. if (o.debugging) {
  2103. Tbl.addItem(row, RTT_COL, false, "...");
  2104. it++;
  2105. } else {
  2106. /* The beginning and end of timeout consolidation. */
  2107. int begin_ttl, end_ttl;
  2108. begin_ttl = end_ttl = it->ttl;
  2109. for (; it != currenths->traceroute_hops.end() && it->timedout; it++)
  2110. end_ttl = it->ttl;
  2111. if (begin_ttl == end_ttl)
  2112. Tbl.addItem(row, RTT_COL, false, "...");
  2113. else
  2114. Tbl.addItemFormatted(row, RTT_COL, false, "... %d", end_ttl);
  2115. }
  2116. row++;
  2117. } else {
  2118. /* Normal hop output. */
  2119. char namebuf[256];
  2120. it->display_name(namebuf, sizeof(namebuf));
  2121. if (it->rtt < 0)
  2122. Tbl.addItem(row, RTT_COL, false, "--");
  2123. else
  2124. Tbl.addItemFormatted(row, RTT_COL, false, "%.2f ms", it->rtt);
  2125. Tbl.addItemFormatted(row, HOST_COL, false, "%s", namebuf);
  2126. row++;
  2127. it++;
  2128. }
  2129. }
  2130. log_write(LOG_PLAIN, "%s", Tbl.printableTable(NULL));
  2131. log_flush(LOG_PLAIN);
  2132. }
  2133. static void printtraceroute_xml(Target *currenths) {
  2134. struct probespec probe;
  2135. std::list<TracerouteHop>::iterator it;
  2136. /* No trace, must be localhost. */
  2137. if (currenths->traceroute_hops.size() == 0)
  2138. return;
  2139. /* XML traceroute header */
  2140. xml_open_start_tag("trace");
  2141. probe = currenths->traceroute_probespec;
  2142. if (probe.type == PS_TCP) {
  2143. xml_attribute("port", "%d", probe.pd.tcp.dport);
  2144. xml_attribute("proto", "%s", proto2ascii_lowercase(probe.proto));
  2145. } else if (probe.type == PS_UDP) {
  2146. xml_attribute("port", "%d", probe.pd.udp.dport);
  2147. xml_attribute("proto", "%s", proto2ascii_lowercase(probe.proto));
  2148. } else if (probe.type == PS_SCTP) {
  2149. xml_attribute("port", "%d", probe.pd.sctp.dport);
  2150. xml_attribute("proto", "%s", proto2ascii_lowercase(probe.proto));
  2151. } else if (probe.type == PS_ICMP || probe.type == PS_PROTO) {
  2152. struct protoent *proto = nmap_getprotbynum(probe.proto);
  2153. if (proto == NULL)
  2154. xml_attribute("proto", "%d", probe.proto);
  2155. else
  2156. xml_attribute("proto", "%s", proto->p_name);
  2157. }
  2158. xml_close_start_tag();
  2159. xml_newline();
  2160. for (it = currenths->traceroute_hops.begin();
  2161. it != currenths->traceroute_hops.end();
  2162. it++) {
  2163. if (it->timedout)
  2164. continue;
  2165. xml_open_start_tag("hop");
  2166. xml_attribute("ttl", "%d", it->ttl);
  2167. xml_attribute("ipaddr", "%s", inet_ntop_ez(&it->addr, sizeof(it->addr)));
  2168. if (it->rtt < 0)
  2169. xml_attribute("rtt", "--");
  2170. else
  2171. xml_attribute("rtt", "%.2f", it->rtt);
  2172. if (!it->name.empty())
  2173. xml_attribute("host", "%s", it->name.c_str());
  2174. xml_close_empty_tag();
  2175. xml_newline();
  2176. }
  2177. /* traceroute XML footer */
  2178. xml_end_tag();
  2179. xml_newline();
  2180. log_flush(LOG_XML);
  2181. }
  2182. void printtraceroute(Target *currenths) {
  2183. printtraceroute_normal(currenths);
  2184. printtraceroute_xml(currenths);
  2185. }
  2186. void printtimes(Target *currenths) {
  2187. if (currenths->to.srtt != -1 || currenths->to.rttvar != -1) {
  2188. if (o.debugging) {
  2189. log_write(LOG_STDOUT, "Final times for host: srtt: %d rttvar: %d to: %d\n",
  2190. currenths->to.srtt, currenths->to.rttvar, currenths->to.timeout);
  2191. }
  2192. xml_open_start_tag("times");
  2193. xml_attribute("srtt", "%d", currenths->to.srtt);
  2194. xml_attribute("rttvar", "%d", currenths->to.rttvar);
  2195. xml_attribute("to", "%d", currenths->to.timeout);
  2196. xml_close_empty_tag();
  2197. xml_newline();
  2198. }
  2199. }
  2200. /* Prints a status message while the program is running */
  2201. void printStatusMessage() {
  2202. // Pre-computations
  2203. struct timeval tv;
  2204. gettimeofday(&tv, NULL);
  2205. int time = (int) (o.TimeSinceStart(&tv));
  2206. log_write(LOG_STDOUT, "Stats: %d:%02d:%02d elapsed; %d hosts completed (%d up), %d undergoing %s\n",
  2207. time / 60 / 60, time / 60 % 60, time % 60, o.numhosts_scanned,
  2208. o.numhosts_up, o.numhosts_scanning,
  2209. scantype2str(o.current_scantype));
  2210. }
  2211. /* Prints the beginning of a "finished" start tag, with time, timestr, and
  2212. elapsed attributes. Leaves the start tag open so you can add more attributes.
  2213. You have to close the tag with xml_close_empty_tag. */
  2214. void print_xml_finished_open(time_t timep, const struct timeval *tv) {
  2215. char mytime[128];
  2216. Strncpy(mytime, ctime(&timep), sizeof(mytime));
  2217. chomp(mytime);
  2218. xml_open_start_tag("finished");
  2219. xml_attribute("time", "%lu", (unsigned long) timep);
  2220. xml_attribute("timestr", "%s", mytime);
  2221. xml_attribute("elapsed", "%.2f", o.TimeSinceStart(tv));
  2222. xml_attribute("summary",
  2223. "Nmap done at %s; %d %s (%d %s up) scanned in %.2f seconds",
  2224. mytime, o.numhosts_scanned,
  2225. (o.numhosts_scanned == 1) ? "IP address" : "IP addresses",
  2226. o.numhosts_up, (o.numhosts_up == 1) ? "host" : "hosts",
  2227. o.TimeSinceStart(tv));
  2228. }
  2229. void print_xml_hosts() {
  2230. xml_open_start_tag("hosts");
  2231. xml_attribute("up", "%d", o.numhosts_up);
  2232. xml_attribute("down", "%d", o.numhosts_scanned - o.numhosts_up);
  2233. xml_attribute("total", "%d", o.numhosts_scanned);
  2234. xml_close_empty_tag();
  2235. }
  2236. /* Prints the statistics and other information that goes at the very end
  2237. of an Nmap run */
  2238. void printfinaloutput() {
  2239. time_t timep;
  2240. char mytime[128];
  2241. struct timeval tv;
  2242. char statbuf[128];
  2243. gettimeofday(&tv, NULL);
  2244. timep = time(NULL);
  2245. if (o.numhosts_scanned == 0
  2246. #ifndef NOLUA
  2247. && o.scriptupdatedb == 0
  2248. #endif
  2249. )
  2250. error("WARNING: No targets were specified, so 0 hosts scanned.");
  2251. if (o.numhosts_scanned == 1 && o.numhosts_up == 0 && !o.listscan &&
  2252. o.pingtype != PINGTYPE_NONE)
  2253. log_write(LOG_STDOUT, "Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn\n");
  2254. else if (o.numhosts_up > 0) {
  2255. if (o.osscan && o.servicescan)
  2256. log_write(LOG_PLAIN, "OS and Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .\n");
  2257. else if (o.osscan)
  2258. log_write(LOG_PLAIN, "OS detection performed. Please report any incorrect results at https://nmap.org/submit/ .\n");
  2259. else if (o.servicescan)
  2260. log_write(LOG_PLAIN, "Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .\n");
  2261. }
  2262. log_write(LOG_STDOUT | LOG_SKID,
  2263. "Nmap done: %d %s (%d %s up) scanned in %.2f seconds\n",
  2264. o.numhosts_scanned,
  2265. (o.numhosts_scanned == 1) ? "IP address" : "IP addresses",
  2266. o.numhosts_up, (o.numhosts_up == 1) ? "host" : "hosts",
  2267. o.TimeSinceStart(&tv));
  2268. if (o.verbose && o.isr00t && o.RawScan())
  2269. log_write(LOG_STDOUT | LOG_SKID, " %s\n",
  2270. getFinalPacketStats(statbuf, sizeof(statbuf)));
  2271. Strncpy(mytime, ctime(&timep), sizeof(mytime));
  2272. chomp(mytime);
  2273. xml_start_tag("runstats");
  2274. print_xml_finished_open(timep, &tv);
  2275. xml_attribute("exit", "success");
  2276. xml_close_empty_tag();
  2277. print_xml_hosts();
  2278. xml_newline();
  2279. xml_end_tag();
  2280. xml_newline();
  2281. log_write(LOG_NORMAL | LOG_MACHINE,
  2282. "# Nmap done at %s -- %d %s (%d %s up) scanned in %.2f seconds\n",
  2283. mytime, o.numhosts_scanned,
  2284. (o.numhosts_scanned == 1) ? "IP address" : "IP addresses",
  2285. o.numhosts_up, (o.numhosts_up == 1) ? "host" : "hosts",
  2286. o.TimeSinceStart(&tv));
  2287. xml_end_tag(); /* nmaprun */
  2288. xml_newline();
  2289. log_flush_all();
  2290. }
  2291. /* A record consisting of a data file name ("nmap-services", "nmap-os-db",
  2292. etc.), and the directory and file in which is was found. This is a
  2293. broken-down version of what is stored in o.loaded_data_files. It is used in
  2294. printdatafilepaths. */
  2295. struct data_file_record {
  2296. std::string data_file;
  2297. std::string dir;
  2298. std::string file;
  2299. /* Compares this record to another. First compare the directory names, then
  2300. compare the file names. */
  2301. bool operator<(const struct data_file_record &other) const {
  2302. int cmp;
  2303. cmp = dir.compare(other.dir);
  2304. if (cmp == 0)
  2305. cmp = file.compare(other.file);
  2306. return cmp < 0;
  2307. }
  2308. };
  2309. /* Prints the names of data files that were loaded and the paths at which they
  2310. were found. */
  2311. void printdatafilepaths() {
  2312. std::list<struct data_file_record> df;
  2313. std::list<struct data_file_record>::iterator iter;
  2314. std::map<std::string, std::string>::iterator map_iter;
  2315. std::string dir;
  2316. unsigned int num_dirs;
  2317. /* Copy the elements of o.loaded_data_files (each a (data file, path) pair) to
  2318. a list of data_file_records to make them easier to manipulate. */
  2319. for (map_iter = o.loaded_data_files.begin();
  2320. map_iter != o.loaded_data_files.end(); map_iter++) {
  2321. struct data_file_record r;
  2322. char *s;
  2323. r.data_file = map_iter->first;
  2324. s = path_get_dirname(map_iter->second.c_str());
  2325. if (s == NULL)
  2326. fatal("%s: failed to allocate temporary memory", __func__);
  2327. r.dir = std::string(s);
  2328. free(s);
  2329. s = path_get_basename(map_iter->second.c_str());
  2330. if (s == NULL)
  2331. fatal("%s: failed to allocate temporary memory", __func__);
  2332. r.file = std::string(s);
  2333. free(s);
  2334. df.push_back(r);
  2335. }
  2336. /* Sort the list, first by directory name, then by file name. This ensures
  2337. that records with the same directory name are contiguous. */
  2338. df.sort();
  2339. /* Count the number of distinct directories. Normally we print something only
  2340. if files came from more than one directory. */
  2341. if (df.empty()) {
  2342. num_dirs = 0;
  2343. } else {
  2344. num_dirs = 1;
  2345. iter = df.begin();
  2346. dir = iter->dir;
  2347. for (iter++; iter != df.end(); iter++) {
  2348. if (iter->dir != dir) {
  2349. num_dirs++;
  2350. dir = iter->dir;
  2351. }
  2352. }
  2353. }
  2354. /* Decide what to print out based on the number of distinct directories and
  2355. the verbosity and debugging levels. */
  2356. if (num_dirs == 0) {
  2357. /* If no files were read, print a message only in debugging mode. */
  2358. if (o.debugging > 0)
  2359. log_write(LOG_PLAIN, "No data files read.\n");
  2360. } else if (num_dirs == 1 && o.verbose && !o.debugging) {
  2361. /* If all the files were from the same directory and we're in verbose mode,
  2362. print a brief message unless we are also in debugging mode. */
  2363. log_write(LOG_PLAIN, "Read data files from: %s\n", dir.c_str());
  2364. } else if ((num_dirs == 1 && o.debugging) || num_dirs > 1) {
  2365. /* If files were read from more than one directory, or if they were read
  2366. from one directory and we are in debugging mode, display all the files
  2367. grouped by directory. */
  2368. iter = df.begin();
  2369. while (iter != df.end()) {
  2370. dir = iter->dir;
  2371. /* Write the directory name. */
  2372. log_write(LOG_PLAIN, "Read from %s:", dir.c_str());
  2373. /* Write files in that directory on the same line. */
  2374. while (iter != df.end() && iter->dir == dir) {
  2375. log_write(LOG_PLAIN, " %s", iter->file.c_str());
  2376. iter++;
  2377. }
  2378. log_write(LOG_PLAIN, ".\n");
  2379. }
  2380. }
  2381. }
  2382. static inline const char *nslog2str(nsock_loglevel_t loglevel) {
  2383. switch(loglevel) {
  2384. case NSOCK_LOG_DBG_ALL:
  2385. return "DEBUG FULL";
  2386. case NSOCK_LOG_DBG:
  2387. return "DEBUG";
  2388. case NSOCK_LOG_INFO:
  2389. return "INFO";
  2390. case NSOCK_LOG_ERROR:
  2391. return "ERROR";
  2392. default:
  2393. return "???";
  2394. };
  2395. }
  2396. void nmap_adjust_loglevel(bool trace) {
  2397. nsock_loglevel_t nsock_loglevel;
  2398. if (o.debugging >= 7)
  2399. nsock_loglevel = NSOCK_LOG_DBG_ALL;
  2400. else if (o.debugging >= 4)
  2401. nsock_loglevel = NSOCK_LOG_DBG;
  2402. else if (trace || o.debugging >= 2)
  2403. nsock_loglevel = NSOCK_LOG_INFO;
  2404. else
  2405. nsock_loglevel = NSOCK_LOG_ERROR;
  2406. nsock_set_loglevel(nsock_loglevel);
  2407. }
  2408. void nmap_nsock_stderr_logger(const struct nsock_log_rec *rec) {
  2409. int elapsed_time;
  2410. elapsed_time = TIMEVAL_MSEC_SUBTRACT(rec->time, *(o.getStartTime()));
  2411. log_write(LOG_STDERR, "NSOCK %s [%.4fs] %s(): %s\n", nslog2str(rec->level),
  2412. elapsed_time/1000.0, rec->func, rec->msg);
  2413. }