Python y VPNs (2)
Hi again, in the previous episode we say that with certain flexibility now we c an rotate our IPs easily using NordVPN, let's squeeze it a bit more.
This post is quite old, the mentioned API was deprecated logn ago, but I’ll keep the post here.
For convenience, I’ll upload to Github both the previous code and today’s code there, along with anything that might be useful.
I have nothing to do with the company. I’m probably not continuing the subscription, but as long at it’s valid, let’s use it for good.
The first episode was specific for that client, because we were using their own commands. In this episode let’s try to dig a bit deeper and start using OpenVPN.
Good, now we have a simple script that will iterate randomly through all the available IPs. What happens if we want to do it in parallel? I mean, we can have up to 5 clients simultaneously connected to the VPN, why not using them and having 5 different ips iterating in a same device? This would reduce a lot the time we might need in some operations.
LXC Containers
To achieve this virtualization, we are going to use LXC.
LXC is an operating-system-level virtualization method for running multiple isolated Linux systems on a control host using a single Linux kernel.
In short, we are going to have isolated Linux machines (containers) running in the same host, each one with its own network interface, leaving through the host. There’s only one drawback, the nordVPN client doesn’t work inside virtualized environments (nordVPN’s fault). This mean that we have to connect directly through openVPN with nordVPNś credentials.
Let’s start creating our LXC container and then we can see how use a workaround.
Creating our LXC container
Firstly we make sure that we have lxc and lxd installed, most probably we will, if not:
$ sudo apt-get install lxc
We make sure that we also have lxd which is an extension from lxc, it adds new functionality and facilitates the administration:
$ sudo snap install lxd
Once we have them both correctly installed let’s start the configuration of our containers with:
$ lxd init
root@prueba:~# lxd init
Would you like to use LXD clustering? (yes/no) [default=no]:
Do you want to configure a new storage pool? (yes/no) [default=yes]:
Name of the new storage pool [default=default]:
Would you like to connect to a MAAS server? (yes/no) [default=no]:
Would you like to create a new local network bridge? (yes/no) [default=yes]:
What should the new bridge be called? [default=lxdbr0]:
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
Would you like the LXD server to be available over the network? (yes/no) [default=no]:
Would you like stale cached images to be updated automatically? (yes/no) [default=yes]
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]:
root@prueba:~#
Now with:
$ lxc list
We can see the container list, which will obviously be empty:
root@prueba:~# lxc list
+------+-------+------+------+------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+------+-------+------+------+------+-----------+
Now we create a container:
$ lxc launch ubuntu:20.04 nombre_contenedor
And we check that, in fact, it has successfully created a container with the name vpn1, which will be already running and a working network interface:
root@prueba:~# lxc launch ubuntu:20.04 vpn1
Creating vpn1
Starting vpn1
root@prueba:~# lxc list
+------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| vpn1 | RUNNING | 10.42.55.134 (eth0) | fd42:3ede:c779:2b4e:216:3eff:fe8b:b3e4 (eth0) | CONTAINER | 0 |
+------+---------+----------------------+-----------------------------------------------+-----------+-----------+
root@prueba:~#
Now we enter into the aforementioned container:
$ lxc exec vpn1 /bin/bash/
root@prueba:~# lxc exec vpn1 /bin/bash
root@vpn1:~#
Perfect, now we are inside our container, now let’s see how to connect to the VPN.
We can manually connect to the nordVPN servers which config fiels, which are posted in a .zip in their site.
The problem is that they’re not updated. The majority of them doesn’t work, they dropped the connection, etc. Here is when I thought about the API that we mentioned on the fist episode. This API yields the updated active server list. In fact, I’ve realized that there were so many new servers available on this API, that weren’t on their .zip file.
So, we can get a config file and use it as a template as it comes with the TLZ Key that the client uses for verification against the server (this is a required step before using our user/pass). After we will extract the ip of the servers from their API and then access to their VPN.
$ openvpn –config archivo_configuracion –remote ip_server –auth-user-pass/etc/openvpn/secret
We will also need to have a file at /etc/openvpn/secret, which will ocnsist of our user and password, separated by \n (newline). This is not the safest approach, there are some solutions that try to avoid this, but we will keep it simple here.
Nice, so now combining the API extraction, the management from openvpn, and so on, I’ve uploaded it to the repository of Github
Now that this part is expained, let’s continue with the container.
Script usage
$ git clone https://github.com/b0n3sh/sh3llcon_2021.git
Now we just need to import vpn.py to our script/project and launch vpn.get_vpn(), which will give us a new ip. We have to execute this script with privileges. Once we install the required libraries from requirements.ex it would be reade to use.
This code doesn´t work because the API was deprecated sometime ago by nordVPN