From 54e519eeaa89cc5bd0ec819d253a9627098c0f32 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 19 Aug 2021 12:55:40 -0400 Subject: [PATCH] Agent: Gracefully handle character decode errors in ping command --- monkey/infection_monkey/network/ping_scanner.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/monkey/infection_monkey/network/ping_scanner.py b/monkey/infection_monkey/network/ping_scanner.py index 38195e44b..64cf3794f 100644 --- a/monkey/infection_monkey/network/ping_scanner.py +++ b/monkey/infection_monkey/network/ping_scanner.py @@ -38,22 +38,24 @@ class PingScanner(HostScanner, HostFinger): ) def get_host_fingerprint(self, host): - timeout = self._config.ping_scan_timeout if not "win32" == sys.platform: timeout /= 1000 ping_cmd = ["ping", PING_COUNT_FLAG, "1", PING_TIMEOUT_FLAG, str(timeout), host.ip_addr] - encoding = os.device_encoding(1) - LOG.debug(f"Running ping command: {' '.join(ping_cmd)}") + # If stdout is not connected to a terminal (i.e. redirected to a pipe or file), the result + # of os.device_encoding(1) will be None. Setting errors="backslashreplace" prevents a crash + # in this case. See #1175 and #1403 for more information. + encoding = os.device_encoding(1) sub_proc = subprocess.Popen( ping_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, encoding=encoding, + errors="backslashreplace", ) LOG.debug(f"Retrieving ping command output using {encoding} encoding")