From 480f6ccc7f21d1bc086e6217b49dffe5ce3dbd81 Mon Sep 17 00:00:00 2001 From: Shreya Date: Mon, 5 Apr 2021 15:46:45 +0530 Subject: [PATCH 01/12] Modify configuration for flake8 --- ci_scripts/flake8_linter_check.ini | 8 +++++--- ci_scripts/flake8_syntax_check.ini | 3 +-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/ci_scripts/flake8_linter_check.ini b/ci_scripts/flake8_linter_check.ini index b8daeaf7027..008f0fa8271 100644 --- a/ci_scripts/flake8_linter_check.ini +++ b/ci_scripts/flake8_linter_check.ini @@ -1,11 +1,13 @@ [flake8] ## Warn about linter issues. -exclude = ../monkey/monkey_island/cc/ui, - ../monkey/common/cloud +exclude = ../monkey/monkey_island/cc/ui show-source = True max-complexity = 10 -max-line-length = 127 +max-line-length = 100 + +### ignore "whitespace before ':'" and "line break before binary operator" for compatibility with black +extend-ignore = E203, W503 ### --statistics Count the number of occurrences of each error/warning code and print a report. statistics = True diff --git a/ci_scripts/flake8_syntax_check.ini b/ci_scripts/flake8_syntax_check.ini index 969379326f3..55e470e00a7 100644 --- a/ci_scripts/flake8_syntax_check.ini +++ b/ci_scripts/flake8_syntax_check.ini @@ -2,8 +2,7 @@ ## Check syntax errors and fail the build if any are found. exclude = - ../monkey/monkey_island/cc/ui, - ../monkey/common/cloud + ../monkey/monkey_island/cc/ui select = E901, E999, From 278326e4e474c31d479d48b86989be9c625c36f7 Mon Sep 17 00:00:00 2001 From: Shreya Date: Mon, 5 Apr 2021 17:46:43 +0530 Subject: [PATCH 02/12] Fix W293 warnings --- monkey/infection_monkey/exploit/web_rce.py | 2 +- monkey/infection_monkey/post_breach/actions/schedule_jobs.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/infection_monkey/exploit/web_rce.py b/monkey/infection_monkey/exploit/web_rce.py index d12e4eaa927..c5563ef72bf 100644 --- a/monkey/infection_monkey/exploit/web_rce.py +++ b/monkey/infection_monkey/exploit/web_rce.py @@ -512,7 +512,7 @@ def get_target_url(self): :return: a vulnerable URL """ return self.vulnerable_urls[0] - + def are_vulnerable_urls_sufficient(self): """ Determine whether the number of vulnerable URLs is sufficient in order to perform the full attack. diff --git a/monkey/infection_monkey/post_breach/actions/schedule_jobs.py b/monkey/infection_monkey/post_breach/actions/schedule_jobs.py index 97ad75923f4..fda4a737909 100644 --- a/monkey/infection_monkey/post_breach/actions/schedule_jobs.py +++ b/monkey/infection_monkey/post_breach/actions/schedule_jobs.py @@ -15,7 +15,7 @@ def __init__(self): super(ScheduleJobs, self).__init__(name=POST_BREACH_JOB_SCHEDULING, linux_cmd=' '.join(linux_cmds), windows_cmd=windows_cmds) - + def run(self): super(ScheduleJobs, self).run() remove_scheduled_jobs() From 7bfdc3c7e5c8b307c2d81d68a08ec9fd2f67a307 Mon Sep 17 00:00:00 2001 From: Shreya Date: Mon, 5 Apr 2021 18:09:38 +0530 Subject: [PATCH 03/12] Fix W605 warnings --- monkey/common/network/network_utils.py | 2 +- monkey/infection_monkey/exploit/web_rce.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/common/network/network_utils.py b/monkey/common/network/network_utils.py index e99d0cf2bf7..eaa2bc19585 100644 --- a/monkey/common/network/network_utils.py +++ b/monkey/common/network/network_utils.py @@ -16,5 +16,5 @@ def get_host_from_network_location(network_location: str) -> str: def remove_port(url): parsed = urlparse(url) with_port = f'{parsed.scheme}://{parsed.netloc}' - without_port = re.sub(':[0-9]+(?=$|\/)', '', with_port) + without_port = re.sub(':[0-9]+(?=$|/)', '', with_port) return without_port diff --git a/monkey/infection_monkey/exploit/web_rce.py b/monkey/infection_monkey/exploit/web_rce.py index c5563ef72bf..069cbcada3d 100644 --- a/monkey/infection_monkey/exploit/web_rce.py +++ b/monkey/infection_monkey/exploit/web_rce.py @@ -235,7 +235,7 @@ def get_host_arch(self, url): resp = self.exploit(url, GET_ARCH_LINUX) if resp: # Pulls architecture string - arch = re.search('(?<=Architecture:)\s+(\w+)', resp) + arch = re.search(r'(?<=Architecture:)\s+(\w+)', resp) try: arch = arch.group(1) except AttributeError: From d0168dfb9ead5b4e314dd0bf4e61a8c9b968f462 Mon Sep 17 00:00:00 2001 From: Shreya Date: Mon, 5 Apr 2021 18:22:32 +0530 Subject: [PATCH 04/12] Fix E722 warnings --- monkey/common/utils/mongo_utils.py | 4 ++-- monkey/infection_monkey/exploit/smbexec.py | 2 +- .../exploit/tools/smb_tools.py | 6 +++--- .../exploit/tools/wmi_tools.py | 2 +- monkey/infection_monkey/network/firewall.py | 18 +++++++++--------- monkey/infection_monkey/transport/http.py | 2 +- monkey/infection_monkey/transport/tcp.py | 4 ++-- 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/monkey/common/utils/mongo_utils.py b/monkey/common/utils/mongo_utils.py index 66f60647395..6d784d7ac50 100644 --- a/monkey/common/utils/mongo_utils.py +++ b/monkey/common/utils/mongo_utils.py @@ -37,12 +37,12 @@ def fix_obj_for_mongo(o): # ISWbemObjectEx interface. Class Uint8Array ? if str(o._oleobj_.GetTypeInfo().GetTypeAttr().iid) == "{269AD56A-8A67-4129-BC8C-0506DCFE9880}": return o.Value - except: + except Exception: pass try: return o.GetObjectText_() - except: + except Exception: pass return repr(o) diff --git a/monkey/infection_monkey/exploit/smbexec.py b/monkey/infection_monkey/exploit/smbexec.py index a9776136b4e..193b81ecf87 100644 --- a/monkey/infection_monkey/exploit/smbexec.py +++ b/monkey/infection_monkey/exploit/smbexec.py @@ -148,7 +148,7 @@ def _exploit_host(self): try: scmr.hRStartServiceW(scmr_rpc, service) status = ScanStatus.USED - except: + except Exception: status = ScanStatus.SCANNED pass T1035Telem(status, UsageEnum.SMB).send() diff --git a/monkey/infection_monkey/exploit/tools/smb_tools.py b/monkey/infection_monkey/exploit/tools/smb_tools.py index e5185b266cd..a68d52471b3 100644 --- a/monkey/infection_monkey/exploit/tools/smb_tools.py +++ b/monkey/infection_monkey/exploit/tools/smb_tools.py @@ -42,7 +42,7 @@ def copy_file(host, src_path, dst_path, username, password, lm_hash='', ntlm_has try: smb.logoff() - except: + except Exception: pass return None @@ -134,7 +134,7 @@ def copy_file(host, src_path, dst_path, username, password, lm_hash='', ntlm_has return remote_full_path LOG.debug("Remote monkey file is found but different, moving along with attack") - except: + except Exception: pass # file isn't found on remote victim, moving on try: @@ -163,7 +163,7 @@ def copy_file(host, src_path, dst_path, username, password, lm_hash='', ntlm_has finally: try: smb.logoff() - except: + except Exception: pass smb = None diff --git a/monkey/infection_monkey/exploit/tools/wmi_tools.py b/monkey/infection_monkey/exploit/tools/wmi_tools.py index e1e002d720c..f621900766d 100644 --- a/monkey/infection_monkey/exploit/tools/wmi_tools.py +++ b/monkey/infection_monkey/exploit/tools/wmi_tools.py @@ -58,7 +58,7 @@ def connect(self, host, username, password, domain=None, lmhash="", nthash=""): try: self._iWbemServices = iWbemLevel1Login.NTLMLogin('//./root/cimv2', NULL, NULL) self._dcom = dcom - except: + except Exception: dcom.disconnect() raise diff --git a/monkey/infection_monkey/network/firewall.py b/monkey/infection_monkey/network/firewall.py index a88427650ce..85191551fbb 100644 --- a/monkey/infection_monkey/network/firewall.py +++ b/monkey/infection_monkey/network/firewall.py @@ -46,7 +46,7 @@ def is_enabled(self): state = l.split()[-1].strip() return state == "ON" - except: + except Exception: return None def add_firewall_rule(self, name="Firewall", direction="in", action="allow", program=sys.executable, **kwargs): @@ -61,7 +61,7 @@ def add_firewall_rule(self, name="Firewall", direction="in", action="allow", pro return True else: return False - except: + except Exception: return None def remove_firewall_rule(self, name="Firewall", **kwargs): @@ -75,7 +75,7 @@ def remove_firewall_rule(self, name="Firewall", **kwargs): return True else: return False - except: + except Exception: return None def listen_allowed(self, **kwargs): @@ -94,7 +94,7 @@ def close(self): try: for rule in list(self._rules.keys()): self.remove_firewall_rule(name=rule) - except: + except Exception: pass @@ -114,7 +114,7 @@ def is_enabled(self): return False return state == "Enable" - except: + except Exception: return None def add_firewall_rule(self, rule='allowedprogram', name="Firewall", mode="ENABLE", program=sys.executable, @@ -131,7 +131,7 @@ def add_firewall_rule(self, rule='allowedprogram', name="Firewall", mode="ENABLE return True else: return False - except: + except Exception: return None def remove_firewall_rule(self, rule='allowedprogram', name="Firewall", mode="ENABLE", program=sys.executable, @@ -145,7 +145,7 @@ def remove_firewall_rule(self, rule='allowedprogram', name="Firewall", mode="ENA return True else: return False - except: + except Exception: return None def listen_allowed(self, **kwargs): @@ -161,14 +161,14 @@ def close(self): try: for rule in list(self._rules.values()): self.remove_firewall_rule(**rule) - except: + except Exception: pass if sys.platform == "win32": try: win_ver = int(platform.version().split('.')[0]) - except: + except Exception: win_ver = 0 if win_ver > 5: app = WinAdvFirewall() diff --git a/monkey/infection_monkey/transport/http.py b/monkey/infection_monkey/transport/http.py index 1502e844c3e..e2ed053af19 100644 --- a/monkey/infection_monkey/transport/http.py +++ b/monkey/infection_monkey/transport/http.py @@ -47,7 +47,7 @@ def do_GET(self): chunk = end_range - start_range try: self.wfile.write(f.read(chunk)) - except: + except Exception: break total += chunk start_range += chunk diff --git a/monkey/infection_monkey/transport/tcp.py b/monkey/infection_monkey/transport/tcp.py index 329ef187563..dac2a093864 100644 --- a/monkey/infection_monkey/transport/tcp.py +++ b/monkey/infection_monkey/transport/tcp.py @@ -32,13 +32,13 @@ def run(self): other = self.dest if r is self.source else self.source try: data = r.recv(READ_BUFFER_SIZE) - except: + except Exception: break if data: try: other.sendall(data) update_last_serve_time() - except: + except Exception: break self._keep_connection = True From d8e1be7d6f5f4177f5166ce75eb897c2dacde6a9 Mon Sep 17 00:00:00 2001 From: Shreya Date: Mon, 5 Apr 2021 18:35:07 +0530 Subject: [PATCH 05/12] Fix F401 warnings --- monkey/common/cloud/aws/test_aws_instance.py | 2 -- monkey/infection_monkey/exploit/smbexec.py | 3 ++- monkey/infection_monkey/network/info.py | 1 - monkey/infection_monkey/network/test_postgresql_finger.py | 1 - .../post_breach/tests/actions/test_users_custom_pba.py | 3 +-- .../telemetry/tests/attack/test_victim_host_telem.py | 2 +- monkey/monkey_island/cc/environment/environment_singleton.py | 2 +- monkey/monkey_island/cc/services/tests/test_config.py | 1 - 8 files changed, 5 insertions(+), 10 deletions(-) diff --git a/monkey/common/cloud/aws/test_aws_instance.py b/monkey/common/cloud/aws/test_aws_instance.py index 0353a0b9f0f..30f0c9d8655 100644 --- a/monkey/common/cloud/aws/test_aws_instance.py +++ b/monkey/common/cloud/aws/test_aws_instance.py @@ -1,5 +1,3 @@ -import json - import pytest import requests import requests_mock diff --git a/monkey/infection_monkey/exploit/smbexec.py b/monkey/infection_monkey/exploit/smbexec.py index 193b81ecf87..c6e2424c1b6 100644 --- a/monkey/infection_monkey/exploit/smbexec.py +++ b/monkey/infection_monkey/exploit/smbexec.py @@ -7,7 +7,8 @@ from infection_monkey.exploit.HostExploiter import HostExploiter from infection_monkey.exploit.tools.helpers import build_monkey_commandline, get_monkey_depth, get_target_monkey from infection_monkey.exploit.tools.smb_tools import SmbTools -from infection_monkey.model import DROPPER_CMDLINE_DETACHED_WINDOWS, MONKEY_CMDLINE_DETACHED_WINDOWS, VictimHost +from infection_monkey.model import (DROPPER_CMDLINE_DETACHED_WINDOWS, + MONKEY_CMDLINE_DETACHED_WINDOWS) from infection_monkey.network.smbfinger import SMBFinger from infection_monkey.network.tools import check_tcp_port from infection_monkey.telemetry.attack.t1035_telem import T1035Telem diff --git a/monkey/infection_monkey/network/info.py b/monkey/infection_monkey/network/info.py index 0aafe05402e..bc8189dc41e 100644 --- a/monkey/infection_monkey/network/info.py +++ b/monkey/infection_monkey/network/info.py @@ -1,4 +1,3 @@ -import ipaddress import itertools import socket import struct diff --git a/monkey/infection_monkey/network/test_postgresql_finger.py b/monkey/infection_monkey/network/test_postgresql_finger.py index 6eb01fecd67..bb6bdc49b3b 100644 --- a/monkey/infection_monkey/network/test_postgresql_finger.py +++ b/monkey/infection_monkey/network/test_postgresql_finger.py @@ -1,6 +1,5 @@ import pytest -import infection_monkey.network.postgresql_finger from infection_monkey.network.postgresql_finger import PostgreSQLFinger IRRELEVANT_EXCEPTION_STRING = "This is an irrelevant exception string." diff --git a/monkey/infection_monkey/post_breach/tests/actions/test_users_custom_pba.py b/monkey/infection_monkey/post_breach/tests/actions/test_users_custom_pba.py index 83af6e00aa0..5638e16ccee 100644 --- a/monkey/infection_monkey/post_breach/tests/actions/test_users_custom_pba.py +++ b/monkey/infection_monkey/post_breach/tests/actions/test_users_custom_pba.py @@ -1,7 +1,6 @@ import pytest -from infection_monkey.post_breach.actions.users_custom_pba import ( - DIR_CHANGE_LINUX, DIR_CHANGE_WINDOWS, UsersPBA) +from infection_monkey.post_breach.actions.users_custom_pba import UsersPBA MONKEY_DIR_PATH = "/dir/to/monkey/" CUSTOM_LINUX_CMD = "command-for-linux" diff --git a/monkey/infection_monkey/telemetry/tests/attack/test_victim_host_telem.py b/monkey/infection_monkey/telemetry/tests/attack/test_victim_host_telem.py index 014aadb8f57..59eefc150c1 100644 --- a/monkey/infection_monkey/telemetry/tests/attack/test_victim_host_telem.py +++ b/monkey/infection_monkey/telemetry/tests/attack/test_victim_host_telem.py @@ -2,7 +2,7 @@ import pytest -from common.utils.attack_utils import ScanStatus, UsageEnum +from common.utils.attack_utils import ScanStatus from infection_monkey.model import VictimHost from infection_monkey.telemetry.attack.victim_host_telem import VictimHostTelem diff --git a/monkey/monkey_island/cc/environment/environment_singleton.py b/monkey/monkey_island/cc/environment/environment_singleton.py index 01e83096d0b..0c7262a96b5 100644 --- a/monkey/monkey_island/cc/environment/environment_singleton.py +++ b/monkey/monkey_island/cc/environment/environment_singleton.py @@ -2,7 +2,7 @@ import monkey_island.cc.resources.auth.user_store as user_store from monkey_island.cc.environment import (EnvironmentConfig, aws, password, - standard, testing) + standard) from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH __author__ = 'itay.mizeretz' diff --git a/monkey/monkey_island/cc/services/tests/test_config.py b/monkey/monkey_island/cc/services/tests/test_config.py index 6cee39fbbff..efc04ed89bd 100644 --- a/monkey/monkey_island/cc/services/tests/test_config.py +++ b/monkey/monkey_island/cc/services/tests/test_config.py @@ -1,6 +1,5 @@ import pytest -import monkey_island.cc.services.config from monkey_island.cc.environment import Environment from monkey_island.cc.services.config import ConfigService From 3e419478afe27bf3b872d428db7355a3b2d0a8b7 Mon Sep 17 00:00:00 2001 From: Shreya Date: Mon, 5 Apr 2021 18:42:25 +0530 Subject: [PATCH 06/12] Fix F841 warnings --- monkey/infection_monkey/exploit/tools/smb_tools.py | 2 +- monkey/infection_monkey/exploit/win_ms08_067.py | 2 +- monkey/infection_monkey/network/info.py | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/monkey/infection_monkey/exploit/tools/smb_tools.py b/monkey/infection_monkey/exploit/tools/smb_tools.py index a68d52471b3..705f691e569 100644 --- a/monkey/infection_monkey/exploit/tools/smb_tools.py +++ b/monkey/infection_monkey/exploit/tools/smb_tools.py @@ -113,7 +113,7 @@ def copy_file(host, src_path, dst_path, username, password, lm_hash='', ntlm_has return None try: - tid = smb.connectTree(share_name) + smb.connectTree(share_name) except Exception as exc: LOG.debug("Error connecting tree to share '%s' on victim %r: %s", share_name, host, exc) diff --git a/monkey/infection_monkey/exploit/win_ms08_067.py b/monkey/infection_monkey/exploit/win_ms08_067.py index 7690f33c19f..4a5e059b94f 100644 --- a/monkey/infection_monkey/exploit/win_ms08_067.py +++ b/monkey/infection_monkey/exploit/win_ms08_067.py @@ -227,7 +227,7 @@ def _exploit_host(self): self._config.remote_user_pass, self._config.user_to_add).encode()) time.sleep(2) - reply = sock.recv(1000) + sock.recv(1000) LOG.debug("Exploited into %r using MS08-067", self.host) exploited = True diff --git a/monkey/infection_monkey/network/info.py b/monkey/infection_monkey/network/info.py index bc8189dc41e..cf43271c525 100644 --- a/monkey/infection_monkey/network/info.py +++ b/monkey/infection_monkey/network/info.py @@ -144,7 +144,6 @@ def get_interfaces_ranges(): for net_interface in ifs: address_str = net_interface['addr'] netmask_str = net_interface['netmask'] - ip_interface = ipaddress.ip_interface("%s/%s" % (address_str, netmask_str)) # limit subnet scans to class C only res.append(CidrRange(cidr_range="%s/%s" % (address_str, netmask_str))) return res From 8ed422434195d56f76dd1070e5333f4f8eb34f80 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 5 Apr 2021 13:38:31 -0400 Subject: [PATCH 07/12] build: Move flake8 config to `.flake8` --- ci_scripts/flake8_linter_check.ini => .flake8 | 0 ci_scripts/flake8_syntax_check.ini | 14 -------------- 2 files changed, 14 deletions(-) rename ci_scripts/flake8_linter_check.ini => .flake8 (100%) delete mode 100644 ci_scripts/flake8_syntax_check.ini diff --git a/ci_scripts/flake8_linter_check.ini b/.flake8 similarity index 100% rename from ci_scripts/flake8_linter_check.ini rename to .flake8 diff --git a/ci_scripts/flake8_syntax_check.ini b/ci_scripts/flake8_syntax_check.ini deleted file mode 100644 index 55e470e00a7..00000000000 --- a/ci_scripts/flake8_syntax_check.ini +++ /dev/null @@ -1,14 +0,0 @@ -[flake8] - -## Check syntax errors and fail the build if any are found. -exclude = - ../monkey/monkey_island/cc/ui -select = - E901, - E999, - F821, - F822, - F823 -count = True -show-source = True -statistics = True From a7f8efa102c352d937b819b6b8d0a9b9576d2e86 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 5 Apr 2021 13:45:18 -0400 Subject: [PATCH 08/12] ci: fail travis build if flake8 finds any issues --- .travis.yml | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 509da86acd0..f8425533c3b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -55,17 +55,7 @@ install: script: # Check Python code ## Check syntax errors and fail the build if any are found. -- flake8 ./monkey --config=./ci_scripts/flake8_syntax_check.ini - -## Warn about linter issues. -### --exit-zero forces Flake8 to use the exit status code 0 even if there are errors, which means this will NOT fail the build. -### The output is redirected to a file. -- flake8 ./monkey --exit-zero --config=./ci_scripts/flake8_linter_check.ini > ./ci_scripts/flake8_warnings.txt -## Display the linter issues -- cat ./ci_scripts/flake8_warnings.txt -## Make sure that we haven't increased the amount of warnings. -- PYTHON_WARNINGS_AMOUNT_UPPER_LIMIT=80 -- if [ $(tail -n 1 ./ci_scripts/flake8_warnings.txt) -gt $PYTHON_WARNINGS_AMOUNT_UPPER_LIMIT ]; then echo "Too many python linter warnings! Failing this build. Lower the amount of linter errors in this and try again. " && exit 1; fi +- flake8 ./monkey ## Check import order - python -m isort ./monkey --settings-file ./ci_scripts/isort.cfg From c1929c2bd358bfd600aab226dcbe924fd6f32ca5 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 5 Apr 2021 14:26:24 -0400 Subject: [PATCH 09/12] ci: remove dlint from travis Issue #1075 has been created to track the task to add this back in and integrate with pre-commit --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f8425533c3b..6796583d3f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ install: # Python - pip freeze - pip install -r monkey/monkey_island/requirements.txt # for unit tests -- pip install flake8 pytest pytest-cov dlint isort # for next stages +- pip install flake8 pytest pytest-cov isort # for next stages - pip install coverage # for code coverage - pip install -r monkey/infection_monkey/requirements.txt # for unit tests - pip install pipdeptree From 07afa27ec19ce2ea22d8ad3b8728b76fd8e2eb71 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 5 Apr 2021 14:44:42 -0400 Subject: [PATCH 10/12] ci: add C901 to flake8's `extend-ignore` list Issue #1076 has been created to track the task of resolving C901 errors and removing C901 from the ignore list. --- .flake8 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.flake8 b/.flake8 index 008f0fa8271..4c673215a6a 100644 --- a/.flake8 +++ b/.flake8 @@ -7,7 +7,7 @@ max-complexity = 10 max-line-length = 100 ### ignore "whitespace before ':'" and "line break before binary operator" for compatibility with black -extend-ignore = E203, W503 +extend-ignore = E203, W503, C901 ### --statistics Count the number of occurrences of each error/warning code and print a report. statistics = True From 46be7ae0d4e08cf3cb7f0bfffd4a81f54190ff4e Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 5 Apr 2021 14:48:33 -0400 Subject: [PATCH 11/12] agent: resolve E741 flake8 warnings --- monkey/infection_monkey/network/firewall.py | 14 +++++++------- monkey/infection_monkey/network/info.py | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/monkey/infection_monkey/network/firewall.py b/monkey/infection_monkey/network/firewall.py index 85191551fbb..f66bea7f4e5 100644 --- a/monkey/infection_monkey/network/firewall.py +++ b/monkey/infection_monkey/network/firewall.py @@ -41,9 +41,9 @@ def is_enabled(self): cmd = subprocess.Popen('netsh advfirewall show currentprofile', stdout=subprocess.PIPE) out = cmd.stdout.readlines() - for l in out: - if l.startswith('State'): - state = l.split()[-1].strip() + for line in out: + if line.startswith('State'): + state = line.split()[-1].strip() return state == "ON" except Exception: @@ -107,10 +107,10 @@ def is_enabled(self): cmd = subprocess.Popen('netsh firewall show state', stdout=subprocess.PIPE) out = cmd.stdout.readlines() - for l in out: - if l.startswith('Operational mode'): - state = l.split('=')[-1].strip() - elif l.startswith('The service has not been started.'): + for line in out: + if line.startswith('Operational mode'): + state = line.split('=')[-1].strip() + elif line.startswith('The service has not been started.'): return False return state == "Enable" diff --git a/monkey/infection_monkey/network/info.py b/monkey/infection_monkey/network/info.py index cf43271c525..22de0eebbc3 100644 --- a/monkey/infection_monkey/network/info.py +++ b/monkey/infection_monkey/network/info.py @@ -75,8 +75,8 @@ def get_routes(): # based on scapy implementation for route parsing ifaddr = socket.inet_ntoa(ifreq[20:24]) routes.append((dst, msk, "0.0.0.0", LOOPBACK_NAME, ifaddr)) - for l in f.readlines()[1:]: - iff, dst, gw, flags, x, x, x, msk, x, x, x = [var.encode() for var in l.split()] + for line in f.readlines()[1:]: + iff, dst, gw, flags, x, x, x, msk, x, x, x = [var.encode() for var in line.split()] flags = int(flags, 16) if flags & RTF_UP == 0: continue From b94568f976fbd194b0b6005921bcb1b49e5b0bb4 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 6 Apr 2021 06:59:03 -0400 Subject: [PATCH 12/12] ci: add comment about what flake8 C901 warnings are --- .flake8 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.flake8 b/.flake8 index 4c673215a6a..4bf12711479 100644 --- a/.flake8 +++ b/.flake8 @@ -6,7 +6,8 @@ show-source = True max-complexity = 10 max-line-length = 100 -### ignore "whitespace before ':'" and "line break before binary operator" for compatibility with black +### ignore "whitespace before ':'", "line break before binary operator" for +### compatibility with black, and cyclomatic complexity (for now). extend-ignore = E203, W503, C901 ### --statistics Count the number of occurrences of each error/warning code and print a report.