--- # tasks/port_randomization.yml # Task file to randomize ports for C2 infrastructure - name: Create port randomization script copy: src: "../files/randomize_ports.sh" dest: "/root/Tools/randomize_ports.sh" mode: '0700' owner: root group: root - name: Execute port randomization script shell: | cd /root/Tools && ./randomize_ports.sh register: randomize_result when: randomize_ports | default(true) | bool - name: Display port randomization results debug: msg: "{{ randomize_result.stdout_lines }}" when: randomize_ports | default(true) | bool - name: Store randomized ports in variables shell: | PORT_CONFIG="/root/Tools/port_config.json" if [ -f "$PORT_CONFIG" ]; then cat "$PORT_CONFIG" else echo '{"error": "Port configuration not found"}' fi register: port_config_result when: randomize_ports | default(true) | bool - name: Set port fact variables set_fact: randomized_http_port: "{{ (port_config_result.stdout | from_json).http_c2_port | default(8888) }}" randomized_https_port: "{{ (port_config_result.stdout | from_json).https_c2_port | default(443) }}" randomized_mtls_port: "{{ (port_config_result.stdout | from_json).mtls_c2_port | default(31337) }}" randomized_shell_handler_port: "{{ (port_config_result.stdout | from_json).shell_handler_port | default(shell_handler_port) }}" when: randomize_ports | default(true) | bool and port_config_result.rc == 0 - name: Update shell handler configuration with randomized port replace: path: "/root/Tools/shell-handler/persistent-listener.sh" regexp: "LISTEN_PORT=.*" replace: "LISTEN_PORT={{ randomized_shell_handler_port | default(shell_handler_port) }}" when: randomize_ports | default(true) | bool and port_config_result.rc == 0 - name: Update shell handler service with randomized port lineinfile: path: "/etc/systemd/system/shell-handler.service" regexp: 'Environment="LISTEN_PORT=' line: 'Environment="LISTEN_PORT={{ randomized_shell_handler_port | default(shell_handler_port) }}"' insertafter: '^\\[Service\\]' when: randomize_ports | default(true) | bool and port_config_result.rc == 0 - name: Reload and restart services systemd: daemon_reload: yes name: "{{ item }}" state: restarted with_items: - havoc - shell-handler when: randomize_ports | default(true) | bool and port_config_result.rc == 0