Files
CoM-c2itall/AWS/c2.yml
T
2025-05-02 10:01:07 -04:00

156 lines
5.1 KiB
YAML

---
# AWS C2 Deployment Playbook
- name: Deploy AWS C2 server
hosts: localhost
gather_facts: false
connection: local
vars_files:
- vars.yaml
vars:
# Default values for required variables
ssh_user: "{{ ssh_user | default('kali') }}"
aws_region: "{{ aws_region | default(aws_region_choices | random) }}"
instance_type: "{{ aws_instance_type | default('t2.medium') }}"
# Generate random instance name if not provided
c2_name: "{{ c2_name | default('node-' + 9999999999 | random | to_uuid | hash('md5') | truncate(8, True, '')) }}"
tasks:
- name: Validate required AWS credentials
assert:
that:
- aws_access_key is defined and aws_access_key != ""
- aws_secret_key is defined and aws_secret_key != ""
fail_msg: "AWS credentials are required. Set aws_access_key and aws_secret_key."
- name: Create EC2 key pair for C2
amazon.aws.ec2_key:
access_key: "{{ aws_access_key }}"
secret_key: "{{ aws_secret_key }}"
name: "{{ c2_name }}"
region: "{{ aws_region }}"
state: present
register: c2_key_pair
- name: Save C2 private key locally
copy:
content: "{{ c2_key_pair.key.private_key }}"
dest: "~/.ssh/{{ c2_name }}.pem"
mode: "0600"
when: c2_key_pair.changed and c2_key_pair.key.private_key is defined
- name: Create a security group for the C2 server
amazon.aws.ec2_security_group:
name: "{{ c2_name }}-sg"
description: "Security group for C2 server {{ c2_name }}"
vpc_id: "{{ vpc_id }}"
region: "{{ aws_region }}"
rules:
- proto: tcp
ports:
- 22
- 80
- 443
- "{{ havoc_teamserver_port | default(40056) }}"
- "{{ havoc_http_port | default(8080) }}"
- "{{ havoc_https_port | default(443) }}"
- "{{ gophish_admin_port | default(3333) }}"
cidr_ip: 0.0.0.0/0
rules_egress:
- proto: -1
cidr_ip: 0.0.0.0/0
state: present
register: security_group
- name: Set region for AWS C2 server
set_fact:
aws_c2_region: "{{ aws_region | default(region, true) | default('us-east-1', true) }}"
when: aws_region_choices is not defined or aws_region_choices|length == 0
- name: Set random region for AWS C2 server
set_fact:
aws_c2_region: "{{ aws_region_choices | random }}"
when: aws_region_choices is defined and aws_region_choices|length > 0 and aws_c2_region is not defined
- name: Set AMI ID for the selected region
set_fact:
ami_id: "{{ ami_map[aws_redirector_region] }}"
when: ami_map is defined and aws_redirector_region in ami_map
- name: Launch EC2 instance for C2 server
amazon.aws.ec2_instance:
name: "{{ c2_name }}"
key_name: "{{ aws_keypair_name }}"
instance_type: "{{ instance_type | default('t2.medium') }}"
security_group: "{{ c2_name }}-sg"
image_id: "{{ ami_id }}"
region: "{{ aws_c2_region }}"
state: present
wait: yes
tags:
Name: "{{ c2_name }}"
register: ec2
- name: Set c2_ip for later use
set_fact:
c2_ip: "{{ c2_instance.instances[0].public_ip_address }}"
c2_instance_id: "{{ c2_instance.instances[0].instance_id }}"
- name: Wait for C2 SSH to be available
wait_for:
host: "{{ c2_ip }}"
port: 22
delay: 30
timeout: 300
state: started
- name: Add C2 to inventory
add_host:
name: "c2"
groups: "c2servers"
ansible_host: "{{ c2_ip }}"
ansible_user: "{{ ssh_user }}"
ansible_ssh_private_key_file: "~/.ssh/{{ c2_name }}.pem"
ansible_ssh_common_args: "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
- name: Configure C2 server
hosts: c2servers
become: true
gather_facts: true
vars_files:
- vars.yaml
vars:
redirector_ip: "{{ redirector_ip | default('127.0.0.1') }}"
c2_subdomain: "{{ c2_subdomain | default('mail') }}"
tasks:
- name: Wait for apt to be available
apt:
update_cache: yes
register: apt_result
until: apt_result is success
retries: 5
delay: 10
- name: Include common tool installation tasks
include_tasks: "../tasks/install_tools.yml"
- name: Include common C2 configuration tasks
include_tasks: "../tasks/configure_c2.yml"
- name: Include common security hardening tasks
include_tasks: "../tasks/security_hardening.yml"
- name: Include common mail server configuration tasks
include_tasks: "../tasks/configure_mail.yml"
- name: Print deployment summary
debug:
msg:
- "C2 Server Deployment Complete!"
- "-----------------------------"
- "C2 Server IP: {{ ansible_host }}"
- "C2 Server Domain: {{ c2_subdomain }}.{{ domain }} (Update DNS A record)"
- "GoPhish Admin Port: {{ gophish_admin_port }}"
- "SSH Key: ~/.ssh/{{ hostvars['localhost']['c2_name'] }}.pem"
when: not disable_summary | default(false)