diff --git a/AWS/c2.yml b/AWS/c2.yml index b9505e6..cecb8a2 100644 --- a/AWS/c2.yml +++ b/AWS/c2.yml @@ -169,6 +169,39 @@ c2_vpc_id: "{{ vpc_result.vpc.id }}" # Store for cleanup reference when: not (use_shared_infra | bool and infra_state_file.stat.exists | default(false)) + - name: Set default redirector_ip for C2-only deployments + set_fact: + redirector_ip: "{{ operator_ip }}" + when: c2_only | default(false) | bool and redirector_ip is not defined + + - name: Load redirector_ip from infrastructure state + block: + - name: Check if infrastructure state file exists + stat: + path: "infrastructure_state_{{ deployment_id }}.json" + register: infra_state_check + + - name: Load infrastructure state + include_vars: + file: "infrastructure_state_{{ deployment_id }}.json" + name: infra_state + when: infra_state_check.stat.exists + + - name: Set redirector IP from infrastructure state + set_fact: + redirector_ip: "{{ infra_state.redirector_ip | default(operator_ip) }}" + when: infra_state_check.stat.exists and infra_state.redirector_ip is defined + + - name: Debug redirector IP + debug: + msg: "Using redirector IP: {{ redirector_ip | default('undefined') }}" + when: redirector_ip is undefined + + - name: Default to operator IP if redirector IP is still undefined + set_fact: + redirector_ip: "{{ operator_ip }}" + when: redirector_ip is undefined + - name: Create security group for C2 server amazon.aws.ec2_security_group: name: "{{ c2_name }}-sg" diff --git a/AWS/cleanup.yml b/AWS/cleanup.yml index 5ebff7d..ed1a305 100644 --- a/AWS/cleanup.yml +++ b/AWS/cleanup.yml @@ -389,33 +389,20 @@ - "VPC resources deleted: {{ vpc_check.vpcs | length == 0 }}" when: not disable_summary | default(false) - # STEP 14: Delete key pairs - - name: Find key pairs by name patterns - amazon.aws.ec2_key: - name: "{{ item }}" + # STEP 14: Delete key pairs - more reliable implementation + - name: Find key pairs related to deployment + amazon.aws.ec2_key_info: region: "{{ aws_region }}" - state: present - register: keys_check - ignore_errors: yes - with_items: - - "{{ redirector_name }}" - - "{{ c2_name }}" - - "{{ tracker_name }}" - - - name: Delete key pairs + register: all_key_pairs + + - name: Delete deployment related key pairs amazon.aws.ec2_key: - name: "{{ item.invocation.module_args.name }}" + name: "{{ item.key_name }}" region: "{{ aws_region }}" state: absent - loop: "{{ keys_check.results }}" - when: keys_check.results | length > 0 and item.failed is not defined and item.key is defined - register: deleted_keys + loop: "{{ all_key_pairs.key_pairs | selectattr('key_name', 'search', deployment_id) | list }}" + when: all_key_pairs.key_pairs | length > 0 - - name: Count deleted keys - set_fact: - cleanup_summary: "{{ cleanup_summary | combine({ - 'keypairs_deleted': (deleted_keys.results | default([]) | selectattr('changed', 'defined') | selectattr('changed') | list | length)}) }}" - # Add this after your existing key pair finding task - name: Find c2deploy key pairs amazon.aws.ec2_key: diff --git a/deploy.py b/deploy.py index 1645ae2..fe4f16b 100755 --- a/deploy.py +++ b/deploy.py @@ -1404,9 +1404,17 @@ def deploy_infrastructure(config): cleanup_resources(config, interactive=True) return False - # Extract and save redirector IP for C2 configuration + # Extract redirector IP from output if 'redirector_ip' in redirector_config: config['redirector_ip'] = redirector_config['redirector_ip'] + elif stdout: + # Try to extract redirector IP from stdout + import re + ip_pattern = re.compile(r'Redirector IP: (\d+\.\d+\.\d+\.\d+)') + ip_match = ip_pattern.search(stdout) + if ip_match: + config['redirector_ip'] = ip_match.group(1) + logging.info(f"Extracted redirector IP from output: {config['redirector_ip']}") # Deploy C2 if needed if not config.get('redirector_only'): @@ -1432,10 +1440,45 @@ def deploy_infrastructure(config): # Run cleanup before returning cleanup_resources(config, interactive=True) return False - - # Extract and save C2 IP for reference + + # Extract and save C2 IP from output if 'c2_ip' in c2_config: config['c2_ip'] = c2_config['c2_ip'] + elif stdout: + # Try to extract C2 IP from stdout + import re + ip_pattern = re.compile(r'C2 IP: (\d+\.\d+\.\d+\.\d+)') + ip_match = ip_pattern.search(stdout) + if ip_match: + config['c2_ip'] = ip_match.group(1) + logging.info(f"Extracted C2 IP from output: {config['c2_ip']}") + + # Also check for debugging output patterns + debug_pattern = re.compile(r'C2 Server IP: (\d+\.\d+\.\d+\.\d+)') + debug_match = debug_pattern.search(stdout) + if debug_match and not config.get('c2_ip'): + config['c2_ip'] = debug_match.group(1) + logging.info(f"Extracted C2 IP from deployment summary: {config['c2_ip']}") + + # Write deployment info to a state file for reference + if config.get('c2_ip') or config.get('redirector_ip'): + state_data = { + "deployment_id": config.get('deployment_id'), + "c2_ip": config.get('c2_ip', ''), + "redirector_ip": config.get('redirector_ip', ''), + "provider": provider, + "deployment_time": datetime.now().strftime('%Y-%m-%d %H:%M:%S') + } + state_file = f"deployment_state_{config.get('deployment_id')}.json" + with open(state_file, 'w') as f: + json.dump(state_data, f, indent=2) + logging.info(f"Saved deployment state to {state_file}") + + # Verbose info about IPs for debugging + if config.get('c2_ip'): + logging.info(f"C2 server deployed with IP: {config['c2_ip']}") + if config.get('redirector_ip'): + logging.info(f"Redirector deployed with IP: {config['redirector_ip']}") return True except Exception as e: