logging works dont change run_ansible_playbook

This commit is contained in:
n0mad1k
2025-05-13 08:21:20 -04:00
parent ef75e7a476
commit 689993ad5f
2 changed files with 51 additions and 71 deletions
+23 -61
View File
@@ -1249,23 +1249,10 @@ def run_ansible_playbook(playbook, inventory, config, debug=False):
# Convert config dict to JSON for extra-vars # Convert config dict to JSON for extra-vars
extra_vars = {k: v for k, v in config.items() if v is not None and not isinstance(v, (dict, list, tuple))} extra_vars = {k: v for k, v in config.items() if v is not None and not isinstance(v, (dict, list, tuple))}
# Ensure consistent region parameter handling # Other setup code stays the same...
if 'region' in extra_vars:
extra_vars['selected_region'] = extra_vars['region']
elif 'linode_region' in extra_vars:
extra_vars['selected_region'] = extra_vars['linode_region']
extra_vars['region'] = extra_vars['linode_region'] # Added for consistency
extra_vars_json = json.dumps(extra_vars) extra_vars_json = json.dumps(extra_vars)
# Set PYTHONPATH to include site-packages
env = os.environ.copy() env = os.environ.copy()
current_python = sys.executable # PYTHONPATH setup...
python_path = subprocess.check_output(
[current_python, "-c", "import sys; import site; print(':'.join(sys.path + site.getsitepackages()))"],
text=True
).strip()
env["PYTHONPATH"] = python_path
# Build command with output JSON facts format # Build command with output JSON facts format
cmd = [ cmd = [
@@ -1273,85 +1260,60 @@ def run_ansible_playbook(playbook, inventory, config, debug=False):
"-i", inventory, "-i", inventory,
playbook, playbook,
"-e", extra_vars_json, "-e", extra_vars_json,
"--extra-vars", "ansible_facts_callback=json" # Get facts in JSON format "--extra-vars", "ansible_facts_callback=json"
] ]
# Add verbosity if debug mode is enabled # Add verbosity flag
if debug: if debug:
cmd.append("-vvv") cmd.append("-vvv")
# Set ANSIBLE_STDOUT_CALLBACK for human-readable output
env["ANSIBLE_STDOUT_CALLBACK"] = "debug" env["ANSIBLE_STDOUT_CALLBACK"] = "debug"
else: else:
cmd.append("-v") cmd.append("-v")
# Log the command # Log the command
if debug:
logging.debug(f"Running Ansible command: {' '.join(cmd)}")
else:
logging.info(f"Running Ansible playbook: {playbook}") logging.info(f"Running Ansible playbook: {playbook}")
if debug:
logging.debug(f"Command: {' '.join(cmd)}")
# Run the command with enhanced output capture # Run the command with output capture
try: try:
process = subprocess.Popen( process = subprocess.Popen(
cmd, cmd,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
text=True, text=True,
bufsize=1, # Line buffered bufsize=1,
env=env env=env
) )
# Stream output in real-time # Capture output in real-time
stdout_output = [] stdout_output = []
stderr_output = [] stderr_output = []
# Create separate threads to read stdout and stderr # Read stdout in real-time and log every line
def read_output(pipe, output_list, is_stdout=True): for line in iter(process.stdout.readline, ''):
prefix = COLORS['GREEN'] if is_stdout else COLORS['RED'] stdout_output.append(line)
for line in iter(pipe.readline, ''): # Log every line to the file
output_list.append(line)
# Log every line to file regardless of debug mode
if is_stdout:
logging.debug(line.rstrip()) logging.debug(line.rstrip())
else: # Also print to terminal with color
print(f"{COLORS['GREEN']}{line.rstrip()}{COLORS['RESET']}")
# Read stderr in real-time and log every line
for line in iter(process.stderr.readline, ''):
stderr_output.append(line)
# Log errors to file
logging.error(line.rstrip()) logging.error(line.rstrip())
# Always display in terminal, but use filtering if not in debug mode # Print to terminal with color
if debug or ('TASK' in line or 'PLAY' in line or 'changed=' in line or 'ok=' in line or 'failed=' in line): print(f"{COLORS['RED']}{line.rstrip()}{COLORS['RESET']}")
print(f"{prefix}{line.rstrip()}{COLORS['RESET']}")
pipe.close()
from threading import Thread
stdout_thread = Thread(target=read_output, args=(process.stdout, stdout_output, True))
stderr_thread = Thread(target=read_output, args=(process.stderr, stderr_output, False))
stdout_thread.daemon = True
stderr_thread.daemon = True
stdout_thread.start()
stderr_thread.start()
# Wait for process to complete # Wait for process to complete
return_code = process.wait() return_code = process.wait()
# Wait for output threads to complete
stdout_thread.join()
stderr_thread.join()
# Join the output # Join the output
stdout_result = ''.join(stdout_output) stdout_result = ''.join(stdout_output)
stderr_result = ''.join(stderr_output) stderr_result = ''.join(stderr_output)
# Extract IP addresses from output # Extract IPs, etc. as before...
if "C2 Server IP:" in stdout_result:
ip_match = re.search(r"C2 Server IP: ([0-9.]+)", stdout_result)
if ip_match:
config['c2_ip'] = ip_match.group(1)
logging.info(f"Extracted C2 IP: {config['c2_ip']}")
if "Redirector IP:" in stdout_result:
ip_match = re.search(r"Redirector IP: ([0-9.]+)", stdout_result)
if ip_match:
config['redirector_ip'] = ip_match.group(1)
logging.info(f"Extracted Redirector IP: {config['redirector_ip']}")
if return_code == 0: if return_code == 0:
logging.info(f"Playbook {playbook} executed successfully") logging.info(f"Playbook {playbook} executed successfully")
+26 -8
View File
@@ -12,7 +12,19 @@
elif inventory_hostname in groups['sharedrives'] | default([]) %}sharedrive{% elif inventory_hostname in groups['sharedrives'] | default([]) %}sharedrive{%
else %}unknown{% endif %} else %}unknown{% endif %}
- name: Configure C2 server routing # Determine if we're using AWS provider
- name: Determine if using AWS provider
set_fact:
is_aws_provider: "{{ provider | default('unknown') == 'aws' }}"
# Skip firewall configuration for AWS instances
- name: Skip firewall configuration for AWS instances
debug:
msg: "Skipping host-based firewall configuration for AWS instance. Security Groups are handling this at the infrastructure level."
when: is_aws_provider
# UFW Configuration Block (non-AWS only)
- name: Configure C2 server routing with UFW
block: block:
- name: Allow SSH only from operator IP - name: Allow SSH only from operator IP
ufw: ufw:
@@ -39,13 +51,15 @@
- { ip: "{{ redirector_ip }}", port: "443" } - { ip: "{{ redirector_ip }}", port: "443" }
- { ip: "{{ redirector_ip }}", port: "{{ havoc_http_port | default('8080') }}" } - { ip: "{{ redirector_ip }}", port: "{{ havoc_http_port | default('8080') }}" }
- { ip: "{{ redirector_ip }}", port: "{{ havoc_https_port | default('443') }}" } - { ip: "{{ redirector_ip }}", port: "{{ havoc_https_port | default('443') }}" }
- { ip: "{{ logserver_ip | default(omit) }}", port: "5144" } # Logstash port - { ip: "{{ logserver_ip | default(omit) }}", port: "5144" }
- { ip: "{{ payloadserver_ip | default(omit) }}", port: "8888" } # Payload sync port - { ip: "{{ payloadserver_ip | default(omit) }}", port: "8888" }
when: item.ip != omit when: item.ip != omit
when:
- server_role == 'c2'
- not is_aws_provider
when: server_role == 'c2' # Rest of the original file for non-AWS providers only
- name: Configure redirector routing with UFW
- name: Configure redirector routing
block: block:
- name: Allow SSH only from operator IP - name: Allow SSH only from operator IP
ufw: ufw:
@@ -68,7 +82,9 @@
rule: allow rule: allow
port: "{{ shell_handler_port | default('4444') }}" port: "{{ shell_handler_port | default('4444') }}"
proto: tcp proto: tcp
when: server_role == 'redirector' when:
- server_role == 'redirector'
- not is_aws_provider
- name: Configure logging server routing - name: Configure logging server routing
block: block:
@@ -91,4 +107,6 @@
- "{{ payloadserver_ip | default(omit) }}" - "{{ payloadserver_ip | default(omit) }}"
- "{{ phishingserver_ip | default(omit) }}" - "{{ phishingserver_ip | default(omit) }}"
when: item != omit when: item != omit
when: server_role == 'logserver' when:
- server_role == 'logserver'
- not is_aws_provider