Saturday, January 7, 2017

Python-rq example

Introduction:
This post is about Python-RQ and job distribution. Python-RQ is a python library that utilizes redis queues to queue jobs. These jobs can be processed by multiple workers you have on the node. This library is very simple to utilize.

Using this library consists of three parts, which are Redis, job creator/distributors, and workers.
Redis is used to make queues for jobs. Both job creators and workers access Redis.
You have your job creation/distribution script, which puts jobs/tasks on Redis queue.
Your worker nodes take the jobs/tasks from Redis and perform their tasks.

Below you can see what one of the ways to set this up is:

You can have Redis and Job Creator and Worker run on the same machine or separate it all out depending on what your needs are.

Problem:
I needed to analyze bunch of binary files quickly and besides that, I just wanted to have infrastructure in place to let me run my python code distributed.

Installation:
I am using Proxmox to do all of this, in real life, you would probably multiple physical machines. I am using Ubuntu 14.04 containers.

We’ll start by installing redis-server and pip.
Run:
apt-get update
apt-get install python-pip redis-server
pip install rq rq-dashboard
To test installation, run:
redis-server –v
rq info
rq-dashboard --help
If everything installed correctly, you shouldn’t have gotten any errors.

Now you need to change redis configuration to make it bind to 0.0.0.0. (or whatever IP address you want Redis to bind to. FYI, Shodan does scan for Redis...)   Edit /etc/redis/redis.conf. Look for ‘bind 127.0.0.1’ and change it to ‘bind 0.0.0.0’
After changing the settings, run:
service redis-server restart

For your worker node and job creator, you would only install pip and rq, and other dependencies you need for your jobs or tasks. In the above example, I’m putting job distributor and redis on the same machine.

Simple example:
You will need to know IP of your redis server, in my case it’s 10.0.0.32.

On my worker node(s), I have the python script that I want to run.
File name: mod1.py
Code:
def func1(x,y):
   z = (x+y)*x
   return z

On my worker node, in the same directory as my mod1.py, I run the following command to start rqworker:
rqworker -u “redis://10.0.0.32”

On my job distribution container, I can run the following code to create and put jobs on Redis:
#Setup connection
from redis import Redis
from rq import Queue
q = Queue(connection=Redis(‘10.0.0.32’))
#Create a task
results = q.enqueue('mod1.func1',1,2)

Remember that mod1.py is our python file with func1 being the function we want to use from that file. We’re also running rqworker in the same directory as that file.
results.result variable will contain the returned value.


Another thing you installed is rq-dashboard. Rq-dashboard is pretty awesome looking webUI that lets you see your queues, jobs, and workers that are connected. If you have any jobs that fail, you can requeue them or cancel them from the webUI.

You can run it by typing rq-dashboard then visit http://IP_OF_SERVER:9181/ to view the dashboard.
Here are some screenshots:

Next time (assuming I have more time), I’ll try to cover using python-rq for PE file analysis.

Resources:

Thursday, January 5, 2017

Creating a VPN Pentest lab

I recommend looking at this first: http://boredhackerblog.blogspot.com/2016/12/setting-up-pentesting-lab-for-school.html

Introduction:
In this post, I’ll be focusing on setting up a pentesting lab that allows students to VPN in and attack virtual machines. I will be using ESXi, Pfsense, and OpenVPN. You are not limited to using ESXi, pfsense, and OpenVPN. First time I set this up, I ended up using PPTP VPN instead. I don’t recommend using PPTP.

Goal was to allow students to remotely hack machines and improve their skills. It would be better if they could do attacks from home instead of them having to be at the school just to learn this.


ESXi:
If you’re not familiar with ESXi then imagine using Virtualbox or VMWare workstation but imagine having a dedicated host for just virtualization.
You can learn more about ESXi here: https://en.wikipedia.org/wiki/VMware_ESXi
If you’re a university student, check to see if you can get a free license. Alternatively, you can use Proxmox VE: https://www.proxmox.com/en/proxmox-ve Proxmox supports VM’s and containers.

My specs for the server I’m running ESXi on:
8 CPUs x Intel(R) Xeon(R) CPU E5410 @ 2.33GHz (2 sockets * 4 cores/per socket)
32 GB RAM
700GB Storage

