Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flake8 improvements #1078

Merged
merged 12 commits into from
Apr 6, 2021
9 changes: 6 additions & 3 deletions ci_scripts/flake8_linter_check.ini → .flake8
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
[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 ':'", "line break before binary operator" for
### compatibility with black, and cyclomatic complexity (for now).
extend-ignore = E203, W503, C901
shreyamalviya marked this conversation as resolved.
Show resolved Hide resolved

### --statistics Count the number of occurrences of each error/warning code and print a report.
statistics = True
Expand Down
14 changes: 2 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 0 additions & 15 deletions ci_scripts/flake8_syntax_check.ini

This file was deleted.

2 changes: 0 additions & 2 deletions monkey/common/cloud/aws/test_aws_instance.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

import pytest
import requests
import requests_mock
Expand Down
2 changes: 1 addition & 1 deletion monkey/common/network/network_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions monkey/common/utils/mongo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions monkey/infection_monkey/exploit/smbexec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -148,7 +149,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()
Expand Down
8 changes: 4 additions & 4 deletions monkey/infection_monkey/exploit/tools/smb_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion monkey/infection_monkey/exploit/tools/wmi_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions monkey/infection_monkey/exploit/web_rce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion monkey/infection_monkey/exploit/win_ms08_067.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 16 additions & 16 deletions monkey/infection_monkey/network/firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ 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:
except Exception:
return None

def add_firewall_rule(self, name="Firewall", direction="in", action="allow", program=sys.executable, **kwargs):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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


Expand All @@ -107,14 +107,14 @@ 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"
except:
except Exception:
return None

def add_firewall_rule(self, rule='allowedprogram', name="Firewall", mode="ENABLE", program=sys.executable,
Expand All @@ -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,
Expand All @@ -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):
Expand All @@ -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()
Expand Down
6 changes: 2 additions & 4 deletions monkey/infection_monkey/network/info.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ipaddress
import itertools
import socket
import struct
Expand Down Expand Up @@ -76,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
Expand Down Expand Up @@ -145,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
Expand Down
1 change: 0 additions & 1 deletion monkey/infection_monkey/network/test_postgresql_finger.py
Original file line number Diff line number Diff line change
@@ -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."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion monkey/infection_monkey/transport/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions monkey/infection_monkey/transport/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 0 additions & 1 deletion monkey/monkey_island/cc/services/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down