I decided to create this small document to collect few basic ideas about Ansible and how it can be used during a ‘day-to-day’ scenarios for pentest and red team projects. If you’re already familiar with Ansible – this document more likely will be a small ‘cheat sheet’ if you’d like to use Ansible to perform some actions during the projects. Anyhow… Enjoy and have fun! ;) Here we go...This time we'll start here:
Main goal of this small document was to learn and understand a bit more about Ansible and how it can be used during a quick ‘pentests’ and/or red team scenarios. (The idea was to create something like a cheat sheet rather than a proper full ‘Ansible tutorial’.) Use each paragraph here as a ‘dot’ that will (or at least “should”) in the end ‘connect with other dots’ to create “some more interesting ideas…” ;)
Similar to the previous adventures already described on the blog – to this exercise I used Ubuntu 20 VM started in VirtualBox. When your VM is ready to go – in a next step you should run those 2 commands to install Ansible:
# apt update
# apt install ansible -y
When apt will finish the installation we should be ready to continue.
Agent or Not (ssh-agent)
At this step – reading multiple online tutorials related to Ansible “for beginners” - you’ll find the part for “configure your ssh-agent”. My goal was to avoid that solution so all examples presented below are not using this ‘opportunity’. (Below you’ll get the idea.
Intro to Ad Hoc
One of my favorite ‘Ansible-feature’ is the possibility of using something that works similar to the one-liners – it’s called “Ad Hoc”. Pretty useful if you want to run one command against (for example) a target host (or hosts; see below for more details). Again I strongly recommend to read the fantastic manual (you can start here):
Example of this kind of (Ansible-)‘one-liner’ is presented below. We’ll try to connect to localhost as a specific (-u) user asking for the password first (-k option to “ask for password”). Using module (-m) called shell we’ll run an example command (-a) “echo $PATH”:
More Ansible modules you can find here .
Let’s move forward.
Intro to Inventory
TL;DR - According to the documentation:
“Ansible works against multiple managed nodes or “hosts” in your infrastructure at the same time, using a list or group of lists known as inventory. Once your inventory is defined, you use patterns to select the hosts or groups you want Ansible to run against.”
We’ll use an inventory file later [7, 8, 9].
Jumping to the next dot...
According to the documentation (and few previous words about the basics of my adventures with Ansible) now we’ll use an inventory file to perform some “basic tasks” against remote host(s). For my case I used only my “local lab/environment” but feel free to extend those tests/tasks to your own networks. Long story short: for now you can think about an inventory that this is our file “with target hosts/IP’s”. ;)
Ad-hoc connection to remote SSH
To connect to my-remote-host I created a new_inventory_file contains few IP’s:
Next I decided to update the file and add few of the ‘Ansible variables’ (you can read more about them here).
Updated file is presented below:
*(One of the reasons I did not “configure” the ssh-agent mentioned above. ;))
Inventory connection to remote SSH
Connecting to remote SSH using ad-hoc and our created new_inventory_file is presented on the screen below:
At this stage [1, 5, 8] the output should be pretty obvious.
Jumping to another dot…
Intro to Collections
Again, according to the docs:
“Collections are a distribution format for Ansible content that can include playbooks, roles, modules, and plugins.”
If we found a collection we can use for our pentest/redteam scenario(s) – we can install it using ansible-galaxy like it is presented in the docs:
We’ll use that when we’ll later prepare a future scenarios. See this page for more details:
For now we’ll use a basic/default examples but – if needed in the future – we’ll install more to do a specific action. See below for more details…
Intro to Playbooks
“Documentation is the key” so again: following the friendly manuals:
“Ansible Playbooks offer a repeatable, re-usable, simple configuration management (...). If you need to execute a task with Ansible more than once, write a playbook and put it under source control.”
Let’s see some basic example (grabbed somewhere online but modified a bit for our purposes). Very basic “playbook” below:
Let’s run this playbook with our new_inventory_file:
As you can see during this simple test we were able to run command on remote host(s) and get a response if needed. At this stage I strongly recommend to get back to  and read about variables and modules.
For now – we’re moving forward...
Ansible for Red Teams – Basic Scenarios
While I was reading more and more about Ansible and all the Modules and Collections  I started wondering how it can be used to perform a “daily basis tasks” for pentests or red team projects. Few simple examples you’ll find below.
First of all – an easy way to use Ansible as a ‘portscanner’? Searching for an online-answers I found an example of a playbook (I modified a bit;) ):
(As you remember from  about the “state” variable… ;)) State if sshd on remote host(s) is closed:
Next test – let’s run sshd to see the difference(s) in the response:State if sshd is started:
At this stage – preparing “more and more advanced examples of the scan” - I will leave to you as an exercise. ;) For example – let’s think about the parser for nmap’s output that is able to prepare an ansible-inventory-file for us… ;]
For now - moving to the next example case…
Example Spray with Ansible
Reading the docs we can easily see that there is an excellent opportunity to prepare a playbook and inventory-file to perform a ‘spraying attack’ .
For example let’s think about a linux-based-server-network where you can find hosts with enabled sshd. Let’s use Ansible to check if we are be able to access those hosts using default credentials, like “admin:admin” (or for our case: “tester:tester”).
I created a new inventory file with few IP’s (that I used in examples described before) and few of the “inventory variables” (you can read more about them on this page [7, 8]):
As you can see – this example is pretty similar to the “Intro to Inventory” part of this document – we talked above:
Let's move forward...
Reverse Shell with Ansible
Why we would use a ‘date’ or ‘pwd’ or ‘id’ command when we can use a reverse shell to spray the target network/hosts and graba shell on remote box?
Preparing a playbook.yml:
Starting a new playbook with our new inventory file:
More details - below:
New run with new sessions – presented on the screen below:
New playbook - new session(s):
As we mentioned before – we can always use a ‘one liner’ during our spray-attack-scenario:
(Un)Real Life Example: Juniper vs. Ansible
During few of my projects one of the machine on the Client’s network was indeed a Juniper. I decided to check if there is a way to access this box using Ansible. Below you’ll find few notes about it. To not spoil it too much for you - here we go:
Preparing an example inventory-file:
Verifying our idea on remote hosts:
For now it should be enough “to start reading and learn more about Ansible”. ;)
*(PDF version you can find here.)
Enjoy and have fun!
Interesting resources I found during the learning process:
1 - https://docs.ansible.com/ansible_community.html
2 – https://code610.blogspot.com
3 - https://docs.ansible.com/ansible/latest/user_guide/connection_details.html
4 - https://github.com/swisskyrepo/PayloadsAllTheThings/
5 - https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html
6 - https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html
7 - https://docs.ansible.com/ansible/2.7/user_guide/intro_inventory.html
8 - https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html
9 - https://docs.ansible.com/ansible/2.3/intro_inventory.html#hosts-and-groups
10 - https://docs.ansible.com/ansible/latest/user_guide/collections_using.html
11 - https://docs.ansible.com/ansible/latest/collections/index.html
12 - https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html
13 - https://attack.mitre.org/techniques/T1110/003/
14 - https://docs.ansible.com/ansible/latest/collections/junipernetworks/junos/index.html
15 - https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html
16 - Bonus link with "tips&tricks" from Spacelift - thanks!