1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 20:51:30 +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-*- #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#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 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 # 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 # this as the location of the interpreter whenever we're not doing a
# local installation. # 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 build-zenmap: $(ZENMAPDIR)/setup.py $(ZENMAPDIR)/zenmapCore/Version.py
# When DESTDIR is defined, assume we're building an executable # 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. # Unit tests for Ndiff.
@@ -22,7 +22,7 @@ for x in dir(ndiff):
sys.dont_write_bytecode = dont_write_bytecode sys.dont_write_bytecode = dont_write_bytecode
del dont_write_bytecode del dont_write_bytecode
import StringIO import io
class scan_test(unittest.TestCase): class scan_test(unittest.TestCase):
@@ -52,7 +52,7 @@ class scan_test(unittest.TestCase):
scan.load_from_file("test-scans/single.xml") scan.load_from_file("test-scans/single.xml")
host = scan.hosts[0] host = scan.hosts[0]
self.assertEqual(len(host.ports), 5) 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): def test_extraports_multi(self):
"""Test that the correct number of known ports is returned when there """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.""" """Test that nmaprun information is recorded."""
scan = Scan() scan = Scan()
scan.load_from_file("test-scans/empty.xml") scan.load_from_file("test-scans/empty.xml")
self.assertEqual(scan.scanner, u"nmap") self.assertEqual(scan.scanner, "nmap")
self.assertEqual(scan.version, u"4.90RC2") self.assertEqual(scan.version, "4.90RC2")
self.assertEqual(scan.args, u"nmap -oX empty.xml -p 1-100") self.assertEqual(scan.args, "nmap -oX empty.xml -p 1-100")
def test_addresses(self): def test_addresses(self):
"""Test that addresses are recorded.""" """Test that addresses are recorded."""
@@ -84,7 +84,7 @@ class scan_test(unittest.TestCase):
scan = Scan() scan = Scan()
scan.load_from_file("test-scans/simple.xml") scan.load_from_file("test-scans/simple.xml")
host = scan.hosts[0] host = scan.hosts[0]
self.assertEqual(host.hostnames, [u"scanme.nmap.org"]) self.assertEqual(host.hostnames, ["scanme.nmap.org"])
def test_os(self): def test_os(self):
"""Test that OS information is recorded.""" """Test that OS information is recorded."""
@@ -99,7 +99,7 @@ class scan_test(unittest.TestCase):
scan.load_from_file("test-scans/complex.xml") scan.load_from_file("test-scans/complex.xml")
host = scan.hosts[0] host = scan.hosts[0]
self.assertTrue(len(host.script_results) > 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 # 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 # 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): def test_format_name(self):
h = Host() h = Host()
self.assertTrue(isinstance(h.format_name(), basestring)) self.assertTrue(isinstance(h.format_name(), str))
h.add_address(IPv4Address(u"127.0.0.1")) h.add_address(IPv4Address("127.0.0.1"))
self.assertTrue(u"127.0.0.1" in h.format_name()) self.assertTrue("127.0.0.1" in h.format_name())
h.add_address(IPv6Address("::1")) h.add_address(IPv6Address("::1"))
self.assertTrue(u"127.0.0.1" in h.format_name()) self.assertTrue("127.0.0.1" in h.format_name())
self.assertTrue(u"::1" in h.format_name()) self.assertTrue("::1" in h.format_name())
h.add_hostname(u"localhost") h.add_hostname("localhost")
self.assertTrue(u"127.0.0.1" in h.format_name()) self.assertTrue("127.0.0.1" in h.format_name())
self.assertTrue(u"::1" in h.format_name()) self.assertTrue("::1" in h.format_name())
self.assertTrue(u"localhost" in h.format_name()) self.assertTrue("localhost" in h.format_name())
def test_empty_get_port(self): def test_empty_get_port(self):
h = Host() h = Host()
@@ -197,8 +197,8 @@ class host_test(unittest.TestCase):
h = s.hosts[0] h = s.hosts[0]
self.assertEqual(len(h.ports), 5) self.assertEqual(len(h.ports), 5)
self.assertEqual(len(h.extraports), 1) self.assertEqual(len(h.extraports), 1)
self.assertEqual(h.extraports.keys()[0], u"filtered") self.assertEqual(list(h.extraports.keys())[0], "filtered")
self.assertEqual(h.extraports.values()[0], 95) self.assertEqual(list(h.extraports.values())[0], 95)
self.assertEqual(h.state, "up") self.assertEqual(h.state, "up")
@@ -241,13 +241,13 @@ class port_test(unittest.TestCase):
"""Test the Port class.""" """Test the Port class."""
def test_spec_string(self): def test_spec_string(self):
p = Port((10, "tcp")) p = Port((10, "tcp"))
self.assertEqual(p.spec_string(), u"10/tcp") self.assertEqual(p.spec_string(), "10/tcp")
p = Port((100, "ip")) p = Port((100, "ip"))
self.assertEqual(p.spec_string(), u"100/ip") self.assertEqual(p.spec_string(), "100/ip")
def test_state_string(self): def test_state_string(self):
p = Port((10, "tcp")) p = Port((10, "tcp"))
self.assertEqual(p.state_string(), u"unknown") self.assertEqual(p.state_string(), "unknown")
class service_test(unittest.TestCase): class service_test(unittest.TestCase):
@@ -255,47 +255,47 @@ class service_test(unittest.TestCase):
def test_compare(self): def test_compare(self):
"""Test that services with the same contents compare equal.""" """Test that services with the same contents compare equal."""
a = Service() a = Service()
a.name = u"ftp" a.name = "ftp"
a.product = u"FooBar FTP" a.product = "FooBar FTP"
a.version = u"1.1.1" a.version = "1.1.1"
a.tunnel = u"ssl" a.tunnel = "ssl"
self.assertEqual(a, a) self.assertEqual(a, a)
b = Service() b = Service()
b.name = u"ftp" b.name = "ftp"
b.product = u"FooBar FTP" b.product = "FooBar FTP"
b.version = u"1.1.1" b.version = "1.1.1"
b.tunnel = u"ssl" b.tunnel = "ssl"
self.assertEqual(a, b) self.assertEqual(a, b)
b.name = u"http" b.name = "http"
self.assertNotEqual(a, b) self.assertNotEqual(a, b)
c = Service() c = Service()
self.assertNotEqual(a, c) self.assertNotEqual(a, c)
def test_tunnel(self): def test_tunnel(self):
serv = Service() serv = Service()
serv.name = u"http" serv.name = "http"
serv.tunnel = u"ssl" serv.tunnel = "ssl"
self.assertEqual(serv.name_string(), u"ssl/http") self.assertEqual(serv.name_string(), "ssl/http")
def test_version_string(self): def test_version_string(self):
serv = Service() serv = Service()
serv.product = u"FooBar" serv.product = "FooBar"
self.assertTrue(len(serv.version_string()) > 0) self.assertTrue(len(serv.version_string()) > 0)
serv = Service() serv = Service()
serv.version = u"1.2.3" serv.version = "1.2.3"
self.assertTrue(len(serv.version_string()) > 0) self.assertTrue(len(serv.version_string()) > 0)
serv = Service() serv = Service()
serv.extrainfo = u"misconfigured" serv.extrainfo = "misconfigured"
self.assertTrue(len(serv.version_string()) > 0) self.assertTrue(len(serv.version_string()) > 0)
serv = Service() serv = Service()
serv.product = u"FooBar" serv.product = "FooBar"
serv.version = u"1.2.3" serv.version = "1.2.3"
# Must match Nmap output. # Must match Nmap output.
self.assertEqual(serv.version_string(), self.assertEqual(serv.version_string(),
u"%s %s" % (serv.product, serv.version)) "%s %s" % (serv.product, serv.version))
serv.extrainfo = u"misconfigured" serv.extrainfo = "misconfigured"
self.assertEqual(serv.version_string(), 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): class ScanDiffSub(ScanDiff):
@@ -703,7 +703,7 @@ class scan_diff_xml_test(unittest.TestCase):
a.load_from_file("test-scans/empty.xml") a.load_from_file("test-scans/empty.xml")
b = Scan() b = Scan()
b.load_from_file("test-scans/simple.xml") b.load_from_file("test-scans/simple.xml")
f = StringIO.StringIO() f = io.StringIO()
self.scan_diff = ScanDiffXML(a, b, f) self.scan_diff = ScanDiffXML(a, b, f)
self.scan_diff.output() self.scan_diff.output()
self.xml = f.getvalue() self.xml = f.getvalue()
@@ -712,8 +712,8 @@ class scan_diff_xml_test(unittest.TestCase):
def test_well_formed(self): def test_well_formed(self):
try: try:
document = xml.dom.minidom.parseString(self.xml) document = xml.dom.minidom.parseString(self.xml)
except Exception, e: except Exception as e:
self.fail(u"Parsing XML diff output caused the exception: %s" self.fail("Parsing XML diff output caused the exception: %s"
% str(e)) % str(e))
@@ -739,8 +739,8 @@ def host_apply_diff(host, diff):
host.os = diff.host_b.os[:] host.os = diff.host_b.os[:]
if diff.extraports_changed: if diff.extraports_changed:
for state in host.extraports.keys(): for state in list(host.extraports.keys()):
for port in host.ports.values(): for port in list(host.ports.values()):
if port.state == state: if port.state == state:
del host.ports[port.spec] del host.ports[port.spec]
host.extraports = diff.host_b.extraports.copy() host.extraports = diff.host_b.extraports.copy()

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# Ndiff # Ndiff
# #
@@ -66,15 +66,15 @@ if INSTALL_LIB is not None and is_secure_dir(INSTALL_LIB):
try: try:
import ndiff import ndiff
except ImportError, e: except ImportError as e:
print >> sys.stderr, """\ print("""\
Could not import the ndiff module: %s. 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: for dir in sys.path:
print >> sys.stderr, " %s" % dir print(" %s" % dir, file=sys.stderr)
print >> sys.stderr, """\ print("""\
If you installed Ndiff in another directory, you may have to add the 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) sys.exit(1)
import ndiff import ndiff

View File

@@ -94,7 +94,7 @@ class checked_install(distutils.command.install.install):
self.saved_prefix = sys.prefix self.saved_prefix = sys.prefix
try: try:
distutils.command.install.install.finalize_options(self) distutils.command.install.install.finalize_options(self)
except distutils.errors.DistutilsPlatformError, e: except distutils.errors.DistutilsPlatformError as e:
raise distutils.errors.DistutilsPlatformError(str(e) + """ raise distutils.errors.DistutilsPlatformError(str(e) + """
Installing your distribution's python-dev package may solve this problem.""") Installing your distribution's python-dev package may solve this problem.""")
@@ -227,7 +227,7 @@ for dir in dirs:
uninstaller_file.close() uninstaller_file.close()
# Set exec bit for uninstaller # 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) os.chmod(uninstaller_filename, mode)
def write_installed_files(self): def write_installed_files(self):
@@ -241,7 +241,7 @@ for dir in dirs:
with open(INSTALLED_FILES_NAME, "w") as f: with open(INSTALLED_FILES_NAME, "w") as f:
for output in self.get_installed_files(): for output in self.get_installed_files():
assert "\n" not in output assert "\n" not in output
print >> f, output print(output, file=f)
class my_uninstall(distutils.cmd.Command): class my_uninstall(distutils.cmd.Command):
@@ -263,7 +263,7 @@ class my_uninstall(distutils.cmd.Command):
# Read the list of installed files. # Read the list of installed files.
try: try:
f = open(INSTALLED_FILES_NAME, "r") f = open(INSTALLED_FILES_NAME, "r")
except IOError, e: except IOError as e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
log.error("Couldn't open the installation record '%s'. " log.error("Couldn't open the installation record '%s'. "
"Have you installed yet?" % INSTALLED_FILES_NAME) "Have you installed yet?" % INSTALLED_FILES_NAME)
@@ -286,7 +286,7 @@ class my_uninstall(distutils.cmd.Command):
try: try:
if not self.dry_run: if not self.dry_run:
os.remove(file) os.remove(file)
except OSError, e: except OSError as e:
log.error(str(e)) log.error(str(e))
# Delete the directories. First reverse-sort the normalized paths by # Delete the directories. First reverse-sort the normalized paths by
# length so that child directories are deleted before their parents. # 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) log.info("Removing the directory '%s'." % dir)
if not self.dry_run: if not self.dry_run:
os.rmdir(dir) os.rmdir(dir)
except OSError, e: except OSError as e:
if e.errno == errno.ENOTEMPTY: if e.errno == errno.ENOTEMPTY:
log.info("Directory '%s' not empty; not removing." % dir) log.info("Directory '%s' not empty; not removing." % dir)
else: else:
log.error(str(e)) log.error(str(e))
distutils.core.setup(name=u"ndiff", scripts=[u"scripts/ndiff"], distutils.core.setup(name="ndiff", scripts=["scripts/ndiff"],
py_modules=[u"ndiff"], py_modules=["ndiff"],
data_files=[(u"share/man/man1", [u"docs/ndiff.1"])], data_files=[("share/man/man1", ["docs/ndiff.1"])],
cmdclass={ cmdclass={
"install_egg_info": null_command, "install_egg_info": null_command,
"install": checked_install, "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 # Anonymize an Nmap XML file, replacing host name and IP addresses with random
# anonymous ones. Anonymized names will be consistent between runs of the # anonymous ones. Anonymized names will be consistent between runs of the
@@ -20,20 +20,20 @@ r = random.Random()
def hash(s): def hash(s):
digest = hashlib.sha512(s).hexdigest() digest = hashlib.sha512(s.encode()).hexdigest()
return int(digest, 16) return int(digest, 16)
def anonymize_mac_address(addr): def anonymize_mac_address(addr):
r.seed(hash(addr)) r.seed(hash(addr))
nums = (0, 0, 0) + tuple(r.randrange(256) for i in range(3)) 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): def anonymize_ipv4_address(addr):
r.seed(hash(addr)) r.seed(hash(addr))
nums = (10,) + tuple(r.randrange(256) for i in range(3)) 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): def anonymize_ipv6_address(addr):
@@ -41,7 +41,7 @@ def anonymize_ipv6_address(addr):
# RFC 4193. # RFC 4193.
nums = (0xFD00 + r.randrange(256),) nums = (0xFD00 + r.randrange(256),)
nums = nums + tuple(r.randrange(65536) for i in range(7)) 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. # Maps to memoize address and host name conversions.
hostname_map = {} hostname_map = {}
@@ -54,11 +54,11 @@ def anonymize_hostname(name):
LETTERS = "acbdefghijklmnopqrstuvwxyz" LETTERS = "acbdefghijklmnopqrstuvwxyz"
r.seed(hash(name)) r.seed(hash(name))
length = r.randrange(5, 10) length = r.randrange(5, 10)
prefix = u"".join(r.sample(LETTERS, length)) prefix = "".join(r.sample(LETTERS, length))
num = r.randrange(1000) 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: 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] return hostname_map[name]
mac_re = re.compile(r'\b([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}\b') 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: else:
assert False assert False
if VERBOSE: 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] return address_map[addr]