fix a number of unicode-related issues #1295 (thanks consen)

The PR identified the issue, but resolved it incorrectly. Instead,
properly log unicode strings in the Cuckoo Guest already. We should
still feature something to avoid hard crashes on the Host, though.
This commit is contained in:
Jurriaan Bremer
2017-06-24 04:40:25 +02:00
parent 06008813e9
commit 0cd42b7cb7
4 changed files with 58 additions and 6 deletions
+1
View File
@@ -202,4 +202,5 @@ da39a3ee5e6b4b0d3255bfef95601890afd80709 yara/scripts/.gitignore
da39a3ee5e6b4b0d3255bfef95601890afd80709 yara/shellcode/.gitignore
# TBD release
9486762bbe1fa60c67b17887f13be0b37ebfcfcd analyzer/windows/lib/common/results.py
b70c7d3e30781a4858f20d0276b6bbccd5993c37 analyzer/windows/modules/packages/pdf.py
@@ -1,5 +1,5 @@
# Copyright (C) 2010-2013 Claudio Guarnieri.
# Copyright (C) 2014-2016 Cuckoo Foundation.
# Copyright (C) 2013 Claudio Guarnieri.
# Copyright (C) 2014-2017 Cuckoo Foundation.
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.
@@ -26,7 +26,7 @@ def upload_to_host(file_path, dump_path, pids=[]):
nc.send(buf, retry=False)
buf = infd.read(BUFSIZE)
except Exception as e:
log.error("Exception uploading file %s to host: %s", file_path, e)
log.error("Exception uploading file %r to host: %s", file_path, e)
finally:
if infd:
infd.close()
@@ -83,11 +83,12 @@ class NetlogConnection(object):
class NetlogFile(NetlogConnection):
def init(self, dump_path, filepath=None, pids=[]):
if filepath:
self.proto = "FILE 2\n{0}\n{1}\n{2}\n".format(
dump_path, filepath, " ".join(pids)
self.proto = "FILE 2\n%s\n%s\n%s\n" % (
dump_path.encode("utf8"), filepath.encode("utf8"),
" ".join(pids)
)
else:
self.proto = "FILE\n{0}\n".format(dump_path)
self.proto = "FILE\n%s\n" % dump_path.encode("utf8")
self.connect()
+47
View File
@@ -0,0 +1,47 @@
# Copyright (C) 2017 Cuckoo Foundation.
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.
import logging
import mock
import socket
from lib.common.results import NetlogFile, upload_to_host
from lib.core.startup import init_logging
@mock.patch("socket.create_connection")
def test_netlogfile_init(p):
nf = NetlogFile()
nf.init(u"dump-\u202e.exe")
nf.sock = None
nf.init(u"dump-\u202e.exe", u"file-\u202e.exe")
a, b = p.return_value.sendall.call_args_list
assert a[0][0] == str(a[0][0])
assert b[0][0] == str(b[0][0])
def test_upload_to_host():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 0))
s.listen(5)
with open("analysis.conf", "wb") as f:
f.write("[hello]\nip = %s\nport = %d" % s.getsockname())
handlers = logging.getLogger().handlers[:]
init_logging()
# Test file not found exception.
upload_to_host(u"\u202ethisis404.exe", "1.exe")
c, _ = s.accept()
assert "Exception uploading file u'\\u202e" in c.recv(0x1000)
c, _ = s.accept()
assert "FILE 2\n1.exe\n\xe2\x80\xaethisis404.exe\n" in c.recv(0x1000)
# Test correct upload.
upload_to_host(__file__, "1.py", ["1", "2", "3"])
c, _ = s.accept()
assert c.recv(0x1000).startswith(
"FILE 2\n1.py\n%s\n1 2 3\n# Copyright (C" % __file__
)
logging.getLogger().handlers = handlers
+3
View File
@@ -2,6 +2,7 @@
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.
import logging
import socket
from lib.api.process import Process
@@ -15,8 +16,10 @@ def test_execute_correct_logging():
with open("analysis.conf", "wb") as f:
f.write("[hello]\nip = %s\nport = %d" % s.getsockname())
handlers = logging.getLogger().handlers[:]
init_logging()
Process().execute(u"unicodefile\u202ethatdoesnotexist")
logging.getLogger().handlers = handlers
c, _ = s.accept()
assert "202e" in c.recv(0x1000)