Skip to content

Commit

Permalink
remove name arg from listener start and shutdown (BC-SECURITY#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinnybod authored Nov 30, 2023
1 parent 45ff7d1 commit bde2f71
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 228 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed usages of deprecated `Credentials` and `Listeners` functions (@Vinnybod)
- Remove usages of deprecated `Agents` functions (@Vinnybod)
- Add typehinting for `MainMenu` object in modules (@Vinnybod)
- Removed `name` property from listener start and shutdown functions (@Vinnybod)

### Removed

Expand Down
2 changes: 1 addition & 1 deletion empire/server/common/empire.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def shutdown(self):
"""
log.info("Empire shutting down...")

# enumerate all active servers/listeners and shut them down
log.info("Shutting down listeners...")
self.listenersv2.shutdown_listeners()

log.info("Shutting down plugins...")
Expand Down
6 changes: 3 additions & 3 deletions empire/server/core/listener_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def create_listener(self, db: Session, listener_req):

def stop_listener(self, db_listener: models.Listener):
if self._active_listeners.get(db_listener.id):
self._active_listeners[db_listener.id].shutdown(name=db_listener.name)
self._active_listeners[db_listener.id].shutdown()
del self._active_listeners[db_listener.id]

def delete_listener(self, db: Session, db_listener: models.Listener):
Expand All @@ -135,7 +135,7 @@ def start_existing_listener(self, db: Session, listener: models.Listener):
log.error(err)
return None, err

success = template_instance.start(name=listener.name)
success = template_instance.start()
db.flush()

if success:
Expand All @@ -160,7 +160,7 @@ def _start_listener(self, db: Session, template_instance, template_name):
name = template_instance.options["Name"]["Value"]
try:
log.info(f"v2: Starting listener '{name}'")
success = template_instance.start(name=name)
success = template_instance.start()

if success:
listener_options = copy.deepcopy(template_instance.options)
Expand Down
41 changes: 12 additions & 29 deletions empire/server/listeners/dbx.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __init__(self, mainMenu: MainMenu):

# required:
self.mainMenu = mainMenu
self.threads = {}
self.thread = None

# optional/specific for this module

Expand Down Expand Up @@ -1016,40 +1016,23 @@ def delete_file(dbx, path):
stagingKey, responseData, listenerOptions
)

def start(self, name=""):
def start(self):
"""
Start a threaded instance of self.start_server() and store it in the
self.threads dictionary keyed by the listener name.
self.thread property.
"""
listenerOptions = self.options
if name and name != "":
self.threads[name] = helpers.KThread(
target=self.start_server, args=(listenerOptions,)
)
self.threads[name].start()
time.sleep(3)
# returns True if the listener successfully started, false otherwise
return self.threads[name].is_alive()
else:
name = listenerOptions["Name"]["Value"]
self.threads[name] = helpers.KThread(
target=self.start_server, args=(listenerOptions,)
)
self.threads[name].start()
time.sleep(3)
# returns True if the listener successfully started, false otherwise
return self.threads[name].is_alive()
self.thread = helpers.KThread(target=self.start_server, args=(listenerOptions,))
self.thread.start()
time.sleep(3)
# returns True if the listener successfully started, false otherwise
return self.thread.is_alive()

def shutdown(self, name=""):
def shutdown(self):
"""
Terminates the server thread stored in the self.threads dictionary,
keyed by the listener name.
Terminates the server thread stored in the self.thread property.
"""
if name and name != "":
to_kill = name
else:
to_kill = self.options["Name"]["Value"]

to_kill = self.options["Name"]["Value"]
self.instance_log.info(f"{to_kill}: shutting down...")
log.info(f"{to_kill}: shutting down...")
self.threads[to_kill].kill()
self.thread.kill()
41 changes: 12 additions & 29 deletions empire/server/listeners/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def __init__(self, mainMenu: MainMenu):

# required:
self.mainMenu = mainMenu
self.threads = {}
self.thread = None

# optional/specific for this module
self.app = None
Expand Down Expand Up @@ -1251,40 +1251,23 @@ def handle_post(request_uri):
exc_info=True,
)

def start(self, name=""):
def start(self):
"""
Start a threaded instance of self.start_server() and store it in the
self.threads dictionary keyed by the listener name.
self.thread property.
"""
listenerOptions = self.options
if name and name != "":
self.threads[name] = helpers.KThread(
target=self.start_server, args=(listenerOptions,)
)
self.threads[name].start()
time.sleep(1)
# returns True if the listener successfully started, false otherwise
return self.threads[name].is_alive()
else:
name = listenerOptions["Name"]["Value"]
self.threads[name] = helpers.KThread(
target=self.start_server, args=(listenerOptions,)
)
self.threads[name].start()
time.sleep(1)
# returns True if the listener successfully started, false otherwise
return self.threads[name].is_alive()
self.thread = helpers.KThread(target=self.start_server, args=(listenerOptions,))
self.thread.start()
time.sleep(1)
# returns True if the listener successfully started, false otherwise
return self.thread.is_alive()

def shutdown(self, name=""):
def shutdown(self):
"""
Terminates the server thread stored in the self.threads dictionary,
keyed by the listener name.
Terminates the server thread stored in the self.thread property.
"""
if name and name != "":
to_kill = name
else:
to_kill = self.options["Name"]["Value"]

to_kill = self.options["Name"]["Value"]
self.instance_log.info(f"{to_kill}: shutting down...")
log.info(f"{to_kill}: shutting down...")
self.threads[to_kill].kill()
self.thread.kill()
41 changes: 12 additions & 29 deletions empire/server/listeners/http_com.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def __init__(self, mainMenu: MainMenu):

# required:
self.mainMenu = mainMenu
self.threads = {}
self.thread = None

# optional/specific for this module
self.app = None
Expand Down Expand Up @@ -849,40 +849,23 @@ def handle_post(request_uri):
self.instance_log.error(message1, exc_info=True)
self.instance_log.error(message2, exc_info=True)

def start(self, name=""):
def start(self):
"""
Start a threaded instance of self.start_server() and store it in the
self.threads dictionary keyed by the listener name.
self.thread property.
"""
listenerOptions = self.options
if name and name != "":
self.threads[name] = helpers.KThread(
target=self.start_server, args=(listenerOptions,)
)
self.threads[name].start()
time.sleep(1)
# returns True if the listener successfully started, false otherwise
return self.threads[name].is_alive()
else:
name = listenerOptions["Name"]["Value"]
self.threads[name] = helpers.KThread(
target=self.start_server, args=(listenerOptions,)
)
self.threads[name].start()
time.sleep(1)
# returns True if the listener successfully started, false otherwise
return self.threads[name].is_alive()
self.thread = helpers.KThread(target=self.start_server, args=(listenerOptions,))
self.thread.start()
time.sleep(1)
# returns True if the listener successfully started, false otherwise
return self.thread.is_alive()

def shutdown(self, name=""):
def shutdown(self):
"""
Terminates the server thread stored in the self.threads dictionary,
keyed by the listener name.
Terminates the server thread stored in the self.thread property.
"""
if name and name != "":
to_kill = name
else:
to_kill = self.options["Name"]["Value"]

to_kill = self.options["Name"]["Value"]
self.instance_log.info(f"{to_kill}: shutting down...")
log.info(f"{to_kill}: shutting down...")
self.threads[to_kill].kill()
self.thread.kill()
6 changes: 3 additions & 3 deletions empire/server/listeners/http_foreign.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __init__(self, mainMenu: MainMenu):

# required:
self.mainMenu = mainMenu
self.threads = {}
self.thread = None

# optional/specific for this module
self.app = None
Expand Down Expand Up @@ -465,13 +465,13 @@ def generate_comms(self, listenerOptions, language=None):
else:
log.error("listeners/http_foreign generate_comms(): no language specified!")

def start(self, name=""):
def start(self):
"""
Nothing to actually start for a foreign listner.
"""
return True

def shutdown(self, name=""):
def shutdown(self):
"""
Nothing to actually shut down for a foreign listner.
"""
Expand Down
4 changes: 2 additions & 2 deletions empire/server/listeners/http_hop.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __init__(self, mainMenu: MainMenu):

# required:
self.mainMenu = mainMenu
self.threads = {}
self.thread = None

self.instance_log = log

Expand Down Expand Up @@ -540,7 +540,7 @@ def generate_comms(self, listenerOptions, language=None):
else:
log.error("listeners/http_hop generate_comms(): no language specified!")

def start(self, name=""):
def start(self):
"""
Nothing to actually start for a hop listner, but ensure the stagingKey is
synced with the redirect listener.
Expand Down
44 changes: 13 additions & 31 deletions empire/server/listeners/http_malleable.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def __init__(self, mainMenu: MainMenu):

# required:
self.mainMenu = mainMenu
self.threads = {} # used to keep track of any threaded instances of this server
self.thread = None

# optional/specific for this module
self.app = None
Expand Down Expand Up @@ -1764,44 +1764,26 @@ def handle_request(request_uri="", tempListenerOptions=None):
self.instance_log.error(message, exc_info=True)
log.error(message, exc_info=True)

def start(self, name=""):
def start(self):
"""
Start a threaded instance of self.start_server() and store it in
the self.threads dictionary keyed by the listener name.
Start a threaded instance of self.start_server() and store it in the
self.thread property.
"""
self.instance_log = log_util.get_listener_logger(
LOG_NAME_PREFIX, self.options["Name"]["Value"]
)

listenerOptions = self.options
if name and name != "":
self.threads[name] = helpers.KThread(
target=self.start_server, args=(listenerOptions,)
)
self.threads[name].start()
time.sleep(1)
# returns True if the listener successfully started, false otherwise
return self.threads[name].is_alive()
else:
name = listenerOptions["Name"]["Value"]
self.threads[name] = helpers.KThread(
target=self.start_server, args=(listenerOptions,)
)
self.threads[name].start()
time.sleep(1)
# returns True if the listener successfully started, false otherwise
return self.threads[name].is_alive()
self.thread = helpers.KThread(target=self.start_server, args=(listenerOptions,))
self.thread.start()
time.sleep(1)
# returns True if the listener successfully started, false otherwise
return self.thread.is_alive()

def shutdown(self, name=""):
def shutdown(self):
"""
Terminates the server thread stored in the self.threads dictionary,
keyed by the listener name.
Terminates the server thread stored in the self.thread property.
"""
if name and name != "":
to_kill = name
else:
to_kill = self.options["Name"]["Value"]

to_kill = self.options["Name"]["Value"]
self.instance_log.info(f"{to_kill}: shutting down...")
log.info(f"{to_kill}: shutting down...")
self.threads[to_kill].kill()
self.thread.kill()
41 changes: 12 additions & 29 deletions empire/server/listeners/onedrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def __init__(self, mainMenu: MainMenu):
self.stager_url = ""

self.mainMenu = mainMenu
self.threads = {}
self.thread = None

self.options["StagingKey"]["Value"] = str(
data_util.get_config("staging_key")[0]
Expand Down Expand Up @@ -885,40 +885,23 @@ def upload_stager():

s.close()

def start(self, name=""):
def start(self):
"""
Start a threaded instance of self.start_server() and store it in the
self.threads dictionary keyed by the listener name.
self.thread property.
"""
listenerOptions = self.options
if name and name != "":
self.threads[name] = helpers.KThread(
target=self.start_server, args=(listenerOptions,)
)
self.threads[name].start()
time.sleep(3)
# returns True if the listener successfully started, false otherwise
return self.threads[name].is_alive()
else:
name = listenerOptions["Name"]["Value"]
self.threads[name] = helpers.KThread(
target=self.start_server, args=(listenerOptions,)
)
self.threads[name].start()
time.sleep(3)
# returns True if the listener successfully started, false otherwise
return self.threads[name].is_alive()
self.thread = helpers.KThread(target=self.start_server, args=(listenerOptions,))
self.thread.start()
time.sleep(3)
# returns True if the listener successfully started, false otherwise
return self.thread.is_alive()

def shutdown(self, name=""):
def shutdown(self):
"""
Terminates the server thread stored in the self.threads dictionary,
keyed by the listener name.
Terminates the server thread stored in the self.thread property.
"""
if name and name != "":
to_kill = name
else:
to_kill = self.options["Name"]["Value"]

to_kill = self.options["Name"]["Value"]
self.instance_log.info(f"{to_kill}: shutting down...")
log.info(f"{to_kill}: shutting down...")
self.threads[to_kill].kill()
self.thread.kill()
Loading

0 comments on commit bde2f71

Please sign in to comment.