Networking:
This is what our overall setup will look like:

VPN Network will be 10.0.8.0/24
LAN Network (with our vulnerable VM’s) will be 192.168.1.0/24

Students will connect to WAN IP address, which will have Pfsense port forwarding VPN port to OpenVPN box. I realize that I can run OpenVPN server on Pfsense, but for me it was easier to setup OpenVPN server on Ubuntu using a script that I found.

ESXi Networking:
You will at least have 2 networks. One will be attached to an interface, one will not be attached to an interface.
Network that’s attached to an interface is your WAN. Network that’s not is your LAN.
You can create more networks, especially if you want people to learn tunneling and pivoting.

To set up WAN, login to ESXi with vSphere Client or Web Client. Go to Configuration, then Networking.
Click “Add Networking…” on the top right corner. You should see something similar to this:

Leave Virtual Machine option selected, then click Next.
You should see a prompt similar to one shown below:


Select the vSwitch that is connected to your WAN network / the network you want to have your OpenVPN server running on. Click Next.
Configure Network Label and VLAN ID (VLAN ID is optional). My Network Label is “Attacker_IN.” Click Next.
You should see your network configuration preview. If it looks good, click Finish.

Now we’ll create LAN / network for our vulnerable VM’s.
You will click “Add Networking…” again, click Next. Instead of selecting an option to use a vSwitch, we will select “Create vSphere standard switch” as shown in the screenshot below.
Click Next, then add your Network Label. I used “Vuln1” for my setup. Click Next again.
Your preview should look similar to the screenshot below:


Click Finish to create the vSwitch.

Our Pfsense firewall will be on both vuln1 network and Attacker_IN. This will allow people from WAN to connect in and access vuln1 network.


Pfsense:
Now that we have our network configured, we can configure pfsense. Pfsense is a firewall/router. You can download pfsense here: https://pfsense.org/ You can use another alternative as well. You should upload the ISO to your datastore.

You need to create a virtual machine. You should have similar configuration to the screenshot below:
Make sure your CD/DVD drive is connected at the start up so you can boot from it and install pfsense.

Do the default installation. Pfsense should automatically recognize your WAN and your LAN. If you have DHCP, then it should get an IP address, alternatively, you can always go in and change it to static.


Management:
You can manage Pfsense via webUI. I highly recommend downloading Kali ISO and creating a virtual machine on Vuln1/LAN network. If you went with the default settings, than pfsense will have IP address of 192.168.1.1. The default username is admin and the password is pfsense.

Visit the IP address via your browser and finish up the configuration and change your password.

The management machine should normally be turned off, unless you need to use it to configure pfsense. You also can configure pfsense via command line.


OpenVPN Server:
For OpenVPN server, I will be using Ubuntu Server 16.04. You can download it from here: https://www.ubuntu.com/download/server
After I have Ubuntu server installed, I will be installing OpenVPN on it using the script here: https://github.com/Angristan/OpenVPN-install We will also be able to manage OpenVPN users using that script. (fyi, always be careful when using scripts written by other people…, don’t just git clone and run random shit. I’ve made this mistake before.)

Your VM configuration should look similar to the screenshot:
You will need to adjust your RAM and CPU based on how many users you have. This is the first time I’m setting this up, so I don’t know if my RAM and CPU settings will work perfectly or not.

After Ubuntu server is installed, you will need to give it a static IP address.
Edit /etc/network/interfaces to look similar to this:

Reboot the system and confirm that IP address is correct.

You may want to add OpenVPN repo so you can get OpenVPN 2.4: https://community.openvpn.net/openvpn/wiki/OpenvpnSoftwareRepos

Now we can set up OpenVPN service.
Run the following commands (obtained from project github page):
wget https://raw.githubusercontent.com/Angristan/OpenVPN-install/master/openvpn-install.sh
chmod +x openvpn-install.sh

Run the script as root.
You can go with all of the default recommended settings. I decided to select the fastest algorithms.

Since our OpenVPN server is running being NAT, you will see this prompt:

Enter your WAN IP address here. Later on, we’ll set up port forwarding from pfsense to OpenVPN.

After you’re done entering the IP address, it should have generated client.ovpn file for you. This file is given to your client. We’re gonna use this for testing, then add additional users. Additional users will have their own .ovpn files.

