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

Remove dead python code reported by Vulture #1149

Merged
merged 49 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
2f6803d
Add vulture pre-commit hook
mssalvatore May 3, 2021
b41a2f2
travis: Fail build if vulture finds dead code
mssalvatore May 3, 2021
9649f90
agent: Remove unused parameter securityFlags from create_smb()
mssalvatore May 3, 2021
33e74b1
agent: Use consistent naming for parameters to __exit__()
mssalvatore May 3, 2021
638e70e
Remove unused code in unit tests
shreyamalviya May 4, 2021
df7759e
Remove unused variable `DETAILS_DTO` from `monkey_island/cc/services/…
shreyamalviya May 4, 2021
9bcaa2e
Remove unused `did_exploit_type_succeed()` from `monkey_island/cc/ser…
shreyamalviya May 4, 2021
f32e1e0
Remove unused variable `is_auth` in `monkey_island/cc/services/remote…
shreyamalviya May 4, 2021
9c629f9
Remove unused variable `UPLOADS_DIR_NAME` in `monkey_island/cc/servic…
shreyamalviya May 4, 2021
e4c4515
Remove unused `get_monkey_critical_services()` and `get_monkey_label_…
shreyamalviya May 4, 2021
a5481c1
Remove unused `get_latest_attack_telem_time()` from `monkey_island/cc…
shreyamalviya May 4, 2021
eeaafc9
Remove ununsed variable `user_id_table` in `monkey_island/cc/resource…
shreyamalviya May 4, 2021
b1ce5d3
Remove unused variable `MAX_MONKEYS_AMOUNT_TO_CACHE` in `monkey_islan…
shreyamalviya May 4, 2021
b7535dc
Remove unused variables `GENERAL_CMDLINE_LINUX` and `MONKEY_CMDLINE_H…
shreyamalviya May 4, 2021
6cc22af
Remove unused `get_ip_for_connection()` in `infection_monkey/network/…
shreyamalviya May 4, 2021
2f5d338
Remove unused variable `PATH_TO_ACTIONS` from `infection_monkey/post_…
shreyamalviya May 4, 2021
8391df9
Remove unused `check_udp_port`, `traceroute()`, `_traceroute_windows(…
shreyamalviya May 4, 2021
35f4515
Remove unused `get_pba()` in `infection_monkey/post_breach/pba.py`
shreyamalviya May 4, 2021
0dc6005
Remove unused variable `os_is_linux` in `infection_monkey/post_breach…
shreyamalviya May 4, 2021
536d0bc
Remove unused `mongo_client` in `monkey_island/cc/server_utils/bootlo…
shreyamalviya May 5, 2021
380afa6
Remove unused `report_generating_lock` in `monkey_island/cc/resources…
shreyamalviya May 5, 2021
8c97f32
Remove unused `_instance_id`, `region`, `_get_instance_id()`, and `_g…
shreyamalviya May 5, 2021
edff2c5
Remove unused `set_deployment()` in `monkey_island/cc/environment/__i…
shreyamalviya May 5, 2021
c848581
Remove unused `hash_secret()` from `monkey_island/cc/environment/__i…
shreyamalviya May 5, 2021
b59213f
Delete unused file `monkey_island/scripts/island_password_hasher.py`
shreyamalviya May 5, 2021
635418b
Remove unused `get_logon_handle()` from `infection_monkey/utils/windo…
shreyamalviya May 6, 2021
3a346b5
Remove unused `NoInheritance` in `infection_monkey/utils/plugins/plug…
shreyamalviya May 6, 2021
962e785
Remove unused `proxy_via` in `infection_monkey/transport/http.py`
shreyamalviya May 6, 2021
e33288a
Remove unused property `locked` in `infection_monkey/system_singleton…
shreyamalviya May 6, 2021
e59f7a5
Remove unused const `WMI_LDAP_CLASSES` in `infection_monkey/system_in…
shreyamalviya May 6, 2021
3a8591e
Remove unused `_get_traceroute_bin_path`, and `_parse_traceroute` in …
shreyamalviya May 6, 2021
dd1eaab
Remove unused `IP_ADDR_RE`, and `IP_ADDR_PARENTHESES_RE` from `infect…
shreyamalviya May 6, 2021
adb90d1
Remove unused `_mode` in `infection_monkey/monkeyfs.py`
shreyamalviya May 6, 2021
1cd746c
Remove unused `_dropper_path` and `_depth` in `infection_monkey/monke…
shreyamalviya May 6, 2021
5f8547a
Remove unused `set_vulnerable_port_from_url()` from `infection_monkey…
shreyamalviya May 6, 2021
0c3e385
Remove unused `check_if_port_open` in `infection_monkey/exploit/web_r…
shreyamalviya May 6, 2021
e30c37f
Remove unused `DceRpcException` in `infection_monkey/exploit/tools/wm…
shreyamalviya May 6, 2021
67d7ad8
Remove unused `SAMBACRY_MONKEY_COPY_FILENAME_32` and `SAMBACRY_MONKEY…
shreyamalviya May 6, 2021
aed9c4a
Remove unused `should_exploit` in `infection_monkey/config.py` and `i…
shreyamalviya May 6, 2021
81f785e
Remove unused `OTHER` in `common/utils/exploit_enum.py`
shreyamalviya May 6, 2021
df8f9c8
Remove unused `format_time()` in `common/utils/attack_utils.py`
shreyamalviya May 6, 2021
9b6ecd5
Remove unused `SCOUTSUITE_COLLECTOR` in `common/common_consts/system_…
shreyamalviya May 6, 2021
897a92b
Remove unused `run_command` in `common/cmd/cmd_runner.py`
shreyamalviya May 6, 2021
6c80335
Remove unused `get_regions`, `get_session`, and `test_client` in `com…
shreyamalviya May 6, 2021
060b7fd
Remove unused `TEST_SALT` from `tests/monkey_island/cc/environment/te…
shreyamalviya May 6, 2021
55208fd
Add CHANGELOG entry for Vulture fixes
shreyamalviya May 6, 2021
e1ef807
Remove unused import in infection_monkey/network/info.py
shreyamalviya May 6, 2021
e8947a3
Add a whitelist for vulture
mssalvatore May 6, 2021
69af8a8
island: Remove MongoClient() call from BootloaderHttpServer
mssalvatore May 7, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[flake8]
## Warn about linter issues.

exclude = monkey/monkey_island/cc/ui
exclude = monkey/monkey_island/cc/ui,whitelist.py
show-source = True
max-complexity = 10
max-line-length = 100
Expand All @@ -15,4 +15,3 @@ statistics = True

### --count will print the total number of errors.
count = True

4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ repos:
rev: v0.2
hooks:
- id: swimm-verify
- repo: https://github.com/jendrikseipp/vulture
rev: v2.3
hooks:
- id: vulture
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ script:
## Check that all python is properly formatted. Fail otherwise.
- python -m black --check .

## Check that there is no dead python code
- python -m vulture .

## Run unit tests and generate coverage data
- cd monkey # This is our source dir
- python -m pytest --cov=. # Have to use `python -m pytest` instead of `pytest` to add "{$builddir}/monkey/monkey" to sys.path.
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Zerologon exploiter writes runtime artifacts to a secure temporary directory
instead of $HOME. #1143
- Authentication mechanism to use bcrypt on server side. #1139
- Removed relevant dead code as reported by Vulture. #1149

### Fixed
- Attempted to delete a directory when monkey config reset was called. #1054
Expand Down
17 changes: 0 additions & 17 deletions monkey/common/cloud/aws/aws_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import boto3
import botocore
from botocore.exceptions import ClientError

from common.cloud.aws.aws_instance import AwsInstance

Expand Down Expand Up @@ -53,22 +52,6 @@ def get_client(client_type, region=None):
client_type, region_name=region if region is not None else AwsService.region
)

