PageRenderTime 60ms CodeModel.GetById 19ms 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

Large files files are truncated, but you can click here to view the full file

  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

Large files files are truncated, but you can click here to view the full file