You will also need to install openssh-server (apt-get install openssh-server). This will allow you to SSH in, in order to add or retrieve files.

We’ll modify our Pfsense settings now. Start up your management VM and login to pfsense webUI.

Click Firewall, then NAT, and that will get you to port forwarding page. Add a new rule.
It should look similar to this:
I changed the Protocol to UDP. I changed Destination port range to OpenVPN. I also changed Redirect target IP to our OpenVPN server. Redirect target port is also changed to OpenVPN. Add and apply the rule.

Please add another rule for port 22/SSH, so we can remotely SSH into our OpenVPN machine in order to retrieve .ovpn files. If you clone the old rule, change the protocol to TCP. I wasted like 10 fucking minutes trying to figure out why I couldn’t connect to the SSH server.

You will also need to go to Firewall / Rules / LAN. You will two rules for allowing traffic out.


We will be disabling those. You can do this by clicking the green check marks next to the rules. Click Apply after doing that. You should see the following after that:

All of the outgoing traffic will now be disabled.
The reason for this is, we don’t want students to VPN in, then visit other websites and use the lab as a proxy.

Try pinging 8.8.8.8 to confirm that outgoing traffic has been disabled.

Now we can remotely SSH into our OpenVPN server, retrieve .ovpn file, then test our connection.

On the client machine, you will need openvpn (2.4) client and SCP client. I followed the instructions here to add the repo and get openvpn 2.4: https://community.openvpn.net/openvpn/wiki/OpenvpnSoftwareRepos

After you have openvpn installed, you can connect to VPN server by running: openvpn --config client.ovpn

After you’re connected, ping/curl some of the machines to make sure everything is working. Try going out to the internet and make sure you can’t.

If everything seems to be working, then we’re done with our connection infrastructure.

Now we have to create more .ovpn files for our clients and set up vulnerable machines, then we’re done.

To create more .ovpn files, just re-run the openvpn-install.sh again, as root.
You will see a menu, as shown below. Enter 1, give a meaningful client name (This will be in the logs) and press enter. The script will create .ovpn file.

You can also remove clients with the script.

Now you can share .ovpn files with your students and have them use your pentest lab. If you care about logging, then add a client for each student.


Now you can set up vulnerable virtual machines. All of the vulnerable virtual machines will be put on vuln1 network.

Vulnerable VM's:
There are some of the vulnerable VM’s I had when I first built the lab (ignoring management and pfsense):

I generally prefer vulnerable ISO’s, since I don’t have to take VM snapshot and I can just reboot if a student decides to do something to prevent other students from doing their attacks.

Here are links to some of the VM’s:

VulnHub and PentesterLab will be your best resources for premade VM’s. The downside is, students will be able to find solutions rather quickly. You can always make your own VM’s. In my screenshot, you’ll see XP and Win7.


Additional considerations:
I highly recommend taking snapshots of virtual machines that are not ISO based. If anything goes wrong, you can always revert back to known-good configuration. Another thing you can do, which I haven’t gotten to yet, is to use ESXi command line utility and write a script to power off, revert, and start the VM’s every couple of hours.

To have students keep track of how they’re doing. You can use something like CTFd (https://github.com/isislab/CTFd) and create questions with different points, based on different VM’s. if a student is able to pwn a VM, they can submit something like “pwned”. Of course they can submit that for all VM’s at the same time but it’s there for them to track their progress so whatever.

Letting students access:
When you want students to access the lab and when you’re providing them .ovpn files, you need to give them additional information about what they can and cannot do in the lab.
This is what I sent out last time I had the pentest lab working:
“You are allowed to attack the IPs in the following range(s): X.X.X.X - X.X.X.X
Do NOT attack: X.X.X.X
You CANNOT use the following attacks: IP or ARP Spoofing, DoS”

Since I was running my pentest lab as a side project and not for any teacher or class, I didn’t expect students to dedicate too much time to it but I did ask them for monthly writeups (either public them on blog or email me) on what they did on the lab.

Next time, I’ll cover setting up a VNC based lab.

If I made mistakes, if you have questions, or have more ideas then leave comments. Since school is about to start soon, I can’t guarantee I’ll respond. Hopefully this post was useful to someone.