@staticmethod
def get_session():
return boto3.session.Session()

@staticmethod
def get_regions():
return AwsService.get_session().get_available_regions("ssm")

@staticmethod
def test_client():
try:
AwsService.get_client("ssm").describe_instance_information()
return True
except ClientError:
return False

@staticmethod
def get_instances():
"""
Expand Down
11 changes: 0 additions & 11 deletions monkey/common/cmd/cmd_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import time
from abc import abstractmethod

from common.cmd.cmd import Cmd
from common.cmd.cmd_result import CmdResult
from common.cmd.cmd_status import CmdStatus

Expand Down Expand Up @@ -36,16 +35,6 @@ class CmdRunner(object):
def __init__(self, is_linux):
self.is_linux = is_linux

def run_command(self, command_line, timeout=DEFAULT_TIMEOUT):
"""
Runs the given command on the remote machine
:param command_line: The command line to run
:param timeout: Timeout in seconds for command.
:return: Command result
"""
c_id = self.run_command_async(command_line)
return self.wait_commands([Cmd(self, c_id)], timeout)[1]

@staticmethod
def run_multiple_commands(instances, inst_to_cmd, inst_n_cmd_res_to_res):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
PROCESS_LIST_COLLECTOR = "ProcessListCollector"
MIMIKATZ_COLLECTOR = "MimikatzCollector"
AZURE_CRED_COLLECTOR = "AzureCollector"
SCOUTSUITE_COLLECTOR = "ScoutSuiteCollector"
10 changes: 0 additions & 10 deletions monkey/common/utils/attack_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,3 @@ class UsageEnum(Enum):

# Dict that describes what BITS job was used for
BITS_UPLOAD_STRING = "BITS job was used to upload monkey to a remote system."


def format_time(time):
return "%s-%s %s:%s:%s" % (
time.date().month,
time.date().day,
time.time().hour,
time.time().minute,
time.time().second,
)
1 change: 0 additions & 1 deletion monkey/common/utils/exploit_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@

class ExploitType(Enum):
VULNERABILITY = 1
OTHER = 8
BRUTE_FORCE = 9
mssalvatore marked this conversation as resolved.
Show resolved Hide resolved
1 change: 0 additions & 1 deletion monkey/infection_monkey/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ def as_dict(self):
# exploiters config
###########################

should_exploit = True
skip_exploit_if_file_exist = False

ms08_067_exploit_attempts = 5
Expand Down
1 change: 0 additions & 1 deletion monkey/infection_monkey/example.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"should_exploit": true,
"command_servers": [
"192.0.2.0:5000"
],
Expand Down
5 changes: 0 additions & 5 deletions monkey/infection_monkey/exploit/sambacry.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ class SambaCryExploiter(HostExploiter):
SAMBACRY_MONKEY_FILENAME_32 = "monkey32"
# Monkey filename on share (64 bit)
SAMBACRY_MONKEY_FILENAME_64 = "monkey64"
# Monkey copy filename on share (32 bit)
SAMBACRY_MONKEY_COPY_FILENAME_32 = "monkey32_2"
# Monkey copy filename on share (64 bit)
SAMBACRY_MONKEY_COPY_FILENAME_64 = "monkey64_2"
# Supported samba port
SAMBA_PORT = 445

Expand Down Expand Up @@ -465,7 +461,6 @@ def create_smb(
creationDisposition,
fileAttributes,
impersonationLevel=SMB2_IL_IMPERSONATION,
securityFlags=0,
oplockLevel=SMB2_OPLOCK_LEVEL_NONE,
createContexts=None,
):
Expand Down
4 changes: 0 additions & 4 deletions monkey/infection_monkey/exploit/tools/wmi_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
LOG = logging.getLogger(__name__)


class DceRpcException(Exception):
pass


class AccessDeniedException(Exception):
def __init__(self, host, username, password, domain):
super(AccessDeniedException, self).__init__(
Expand Down
12 changes: 1 addition & 11 deletions monkey/infection_monkey/exploit/web_rce.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
RUN_MONKEY,
WGET_HTTP_UPLOAD,
)
from infection_monkey.network.tools import check_tcp_port, tcp_port_to_service
from infection_monkey.network.tools import tcp_port_to_service
from infection_monkey.telemetry.attack.t1197_telem import T1197Telem
from infection_monkey.telemetry.attack.t1222_telem import T1222Telem

Expand Down Expand Up @@ -188,13 +188,6 @@ def get_open_service_ports(self, port_list, names):

return valid_ports

def check_if_port_open(self, port):
is_open, _ = check_tcp_port(self.host.ip_addr, port)
if not is_open:
LOG.info("Port %d is closed on %r, skipping", port, self.host)
return False
return True

def get_command(self, path, http_path, commands):
try:
if "linux" in self.host.os["type"]:
Expand Down Expand Up @@ -578,9 +571,6 @@ def get_default_dropper_path(self):
LOG.debug("Target's machine type was not set. Using win-32 dropper path.")
return self._config.dropper_target_path_win_32

def set_vulnerable_port_from_url(self, url):
self.vulnerable_port = HTTPTools.get_port_from_url(url)

def get_target_url(self):
"""
This method allows "configuring" the way in which a vulnerable URL is picked.
Expand Down
9 changes: 0 additions & 9 deletions monkey/infection_monkey/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
MONKEY_ARG,
)
MONKEY_CMDLINE_LINUX = "./%%(monkey_filename)s %s" % (MONKEY_ARG,)
GENERAL_CMDLINE_LINUX = "(cd %(monkey_directory)s && %(monkey_commandline)s)"
DROPPER_CMDLINE_DETACHED_WINDOWS = "%s start cmd /c %%(dropper_path)s %s" % (
CMD_PREFIX,
DROPPER_ARG,
Expand All @@ -26,14 +25,6 @@
CMD_PREFIX,
MONKEY_ARG,
)
MONKEY_CMDLINE_HTTP = (
'%s /c "bitsadmin /transfer Update /download /priority high %%(http_path)s %%(monkey_path)s'
'&cmd /c %%(monkey_path)s %s"'
% (
CMD_PREFIX,
MONKEY_ARG,
)
)
DELAY_DELETE_CMD = (
"cmd /c (for /l %%i in (1,0,2) do (ping -n 60 127.0.0.1 & del /f /q %(file_path)s & "
"if not exist %(file_path)s exit)) > NUL 2>&1 "
Expand Down
3 changes: 0 additions & 3 deletions monkey/infection_monkey/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ def __init__(self, args):
self._default_tunnel = None
self._args = args
self._network = None
self._dropper_path = None
self._exploiters = None
self._fingerprint = None
self._default_server = None
self._default_server_port = None
self._depth = 0
self._opts = None
self._upgrading_to_64 = False

Expand Down Expand Up @@ -92,7 +90,6 @@ def initialize(self):

self._keep_running = True
self._network = NetworkScanner()
self._dropper_path = sys.argv[0]

if self._default_server:
if self._default_server not in WormConfiguration.command_servers:
Expand Down
1 change: 0 additions & 1 deletion monkey/infection_monkey/monkeyfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def __init__(self, name, mode="r", buffering=None):
if not name.startswith(MONKEYFS_PREFIX):
name = MONKEYFS_PREFIX + name
self.name = name
self._mode = mode
if name in VirtualFile._vfs:
super(VirtualFile, self).__init__(self._vfs[name])
else:
Expand Down
2 changes: 1 addition & 1 deletion monkey/infection_monkey/network/firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def listen_allowed(self, **kwargs):
def __enter__(self):
return self

def __exit__(self, exc_type, value, traceback):
def __exit__(self, _exc_type, value, traceback):
self.close()

def close(self):
Expand Down
20 changes: 0 additions & 20 deletions monkey/infection_monkey/network/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import socket
import struct
from random import randint # noqa: DUO102
from subprocess import check_output

import netifaces
import psutil
Expand Down Expand Up @@ -157,22 +156,3 @@ def get_interfaces_ranges():
# limit subnet scans to class C only
res.append(CidrRange(cidr_range="%s/%s" % (address_str, netmask_str)))
return res


if is_windows_os():

def get_ip_for_connection(target_ip):
return None


else:

def get_ip_for_connection(target_ip):
try:
query_str = "ip route get %s" % target_ip
resp = check_output(query_str.split())
substr = resp.split()
src = substr[substr.index("src") + 1]
return src
except Exception:
return None
Loading