NAT-TRAVERSE(1)

πŸ’– NAT-TRAVERSE πŸ’–

okay bestie, sit down, because this one is so cool. This is nat-traverse β€” a NAT gateway traversal utility β€” and it lives in the User Contributed Perl Documentation corner of your manual pages (that's the (1) up in the title, girlie πŸŽ€). It's a little Perl program, and it will absolutely punch a hole through two NAT gateways at once. No cap. πŸ•³οΈβœ¨


🌸 NAME 🌸

nat-traverse β€” NAT gateway traversal utility. That's the whole vibe. πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

Wanna spin up a simple text-only tunnel? Say less:

user@left  $ nat-traverse 40000:natgw-of-right:40001
user@right $ nat-traverse 40001:natgw-of-left:40000

So 40000 is an unused UDP port on left, and 40001 is an unused port on right. That's just the appetizer though β€” go peek at EXAMPLES for the full menu. πŸ“


⭐ VERSION ⭐

This document describes nat-traverse v0.7. She's vintage, she's iconic, she works. πŸ‘‘


πŸ”₯ DESCRIPTION πŸ”₯

nat-traverse establishes connections between nodes which are behind NAT gateways β€” i.e. hosts which do not have public IP addresses. Bestie, those hosts are shy, they don't have a public-facing number, and yet we're gonna get them talking anyway. 🫢

And bonus: you can set up a small VPN by using pppd on top of nat-traverse (check EXAMPLES, I beg you).

The truly iconic part: nat-traverse does not need an external server on the Internet, and it isn't necessary to reconfigure the involved NAT gateways, either. It just works out-of-the-box. No sysadmin begging, no firewall renegotiation, no drama. 🧚

Curious how?? Go read TECHNIQUE. It's giving sleight of hand.

⚠️ Limitation, and this one's real: nat-traverse does not work with gateways which change the port numbers. This is a fundamental problem of nat-traverse's design, as the changed port numbers are (in general) not predictable. So if your gateway is out here rewriting ports, she cannot help you. 😭

✨ OPTIONS ✨

  • local_port:peer:remote_port (required) β€” sets the local port to use and the remote address to connect to.
Heads up bestie: you have to give the IP address or hostname of the NAT gateway of the host you want to connect to, because the target host doesn't have a public IP address. Don't go typing the actual host's address, it's not reachable like that. πŸ™ˆ
  • --cmd=pppd... β€” runs the specified command after establishing the connection.

The command will be run with its STDIN and STDOUT bound to the socket, i.e. everything the command writes to STDOUT will be forwarded to the peer.

And if no command is specified? nat-traverse will relay input from STDIN to the peer and vice versa β€” i.e. nat-traverse degrades to netcat. Glow-down, but a useful one. ✨

  • --window=10 β€” sets the number of initial garbage packets to send. The default, 10, should work with most firewalls. (Yes, garbage packets are a feature. See TECHNIQUE. πŸ—‘οΈ)
  • --timeout=10 β€” sets the maximum number of seconds to wait for an acknowledgement by the peer. Patience is a virtue, but only up to a point. ⏳
  • --quit-after-connect β€” quits nat-traverse after the tunnel has been established successfully.

nat-traverse returns a non-0 statuscode to indicate that it wasn't able to establish the tunnel.

--quit-after-connect is useful if you want another program to use the tunnel. For example, you could configure OpenVPN to use the same ports as nat-traverse β€” thus OpenVPN would be able to cross NAT gateways. handing the tunnel off like a relay baton. πŸƒβ€β™€οΈπŸ’¨

  • --version, --help β€” they're here. The original gives no blurb, so I shan't invent one. πŸŽ€

🧚 TECHNIQUE 🧚

nat-traverse establishes connections between hosts behind NAT gateways without need for reconfiguration of the involved NAT gateways. The trick is social engineering the firewall, and honestly? she ate. πŸ’…

  1. Firstly, nat-traverse on host left sends garbage UDP packets to the NAT gateway of right. These packets are, of course, discarded by the firewall. Rude, but expected. πŸ—‘οΈ
  2. Then right's nat-traverse sends garbage UDP packets to the NAT gateway of left. These packets are not discarded, as left's NAT gateway thinks these packets are replies to the packets sent in step 1! The firewall is fooled. The firewall is a little gullible. We love her for it. 😌
  3. left's nat-traverse continues to send garbage packets to right's NAT gateway. These packets are now not dropped either, as the NAT gateway thinks the packets are replies to the packets sent in step 2. The lie compounds. Beautiful. πŸ¦‹
  4. Finally, both hosts send an acknowledgement packet to signal readiness. When these packets are received, the connection is established and nat-traverse can either relay STDIN/STDOUT to the socket or execute a program. Periodt. πŸŽ‰

🌈 EXAMPLES 🌈

πŸ’œ Setup of a small VPN with PPP

It's easy to setup a VPN (Virtual Private Network) by using the Point-to-Point Protocol Daemon, pppd:

root@left # nat-traverse \
    --cmd="pppd updetach noauth passive notty \
           ipparam vpn 10.0.0.1:10.0.0.2"
    40000:natgw-of-right:40001
root@right # nat-traverse \
    --cmd="pppd nodetach notty noauth"
    40001:natgw-of-left:40000

pppd creates a new interface, typically ppp0. Using this interface, you can ping 10.0.0.1 or 10.0.0.2. As you can see, pppd upgrades the data-only tunnel nat-traverse provides to a full IP tunnel. So you can establish reliable TCP connections over the tunnel, even though the tunnel uses UDP! And bestie, you could even add IPv6 addresses to ppp0 by running ip -6 addr add...! 🀯

⚠️ Note though that although this VPN is arguably a private network, it is not secured in any way. You may want to use SSH to encrypt the connection. Private β‰  safe, girlie. πŸ”

πŸ’™ Port Forwarding with netcat

You can use netcat to forward one of your local UDP or TCP ports to an arbitrary UDP or TCP port of the remote host, similar to ssh -L or ssh -R:

user@left  $ nat-traverse 10001:natgw-of-right:10002 \
      --cmd="nc -vl 20000"
user@right $ nat-traverse 10002:natgw-of-left:10001 \
      --cmd="nc -v localhost 22"

As soon as the tunnel is established (using UDP ports 10001 and 10002), left's TCP port 20000 is forwarded to right's SSH Daemon (TCP port 22):

user@some-other-host $ ssh -p 20000 user@left
# Will connect to right's SSH daemon!
⚠️ But do note that you lose the reliability of TCP in this example, as the actual data is transported via UDP; so this is only a toy example. If you want reliable streams, use PPP on top of nat-traverse, as described above. She's fun, she's not production. πŸ™ˆ

πŸ’š Setup of a VPN with OpenVPN

You can use OpenVPN <https://openvpn.net/> over nat-traverse if you want to have a secure VPN. Now that's the good stuff. πŸ”₯

Using OpenVPN over nat-traverse requires only one change to OpenVPN's configuration file β€” presuming that you don't want to use OpenVPN's multi-client mode: you have to adjust the code and lport options accordingly, for example:

# Options to add to left's and right's OpenVPN config:
port  60001
lport 60001

# Command to execute on left resp. right:
root@left  # until \
               nat-traverse --quit-after-connect 60001:right:60001 \
             do \
               sleep 5 \
             done; \
             openvpn [...]
root@right # until \
               nat-traverse --quit-after-connect 60001:left:60001 \
             do \
               sleep 5 \
             done; \
             openvpn [...]<!--

The until loop ensures that OpenVPN will not be started before nat-traverse was able to establish the connection. Sequencing, bestie, it's all about sequencing. ⏱️

Michael Kugele (michael (at) kugele.net) also reported a way to still be able to use OpenVPN's multi-client mode with nat-traverse: as all instances of nat-traverse have to use unique ports (because a connection is identified by the source/destination port combination), you've to use redirection rules to redirect the ports used by nat-traverse to the port the OpenVPN daemon listens on:

iptables -t nat -A PREROUTING -p udp \
  --dport $LPORT -j DNAT --to $HOST:$PORT
iptables -t nat -A PREROUTING -p udp \
  --dport $PORT -j REDIRECT --to-port $LPORT

$LPORT specifies the source port nat-traverse uses on the server side, and $HOST:$PORT is the address of the OpenVPN server. Shoutout Michael, absolute legend. πŸ‘‘


πŸ’€ LIMITATIONS πŸ’€

Only IPv4 is supported, nat-traverse won't work with IPv6 addresses. Drop me a note if you do need IPv6 support. πŸ“

nat-traverse does not work with gateways which change the port numbers. This is a fundamental problem of nat-traverse's design, as the changed port numbers are (in general) not predictable. Yes, I'm saying it twice, because it will get you twice. 😭


πŸŽ€ SEE ALSO πŸŽ€


🫢 AUTHOR 🫢

Copyright (C) 2005, 2012, 2017 Ingo Blechschmidt, <iblech@speicherleck.de>.

The source code repository is hosted at <https://gitlab.com/iblech/nat-traverse>. Go star it, it's free. ⭐


πŸ‘‘ LICENSE πŸ‘‘

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


πŸ’– okay that's the whole page bestie!! tiny sign-off before you go: this doc came from the User Contributed Perl Documentation collection, generated by perl v5.24.2, and dated 2017-10-28. She's old, she's stable, she's still crossing NATs. go build your tunnel. slay. πŸ¦‹βœ¨πŸ’