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

2216 notify relay when finished #2257

Merged
merged 3 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 5 additions & 8 deletions monkey/infection_monkey/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from pubsub.core import Publisher

import infection_monkey.tunnel as tunnel
from common.event_queue import IEventQueue, PyPubSubEventQueue
from common.events import CredentialsStolenEvent
from common.network.network_utils import address_to_ip_port
Expand Down Expand Up @@ -45,6 +44,7 @@
from infection_monkey.network.relay import TCPRelay
from infection_monkey.network.relay.utils import (
find_server,
notify_disconnect,
send_remove_from_waitlist_control_message_to_relays,
)
from infection_monkey.network_scanning.elasticsearch_fingerprinter import ElasticSearchFingerprinter
Expand Down Expand Up @@ -415,21 +415,18 @@ def cleanup(self):
).send() # Signal the server (before closing the tunnel)

self._close_tunnel()
self._singleton.unlock()
except Exception as e:
logger.error(f"An error occurred while cleaning up the monkey agent: {e}")
if deleted is None:
InfectionMonkey._self_delete()
finally:
self._singleton.unlock()

logger.info("Monkey is shutting down")

def _close_tunnel(self):
tunnel_address = (
self._control_client.proxies.get("https", "").replace("http://", "").split(":")[0]
)
if tunnel_address:
logger.info("Quitting tunnel %s", tunnel_address)
tunnel.quit_tunnel(tunnel_address)
logger.info(f"Quitting tunnel {self._cmd_island_ip}")
notify_disconnect(self._cmd_island_ip, self._cmd_island_port)

def _send_log(self):
monkey_log_path = get_agent_log_path()
Expand Down
1 change: 1 addition & 0 deletions monkey/infection_monkey/network/relay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .tcp_connection_handler import TCPConnectionHandler
from .tcp_pipe_spawner import TCPPipeSpawner
from .tcp_relay import TCPRelay
from .utils import notify_disconnect
22 changes: 15 additions & 7 deletions monkey/infection_monkey/network/relay/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import socket
from ipaddress import IPv4Address
from typing import Iterable, Optional

import requests
Expand Down Expand Up @@ -49,14 +50,21 @@ def send_remove_from_waitlist_control_message_to_relays(servers: Iterable[str]):


def _send_remove_from_waitlist_control_message_to_relay(server: str):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as d_socket:
d_socket.settimeout(MEDIUM_REQUEST_TIMEOUT)
ip, port = address_to_ip_port(server)
notify_disconnect(IPv4Address(ip), int(port))


ip, port = address_to_ip_port(server)
logger.info(f"Control message was sent to the server/relay {server}")
def notify_disconnect(server_ip: IPv4Address, server_port: int):
"""
Tell upstream relay that we no longer need the relay.

:param server_ip: The IP address of the server to notify.
:param server_port: The port of the server to notify.
"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as d_socket:
try:
d_socket.connect((ip, int(port)))
d_socket.send(RELAY_CONTROL_MESSAGE_REMOVE_FROM_WAITLIST)
d_socket.connect((server_ip, server_port))
d_socket.sendall(RELAY_CONTROL_MESSAGE_REMOVE_FROM_WAITLIST)
logger.info(f"Control message was sent to the server/relay {server_ip}:{server_port}")
except OSError as err:
logger.error(f"Error connecting to socket {server}: {err}")
logger.error(f"Error connecting to socket {server_ip}:{server_port}: {err}")