1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 21:21:31 +00:00

Ensure UTF-8 encoding used throughout zenmap

This commit is contained in:
dmiller
2024-04-26 17:25:40 +00:00
parent 480803e24a
commit c840e236cb
12 changed files with 48 additions and 36 deletions

View File

@@ -93,7 +93,7 @@ import sys
from string import Template from string import Template
from zenmapCore.Version import * from zenmapCore.Version import *
from zenmapCore.Name import * from zenmapCore.Name import *
with open(sys.argv[1],"r") as f: with open(sys.argv[1],"r",encoding="utf-8") as f:
sys.stdout.write(Template(f.read()).substitute( sys.stdout.write(Template(f.read()).substitute(
VERSION=VERSION, VERSION=VERSION,
APP_WEB_SITE=APP_WEB_SITE, APP_WEB_SITE=APP_WEB_SITE,

View File

@@ -73,14 +73,14 @@ NAME_PY = os.path.join("zenmapCore", "Name.py")
def update_date(base_dir): def update_date(base_dir):
name_file = os.path.join(base_dir, NAME_PY) name_file = os.path.join(base_dir, NAME_PY)
print(">>> Updating %s" % name_file) print(">>> Updating %s" % name_file)
nf = open(name_file, "r") nf = open(name_file, "r", encoding="utf-8")
ncontent = nf.read() ncontent = nf.read()
nf.close() nf.close()
ncontent = re.sub(r'APP_COPYRIGHT *= *"Copyright 2005-....', ncontent = re.sub(r'APP_COPYRIGHT *= *"Copyright 2005-....',
'APP_COPYRIGHT = "Copyright 2005-%d' % (datetime.today().year), 'APP_COPYRIGHT = "Copyright 2005-%d' % (datetime.today().year),
ncontent) ncontent)
# Write the modified file. # Write the modified file.
nf = open(name_file, "w") nf = open(name_file, "w", encoding="utf-8")
nf.write(ncontent) nf.write(ncontent)
nf.close() nf.close()
@@ -88,7 +88,7 @@ def update_date(base_dir):
def update_version(base_dir, version): def update_version(base_dir, version):
version = re.sub(r'(?=[^0-9.])', '+', version, 1) version = re.sub(r'(?=[^0-9.])', '+', version, 1)
print(">>> Updating %s" % os.path.join(base_dir, VERSION_PY)) print(">>> Updating %s" % os.path.join(base_dir, VERSION_PY))
vf = open(os.path.join(base_dir, VERSION_PY), "w") vf = open(os.path.join(base_dir, VERSION_PY), "w", encoding="utf-8")
print("VERSION = \"%s\"" % version, file=vf) print("VERSION = \"%s\"" % version, file=vf)
vf.close() vf.close()

View File

@@ -321,5 +321,5 @@ if __name__ == "__main__":
root = reader.get_root() root = reader.get_root()
writer = XMLWriter(open("test.xml", 'w'), root) writer = XMLWriter(open("test.xml", 'wb'), root)
writer.write() writer.write()

View File

@@ -286,7 +286,7 @@ class NetworkInventory(object):
"""Saves the scan with the given list index into a file with a given """Saves the scan with the given list index into a file with a given
path. With format = "xml", saves Nmap XML; otherwise saves plain text path. With format = "xml", saves Nmap XML; otherwise saves plain text
output.""" output."""
f = open(path, 'w') f = open(path, 'wb')
if format == "xml": if format == "xml":
self.get_scans()[index].write_xml(f) self.get_scans()[index].write_xml(f)
self.filenames[self.get_scans()[index]] = f self.filenames[self.get_scans()[index]] = f
@@ -352,7 +352,7 @@ class NetworkInventory(object):
self._generate_filenames(path) self._generate_filenames(path)
for scan, filename in self.filenames.items(): for scan, filename in self.filenames.items():
f = open(os.path.join(path, filename), "w") f = open(os.path.join(path, filename), "wb")
scan.write_xml(f) scan.write_xml(f)
f.close() f.close()

View File

@@ -739,7 +739,7 @@ class NmapParserSAX(ParserBasics, ContentHandler):
def parse_file(self, filename): def parse_file(self, filename):
"""Parse an Nmap XML file from the named file.""" """Parse an Nmap XML file from the named file."""
with open(filename, "r") as f: with open(filename, "rb") as f:
self.parse(f) self.parse(f)
self.filename = filename self.filename = filename
@@ -1002,12 +1002,12 @@ class NmapParserSAX(ParserBasics, ContentHandler):
f.""" f."""
if self.nmap_output == "": if self.nmap_output == "":
return return
f.write(self.nmap_output) f.write(self.nmap_output.encode('utf-8','xmlcharrefreplace'))
def write_xml(self, f): def write_xml(self, f):
"""Write the XML representation of this object to the file-like object """Write the XML representation of this object to the file-like object
f.""" f."""
writer = XMLGenerator(f) writer = XMLGenerator(f, encoding='utf-8')
writer.startDocument() writer.startDocument()
if self.xml_stylesheet_data is not None: if self.xml_stylesheet_data is not None:
writer.processingInstruction( writer.processingInstruction(
@@ -1033,7 +1033,7 @@ class NmapParserSAX(ParserBasics, ContentHandler):
def write_xml_to_file(self, filename): def write_xml_to_file(self, filename):
"""Write the XML representation of this scan to the file whose name is """Write the XML representation of this scan to the file whose name is
given.""" given."""
fd = open(filename, "w") fd = open(filename, "wb")
self.write_xml(fd) self.write_xml(fd)
fd.close() fd.close()

View File

@@ -199,7 +199,7 @@ def return_if_exists(path, create=False):
if os.path.exists(path): if os.path.exists(path):
return path return path
elif create: elif create:
f = open(path, "w") f = open(path, "wb")
f.close() f.close()
return path return path
raise Exception("File '%s' does not exist or could not be found!" % path) raise Exception("File '%s' does not exist or could not be found!" % path)

View File

@@ -76,17 +76,21 @@ class RecentScans(object):
self.using_file = True self.using_file = True
# Recovering saved targets # Recovering saved targets
recent_file = open(self.recent_scans_file, "r") for enc in ('utf-8', None):
try:
with open(self.recent_scans_file, "r", encoding=enc) as recent_file:
self.temp_list = [ self.temp_list = [
t for t in recent_file.read().split(";") t for t in recent_file.read().split(";")
if t != "" and t != "\n"] if t != "" and t != "\n"]
recent_file.close() except UnicodeDecodeError:
continue
break
else: else:
self.using_file = False self.using_file = False
def save(self): def save(self):
if self.using_file: if self.using_file:
recent_file = open(self.recent_scans_file, "w") recent_file = open(self.recent_scans_file, "w", encoding="utf-8")
recent_file.write(";".join(self.temp_list)) recent_file.write(";".join(self.temp_list))
recent_file.close() recent_file.close()

View File

@@ -84,7 +84,7 @@ class ScriptDB (object):
self.lineno = 1 self.lineno = 1
self.line = "" self.line = ""
with open(script_db_path, "r") as self.f: with open(script_db_path, "r", encoding="utf-8") as self.f:
self.entries_list = self.parse() self.entries_list = self.parse()
def syntax_error(self, message): def syntax_error(self, message):
@@ -296,20 +296,20 @@ class ScriptMetadata (object):
self.get_string_variable(filename, "author")] self.get_string_variable(filename, "author")]
filepath = os.path.join(self.scripts_dir, filename) filepath = os.path.join(self.scripts_dir, filename)
with open(filepath, "r") as f: with open(filepath, "r", encoding="utf-8") as f:
for tag_name, tag_text in nsedoc_tags_iter(f): for tag_name, tag_text in nsedoc_tags_iter(f):
if tag_name == "output" and not entry.output: if tag_name == "output" and not entry.output:
entry.output = tag_text entry.output = tag_text
elif tag_name == "usage" and not entry.usage: elif tag_name == "usage" and not entry.usage:
entry.usage = tag_text entry.usage = tag_text
except IOError as e: except (IOError, UnicodeError) as e:
entry.description = "Error getting metadata: {}".format(e) entry.description = "Error getting metadata: {}".format(e)
return entry return entry
@staticmethod @staticmethod
def get_file_contents(filename): def get_file_contents(filename):
with open(filename, "r") as f: with open(filename, "r", encoding="utf-8") as f:
contents = f.read() contents = f.read()
return contents return contents
@@ -343,7 +343,7 @@ class ScriptMetadata (object):
@staticmethod @staticmethod
def get_requires(filename): def get_requires(filename):
with open(filename, "r") as f: with open(filename, "r", encoding="utf-8") as f:
requires = ScriptMetadata.get_requires_from_file(f) requires = ScriptMetadata.get_requires_from_file(f)
return requires return requires
@@ -359,7 +359,7 @@ class ScriptMetadata (object):
@staticmethod @staticmethod
def get_script_args(filename): def get_script_args(filename):
with open(filename, "r") as f: with open(filename, "r", encoding="utf-8") as f:
args = ScriptMetadata.get_script_args_from_file(f) args = ScriptMetadata.get_script_args_from_file(f)
return args return args
@@ -414,8 +414,11 @@ class ScriptMetadata (object):
else: else:
libname = filename libname = filename
try:
self.library_arguments[libname] = self.get_script_args(filepath) self.library_arguments[libname] = self.get_script_args(filepath)
self.library_requires[libname] = self.get_requires(filepath) self.library_requires[libname] = self.get_requires(filepath)
except (IOError, UnicodeError) as e:
log.debug("Unable to process {}: {}".format(libname, e))
def get_script_entries(scripts_dir, nselib_dir): def get_script_entries(scripts_dir, nselib_dir):
@@ -424,7 +427,8 @@ def get_script_entries(scripts_dir, nselib_dir):
metadata = ScriptMetadata(scripts_dir, nselib_dir) metadata = ScriptMetadata(scripts_dir, nselib_dir)
try: try:
scriptdb = ScriptDB(os.path.join(scripts_dir, "script.db")) scriptdb = ScriptDB(os.path.join(scripts_dir, "script.db"))
except IOError: except (IOError, UnicodeError) as e:
log.debug("Unable to process script.db: {}".format(e))
return [] return []
entries = [] entries = []
for dbentry in scriptdb.get_entries_list(): for dbentry in scriptdb.get_entries_list():

View File

@@ -77,17 +77,21 @@ class TargetList(object):
self.using_file = True self.using_file = True
# Recovering saved targets # Recovering saved targets
target_file = open(self.target_list_file, "r") for enc in ('utf-8', None):
try:
with open(self.target_list_file, "r", encoding=enc) as target_file:
self.temp_list = [ self.temp_list = [
t for t in target_file.read().split(";") t for t in target_file.read().split(";")
if t != "" and t != "\n"] if t != "" and t != "\n"]
target_file.close() except UnicodeDecodeError:
continue
break
else: else:
self.using_file = False self.using_file = False
def save(self): def save(self):
if self.using_file: if self.using_file:
target_file = open(self.target_list_file, "w") target_file = open(self.target_list_file, "w", encoding="utf-8")
target_file.write(";".join(self.temp_list)) target_file.write(";".join(self.temp_list))
target_file.close() target_file.close()

View File

@@ -87,7 +87,7 @@ class UmitConfigParser(ConfigParser):
if self.filenames: if self.filenames:
log.debug("saving to %s" % self.filenames) log.debug("saving to %s" % self.filenames)
try: try:
with open(self.filenames, 'w') as fp: with open(self.filenames, 'w', encoding="utf-8") as fp:
self.write(fp) self.write(fp)
except Exception as e: except Exception as e:
self.failed = e self.failed = e

View File

@@ -1 +1 @@
VERSION = "7.95" VERSION = "7.95+SVN"

View File

@@ -48,7 +48,7 @@ if directory is not None:
os.chdir(directory) os.chdir(directory)
for fn in filenames: for fn in filenames:
with open(fn, "r") as f: with open(fn, "rb") as f:
parser = xml.sax.make_parser() parser = xml.sax.make_parser()
parser.setContentHandler(Handler()) parser.setContentHandler(Handler())
parser.parse(f) parser.parse(f)