From f2148db70bc906b85e0be06fa5c8c0f25ff6d40a Mon Sep 17 00:00:00 2001 From: TRGamer-tech Date: Fri, 6 Aug 2021 10:50:46 +0200 Subject: [PATCH 1/5] Add cp850 encoding to subprocess --- monkey/infection_monkey/network/ping_scanner.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/monkey/infection_monkey/network/ping_scanner.py b/monkey/infection_monkey/network/ping_scanner.py index 2f2b2719b..d01a9c56b 100644 --- a/monkey/infection_monkey/network/ping_scanner.py +++ b/monkey/infection_monkey/network/ping_scanner.py @@ -43,11 +43,13 @@ class PingScanner(HostScanner, HostFinger): if not "win32" == sys.platform: timeout /= 1000 + Encoding = "cp850" sub_proc = subprocess.Popen( ["ping", PING_COUNT_FLAG, "1", PING_TIMEOUT_FLAG, str(timeout), host.ip_addr], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, + encoding=Encoding, ) output = " ".join(sub_proc.communicate()) From 769dd67b668ed737a97fc058b03f58e1167b8ddd Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 13 Aug 2021 08:39:08 -0400 Subject: [PATCH 2/5] Agent: Automatically select correct output encoding for ping command --- monkey/infection_monkey/network/ping_scanner.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/monkey/infection_monkey/network/ping_scanner.py b/monkey/infection_monkey/network/ping_scanner.py index d01a9c56b..f43412064 100644 --- a/monkey/infection_monkey/network/ping_scanner.py +++ b/monkey/infection_monkey/network/ping_scanner.py @@ -43,13 +43,12 @@ class PingScanner(HostScanner, HostFinger): if not "win32" == sys.platform: timeout /= 1000 - Encoding = "cp850" sub_proc = subprocess.Popen( ["ping", PING_COUNT_FLAG, "1", PING_TIMEOUT_FLAG, str(timeout), host.ip_addr], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, - encoding=Encoding, + encoding=os.device_encoding(1), ) output = " ".join(sub_proc.communicate()) From ce278297533654784aa1d4fcae4d8f406064599e Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 13 Aug 2021 08:41:11 -0400 Subject: [PATCH 3/5] Update CHANGELOG.md with fix for #1175 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a78318cf..e9388b192 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ Changelog](https://keepachangelog.com/en/1.0.0/). ### Fixed - Misaligned buttons and input fields on exploiter and network configuration pages. #1353 +- Crash when unexpected character encoding is used by ping command on German + language systems. #1175 + ## [1.11.0] - 2021-08-13 ### Added From 5f9e507dc7d98ddcd16a83db64aefe04ce9b5c85 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 17 Aug 2021 11:24:12 -0400 Subject: [PATCH 4/5] Agent: Add debug logging to get_host_fingerprint() --- monkey/infection_monkey/network/ping_scanner.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/monkey/infection_monkey/network/ping_scanner.py b/monkey/infection_monkey/network/ping_scanner.py index f43412064..38195e44b 100644 --- a/monkey/infection_monkey/network/ping_scanner.py +++ b/monkey/infection_monkey/network/ping_scanner.py @@ -43,14 +43,20 @@ class PingScanner(HostScanner, HostFinger): 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)}") + sub_proc = subprocess.Popen( - ["ping", PING_COUNT_FLAG, "1", PING_TIMEOUT_FLAG, str(timeout), host.ip_addr], + ping_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, - encoding=os.device_encoding(1), + encoding=encoding, ) + LOG.debug(f"Retrieving ping command output using {encoding} encoding") output = " ".join(sub_proc.communicate()) regex_result = self._ttl_regex.search(output) if regex_result: From 54e519eeaa89cc5bd0ec819d253a9627098c0f32 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 19 Aug 2021 12:55:40 -0400 Subject: [PATCH 5/5] 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")