NetExec/tests/e2e_test.py

109 lines
3.3 KiB
Python
Raw Normal View History

2023-03-24 18:11:02 +00:00
import argparse
import os
import subprocess
from rich.console import Console
def get_cli_args():
2023-05-08 18:39:36 +00:00
parser = argparse.ArgumentParser(description=f"Script for running end to end tests for CME")
2023-03-24 18:11:02 +00:00
parser.add_argument("-t", "--target", dest="target", required=True)
parser.add_argument("-u", "--user", "--username", dest="username", required=True)
parser.add_argument("-p", "--pass", "--password", dest="password", required=True)
2023-05-02 15:17:59 +00:00
parser.add_argument(
"-k",
"--kerberos",
action="store_true",
required=False,
help="Use kerberos authentication",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
required=False,
help="Display full command output",
)
parser.add_argument(
"-e",
"--errors",
action="store_true",
required=False,
help="Display errors from commands",
)
2023-03-24 18:11:02 +00:00
parsed_args = parser.parse_args()
return parsed_args
2023-03-24 18:11:02 +00:00
def generate_commands(args):
2023-03-24 18:11:02 +00:00
lines = []
if args.kerberos:
kerberos = "-k"
else:
kerberos = ""
file_loc = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
commands_file = os.path.join(file_loc, "e2e_commands.txt")
with open(commands_file) as file:
for line in file:
if line.startswith("#"):
continue
2023-03-24 18:11:02 +00:00
line = line.strip()
2023-05-08 18:39:36 +00:00
line = line.replace("TARGET_HOST", args.target).replace("USERNAME", f'"{args.username}"').replace("PASSWORD", f'"{args.password}"').replace("KERBEROS ", kerberos)
2023-03-24 18:11:02 +00:00
lines.append(line)
return lines
def run_e2e_tests(args):
2023-03-24 18:11:02 +00:00
console = Console()
tasks = generate_commands(args)
2023-03-24 18:11:02 +00:00
result = subprocess.Popen(
"crackmapexec --version",
shell=True,
stdout=subprocess.PIPE,
2023-05-02 15:17:59 +00:00
stderr=subprocess.STDOUT,
)
2023-03-24 18:11:02 +00:00
version = result.communicate()[0].decode().strip()
2023-05-08 18:39:36 +00:00
with console.status(f"[bold green] :brain: Running {len(tasks)} test commands for cme v{version}...") as status:
passed = 0
failed = 0
2023-03-24 18:11:02 +00:00
while tasks:
task = tasks.pop(0)
result = subprocess.Popen(
str(task),
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
2023-05-02 15:17:59 +00:00
stderr=subprocess.STDOUT,
)
2023-03-24 18:11:02 +00:00
# pass in a "y" for things that prompt for it (--ndts, etc)
text = result.communicate(input=b"y")[0]
return_code = result.returncode
2023-03-24 18:11:02 +00:00
if return_code == 0:
console.log(f"{task.strip()} :heavy_check_mark:")
passed += 1
2023-03-24 18:11:02 +00:00
else:
console.log(f"[bold red]{task.strip()} :cross_mark:[/]")
failed += 1
if args.errors:
raw_text = text.decode("utf-8")
if "error" in raw_text.lower() or "failure" in raw_text.lower():
console.log(f"[bold red] Error Detected: {raw_text}")
if args.verbose:
# this prints sorta janky, but it does its job
console.log(f"[*] Results:\n{text.decode('utf-8')}")
console.log(f"Tests [bold green] Passed: {passed} [bold red] Failed: {failed}")
2023-03-24 18:11:02 +00:00
if __name__ == "__main__":
parsed_args = get_cli_args()
run_e2e_tests(parsed_args)