From b1148823a6365c89fae6f3077a33275c2fdc6fa1 Mon Sep 17 00:00:00 2001 From: Michael Weiser Date: Mon, 8 Apr 2019 13:58:32 +0000 Subject: Avoid the str() constructor Do not force string representations of stuff using the str() constructor because in almost all cases it is unnecessary and can cause funky encoding problems, most notably the notorious: UnicodeEncodeError: 'ascii' codec can't encode character '\ua000' in position 0: ordinal not in range(128) Instead, rely on python to convert the input object to the required target type, oblivious of the actual type until we do input/output. --- peekaboo/config.py | 6 +++--- peekaboo/queuing.py | 8 ++++---- peekaboo/ruleset/rules.py | 4 ++-- peekaboo/sample.py | 4 ++-- peekaboo/toolbox/cuckoo.py | 8 ++++---- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/peekaboo/config.py b/peekaboo/config.py index e98b929..f99c398 100644 --- a/peekaboo/config.py +++ b/peekaboo/config.py @@ -247,7 +247,7 @@ class PeekabooConfig(object): # pylint: disable=too-many-instance-attributes if not option.startswith('_'): settings[option] = value - return '' % str(settings) + return '' % settings __repr__ = __str__ @@ -333,7 +333,7 @@ class PeekabooRulesetConfig(object): return config.get('enabled', True) def __str__(self): - return str('' % - (self.config_file, self.ruleset_config)) + return '' % \ + (self.config_file, self.ruleset_config) __repr__ = __str__ diff --git a/peekaboo/queuing.py b/peekaboo/queuing.py index ea21d97..c4cedd7 100644 --- a/peekaboo/queuing.py +++ b/peekaboo/queuing.py @@ -100,7 +100,7 @@ class JobQueue: @raises Full: if the queue is full. """ sample_hash = sample.sha256sum - sample_str = str(sample) + sample_str = "%s" % sample duplicate = None cluster_duplicate = None resubmit = None @@ -180,7 +180,7 @@ class JobQueue: return False if locked: - sample_str = str(sample_duplicates[0]) + sample_str = "%s" % sample_duplicates[0] if self.duplicates.get(sample_hash) is not None: logger.error("Possible backlog corruption for sample " "%s! Please file a bug report. Trying to " @@ -237,7 +237,7 @@ class JobQueue: # submit all samples which have accumulated in the backlog for s in self.duplicates[sample_hash]['duplicates']: - submitted_duplicates.append(str(s)) + submitted_duplicates.append("%s" % s) self.jobs.put(s, True, self.queue_timeout) sample = self.duplicates[sample_hash]['master'] @@ -246,7 +246,7 @@ class JobQueue: except PeekabooDatabaseError as dberr: logger.error(dberr) - sample_str = str(sample) + sample_str = "%s" % sample del self.duplicates[sample_hash] logger.debug("Cleared sample %s from in-flight list" % sample_str) diff --git a/peekaboo/ruleset/rules.py b/peekaboo/ruleset/rules.py index f112a70..e7eff77 100644 --- a/peekaboo/ruleset/rules.py +++ b/peekaboo/ruleset/rules.py @@ -159,7 +159,7 @@ class FileTypeOnGreylistRule(Rule): return self.result(Result.unknown, _("File type is not on the list of types to " - "analyse (%s)") % (str(sample.mimetypes)), + "analyse (%s)") % sample.mimetypes, False) @@ -248,7 +248,7 @@ class CuckooEvilSigRule(CuckooRule): # check if there is a "bad" signatures and return bad matched_bad_sigs = [] for sig in bad_sigs: - match = re.search(sig, str(sigs)) + match = re.search(sig, "\n".join(sigs)) if match: matched_bad_sigs.append(sig) diff --git a/peekaboo/sample.py b/peekaboo/sample.py index 7449e79..f6ae9b6 100644 --- a/peekaboo/sample.py +++ b/peekaboo/sample.py @@ -295,8 +295,8 @@ class Sample(object): """ Add a rule result to the sample. This also adds a message about this to the report and updates the overall analysis result (so far). """ - logger.debug('Adding rule result %s' % str(res)) - self.__report.append(_("File \"%s\": %s") % (self.__filename, str(res))) + logger.debug('Adding rule result %s', res) + self.__report.append(_("File \"%s\": %s") % (self.__filename, res)) logger.debug("Current overall result: %s, new rule result: %s", self.__result, res.result) diff --git a/peekaboo/toolbox/cuckoo.py b/peekaboo/toolbox/cuckoo.py index 9b2372e..0ec59fe 100644 --- a/peekaboo/toolbox/cuckoo.py +++ b/peekaboo/toolbox/cuckoo.py @@ -328,11 +328,11 @@ class CuckooServer(protocol.ProcessProtocol): def outReceived(self, data): """ on receiving output on STDOUT from Cuckoo """ - logger.debug('STDOUT %s' % str(data)) + logger.debug('STDOUT %s', data) def errReceived(self, data): """ on receiving output on STDERR from Cuckoo """ - logger.debug('STDERR %s' % str(data.replace('\n', ''))) + logger.debug('STDERR %s', data.replace('\n', '')) # # FILE SUBMITTED @@ -371,11 +371,11 @@ class CuckooServer(protocol.ProcessProtocol): self.cuckoo.shut_down(1) def processExited(self, reason): - logger.info("Cuckoo exited with status %s" % str(reason.value.exitCode)) + logger.info("Cuckoo exited with status %s", reason.value.exitCode) self.cuckoo.shut_down() def processEnded(self, reason): - logger.info("Cuckoo ended with status %s" % str(reason.value.exitCode)) + logger.info("Cuckoo ended with status %s", reason.value.exitCode) self.cuckoo.shut_down() -- cgit v1.2.3