forked from p15670423/monkey
Agent: Add agent deletion check
This check will avoid duplicate deletion calls in case an exception happens
This commit is contained in:
parent
e40703dcac
commit
64f2d598cf
|
@ -343,6 +343,7 @@ class InfectionMonkey:
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
logger.info("Monkey cleanup started")
|
logger.info("Monkey cleanup started")
|
||||||
|
deleted = None
|
||||||
try:
|
try:
|
||||||
if self._master:
|
if self._master:
|
||||||
self._master.cleanup()
|
self._master.cleanup()
|
||||||
|
@ -357,7 +358,7 @@ class InfectionMonkey:
|
||||||
firewall.remove_firewall_rule()
|
firewall.remove_firewall_rule()
|
||||||
firewall.close()
|
firewall.close()
|
||||||
|
|
||||||
InfectionMonkey._self_delete()
|
deleted = InfectionMonkey._self_delete()
|
||||||
|
|
||||||
InfectionMonkey._send_log()
|
InfectionMonkey._send_log()
|
||||||
|
|
||||||
|
@ -371,7 +372,8 @@ class InfectionMonkey:
|
||||||
self._singleton.unlock()
|
self._singleton.unlock()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"An error occurred while cleaning up the monkey agent: {e}")
|
logger.error(f"An error occurred while cleaning up the monkey agent: {e}")
|
||||||
InfectionMonkey._self_delete()
|
if deleted is None:
|
||||||
|
InfectionMonkey._self_delete()
|
||||||
|
|
||||||
logger.info("Monkey is shutting down")
|
logger.info("Monkey is shutting down")
|
||||||
|
|
||||||
|
@ -400,9 +402,10 @@ class InfectionMonkey:
|
||||||
ControlClient.send_log(log)
|
ControlClient.send_log(log)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _self_delete():
|
def _self_delete() -> bool:
|
||||||
status = ScanStatus.USED if remove_monkey_dir() else ScanStatus.SCANNED
|
status = ScanStatus.USED if remove_monkey_dir() else ScanStatus.SCANNED
|
||||||
T1107Telem(status, get_monkey_dir_path()).send()
|
T1107Telem(status, get_monkey_dir_path()).send()
|
||||||
|
deleted = False
|
||||||
|
|
||||||
if -1 == sys.executable.find("python"):
|
if -1 == sys.executable.find("python"):
|
||||||
try:
|
try:
|
||||||
|
@ -421,11 +424,14 @@ class InfectionMonkey:
|
||||||
close_fds=True,
|
close_fds=True,
|
||||||
startupinfo=startupinfo,
|
startupinfo=startupinfo,
|
||||||
)
|
)
|
||||||
|
deleted = True
|
||||||
else:
|
else:
|
||||||
os.remove(sys.executable)
|
os.remove(sys.executable)
|
||||||
status = ScanStatus.USED
|
status = ScanStatus.USED
|
||||||
|
deleted = True
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.error("Exception in self delete: %s", exc)
|
logger.error("Exception in self delete: %s", exc)
|
||||||
status = ScanStatus.SCANNED
|
status = ScanStatus.SCANNED
|
||||||
if status:
|
if status:
|
||||||
T1107Telem(status, sys.executable).send()
|
T1107Telem(status, sys.executable).send()
|
||||||
|
return deleted
|
||||||
|
|
Loading…
Reference in New Issue