1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 12:41:29 +00:00

Update Ndiff to Python 3. Closes #1807

This commit is contained in:
dmiller
2022-12-07 20:34:05 +00:00
parent 24b26317c7
commit 12d41ec2cd
7 changed files with 325 additions and 329 deletions

View File

@@ -1,5 +1,7 @@
#Nmap Changelog ($Id$); -*-text-*-
o [GH#1807][GH#1176][Ndiff] Updated Ndiff to Python 3. [Brian Quigley]
o [GH#2088][GH#1176][Zenmap] Updated Zenmap to Python 3 and PyGObject. [Jakub Kulík]
o [GH#2541] UDP port scan (-sU) and version scan (-sV) now both use the same

View File

@@ -359,7 +359,7 @@ tests/check_dns: $(OBJS)
# installing on a machine other than the one used to do the build. Use
# this as the location of the interpreter whenever we're not doing a
# local installation.
DEFAULT_PYTHON_PATH = /usr/bin/env python
DEFAULT_PYTHON_PATH = /usr/bin/env python3
build-zenmap: $(ZENMAPDIR)/setup.py $(ZENMAPDIR)/zenmapCore/Version.py
# When DESTDIR is defined, assume we're building an executable

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# Unit tests for Ndiff.
@@ -22,7 +22,7 @@ for x in dir(ndiff):
sys.dont_write_bytecode = dont_write_bytecode
del dont_write_bytecode
import StringIO
import io
class scan_test(unittest.TestCase):
@@ -52,7 +52,7 @@ class scan_test(unittest.TestCase):
scan.load_from_file("test-scans/single.xml")
host = scan.hosts[0]
self.assertEqual(len(host.ports), 5)
self.assertEqual(host.extraports.items(), [("filtered", 95)])
self.assertEqual(list(host.extraports.items()), [("filtered", 95)])
def test_extraports_multi(self):
"""Test that the correct number of known ports is returned when there
@@ -68,9 +68,9 @@ class scan_test(unittest.TestCase):
"""Test that nmaprun information is recorded."""
scan = Scan()
scan.load_from_file("test-scans/empty.xml")
self.assertEqual(scan.scanner, u"nmap")
self.assertEqual(scan.version, u"4.90RC2")
self.assertEqual(scan.args, u"nmap -oX empty.xml -p 1-100")
self.assertEqual(scan.scanner, "nmap")
self.assertEqual(scan.version, "4.90RC2")
self.assertEqual(scan.args, "nmap -oX empty.xml -p 1-100")
def test_addresses(self):
"""Test that addresses are recorded."""
@@ -84,7 +84,7 @@ class scan_test(unittest.TestCase):
scan = Scan()
scan.load_from_file("test-scans/simple.xml")
host = scan.hosts[0]
self.assertEqual(host.hostnames, [u"scanme.nmap.org"])
self.assertEqual(host.hostnames, ["scanme.nmap.org"])
def test_os(self):
"""Test that OS information is recorded."""
@@ -99,7 +99,7 @@ class scan_test(unittest.TestCase):
scan.load_from_file("test-scans/complex.xml")
host = scan.hosts[0]
self.assertTrue(len(host.script_results) > 0)
self.assertTrue(len(host.ports[(22, u"tcp")].script_results) > 0)
self.assertTrue(len(host.ports[(22, "tcp")].script_results) > 0)
# This test is commented out because Nmap XML doesn't store any information
# about down hosts, not even the fact that they are down. Recovering the list
@@ -128,16 +128,16 @@ class host_test(unittest.TestCase):
def test_format_name(self):
h = Host()
self.assertTrue(isinstance(h.format_name(), basestring))
h.add_address(IPv4Address(u"127.0.0.1"))
self.assertTrue(u"127.0.0.1" in h.format_name())
self.assertTrue(isinstance(h.format_name(), str))
h.add_address(IPv4Address("127.0.0.1"))
self.assertTrue("127.0.0.1" in h.format_name())
h.add_address(IPv6Address("::1"))
self.assertTrue(u"127.0.0.1" in h.format_name())
self.assertTrue(u"::1" in h.format_name())
h.add_hostname(u"localhost")
self.assertTrue(u"127.0.0.1" in h.format_name())
self.assertTrue(u"::1" in h.format_name())
self.assertTrue(u"localhost" in h.format_name())
self.assertTrue("127.0.0.1" in h.format_name())
self.assertTrue("::1" in h.format_name())
h.add_hostname("localhost")
self.assertTrue("127.0.0.1" in h.format_name())
self.assertTrue("::1" in h.format_name())
self.assertTrue("localhost" in h.format_name())
def test_empty_get_port(self):
h = Host()
@@ -197,8 +197,8 @@ class host_test(unittest.TestCase):
h = s.hosts[0]
self.assertEqual(len(h.ports), 5)
self.assertEqual(len(h.extraports), 1)
self.assertEqual(h.extraports.keys()[0], u"filtered")
self.assertEqual(h.extraports.values()[0], 95)
self.assertEqual(list(h.extraports.keys())[0], "filtered")
self.assertEqual(list(h.extraports.values())[0], 95)
self.assertEqual(h.state, "up")
@@ -241,13 +241,13 @@ class port_test(unittest.TestCase):
"""Test the Port class."""
def test_spec_string(self):
p = Port((10, "tcp"))
self.assertEqual(p.spec_string(), u"10/tcp")
self.assertEqual(p.spec_string(), "10/tcp")
p = Port((100, "ip"))
self.assertEqual(p.spec_string(), u"100/ip")
self.assertEqual(p.spec_string(), "100/ip")
def test_state_string(self):
p = Port((10, "tcp"))
self.assertEqual(p.state_string(), u"unknown")
self.assertEqual(p.state_string(), "unknown")
class service_test(unittest.TestCase):
@@ -255,47 +255,47 @@ class service_test(unittest.TestCase):
def test_compare(self):
"""Test that services with the same contents compare equal."""
a = Service()
a.name = u"ftp"
a.product = u"FooBar FTP"
a.version = u"1.1.1"
a.tunnel = u"ssl"
a.name = "ftp"
a.product = "FooBar FTP"
a.version = "1.1.1"
a.tunnel = "ssl"
self.assertEqual(a, a)
b = Service()
b.name = u"ftp"
b.product = u"FooBar FTP"
b.version = u"1.1.1"
b.tunnel = u"ssl"
b.name = "ftp"
b.product = "FooBar FTP"
b.version = "1.1.1"
b.tunnel = "ssl"
self.assertEqual(a, b)
b.name = u"http"
b.name = "http"
self.assertNotEqual(a, b)
c = Service()
self.assertNotEqual(a, c)
def test_tunnel(self):
serv = Service()
serv.name = u"http"
serv.tunnel = u"ssl"
self.assertEqual(serv.name_string(), u"ssl/http")
serv.name = "http"
serv.tunnel = "ssl"
self.assertEqual(serv.name_string(), "ssl/http")
def test_version_string(self):
serv = Service()
serv.product = u"FooBar"
serv.product = "FooBar"
self.assertTrue(len(serv.version_string()) > 0)
serv = Service()
serv.version = u"1.2.3"
serv.version = "1.2.3"
self.assertTrue(len(serv.version_string()) > 0)
serv = Service()
serv.extrainfo = u"misconfigured"
serv.extrainfo = "misconfigured"
self.assertTrue(len(serv.version_string()) > 0)
serv = Service()
serv.product = u"FooBar"
serv.version = u"1.2.3"
serv.product = "FooBar"
serv.version = "1.2.3"
# Must match Nmap output.
self.assertEqual(serv.version_string(),
u"%s %s" % (serv.product, serv.version))
serv.extrainfo = u"misconfigured"
"%s %s" % (serv.product, serv.version))
serv.extrainfo = "misconfigured"
self.assertEqual(serv.version_string(),
u"%s %s (%s)" % (serv.product, serv.version, serv.extrainfo))
"%s %s (%s)" % (serv.product, serv.version, serv.extrainfo))
class ScanDiffSub(ScanDiff):
@@ -703,7 +703,7 @@ class scan_diff_xml_test(unittest.TestCase):
a.load_from_file("test-scans/empty.xml")
b = Scan()
b.load_from_file("test-scans/simple.xml")
f = StringIO.StringIO()
f = io.StringIO()
self.scan_diff = ScanDiffXML(a, b, f)
self.scan_diff.output()
self.xml = f.getvalue()
@@ -712,8 +712,8 @@ class scan_diff_xml_test(unittest.TestCase):
def test_well_formed(self):
try:
document = xml.dom.minidom.parseString(self.xml)
except Exception, e:
self.fail(u"Parsing XML diff output caused the exception: %s"
except Exception as e:
self.fail("Parsing XML diff output caused the exception: %s"
% str(e))
@@ -739,8 +739,8 @@ def host_apply_diff(host, diff):
host.os = diff.host_b.os[:]
if diff.extraports_changed:
for state in host.extraports.keys():
for port in host.ports.values():
for state in list(host.extraports.keys()):
for port in list(host.ports.values()):
if port.state == state:
del host.ports[port.spec]
host.extraports = diff.host_b.extraports.copy()

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# Ndiff
#
@@ -66,15 +66,15 @@ if INSTALL_LIB is not None and is_secure_dir(INSTALL_LIB):
try:
import ndiff
except ImportError, e:
print >> sys.stderr, """\
except ImportError as e:
print("""\
Could not import the ndiff module: %s.
I checked in these directories:""" % repr(e.message)
I checked in these directories:""" % repr(e), file=sys.stderr)
for dir in sys.path:
print >> sys.stderr, " %s" % dir
print >> sys.stderr, """\
print(" %s" % dir, file=sys.stderr)
print("""\
If you installed Ndiff in another directory, you may have to add the
modules directory to the PYTHONPATH environment variable."""
modules directory to the PYTHONPATH environment variable.""", file=sys.stderr)
sys.exit(1)
import ndiff

View File

@@ -94,7 +94,7 @@ class checked_install(distutils.command.install.install):
self.saved_prefix = sys.prefix
try:
distutils.command.install.install.finalize_options(self)
except distutils.errors.DistutilsPlatformError, e:
except distutils.errors.DistutilsPlatformError as e:
raise distutils.errors.DistutilsPlatformError(str(e) + """
Installing your distribution's python-dev package may solve this problem.""")
@@ -227,7 +227,7 @@ for dir in dirs:
uninstaller_file.close()
# Set exec bit for uninstaller
mode = ((os.stat(uninstaller_filename)[ST_MODE]) | 0555) & 07777
mode = ((os.stat(uninstaller_filename)[ST_MODE]) | 0o555) & 0o7777
os.chmod(uninstaller_filename, mode)
def write_installed_files(self):
@@ -241,7 +241,7 @@ for dir in dirs:
with open(INSTALLED_FILES_NAME, "w") as f:
for output in self.get_installed_files():
assert "\n" not in output
print >> f, output
print(output, file=f)
class my_uninstall(distutils.cmd.Command):
@@ -263,7 +263,7 @@ class my_uninstall(distutils.cmd.Command):
# Read the list of installed files.
try:
f = open(INSTALLED_FILES_NAME, "r")
except IOError, e:
except IOError as e:
if e.errno == errno.ENOENT:
log.error("Couldn't open the installation record '%s'. "
"Have you installed yet?" % INSTALLED_FILES_NAME)
@@ -286,7 +286,7 @@ class my_uninstall(distutils.cmd.Command):
try:
if not self.dry_run:
os.remove(file)
except OSError, e:
except OSError as e:
log.error(str(e))
# Delete the directories. First reverse-sort the normalized paths by
# length so that child directories are deleted before their parents.
@@ -297,16 +297,16 @@ class my_uninstall(distutils.cmd.Command):
log.info("Removing the directory '%s'." % dir)
if not self.dry_run:
os.rmdir(dir)
except OSError, e:
except OSError as e:
if e.errno == errno.ENOTEMPTY:
log.info("Directory '%s' not empty; not removing." % dir)
else:
log.error(str(e))
distutils.core.setup(name=u"ndiff", scripts=[u"scripts/ndiff"],
py_modules=[u"ndiff"],
data_files=[(u"share/man/man1", [u"docs/ndiff.1"])],
distutils.core.setup(name="ndiff", scripts=["scripts/ndiff"],
py_modules=["ndiff"],
data_files=[("share/man/man1", ["docs/ndiff.1"])],
cmdclass={
"install_egg_info": null_command,
"install": checked_install,

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# Anonymize an Nmap XML file, replacing host name and IP addresses with random
# anonymous ones. Anonymized names will be consistent between runs of the
@@ -20,20 +20,20 @@ r = random.Random()
def hash(s):
digest = hashlib.sha512(s).hexdigest()
digest = hashlib.sha512(s.encode()).hexdigest()
return int(digest, 16)
def anonymize_mac_address(addr):
r.seed(hash(addr))
nums = (0, 0, 0) + tuple(r.randrange(256) for i in range(3))
return u":".join(u"%02X" % x for x in nums)
return ":".join("%02X" % x for x in nums)
def anonymize_ipv4_address(addr):
r.seed(hash(addr))
nums = (10,) + tuple(r.randrange(256) for i in range(3))
return u".".join(unicode(x) for x in nums)
return ".".join(str(x) for x in nums)
def anonymize_ipv6_address(addr):
@@ -41,7 +41,7 @@ def anonymize_ipv6_address(addr):
# RFC 4193.
nums = (0xFD00 + r.randrange(256),)
nums = nums + tuple(r.randrange(65536) for i in range(7))
return u":".join("%04X" % x for x in nums)
return ":".join("%04X" % x for x in nums)
# Maps to memoize address and host name conversions.
hostname_map = {}
@@ -54,11 +54,11 @@ def anonymize_hostname(name):
LETTERS = "acbdefghijklmnopqrstuvwxyz"
r.seed(hash(name))
length = r.randrange(5, 10)
prefix = u"".join(r.sample(LETTERS, length))
prefix = "".join(r.sample(LETTERS, length))
num = r.randrange(1000)
hostname_map[name] = u"%s-%d.example.com" % (prefix, num)
hostname_map[name] = "%s-%d.example.com" % (prefix, num)
if VERBOSE:
print >> sys.stderr, "Replace %s with %s" % (name, hostname_map[name])
print("Replace %s with %s" % (name, hostname_map[name]), file=sys.stderr)
return hostname_map[name]
mac_re = re.compile(r'\b([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}\b')
@@ -78,7 +78,7 @@ def anonymize_address(addr):
else:
assert False
if VERBOSE:
print >> sys.stderr, "Replace %s with %s" % (addr, address_map[addr])
print("Replace %s with %s" % (addr, address_map[addr]), file=sys.stderr)
return address_map[addr]