You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1699 lines
67 KiB

  1. From: Bryan Quigley <bryan.quigley@canonical.com>
  2. Date: Sat, 2 Nov 2019 21:06:44 -0700
  3. Subject: Python3 port of ndiff
  4. Ported all python scrips in ndiff/ except setup.py
  5. Some hints on cmp taken from #1484
  6. Minor tweaks to Makefile to support python3, but unsure if
  7. there is a better way to do that.
  8. Seperated .travis.yml commands for easier debugging where it breaks.
  9. This closes the easy half of #1176
  10. Resolves: #1484
  11. ---
  12. .travis.yml | 8 +-
  13. Makefile.in | 6 +-
  14. ndiff/ndiff.py | 495 +++++++++++++++++++++---------------------
  15. ndiff/ndifftest.py | 94 ++++----
  16. ndiff/scripts/ndiff | 14 +-
  17. ndiff/setup.py | 34 +--
  18. ndiff/test-scans/anonymize.py | 18 +-
  19. 7 files changed, 337 insertions(+), 332 deletions(-)
  20. mode change 100644 => 100755 ndiff/setup.py
  21. --- a/.travis.yml
  22. +++ b/.travis.yml
  23. @@ -4,7 +4,13 @@ compiler:
  24. - clang
  25. # Change this to your needs
  26. sudo: false
  27. -script: mkdir /tmp/n && ./configure $SSL_FLAG $LUA_FLAG --prefix=/tmp/n && make && make check && make install && /tmp/n/bin/nmap -A localhost
  28. +script:
  29. + - "mkdir /tmp/n"
  30. + - "./configure $SSL_FLAG $LUA_FLAG --prefix=/tmp/n"
  31. + - "make"
  32. + - "make check"
  33. + - "make install"
  34. + - "/tmp/n/bin/nmap -A localhost"
  35. env:
  36. - SSL_FLAG="--without-openssl" LUA_FLAG="--without-liblua"
  37. --- a/Makefile.in
  38. +++ b/Makefile.in
  39. @@ -35,6 +35,7 @@ ZENMAPDIR = @ZENMAPDIR@
  40. NDIFFDIR = @NDIFFDIR@
  41. NPINGDIR = @NPINGDIR@
  42. PYTHON = @PYTHON@
  43. +PYTHON3 = /usr/bin/env python3
  44. DEFS = @DEFS@ -DNMAP_PLATFORM=\"$(NMAP_PLATFORM)\" -DNMAPDATADIR=\"$(nmapdatadir)\"
  45. # With GCC, add extra security checks to source code.
  46. # http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
  47. @@ -368,6 +369,7 @@ tests/check_dns: $(OBJS)
  48. # this as the location of the interpreter whenever we're not doing a
  49. # local installation.
  50. DEFAULT_PYTHON_PATH = /usr/bin/env python
  51. +DEFAULT_PYTHON3_PATH = /usr/bin/env python3
  52. build-zenmap: $(ZENMAPDIR)/setup.py $(ZENMAPDIR)/zenmapCore/Version.py
  53. # When DESTDIR is defined, assume we're building an executable
  54. @@ -388,7 +390,7 @@ install-zenmap: $(ZENMAPDIR)/setup.py
  55. ln -sf zenmap $(DESTDIR)$(bindir)/xnmap
  56. build-ndiff:
  57. - cd $(NDIFFDIR) && $(PYTHON) setup.py build $(if $(DESTDIR),--executable "$(DEFAULT_PYTHON_PATH)")
  58. + cd $(NDIFFDIR) && $(PYTHON) setup.py build $(if $(DESTDIR),--executable "$(DEFAULT_PYTHON3_PATH)")
  59. build-nping: $(NPINGDIR)/Makefile build-nbase build-nsock build-netutil $(NPINGDIR)/nping.h @DNET_BUILD@ @PCAP_BUILD@
  60. @cd $(NPINGDIR) && $(MAKE)
  61. @@ -458,7 +460,7 @@ check-ncat:
  62. @cd $(NCATDIR) && $(MAKE) check
  63. check-ndiff:
  64. - @cd $(NDIFFDIR) && $(PYTHON) ndifftest.py
  65. + @cd $(NDIFFDIR) && $(PYTHON3) ndifftest.py
  66. check-nsock:
  67. @cd $(NSOCKDIR)/src && $(MAKE) check
  68. --- a/ndiff/ndiff.py
  69. +++ b/ndiff/ndiff.py
  70. @@ -1,4 +1,4 @@
  71. -#!/usr/bin/env python
  72. +#!/usr/bin/env python3
  73. # Ndiff
  74. #
  75. @@ -26,11 +26,11 @@ xml.__path__ = [x for x in xml.__path__
  76. import xml.sax
  77. import xml.sax.saxutils
  78. import xml.dom.minidom
  79. -from StringIO import StringIO
  80. +from io import StringIO
  81. verbose = False
  82. -NDIFF_XML_VERSION = u"1"
  83. +NDIFF_XML_VERSION = "1"
  84. class OverrideEntityResolver(xml.sax.handler.EntityResolver):
  85. @@ -78,35 +78,35 @@ class Scan(object):
  86. def write_nmaprun_open(self, writer):
  87. attrs = {}
  88. if self.scanner is not None:
  89. - attrs[u"scanner"] = self.scanner
  90. + attrs["scanner"] = self.scanner
  91. if self.args is not None:
  92. - attrs[u"args"] = self.args
  93. + attrs["args"] = self.args
  94. if self.start_date is not None:
  95. - attrs[u"start"] = "%d" % time.mktime(self.start_date.timetuple())
  96. - attrs[u"startstr"] = self.start_date.strftime(
  97. + attrs["start"] = "%d" % time.mktime(self.start_date.timetuple())
  98. + attrs["startstr"] = self.start_date.strftime(
  99. "%a %b %d %H:%M:%S %Y")
  100. if self.version is not None:
  101. - attrs[u"version"] = self.version
  102. - writer.startElement(u"nmaprun", attrs)
  103. + attrs["version"] = self.version
  104. + writer.startElement("nmaprun", attrs)
  105. def write_nmaprun_close(self, writer):
  106. - writer.endElement(u"nmaprun")
  107. + writer.endElement("nmaprun")
  108. def nmaprun_to_dom_fragment(self, document):
  109. frag = document.createDocumentFragment()
  110. - elem = document.createElement(u"nmaprun")
  111. + elem = document.createElement("nmaprun")
  112. if self.scanner is not None:
  113. - elem.setAttribute(u"scanner", self.scanner)
  114. + elem.setAttribute("scanner", self.scanner)
  115. if self.args is not None:
  116. - elem.setAttribute(u"args", self.args)
  117. + elem.setAttribute("args", self.args)
  118. if self.start_date is not None:
  119. elem.setAttribute(
  120. - u"start", "%d" % time.mktime(self.start_date.timetuple()))
  121. + "start", "%d" % time.mktime(self.start_date.timetuple()))
  122. elem.setAttribute(
  123. - u"startstr",
  124. + "startstr",
  125. self.start_date.strftime("%a %b %d %H:%M:%S %Y"))
  126. if self.version is not None:
  127. - elem.setAttribute(u"version", self.version)
  128. + elem.setAttribute("version", self.version)
  129. frag.appendChild(elem)
  130. return frag
  131. @@ -136,17 +136,17 @@ class Host(object):
  132. def format_name(self):
  133. """Return a human-readable identifier for this host."""
  134. - address_s = u", ".join(a.s for a in sorted(self.addresses))
  135. - hostname_s = u", ".join(sorted(self.hostnames))
  136. + address_s = ", ".join(a.s for a in sorted(self.addresses))
  137. + hostname_s = ", ".join(sorted(self.hostnames))
  138. if len(hostname_s) > 0:
  139. if len(address_s) > 0:
  140. - return u"%s (%s)" % (hostname_s, address_s)
  141. + return "%s (%s)" % (hostname_s, address_s)
  142. else:
  143. return hostname_s
  144. elif len(address_s) > 0:
  145. return address_s
  146. else:
  147. - return u"<no name>"
  148. + return "<no name>"
  149. def add_port(self, port):
  150. self.ports[port.spec] = port
  151. @@ -163,46 +163,46 @@ class Host(object):
  152. return state is None or state in self.extraports
  153. def extraports_string(self):
  154. - list = [(count, state) for (state, count) in self.extraports.items()]
  155. + locallist = [(count, state) for (state, count) in list(self.extraports.items())]
  156. # Reverse-sort by count.
  157. - list.sort(reverse=True)
  158. - return u", ".join(
  159. - [u"%d %s ports" % (count, state) for (count, state) in list])
  160. + locallist.sort(reverse=True)
  161. + return ", ".join(
  162. + ["%d %s ports" % (count, state) for (count, state) in locallist])
  163. def state_to_dom_fragment(self, document):
  164. frag = document.createDocumentFragment()
  165. if self.state is not None:
  166. - elem = document.createElement(u"status")
  167. - elem.setAttribute(u"state", self.state)
  168. + elem = document.createElement("status")
  169. + elem.setAttribute("state", self.state)
  170. frag.appendChild(elem)
  171. return frag
  172. def hostname_to_dom_fragment(self, document, hostname):
  173. frag = document.createDocumentFragment()
  174. - elem = document.createElement(u"hostname")
  175. - elem.setAttribute(u"name", hostname)
  176. + elem = document.createElement("hostname")
  177. + elem.setAttribute("name", hostname)
  178. frag.appendChild(elem)
  179. return frag
  180. def extraports_to_dom_fragment(self, document):
  181. frag = document.createDocumentFragment()
  182. - for state, count in self.extraports.items():
  183. - elem = document.createElement(u"extraports")
  184. - elem.setAttribute(u"state", state)
  185. - elem.setAttribute(u"count", unicode(count))
  186. + for state, count in list(self.extraports.items()):
  187. + elem = document.createElement("extraports")
  188. + elem.setAttribute("state", state)
  189. + elem.setAttribute("count", str(count))
  190. frag.appendChild(elem)
  191. return frag
  192. def os_to_dom_fragment(self, document, os):
  193. frag = document.createDocumentFragment()
  194. - elem = document.createElement(u"osmatch")
  195. - elem.setAttribute(u"name", os)
  196. + elem = document.createElement("osmatch")
  197. + elem.setAttribute("name", os)
  198. frag.appendChild(elem)
  199. return frag
  200. def to_dom_fragment(self, document):
  201. frag = document.createDocumentFragment()
  202. - elem = document.createElement(u"host")
  203. + elem = document.createElement("host")
  204. if self.state is not None:
  205. elem.appendChild(self.state_to_dom_fragment(document))
  206. @@ -211,13 +211,13 @@ class Host(object):
  207. elem.appendChild(addr.to_dom_fragment(document))
  208. if len(self.hostnames) > 0:
  209. - hostnames_elem = document.createElement(u"hostnames")
  210. + hostnames_elem = document.createElement("hostnames")
  211. for hostname in self.hostnames:
  212. hostnames_elem.appendChild(
  213. self.hostname_to_dom_fragment(document, hostname))
  214. elem.appendChild(hostnames_elem)
  215. - ports_elem = document.createElement(u"ports")
  216. + ports_elem = document.createElement("ports")
  217. ports_elem.appendChild(self.extraports_to_dom_fragment(document))
  218. for port in sorted(self.ports.values()):
  219. if not self.is_extraports(port.state):
  220. @@ -226,13 +226,13 @@ class Host(object):
  221. elem.appendChild(ports_elem)
  222. if len(self.os) > 0:
  223. - os_elem = document.createElement(u"os")
  224. + os_elem = document.createElement("os")
  225. for os in self.os:
  226. os_elem.appendChild(self.os_to_dom_fragment(document, os))
  227. elem.appendChild(os_elem)
  228. if len(self.script_results) > 0:
  229. - hostscript_elem = document.createElement(u"hostscript")
  230. + hostscript_elem = document.createElement("hostscript")
  231. for sr in self.script_results:
  232. hostscript_elem.appendChild(sr.to_dom_fragment(document))
  233. elem.appendChild(hostscript_elem)
  234. @@ -246,7 +246,7 @@ class Address(object):
  235. self.s = s
  236. def __eq__(self, other):
  237. - return self.__cmp__(other) == 0
  238. + return self.sort_key() == other.sort_key()
  239. def __ne__(self, other):
  240. return not self.__eq__(other)
  241. @@ -254,8 +254,8 @@ class Address(object):
  242. def __hash__(self):
  243. return hash(self.sort_key())
  244. - def __cmp__(self, other):
  245. - return cmp(self.sort_key(), other.sort_key())
  246. + def __lt__(self, other):
  247. + return self.sort_key() < other.sort_key()
  248. def __str__(self):
  249. return str(self.s)
  250. @@ -264,21 +264,21 @@ class Address(object):
  251. return self.s
  252. def new(type, s):
  253. - if type == u"ipv4":
  254. + if type == "ipv4":
  255. return IPv4Address(s)
  256. - elif type == u"ipv6":
  257. + elif type == "ipv6":
  258. return IPv6Address(s)
  259. - elif type == u"mac":
  260. + elif type == "mac":
  261. return MACAddress(s)
  262. else:
  263. - raise ValueError(u"Unknown address type %s." % type)
  264. + raise ValueError("Unknown address type %s." % type)
  265. new = staticmethod(new)
  266. def to_dom_fragment(self, document):
  267. frag = document.createDocumentFragment()
  268. - elem = document.createElement(u"address")
  269. - elem.setAttribute(u"addr", self.s)
  270. - elem.setAttribute(u"addrtype", self.type)
  271. + elem = document.createElement("address")
  272. + elem.setAttribute("addr", self.s)
  273. + elem.setAttribute("addrtype", self.type)
  274. frag.appendChild(elem)
  275. return frag
  276. @@ -287,21 +287,21 @@ class Address(object):
  277. class IPv4Address(Address):
  278. - type = property(lambda self: u"ipv4")
  279. + type = property(lambda self: "ipv4")
  280. def sort_key(self):
  281. return (0, self.s)
  282. class IPv6Address(Address):
  283. - type = property(lambda self: u"ipv6")
  284. + type = property(lambda self: "ipv6")
  285. def sort_key(self):
  286. return (1, self.s)
  287. class MACAddress(Address):
  288. - type = property(lambda self: u"mac")
  289. + type = property(lambda self: "mac")
  290. def sort_key(self):
  291. return (2, self.s)
  292. @@ -320,28 +320,25 @@ class Port(object):
  293. def state_string(self):
  294. if self.state is None:
  295. - return u"unknown"
  296. + return "unknown"
  297. else:
  298. - return unicode(self.state)
  299. + return str(self.state)
  300. def spec_string(self):
  301. - return u"%d/%s" % self.spec
  302. + return "%d/%s" % self.spec
  303. - def __cmp__(self, other):
  304. - d = cmp(self.spec, other.spec)
  305. - if d != 0:
  306. - return d
  307. - return cmp((self.spec, self.service, self.script_results),
  308. - (other.spec, other.service, other.script_results))
  309. + def __lt__(self, other):
  310. + return (self.spec, self.service, self.script_results) < (
  311. + other.spec, other.service, other.script_results)
  312. def to_dom_fragment(self, document):
  313. frag = document.createDocumentFragment()
  314. - elem = document.createElement(u"port")
  315. - elem.setAttribute(u"portid", unicode(self.spec[0]))
  316. - elem.setAttribute(u"protocol", self.spec[1])
  317. + elem = document.createElement("port")
  318. + elem.setAttribute("portid", str(self.spec[0]))
  319. + elem.setAttribute("protocol", self.spec[1])
  320. if self.state is not None:
  321. - state_elem = document.createElement(u"state")
  322. - state_elem.setAttribute(u"state", self.state)
  323. + state_elem = document.createElement("state")
  324. + state_elem.setAttribute("state", self.state)
  325. elem.appendChild(state_elem)
  326. elem.appendChild(self.service.to_dom_fragment(document))
  327. for sr in self.script_results:
  328. @@ -385,7 +382,7 @@ class Service(object):
  329. if len(parts) == 0:
  330. return None
  331. else:
  332. - return u"/".join(parts)
  333. + return "/".join(parts)
  334. def version_string(self):
  335. """Get a string like in the VERSION column of Nmap output."""
  336. @@ -395,17 +392,17 @@ class Service(object):
  337. if self.version is not None:
  338. parts.append(self.version)
  339. if self.extrainfo is not None:
  340. - parts.append(u"(%s)" % self.extrainfo)
  341. + parts.append("(%s)" % self.extrainfo)
  342. if len(parts) == 0:
  343. return None
  344. else:
  345. - return u" ".join(parts)
  346. + return " ".join(parts)
  347. def to_dom_fragment(self, document):
  348. frag = document.createDocumentFragment()
  349. - elem = document.createElement(u"service")
  350. - for attr in (u"name", u"product", u"version", u"extrainfo", u"tunnel"):
  351. + elem = document.createElement("service")
  352. + for attr in ("name", "product", "version", "extrainfo", "tunnel"):
  353. v = getattr(self, attr)
  354. if v is None:
  355. continue
  356. @@ -435,53 +432,53 @@ class ScriptResult(object):
  357. result = []
  358. lines = self.output.splitlines()
  359. if len(lines) > 0:
  360. - lines[0] = self.id + u": " + lines[0]
  361. + lines[0] = self.id + ": " + lines[0]
  362. for line in lines[:-1]:
  363. - result.append(u"| " + line)
  364. + result.append("| " + line)
  365. if len(lines) > 0:
  366. - result.append(u"|_ " + lines[-1])
  367. + result.append("|_ " + lines[-1])
  368. return result
  369. def to_dom_fragment(self, document):
  370. frag = document.createDocumentFragment()
  371. - elem = document.createElement(u"script")
  372. - elem.setAttribute(u"id", self.id)
  373. - elem.setAttribute(u"output", self.output)
  374. + elem = document.createElement("script")
  375. + elem.setAttribute("id", self.id)
  376. + elem.setAttribute("output", self.output)
  377. frag.appendChild(elem)
  378. return frag
  379. def format_banner(scan):
  380. """Format a startup banner more or less like Nmap does."""
  381. - scanner = u"Nmap"
  382. - if scan.scanner is not None and scan.scanner != u"nmap":
  383. + scanner = "Nmap"
  384. + if scan.scanner is not None and scan.scanner != "nmap":
  385. scanner = scan.scanner
  386. parts = [scanner]
  387. if scan.version is not None:
  388. parts.append(scan.version)
  389. - parts.append(u"scan")
  390. + parts.append("scan")
  391. if scan.start_date is not None:
  392. - parts.append(u"initiated %s" % scan.start_date.strftime(
  393. + parts.append("initiated %s" % scan.start_date.strftime(
  394. "%a %b %d %H:%M:%S %Y"))
  395. if scan.args is not None:
  396. - parts.append(u"as: %s" % scan.args)
  397. - return u" ".join(parts)
  398. + parts.append("as: %s" % scan.args)
  399. + return " ".join(parts)
  400. def print_script_result_diffs_text(title, script_results_a, script_results_b,
  401. script_result_diffs, f=sys.stdout):
  402. - table = Table(u"*")
  403. + table = Table("*")
  404. for sr_diff in script_result_diffs:
  405. sr_diff.append_to_port_table(table)
  406. if len(table) > 0:
  407. - print >> f
  408. + print(file=f)
  409. if len(script_results_b) == 0:
  410. - print >> f, u"-%s:" % title
  411. + print("-%s:" % title, file=f)
  412. elif len(script_results_a) == 0:
  413. - print >> f, u"+%s:" % title
  414. + print("+%s:" % title, file=f)
  415. else:
  416. - print >> f, u" %s:" % title
  417. - print >> f, table
  418. + print(" %s:" % title, file=f)
  419. + print(table, file=f)
  420. def script_result_diffs_to_dom_fragment(elem, script_results_a,
  421. @@ -489,13 +486,13 @@ def script_result_diffs_to_dom_fragment(
  422. if len(script_results_a) == 0 and len(script_results_b) == 0:
  423. return document.createDocumentFragment()
  424. elif len(script_results_b) == 0:
  425. - a_elem = document.createElement(u"a")
  426. + a_elem = document.createElement("a")
  427. for sr in script_results_a:
  428. elem.appendChild(sr.to_dom_fragment(document))
  429. a_elem.appendChild(elem)
  430. return a_elem
  431. elif len(script_results_a) == 0:
  432. - b_elem = document.createElement(u"b")
  433. + b_elem = document.createElement("b")
  434. for sr in script_results_b:
  435. elem.appendChild(sr.to_dom_fragment(document))
  436. b_elem.appendChild(elem)
  437. @@ -581,10 +578,10 @@ class ScanDiffText(ScanDiff):
  438. banner_a = format_banner(self.scan_a)
  439. banner_b = format_banner(self.scan_b)
  440. if banner_a != banner_b:
  441. - print >> self.f, u"-%s" % banner_a
  442. - print >> self.f, u"+%s" % banner_b
  443. + print("-%s" % banner_a, file=self.f)
  444. + print("+%s" % banner_b, file=self.f)
  445. elif verbose:
  446. - print >> self.f, u" %s" % banner_a
  447. + print(" %s" % banner_a, file=self.f)
  448. def output_pre_scripts(self, pre_script_result_diffs):
  449. print_script_result_diffs_text("Pre-scan script results",
  450. @@ -597,7 +594,7 @@ class ScanDiffText(ScanDiff):
  451. post_script_result_diffs, self.f)
  452. def output_host_diff(self, h_diff):
  453. - print >> self.f
  454. + print(file=self.f)
  455. h_diff.print_text(self.f)
  456. def output_ending(self):
  457. @@ -622,8 +619,8 @@ class ScanDiffXML(ScanDiff):
  458. def output_beginning(self):
  459. self.writer.startDocument()
  460. - self.writer.startElement(u"nmapdiff", {u"version": NDIFF_XML_VERSION})
  461. - self.writer.startElement(u"scandiff", {})
  462. + self.writer.startElement("nmapdiff", {"version": NDIFF_XML_VERSION})
  463. + self.writer.startElement("scandiff", {})
  464. if self.nmaprun_differs():
  465. self.writer.frag_a(
  466. @@ -636,7 +633,7 @@ class ScanDiffXML(ScanDiff):
  467. def output_pre_scripts(self, pre_script_result_diffs):
  468. if len(pre_script_result_diffs) > 0 or verbose:
  469. - prescript_elem = self.document.createElement(u"prescript")
  470. + prescript_elem = self.document.createElement("prescript")
  471. frag = script_result_diffs_to_dom_fragment(
  472. prescript_elem, self.scan_a.pre_script_results,
  473. self.scan_b.pre_script_results, pre_script_result_diffs,
  474. @@ -646,7 +643,7 @@ class ScanDiffXML(ScanDiff):
  475. def output_post_scripts(self, post_script_result_diffs):
  476. if len(post_script_result_diffs) > 0 or verbose:
  477. - postscript_elem = self.document.createElement(u"postscript")
  478. + postscript_elem = self.document.createElement("postscript")
  479. frag = script_result_diffs_to_dom_fragment(
  480. postscript_elem, self.scan_a.post_script_results,
  481. self.scan_b.post_script_results, post_script_result_diffs,
  482. @@ -660,8 +657,8 @@ class ScanDiffXML(ScanDiff):
  483. frag.unlink()
  484. def output_ending(self):
  485. - self.writer.endElement(u"scandiff")
  486. - self.writer.endElement(u"nmapdiff")
  487. + self.writer.endElement("scandiff")
  488. + self.writer.endElement("nmapdiff")
  489. self.writer.endDocument()
  490. @@ -719,9 +716,9 @@ class HostDiff(object):
  491. self.cost += os_cost
  492. extraports_a = tuple((count, state)
  493. - for (state, count) in self.host_a.extraports.items())
  494. + for (state, count) in list(self.host_a.extraports.items()))
  495. extraports_b = tuple((count, state)
  496. - for (state, count) in self.host_b.extraports.items())
  497. + for (state, count) in list(self.host_b.extraports.items()))
  498. if extraports_a != extraports_b:
  499. self.extraports_changed = True
  500. self.cost += 1
  501. @@ -747,69 +744,69 @@ class HostDiff(object):
  502. # Names and addresses.
  503. if self.id_changed:
  504. if host_a.state is not None:
  505. - print >> f, u"-%s:" % host_a.format_name()
  506. + print("-%s:" % host_a.format_name(), file=f)
  507. if self.host_b.state is not None:
  508. - print >> f, u"+%s:" % host_b.format_name()
  509. + print("+%s:" % host_b.format_name(), file=f)
  510. else:
  511. - print >> f, u" %s:" % host_a.format_name()
  512. + print(" %s:" % host_a.format_name(), file=f)
  513. # State.
  514. if self.state_changed:
  515. if host_a.state is not None:
  516. - print >> f, u"-Host is %s." % host_a.state
  517. + print("-Host is %s." % host_a.state, file=f)
  518. if host_b.state is not None:
  519. - print >> f, u"+Host is %s." % host_b.state
  520. + print("+Host is %s." % host_b.state, file=f)
  521. elif verbose:
  522. - print >> f, u" Host is %s." % host_b.state
  523. + print(" Host is %s." % host_b.state, file=f)
  524. # Extraports.
  525. if self.extraports_changed:
  526. if len(host_a.extraports) > 0:
  527. - print >> f, u"-Not shown: %s" % host_a.extraports_string()
  528. + print("-Not shown: %s" % host_a.extraports_string(), file=f)
  529. if len(host_b.extraports) > 0:
  530. - print >> f, u"+Not shown: %s" % host_b.extraports_string()
  531. + print("+Not shown: %s" % host_b.extraports_string(), file=f)
  532. elif verbose:
  533. if len(host_a.extraports) > 0:
  534. - print >> f, u" Not shown: %s" % host_a.extraports_string()
  535. + print(" Not shown: %s" % host_a.extraports_string(), file=f)
  536. # Port table.
  537. - port_table = Table(u"** * * *")
  538. + port_table = Table("** * * *")
  539. if host_a.state is None:
  540. - mark = u"+"
  541. + mark = "+"
  542. elif host_b.state is None:
  543. - mark = u"-"
  544. + mark = "-"
  545. else:
  546. - mark = u" "
  547. - port_table.append((mark, u"PORT", u"STATE", u"SERVICE", u"VERSION"))
  548. + mark = " "
  549. + port_table.append((mark, "PORT", "STATE", "SERVICE", "VERSION"))
  550. for port in self.ports:
  551. port_diff = self.port_diffs[port]
  552. port_diff.append_to_port_table(port_table, host_a, host_b)
  553. if len(port_table) > 1:
  554. - print >> f, port_table
  555. + print(port_table, file=f)
  556. # OS changes.
  557. if self.os_changed or verbose:
  558. if len(host_a.os) > 0:
  559. if len(host_b.os) > 0:
  560. - print >> f, u" OS details:"
  561. + print(" OS details:", file=f)
  562. else:
  563. - print >> f, u"-OS details:"
  564. + print("-OS details:", file=f)
  565. elif len(host_b.os) > 0:
  566. - print >> f, u"+OS details:"
  567. + print("+OS details:", file=f)
  568. # os_diffs is a list of 5-tuples returned by
  569. # difflib.SequenceMatcher.
  570. for op, i1, i2, j1, j2 in self.os_diffs:
  571. if op == "replace" or op == "delete":
  572. for i in range(i1, i2):
  573. - print >> f, "- %s" % host_a.os[i]
  574. + print("- %s" % host_a.os[i], file=f)
  575. if op == "replace" or op == "insert":
  576. for i in range(j1, j2):
  577. - print >> f, "+ %s" % host_b.os[i]
  578. + print("+ %s" % host_b.os[i], file=f)
  579. if op == "equal":
  580. for i in range(i1, i2):
  581. - print >> f, " %s" % host_a.os[i]
  582. + print(" %s" % host_a.os[i], file=f)
  583. print_script_result_diffs_text("Host script results",
  584. host_a.script_results, host_b.script_results,
  585. @@ -820,32 +817,32 @@ class HostDiff(object):
  586. host_b = self.host_b
  587. frag = document.createDocumentFragment()
  588. - hostdiff_elem = document.createElement(u"hostdiff")
  589. + hostdiff_elem = document.createElement("hostdiff")
  590. frag.appendChild(hostdiff_elem)
  591. if host_a.state is None or host_b.state is None:
  592. # The host is missing in one scan. Output the whole thing.
  593. if host_a.state is not None:
  594. - a_elem = document.createElement(u"a")
  595. + a_elem = document.createElement("a")
  596. a_elem.appendChild(host_a.to_dom_fragment(document))
  597. hostdiff_elem.appendChild(a_elem)
  598. elif host_b.state is not None:
  599. - b_elem = document.createElement(u"b")
  600. + b_elem = document.createElement("b")
  601. b_elem.appendChild(host_b.to_dom_fragment(document))
  602. hostdiff_elem.appendChild(b_elem)
  603. return frag
  604. - host_elem = document.createElement(u"host")
  605. + host_elem = document.createElement("host")
  606. # State.
  607. if host_a.state == host_b.state:
  608. if verbose:
  609. host_elem.appendChild(host_a.state_to_dom_fragment(document))
  610. else:
  611. - a_elem = document.createElement(u"a")
  612. + a_elem = document.createElement("a")
  613. a_elem.appendChild(host_a.state_to_dom_fragment(document))
  614. host_elem.appendChild(a_elem)
  615. - b_elem = document.createElement(u"b")
  616. + b_elem = document.createElement("b")
  617. b_elem.appendChild(host_b.state_to_dom_fragment(document))
  618. host_elem.appendChild(b_elem)
  619. @@ -854,31 +851,31 @@ class HostDiff(object):
  620. addrset_b = set(host_b.addresses)
  621. for addr in sorted(addrset_a.intersection(addrset_b)):
  622. host_elem.appendChild(addr.to_dom_fragment(document))
  623. - a_elem = document.createElement(u"a")
  624. + a_elem = document.createElement("a")
  625. for addr in sorted(addrset_a - addrset_b):
  626. a_elem.appendChild(addr.to_dom_fragment(document))
  627. if a_elem.hasChildNodes():
  628. host_elem.appendChild(a_elem)
  629. - b_elem = document.createElement(u"b")
  630. + b_elem = document.createElement("b")
  631. for addr in sorted(addrset_b - addrset_a):
  632. b_elem.appendChild(addr.to_dom_fragment(document))
  633. if b_elem.hasChildNodes():
  634. host_elem.appendChild(b_elem)
  635. # Host names.
  636. - hostnames_elem = document.createElement(u"hostnames")
  637. + hostnames_elem = document.createElement("hostnames")
  638. hostnameset_a = set(host_a.hostnames)
  639. hostnameset_b = set(host_b.hostnames)
  640. for hostname in sorted(hostnameset_a.intersection(hostnameset_b)):
  641. hostnames_elem.appendChild(
  642. host_a.hostname_to_dom_fragment(document, hostname))
  643. - a_elem = document.createElement(u"a")
  644. + a_elem = document.createElement("a")
  645. for hostname in sorted(hostnameset_a - hostnameset_b):
  646. a_elem.appendChild(
  647. host_a.hostname_to_dom_fragment(document, hostname))
  648. if a_elem.hasChildNodes():
  649. hostnames_elem.appendChild(a_elem)
  650. - b_elem = document.createElement(u"b")
  651. + b_elem = document.createElement("b")
  652. for hostname in sorted(hostnameset_b - hostnameset_a):
  653. b_elem.appendChild(
  654. host_b.hostname_to_dom_fragment(document, hostname))
  655. @@ -887,15 +884,15 @@ class HostDiff(object):
  656. if hostnames_elem.hasChildNodes():
  657. host_elem.appendChild(hostnames_elem)
  658. - ports_elem = document.createElement(u"ports")
  659. + ports_elem = document.createElement("ports")
  660. # Extraports.
  661. if host_a.extraports == host_b.extraports:
  662. ports_elem.appendChild(host_a.extraports_to_dom_fragment(document))
  663. else:
  664. - a_elem = document.createElement(u"a")
  665. + a_elem = document.createElement("a")
  666. a_elem.appendChild(host_a.extraports_to_dom_fragment(document))
  667. ports_elem.appendChild(a_elem)
  668. - b_elem = document.createElement(u"b")
  669. + b_elem = document.createElement("b")
  670. b_elem.appendChild(host_b.extraports_to_dom_fragment(document))
  671. ports_elem.appendChild(b_elem)
  672. # Port list.
  673. @@ -911,18 +908,18 @@ class HostDiff(object):
  674. # OS changes.
  675. if self.os_changed or verbose:
  676. - os_elem = document.createElement(u"os")
  677. + os_elem = document.createElement("os")
  678. # os_diffs is a list of 5-tuples returned by
  679. # difflib.SequenceMatcher.
  680. for op, i1, i2, j1, j2 in self.os_diffs:
  681. if op == "replace" or op == "delete":
  682. - a_elem = document.createElement(u"a")
  683. + a_elem = document.createElement("a")
  684. for i in range(i1, i2):
  685. a_elem.appendChild(host_a.os_to_dom_fragment(
  686. document, host_a.os[i]))
  687. os_elem.appendChild(a_elem)
  688. if op == "replace" or op == "insert":
  689. - b_elem = document.createElement(u"b")
  690. + b_elem = document.createElement("b")
  691. for i in range(j1, j2):
  692. b_elem.appendChild(host_b.os_to_dom_fragment(
  693. document, host_b.os[i]))
  694. @@ -936,7 +933,7 @@ class HostDiff(object):
  695. # Host script changes.
  696. if len(self.script_result_diffs) > 0 or verbose:
  697. - hostscript_elem = document.createElement(u"hostscript")
  698. + hostscript_elem = document.createElement("hostscript")
  699. host_elem.appendChild(script_result_diffs_to_dom_fragment(
  700. hostscript_elem, host_a.script_results,
  701. host_b.script_results, self.script_result_diffs,
  702. @@ -989,38 +986,38 @@ class PortDiff(object):
  703. self.port_b.service.version_string()]
  704. if a_columns == b_columns:
  705. if verbose or self.script_result_diffs > 0:
  706. - table.append([u" "] + a_columns)
  707. + table.append([" "] + a_columns)
  708. else:
  709. if not host_a.is_extraports(self.port_a.state):
  710. - table.append([u"-"] + a_columns)
  711. + table.append(["-"] + a_columns)
  712. if not host_b.is_extraports(self.port_b.state):
  713. - table.append([u"+"] + b_columns)
  714. + table.append(["+"] + b_columns)
  715. for sr_diff in self.script_result_diffs:
  716. sr_diff.append_to_port_table(table)
  717. def to_dom_fragment(self, document):
  718. frag = document.createDocumentFragment()
  719. - portdiff_elem = document.createElement(u"portdiff")
  720. + portdiff_elem = document.createElement("portdiff")
  721. frag.appendChild(portdiff_elem)
  722. if (self.port_a.spec == self.port_b.spec and
  723. self.port_a.state == self.port_b.state):
  724. - port_elem = document.createElement(u"port")
  725. - port_elem.setAttribute(u"portid", unicode(self.port_a.spec[0]))
  726. - port_elem.setAttribute(u"protocol", self.port_a.spec[1])
  727. + port_elem = document.createElement("port")
  728. + port_elem.setAttribute("portid", str(self.port_a.spec[0]))
  729. + port_elem.setAttribute("protocol", self.port_a.spec[1])
  730. if self.port_a.state is not None:
  731. - state_elem = document.createElement(u"state")
  732. - state_elem.setAttribute(u"state", self.port_a.state)
  733. + state_elem = document.createElement("state")
  734. + state_elem.setAttribute("state", self.port_a.state)
  735. port_elem.appendChild(state_elem)
  736. if self.port_a.service == self.port_b.service:
  737. port_elem.appendChild(
  738. self.port_a.service.to_dom_fragment(document))
  739. else:
  740. - a_elem = document.createElement(u"a")
  741. + a_elem = document.createElement("a")
  742. a_elem.appendChild(
  743. self.port_a.service.to_dom_fragment(document))
  744. port_elem.appendChild(a_elem)
  745. - b_elem = document.createElement(u"b")
  746. + b_elem = document.createElement("b")
  747. b_elem.appendChild(
  748. self.port_b.service.to_dom_fragment(document))
  749. port_elem.appendChild(b_elem)
  750. @@ -1028,10 +1025,10 @@ class PortDiff(object):
  751. port_elem.appendChild(sr_diff.to_dom_fragment(document))
  752. portdiff_elem.appendChild(port_elem)
  753. else:
  754. - a_elem = document.createElement(u"a")
  755. + a_elem = document.createElement("a")
  756. a_elem.appendChild(self.port_a.to_dom_fragment(document))
  757. portdiff_elem.appendChild(a_elem)
  758. - b_elem = document.createElement(u"b")
  759. + b_elem = document.createElement("b")
  760. b_elem.appendChild(self.port_b.to_dom_fragment(document))
  761. portdiff_elem.appendChild(b_elem)
  762. @@ -1086,13 +1083,13 @@ class ScriptResultDiff(object):
  763. for op, i1, i2, j1, j2 in diffs.get_opcodes():
  764. if op == "replace" or op == "delete":
  765. for k in range(i1, i2):
  766. - table.append_raw(u"-" + a_lines[k])
  767. + table.append_raw("-" + a_lines[k])
  768. if op == "replace" or op == "insert":
  769. for k in range(j1, j2):
  770. - table.append_raw(u"+" + b_lines[k])
  771. + table.append_raw("+" + b_lines[k])
  772. if op == "equal":
  773. for k in range(i1, i2):
  774. - table.append_raw(u" " + a_lines[k])
  775. + table.append_raw(" " + a_lines[k])
  776. def to_dom_fragment(self, document):
  777. frag = document.createDocumentFragment()
  778. @@ -1102,11 +1099,11 @@ class ScriptResultDiff(object):
  779. frag.appendChild(self.sr_a.to_dom_fragment(document))
  780. else:
  781. if self.sr_a is not None:
  782. - a_elem = document.createElement(u"a")
  783. + a_elem = document.createElement("a")
  784. a_elem.appendChild(self.sr_a.to_dom_fragment(document))
  785. frag.appendChild(a_elem)
  786. if self.sr_b is not None:
  787. - b_elem = document.createElement(u"b")
  788. + b_elem = document.createElement("b")
  789. b_elem.appendChild(self.sr_b.to_dom_fragment(document))
  790. frag.appendChild(b_elem)
  791. return frag
  792. @@ -1120,7 +1117,7 @@ class Table(object):
  793. copied to the output."""
  794. self.widths = []
  795. self.rows = []
  796. - self.prefix = u""
  797. + self.prefix = ""
  798. self.padding = []
  799. j = 0
  800. while j < len(template) and template[j] != "*":
  801. @@ -1145,7 +1142,7 @@ class Table(object):
  802. for i in range(len(row)):
  803. if row[i] is None:
  804. - s = u""
  805. + s = ""
  806. else:
  807. s = str(row[i])
  808. if i == len(self.widths):
  809. @@ -1167,7 +1164,7 @@ class Table(object):
  810. for row in self.rows:
  811. parts = [self.prefix]
  812. i = 0
  813. - if isinstance(row, basestring):
  814. + if isinstance(row, str):
  815. # A raw string.
  816. lines.append(row)
  817. else:
  818. @@ -1176,13 +1173,13 @@ class Table(object):
  819. if i < len(self.padding):
  820. parts.append(self.padding[i])
  821. i += 1
  822. - lines.append(u"".join(parts).rstrip())
  823. - return u"\n".join(lines)
  824. + lines.append("".join(parts).rstrip())
  825. + return "\n".join(lines)
  826. def warn(str):
  827. """Print a warning to stderr."""
  828. - print >> sys.stderr, str
  829. + print(str, file=sys.stderr)
  830. class NmapContentHandler(xml.sax.handler.ContentHandler):
  831. @@ -1200,22 +1197,22 @@ class NmapContentHandler(xml.sax.handler
  832. self.current_port = None
  833. self._start_elem_handlers = {
  834. - u"nmaprun": self._start_nmaprun,
  835. - u"host": self._start_host,
  836. - u"status": self._start_status,
  837. - u"address": self._start_address,
  838. - u"hostname": self._start_hostname,
  839. - u"extraports": self._start_extraports,
  840. - u"port": self._start_port,
  841. - u"state": self._start_state,
  842. - u"service": self._start_service,
  843. - u"script": self._start_script,
  844. - u"osmatch": self._start_osmatch,
  845. - u"finished": self._start_finished,
  846. + "nmaprun": self._start_nmaprun,
  847. + "host": self._start_host,
  848. + "status": self._start_status,
  849. + "address": self._start_address,
  850. + "hostname": self._start_hostname,
  851. + "extraports": self._start_extraports,
  852. + "port": self._start_port,
  853. + "state": self._start_state,
  854. + "service": self._start_service,
  855. + "script": self._start_script,
  856. + "osmatch": self._start_osmatch,
  857. + "finished": self._start_finished,
  858. }
  859. self._end_elem_handlers = {
  860. - u'host': self._end_host,
  861. - u'port': self._end_port,
  862. + 'host': self._end_host,
  863. + 'port': self._end_port,
  864. }
  865. def parent_element(self):
  866. @@ -1245,68 +1242,68 @@ class NmapContentHandler(xml.sax.handler
  867. def _start_nmaprun(self, name, attrs):
  868. assert self.parent_element() is None
  869. if "start" in attrs:
  870. - start_timestamp = int(attrs.get(u"start"))
  871. + start_timestamp = int(attrs.get("start"))
  872. self.scan.start_date = datetime.datetime.fromtimestamp(
  873. start_timestamp)
  874. - self.scan.scanner = attrs.get(u"scanner")
  875. - self.scan.args = attrs.get(u"args")
  876. - self.scan.version = attrs.get(u"version")
  877. + self.scan.scanner = attrs.get("scanner")
  878. + self.scan.args = attrs.get("args")
  879. + self.scan.version = attrs.get("version")
  880. def _start_host(self, name, attrs):
  881. - assert self.parent_element() == u"nmaprun"
  882. + assert self.parent_element() == "nmaprun"
  883. self.current_host = Host()
  884. self.scan.hosts.append(self.current_host)
  885. def _start_status(self, name, attrs):
  886. - assert self.parent_element() == u"host"
  887. + assert self.parent_element() == "host"
  888. assert self.current_host is not None
  889. - state = attrs.get(u"state")
  890. + state = attrs.get("state")
  891. if state is None:
  892. warn(u'%s element of host %s is missing the "state" attribute; '
  893. - 'assuming \unknown\.' % (
  894. + r'assuming \unknown\.' % (
  895. name, self.current_host.format_name()))
  896. return
  897. self.current_host.state = state
  898. def _start_address(self, name, attrs):
  899. - assert self.parent_element() == u"host"
  900. + assert self.parent_element() == "host"
  901. assert self.current_host is not None
  902. - addr = attrs.get(u"addr")
  903. + addr = attrs.get("addr")
  904. if addr is None:
  905. - warn(u'%s element of host %s is missing the "addr" '
  906. + warn('%s element of host %s is missing the "addr" '
  907. 'attribute; skipping.' % (
  908. name, self.current_host.format_name()))
  909. return
  910. - addrtype = attrs.get(u"addrtype", u"ipv4")
  911. + addrtype = attrs.get("addrtype", "ipv4")
  912. self.current_host.add_address(Address.new(addrtype, addr))
  913. def _start_hostname(self, name, attrs):
  914. - assert self.parent_element() == u"hostnames"
  915. + assert self.parent_element() == "hostnames"
  916. assert self.current_host is not None
  917. - hostname = attrs.get(u"name")
  918. + hostname = attrs.get("name")
  919. if hostname is None:
  920. - warn(u'%s element of host %s is missing the "name" '
  921. + warn('%s element of host %s is missing the "name" '
  922. 'attribute; skipping.' % (
  923. name, self.current_host.format_name()))
  924. return
  925. self.current_host.add_hostname(hostname)
  926. def _start_extraports(self, name, attrs):
  927. - assert self.parent_element() == u"ports"
  928. + assert self.parent_element() == "ports"
  929. assert self.current_host is not None
  930. - state = attrs.get(u"state")
  931. + state = attrs.get("state")
  932. if state is None:
  933. - warn(u'%s element of host %s is missing the "state" '
  934. + warn('%s element of host %s is missing the "state" '
  935. 'attribute; assuming "unknown".' % (
  936. name, self.current_host.format_name()))
  937. state = None
  938. if state in self.current_host.extraports:
  939. - warn(u'Duplicate extraports state "%s" in host %s.' % (
  940. + warn('Duplicate extraports state "%s" in host %s.' % (
  941. state, self.current_host.format_name()))
  942. - count = attrs.get(u"count")
  943. + count = attrs.get("count")
  944. if count is None:
  945. - warn(u'%s element of host %s is missing the "count" '
  946. + warn('%s element of host %s is missing the "count" '
  947. 'attribute; assuming 0.' % (
  948. name, self.current_host.format_name()))
  949. count = 0
  950. @@ -1314,99 +1311,99 @@ class NmapContentHandler(xml.sax.handler
  951. try:
  952. count = int(count)
  953. except ValueError:
  954. - warn(u"Can't convert extraports count \"%s\" "
  955. + warn("Can't convert extraports count \"%s\" "
  956. "to an integer in host %s; assuming 0." % (
  957. - attrs[u"count"], self.current_host.format_name()))
  958. + attrs["count"], self.current_host.format_name()))
  959. count = 0
  960. self.current_host.extraports[state] = count
  961. def _start_port(self, name, attrs):
  962. - assert self.parent_element() == u"ports"
  963. + assert self.parent_element() == "ports"
  964. assert self.current_host is not None
  965. - portid_str = attrs.get(u"portid")
  966. + portid_str = attrs.get("portid")
  967. if portid_str is None:
  968. - warn(u'%s element of host %s missing the "portid" '
  969. + warn('%s element of host %s missing the "portid" '
  970. 'attribute; skipping.' % (
  971. name, self.current_host.format_name()))
  972. return
  973. try:
  974. portid = int(portid_str)
  975. except ValueError:
  976. - warn(u"Can't convert portid \"%s\" to an integer "
  977. + warn("Can't convert portid \"%s\" to an integer "
  978. "in host %s; skipping port." % (
  979. portid_str, self.current_host.format_name()))
  980. return
  981. - protocol = attrs.get(u"protocol")
  982. + protocol = attrs.get("protocol")
  983. if protocol is None:
  984. - warn(u'%s element of host %s missing the "protocol" '
  985. + warn('%s element of host %s missing the "protocol" '
  986. 'attribute; skipping.' % (
  987. name, self.current_host.format_name()))
  988. return
  989. self.current_port = Port((portid, protocol))
  990. def _start_state(self, name, attrs):
  991. - assert self.parent_element() == u"port"
  992. + assert self.parent_element() == "port"
  993. assert self.current_host is not None
  994. if self.current_port is None:
  995. return
  996. if "state" not in attrs:
  997. - warn(u'%s element of port %s is missing the "state" '
  998. + warn('%s element of port %s is missing the "state" '
  999. 'attribute; assuming "unknown".' % (
  1000. name, self.current_port.spec_string()))
  1001. return
  1002. - self.current_port.state = attrs[u"state"]
  1003. + self.current_port.state = attrs["state"]
  1004. self.current_host.add_port(self.current_port)
  1005. def _start_service(self, name, attrs):
  1006. - assert self.parent_element() == u"port"
  1007. + assert self.parent_element() == "port"
  1008. assert self.current_host is not None
  1009. if self.current_port is None:
  1010. return
  1011. - self.current_port.service.name = attrs.get(u"name")
  1012. - self.current_port.service.product = attrs.get(u"product")
  1013. - self.current_port.service.version = attrs.get(u"version")
  1014. - self.current_port.service.extrainfo = attrs.get(u"extrainfo")
  1015. - self.current_port.service.tunnel = attrs.get(u"tunnel")
  1016. + self.current_port.service.name = attrs.get("name")
  1017. + self.current_port.service.product = attrs.get("product")
  1018. + self.current_port.service.version = attrs.get("version")
  1019. + self.current_port.service.extrainfo = attrs.get("extrainfo")
  1020. + self.current_port.service.tunnel = attrs.get("tunnel")
  1021. def _start_script(self, name, attrs):
  1022. result = ScriptResult()
  1023. - result.id = attrs.get(u"id")
  1024. + result.id = attrs.get("id")
  1025. if result.id is None:
  1026. - warn(u'%s element missing the "id" attribute; skipping.' % name)
  1027. + warn('%s element missing the "id" attribute; skipping.' % name)
  1028. return
  1029. - result.output = attrs.get(u"output")
  1030. + result.output = attrs.get("output")
  1031. if result.output is None:
  1032. - warn(u'%s element missing the "output" attribute; skipping.'
  1033. + warn('%s element missing the "output" attribute; skipping.'
  1034. % name)
  1035. return
  1036. - if self.parent_element() == u"prescript":
  1037. + if self.parent_element() == "prescript":
  1038. self.scan.pre_script_results.append(result)
  1039. - elif self.parent_element() == u"postscript":
  1040. + elif self.parent_element() == "postscript":
  1041. self.scan.post_script_results.append(result)
  1042. - elif self.parent_element() == u"hostscript":
  1043. + elif self.parent_element() == "hostscript":
  1044. self.current_host.script_results.append(result)
  1045. - elif self.parent_element() == u"port":
  1046. + elif self.parent_element() == "port":
  1047. self.current_port.script_results.append(result)
  1048. else:
  1049. - warn(u"%s element not inside prescript, postscript, hostscript, "
  1050. + warn("%s element not inside prescript, postscript, hostscript, "
  1051. "or port element; ignoring." % name)
  1052. return
  1053. def _start_osmatch(self, name, attrs):
  1054. - assert self.parent_element() == u"os"
  1055. + assert self.parent_element() == "os"
  1056. assert self.current_host is not None
  1057. if "name" not in attrs:
  1058. - warn(u'%s element of host %s is missing the "name" '
  1059. + warn('%s element of host %s is missing the "name" '
  1060. 'attribute; skipping.' % (
  1061. name, self.current_host.format_name()))
  1062. return
  1063. - self.current_host.os.append(attrs[u"name"])
  1064. + self.current_host.os.append(attrs["name"])
  1065. def _start_finished(self, name, attrs):
  1066. - assert self.parent_element() == u"runstats"
  1067. + assert self.parent_element() == "runstats"
  1068. if "time" in attrs:
  1069. - end_timestamp = int(attrs.get(u"time"))
  1070. + end_timestamp = int(attrs.get("time"))
  1071. self.scan.end_date = datetime.datetime.fromtimestamp(end_timestamp)
  1072. def _end_host(self, name):
  1073. @@ -1425,23 +1422,23 @@ class XMLWriter (xml.sax.saxutils.XMLGen
  1074. def frag(self, frag):
  1075. for node in frag.childNodes:
  1076. - node.writexml(self.f, newl=u"\n")
  1077. + node.writexml(self.f, newl="\n")
  1078. def frag_a(self, frag):
  1079. - self.startElement(u"a", {})
  1080. + self.startElement("a", {})
  1081. for node in frag.childNodes:
  1082. - node.writexml(self.f, newl=u"\n")
  1083. - self.endElement(u"a")
  1084. + node.writexml(self.f, newl="\n")
  1085. + self.endElement("a")
  1086. def frag_b(self, frag):
  1087. - self.startElement(u"b", {})
  1088. + self.startElement("b", {})
  1089. for node in frag.childNodes:
  1090. - node.writexml(self.f, newl=u"\n")
  1091. - self.endElement(u"b")
  1092. + node.writexml(self.f, newl="\n")
  1093. + self.endElement("b")
  1094. def usage():
  1095. - print u"""\
  1096. + print("""\
  1097. Usage: %s [option] FILE1 FILE2
  1098. Compare two Nmap XML files and display a list of their differences.
  1099. Differences include host state changes, port state changes, and changes to
  1100. @@ -1451,7 +1448,7 @@ service and OS detection.
  1101. -v, --verbose also show hosts and ports that haven't changed.
  1102. --text display output in text format (default)
  1103. --xml display output in XML format\
  1104. -""" % sys.argv[0]
  1105. +""" % sys.argv[0])
  1106. EXIT_EQUAL = 0
  1107. EXIT_DIFFERENT = 1
  1108. @@ -1459,8 +1456,8 @@ EXIT_ERROR = 2
  1109. def usage_error(msg):
  1110. - print >> sys.stderr, u"%s: %s" % (sys.argv[0], msg)
  1111. - print >> sys.stderr, u"Try '%s -h' for help." % sys.argv[0]
  1112. + print("%s: %s" % (sys.argv[0], msg), file=sys.stderr)
  1113. + print("Try '%s -h' for help." % sys.argv[0], file=sys.stderr)
  1114. sys.exit(EXIT_ERROR)
  1115. @@ -1471,7 +1468,7 @@ def main():
  1116. try:
  1117. opts, input_filenames = getopt.gnu_getopt(
  1118. sys.argv[1:], "hv", ["help", "text", "verbose", "xml"])
  1119. - except getopt.GetoptError, e:
  1120. + except getopt.GetoptError as e:
  1121. usage_error(e.msg)
  1122. for o, a in opts:
  1123. if o == "-h" or o == "--help":
  1124. @@ -1481,15 +1478,15 @@ def main():
  1125. verbose = True
  1126. elif o == "--text":
  1127. if output_format is not None and output_format != "text":
  1128. - usage_error(u"contradictory output format options.")
  1129. + usage_error("contradictory output format options.")
  1130. output_format = "text"
  1131. elif o == "--xml":
  1132. if output_format is not None and output_format != "xml":
  1133. - usage_error(u"contradictory output format options.")
  1134. + usage_error("contradictory output format options.")
  1135. output_format = "xml"
  1136. if len(input_filenames) != 2:
  1137. - usage_error(u"need exactly two input filenames.")
  1138. + usage_error("need exactly two input filenames.")
  1139. if output_format is None:
  1140. output_format = "text"
  1141. @@ -1502,8 +1499,8 @@ def main():
  1142. scan_a.load_from_file(filename_a)
  1143. scan_b = Scan()
  1144. scan_b.load_from_file(filename_b)
  1145. - except IOError, e:
  1146. - print >> sys.stderr, u"Can't open file: %s" % str(e)
  1147. + except IOError as e:
  1148. + print("Can't open file: %s" % str(e), file=sys.stderr)
  1149. sys.exit(EXIT_ERROR)
  1150. if output_format == "text":
  1151. --- a/ndiff/ndifftest.py
  1152. +++ b/ndiff/ndifftest.py
  1153. @@ -1,4 +1,4 @@
  1154. -#!/usr/bin/env python
  1155. +#!/usr/bin/env python3
  1156. # Unit tests for Ndiff.
  1157. @@ -22,7 +22,7 @@ for x in dir(ndiff):
  1158. sys.dont_write_bytecode = dont_write_bytecode
  1159. del dont_write_bytecode
  1160. -import StringIO
  1161. +import io
  1162. class scan_test(unittest.TestCase):
  1163. @@ -52,7 +52,7 @@ class scan_test(unittest.TestCase):
  1164. scan.load_from_file("test-scans/single.xml")
  1165. host = scan.hosts[0]
  1166. self.assertEqual(len(host.ports), 5)
  1167. - self.assertEqual(host.extraports.items(), [("filtered", 95)])
  1168. + self.assertEqual(list(host.extraports.items()), [("filtered", 95)])
  1169. def test_extraports_multi(self):
  1170. """Test that the correct number of known ports is returned when there
  1171. @@ -68,9 +68,9 @@ class scan_test(unittest.TestCase):
  1172. """Test that nmaprun information is recorded."""
  1173. scan = Scan()
  1174. scan.load_from_file("test-scans/empty.xml")
  1175. - self.assertEqual(scan.scanner, u"nmap")
  1176. - self.assertEqual(scan.version, u"4.90RC2")
  1177. - self.assertEqual(scan.args, u"nmap -oX empty.xml -p 1-100")
  1178. + self.assertEqual(scan.scanner, "nmap")
  1179. + self.assertEqual(scan.version, "4.90RC2")
  1180. + self.assertEqual(scan.args, "nmap -oX empty.xml -p 1-100")
  1181. def test_addresses(self):
  1182. """Test that addresses are recorded."""
  1183. @@ -84,7 +84,7 @@ class scan_test(unittest.TestCase):
  1184. scan = Scan()
  1185. scan.load_from_file("test-scans/simple.xml")
  1186. host = scan.hosts[0]
  1187. - self.assertEqual(host.hostnames, [u"scanme.nmap.org"])
  1188. + self.assertEqual(host.hostnames, ["scanme.nmap.org"])
  1189. def test_os(self):
  1190. """Test that OS information is recorded."""
  1191. @@ -99,7 +99,7 @@ class scan_test(unittest.TestCase):
  1192. scan.load_from_file("test-scans/complex.xml")
  1193. host = scan.hosts[0]
  1194. self.assertTrue(len(host.script_results) > 0)
  1195. - self.assertTrue(len(host.ports[(22, u"tcp")].script_results) > 0)
  1196. + self.assertTrue(len(host.ports[(22, "tcp")].script_results) > 0)
  1197. # This test is commented out because Nmap XML doesn't store any information
  1198. # about down hosts, not even the fact that they are down. Recovering the list
  1199. @@ -128,16 +128,16 @@ class host_test(unittest.TestCase):
  1200. def test_format_name(self):
  1201. h = Host()
  1202. - self.assertTrue(isinstance(h.format_name(), basestring))
  1203. - h.add_address(IPv4Address(u"127.0.0.1"))
  1204. - self.assertTrue(u"127.0.0.1" in h.format_name())
  1205. + self.assertTrue(isinstance(h.format_name(), str))
  1206. + h.add_address(IPv4Address("127.0.0.1"))
  1207. + self.assertTrue("127.0.0.1" in h.format_name())
  1208. h.add_address(IPv6Address("::1"))
  1209. - self.assertTrue(u"127.0.0.1" in h.format_name())
  1210. - self.assertTrue(u"::1" in h.format_name())
  1211. - h.add_hostname(u"localhost")
  1212. - self.assertTrue(u"127.0.0.1" in h.format_name())
  1213. - self.assertTrue(u"::1" in h.format_name())
  1214. - self.assertTrue(u"localhost" in h.format_name())
  1215. + self.assertTrue("127.0.0.1" in h.format_name())
  1216. + self.assertTrue("::1" in h.format_name())
  1217. + h.add_hostname("localhost")
  1218. + self.assertTrue("127.0.0.1" in h.format_name())
  1219. + self.assertTrue("::1" in h.format_name())
  1220. + self.assertTrue("localhost" in h.format_name())
  1221. def test_empty_get_port(self):
  1222. h = Host()
  1223. @@ -197,8 +197,8 @@ class host_test(unittest.TestCase):
  1224. h = s.hosts[0]
  1225. self.assertEqual(len(h.ports), 5)
  1226. self.assertEqual(len(h.extraports), 1)
  1227. - self.assertEqual(h.extraports.keys()[0], u"filtered")
  1228. - self.assertEqual(h.extraports.values()[0], 95)
  1229. + self.assertEqual(list(h.extraports.keys())[0], "filtered")
  1230. + self.assertEqual(list(h.extraports.values())[0], 95)
  1231. self.assertEqual(h.state, "up")
  1232. @@ -241,13 +241,13 @@ class port_test(unittest.TestCase):
  1233. """Test the Port class."""
  1234. def test_spec_string(self):
  1235. p = Port((10, "tcp"))
  1236. - self.assertEqual(p.spec_string(), u"10/tcp")
  1237. + self.assertEqual(p.spec_string(), "10/tcp")
  1238. p = Port((100, "ip"))
  1239. - self.assertEqual(p.spec_string(), u"100/ip")
  1240. + self.assertEqual(p.spec_string(), "100/ip")
  1241. def test_state_string(self):
  1242. p = Port((10, "tcp"))
  1243. - self.assertEqual(p.state_string(), u"unknown")
  1244. + self.assertEqual(p.state_string(), "unknown")
  1245. class service_test(unittest.TestCase):
  1246. @@ -255,47 +255,47 @@ class service_test(unittest.TestCase):
  1247. def test_compare(self):
  1248. """Test that services with the same contents compare equal."""
  1249. a = Service()
  1250. - a.name = u"ftp"
  1251. - a.product = u"FooBar FTP"
  1252. - a.version = u"1.1.1"
  1253. - a.tunnel = u"ssl"
  1254. + a.name = "ftp"
  1255. + a.product = "FooBar FTP"
  1256. + a.version = "1.1.1"
  1257. + a.tunnel = "ssl"
  1258. self.assertEqual(a, a)
  1259. b = Service()
  1260. - b.name = u"ftp"
  1261. - b.product = u"FooBar FTP"
  1262. - b.version = u"1.1.1"
  1263. - b.tunnel = u"ssl"
  1264. + b.name = "ftp"
  1265. + b.product = "FooBar FTP"
  1266. + b.version = "1.1.1"
  1267. + b.tunnel = "ssl"
  1268. self.assertEqual(a, b)
  1269. - b.name = u"http"
  1270. + b.name = "http"
  1271. self.assertNotEqual(a, b)
  1272. c = Service()
  1273. self.assertNotEqual(a, c)
  1274. def test_tunnel(self):
  1275. serv = Service()
  1276. - serv.name = u"http"
  1277. - serv.tunnel = u"ssl"
  1278. - self.assertEqual(serv.name_string(), u"ssl/http")
  1279. + serv.name = "http"
  1280. + serv.tunnel = "ssl"
  1281. + self.assertEqual(serv.name_string(), "ssl/http")
  1282. def test_version_string(self):
  1283. serv = Service()
  1284. - serv.product = u"FooBar"
  1285. + serv.product = "FooBar"
  1286. self.assertTrue(len(serv.version_string()) > 0)
  1287. serv = Service()
  1288. - serv.version = u"1.2.3"
  1289. + serv.version = "1.2.3"
  1290. self.assertTrue(len(serv.version_string()) > 0)
  1291. serv = Service()
  1292. - serv.extrainfo = u"misconfigured"
  1293. + serv.extrainfo = "misconfigured"
  1294. self.assertTrue(len(serv.version_string()) > 0)
  1295. serv = Service()
  1296. - serv.product = u"FooBar"
  1297. - serv.version = u"1.2.3"
  1298. + serv.product = "FooBar"
  1299. + serv.version = "1.2.3"
  1300. # Must match Nmap output.
  1301. self.assertEqual(serv.version_string(),
  1302. - u"%s %s" % (serv.product, serv.version))
  1303. - serv.extrainfo = u"misconfigured"
  1304. + "%s %s" % (serv.product, serv.version))
  1305. + serv.extrainfo = "misconfigured"
  1306. self.assertEqual(serv.version_string(),
  1307. - u"%s %s (%s)" % (serv.product, serv.version, serv.extrainfo))
  1308. + "%s %s (%s)" % (serv.product, serv.version, serv.extrainfo))
  1309. class ScanDiffSub(ScanDiff):
  1310. @@ -703,7 +703,7 @@ class scan_diff_xml_test(unittest.TestCa
  1311. a.load_from_file("test-scans/empty.xml")
  1312. b = Scan()
  1313. b.load_from_file("test-scans/simple.xml")
  1314. - f = StringIO.StringIO()
  1315. + f = io.StringIO()
  1316. self.scan_diff = ScanDiffXML(a, b, f)
  1317. self.scan_diff.output()
  1318. self.xml = f.getvalue()
  1319. @@ -712,8 +712,8 @@ class scan_diff_xml_test(unittest.TestCa
  1320. def test_well_formed(self):
  1321. try:
  1322. document = xml.dom.minidom.parseString(self.xml)
  1323. - except Exception, e:
  1324. - self.fail(u"Parsing XML diff output caused the exception: %s"
  1325. + except Exception as e:
  1326. + self.fail("Parsing XML diff output caused the exception: %s"
  1327. % str(e))
  1328. @@ -739,8 +739,8 @@ def host_apply_diff(host, diff):
  1329. host.os = diff.host_b.os[:]
  1330. if diff.extraports_changed:
  1331. - for state in host.extraports.keys():
  1332. - for port in host.ports.values():
  1333. + for state in list(host.extraports.keys()):
  1334. + for port in list(host.ports.values()):
  1335. if port.state == state:
  1336. del host.ports[port.spec]
  1337. host.extraports = diff.host_b.extraports.copy()
  1338. --- a/ndiff/scripts/ndiff
  1339. +++ b/ndiff/scripts/ndiff
  1340. @@ -1,4 +1,4 @@
  1341. -#!/usr/bin/env python
  1342. +#!/usr/bin/env python3
  1343. # Ndiff
  1344. #
  1345. @@ -67,15 +67,15 @@ if INSTALL_LIB is not None and is_secure
  1346. try:
  1347. import ndiff
  1348. -except ImportError, e:
  1349. - print >> sys.stderr, """\
  1350. +except ImportError as e:
  1351. + print("""\
  1352. Could not import the ndiff module: %s.
  1353. -I checked in these directories:""" % repr(e.message)
  1354. +I checked in these directories:""" % repr(e), file=sys.stderr)
  1355. for dir in sys.path:
  1356. - print >> sys.stderr, " %s" % dir
  1357. - print >> sys.stderr, """\
  1358. + print(" %s" % dir, file=sys.stderr)
  1359. + print("""\
  1360. If you installed Ndiff in another directory, you may have to add the
  1361. -modules directory to the PYTHONPATH environment variable."""
  1362. +modules directory to the PYTHONPATH environment variable.""", file=sys.stderr)
  1363. sys.exit(1)
  1364. import ndiff
  1365. --- a/ndiff/setup.py
  1366. +++ b/ndiff/setup.py
  1367. @@ -94,7 +94,7 @@ class checked_install(distutils.command.
  1368. self.saved_prefix = sys.prefix
  1369. try:
  1370. distutils.command.install.install.finalize_options(self)
  1371. - except distutils.errors.DistutilsPlatformError, e:
  1372. + except distutils.errors.DistutilsPlatformError as e:
  1373. raise distutils.errors.DistutilsPlatformError(str(e) + """
  1374. Installing your distribution's python-dev package may solve this problem.""")
  1375. @@ -155,13 +155,13 @@ Installing your distribution's python-de
  1376. #!/usr/bin/env python
  1377. import errno, os, os.path, sys
  1378. -print 'Uninstall %(name)s'
  1379. +print('Uninstall %(name)s')
  1380. answer = raw_input('Are you sure that you want to uninstall '
  1381. '%(name)s (yes/no) ')
  1382. if answer != 'yes' and answer != 'y':
  1383. - print 'Not uninstalling.'
  1384. + print('Not uninstalling.')
  1385. sys.exit(0)
  1386. """ % {'name': APP_NAME}
  1387. @@ -177,8 +177,8 @@ if answer != 'yes' and answer != 'y':
  1388. # This should never happen (everything gets installed
  1389. # inside the root), but if it does, be safe and don't
  1390. # delete anything.
  1391. - uninstaller += ("print '%s was not installed inside "
  1392. - "the root %s; skipping.'\n" % (output, self.root))
  1393. + uninstaller += ("print('%s was not installed inside "
  1394. + "the root %s; skipping.')\n" % (output, self.root))
  1395. continue
  1396. output = path_strip_prefix(output, self.root)
  1397. assert os.path.isabs(output)
  1398. @@ -202,24 +202,24 @@ for path in INSTALLED_FILES:
  1399. dirs.append(path)
  1400. # Delete the files.
  1401. for file in files:
  1402. - print "Removing '%s'." % file
  1403. + print("Removing '%s'." % file)
  1404. try:
  1405. os.remove(file)
  1406. - except OSError, e:
  1407. - print >> sys.stderr, ' Error: %s.' % str(e)
  1408. + except OSError as e:
  1409. + print(' Error: %s.' % str(e), file=sys.stderr)
  1410. # Delete the directories. First reverse-sort the normalized paths by
  1411. # length so that child directories are deleted before their parents.
  1412. dirs = [os.path.normpath(dir) for dir in dirs]
  1413. dirs.sort(key = len, reverse = True)
  1414. for dir in dirs:
  1415. try:
  1416. - print "Removing the directory '%s'." % dir
  1417. + print("Removing the directory '%s'." % dir)
  1418. os.rmdir(dir)
  1419. - except OSError, e:
  1420. + except OSError as e:
  1421. if e.errno == errno.ENOTEMPTY:
  1422. - print "Directory '%s' not empty; not removing." % dir
  1423. + print("Directory '%s' not empty; not removing." % dir)
  1424. else:
  1425. - print >> sys.stderr, str(e)
  1426. + print(str(e), file=sys.stderr)
  1427. """
  1428. uninstaller_file = open(uninstaller_filename, 'w')
  1429. @@ -227,7 +227,7 @@ for dir in dirs:
  1430. uninstaller_file.close()
  1431. # Set exec bit for uninstaller
  1432. - mode = ((os.stat(uninstaller_filename)[ST_MODE]) | 0555) & 07777
  1433. + mode = ((os.stat(uninstaller_filename)[ST_MODE]) | 0o555) & 0o7777
  1434. os.chmod(uninstaller_filename, mode)
  1435. def write_installed_files(self):
  1436. @@ -242,7 +242,7 @@ for dir in dirs:
  1437. try:
  1438. for output in self.get_installed_files():
  1439. assert "\n" not in output
  1440. - print >> f, output
  1441. + print(output, file=f)
  1442. finally:
  1443. f.close()
  1444. @@ -266,7 +266,7 @@ class my_uninstall(distutils.cmd.Command
  1445. # Read the list of installed files.
  1446. try:
  1447. f = open(INSTALLED_FILES_NAME, "r")
  1448. - except IOError, e:
  1449. + except IOError as e:
  1450. if e.errno == errno.ENOENT:
  1451. log.error("Couldn't open the installation record '%s'. "
  1452. "Have you installed yet?" % INSTALLED_FILES_NAME)
  1453. @@ -289,7 +289,7 @@ class my_uninstall(distutils.cmd.Command
  1454. try:
  1455. if not self.dry_run:
  1456. os.remove(file)
  1457. - except OSError, e:
  1458. + except OSError as e:
  1459. log.error(str(e))
  1460. # Delete the directories. First reverse-sort the normalized paths by
  1461. # length so that child directories are deleted before their parents.
  1462. @@ -300,7 +300,7 @@ class my_uninstall(distutils.cmd.Command
  1463. log.info("Removing the directory '%s'." % dir)
  1464. if not self.dry_run:
  1465. os.rmdir(dir)
  1466. - except OSError, e:
  1467. + except OSError as e:
  1468. if e.errno == errno.ENOTEMPTY:
  1469. log.info("Directory '%s' not empty; not removing." % dir)
  1470. else:
  1471. --- a/ndiff/test-scans/anonymize.py
  1472. +++ b/ndiff/test-scans/anonymize.py
  1473. @@ -1,4 +1,4 @@
  1474. -#!/usr/bin/env python
  1475. +#!/usr/bin/env python3
  1476. # Anonymize an Nmap XML file, replacing host name and IP addresses with random
  1477. # anonymous ones. Anonymized names will be consistent between runs of the
  1478. @@ -20,20 +20,20 @@ r = random.Random()
  1479. def hash(s):
  1480. - digest = hashlib.sha512(s).hexdigest()
  1481. + digest = hashlib.sha512(s.encode()).hexdigest()
  1482. return int(digest, 16)
  1483. def anonymize_mac_address(addr):
  1484. r.seed(hash(addr))
  1485. nums = (0, 0, 0) + tuple(r.randrange(256) for i in range(3))
  1486. - return u":".join(u"%02X" % x for x in nums)
  1487. + return ":".join("%02X" % x for x in nums)
  1488. def anonymize_ipv4_address(addr):
  1489. r.seed(hash(addr))
  1490. nums = (10,) + tuple(r.randrange(256) for i in range(3))
  1491. - return u".".join(unicode(x) for x in nums)
  1492. + return ".".join(str(x) for x in nums)
  1493. def anonymize_ipv6_address(addr):
  1494. @@ -41,7 +41,7 @@ def anonymize_ipv6_address(addr):
  1495. # RFC 4193.
  1496. nums = (0xFD00 + r.randrange(256),)
  1497. nums = nums + tuple(r.randrange(65536) for i in range(7))
  1498. - return u":".join("%04X" % x for x in nums)
  1499. + return ":".join("%04X" % x for x in nums)
  1500. # Maps to memoize address and host name conversions.
  1501. hostname_map = {}
  1502. @@ -54,11 +54,11 @@ def anonymize_hostname(name):
  1503. LETTERS = "acbdefghijklmnopqrstuvwxyz"
  1504. r.seed(hash(name))
  1505. length = r.randrange(5, 10)
  1506. - prefix = u"".join(r.sample(LETTERS, length))
  1507. + prefix = "".join(r.sample(LETTERS, length))
  1508. num = r.randrange(1000)
  1509. - hostname_map[name] = u"%s-%d.example.com" % (prefix, num)
  1510. + hostname_map[name] = "%s-%d.example.com" % (prefix, num)
  1511. if VERBOSE:
  1512. - print >> sys.stderr, "Replace %s with %s" % (name, hostname_map[name])
  1513. + print("Replace %s with %s" % (name, hostname_map[name]), file=sys.stderr)
  1514. return hostname_map[name]
  1515. mac_re = re.compile(r'\b([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}\b')
  1516. @@ -78,7 +78,7 @@ def anonymize_address(addr):
  1517. else:
  1518. assert False
  1519. if VERBOSE:
  1520. - print >> sys.stderr, "Replace %s with %s" % (addr, address_map[addr])
  1521. + print("Replace %s with %s" % (addr, address_map[addr]), file=sys.stderr)
  1522. return address_map[addr]