Eu tenho o seguinte layout de rede:
Com a conexão VPN estabelecida, quero que um usuário na Internet possa se conectar à porta 555 no servidor 192.168.1.24, usando o endereço IP do servidor VPS; isto é, 189.182.5.5:555.
Quais são as regras de firewall / iptable que eu preciso colocar no Ubuntu e no DD-WRT para habilitar isso? Eu tenho controle total sobre o roteador DD-WRT, bem como o Ubuntu VPS, e o provedor VPS me permite abrir portas em seu firewall.
Editar 1:
Para esclarecer, 555 é um número de porta arbitrário. A realidade é que existem cerca de 7 portas (TCP e UDP) que precisam ser alcançadas, mas tenho certeza de que se eu puder mostrar como fazer uma, vou descobrir o resto. Também não sou contra a implementação de um proxy SOCKS ou túnel SSH entre o DD-WRT e o VPS, se isso for uma maneira mais fácil de fazer isso.
Editar 2:
Eu fiz mais alguns testes. Como primeiro passo, eu só quero que o servidor VPS encaminhe os pacotes recebidos no eth0 na porta 555 para a VPN (tun0). Aqui está o que eu tenho:
Ativei o rastreamento nas tabelas brutas PREROUTING e OUTPUT para a porta 555 com estes comandos:
iptables -t raw -A PREROUTING -p tcp -m tcp --dport 555 -j TRACE
iptables -t raw -A OUTPUT -p tcp -m tcp --dport 555 -j TRACE
A tabela bruta agora se parece com isso:
-P PREROUTING ACCEPT
-P OUTPUT ACCEPT
-A PREROUTING -p tcp -m tcp --dport 555 -j TRACE
-A OUTPUT -p tcp -m tcp --dport 555 -j TRACE
A tabela NAT é assim (eu adicionei apenas a última linha, o resto que eu estou assumindo é adicionado pelo servidor OpenVPN)
-P PREROUTING ACCEPT
-P INPUT ACCEPT
-P OUTPUT ACCEPT
-P POSTROUTING ACCEPT
-A POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to-source 189.182.5.5
-A POSTROUTING -o eth0 -j MASQUERADE
-A POSTROUTING -o tun0 -j MASQUERADE
O iptables padrão tem esta aparência:
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 1194 -j ACCEPT
-A INPUT -p udp -m udp --dport 1194 -j ACCEPT
-A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i tun0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i eth0 -j ACCEPT
Agora eu executo um traceroute TCP específico da porta no nmap com este comando:
nmap -sT -sU -p T:[555] -n --traceroute 189.182.5.5
As seguintes entradas aparecem no log do VPS:
Sep 13 08:09:01 ubuntu-768mb kernel: [480631.284612] TRACE: raw:PREROUTING:policy:2 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32647 DF PROTO=TCP SPT=31881 DPT=555 SEQ=953464685 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:01 ubuntu-768mb kernel: [480631.284671] TRACE: nat:PREROUTING:policy:1 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32647 DF PROTO=TCP SPT=31881 DPT=555 SEQ=953464685 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:01 ubuntu-768mb kernel: [480631.284704] TRACE: filter:INPUT:policy:6 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32647 DF PROTO=TCP SPT=31881 DPT=555 SEQ=953464685 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:01 ubuntu-768mb kernel: [480631.284722] TRACE: nat:INPUT:policy:1 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32647 DF PROTO=TCP SPT=31881 DPT=555 SEQ=953464685 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:02 ubuntu-768mb kernel: [480631.956697] TRACE: raw:PREROUTING:policy:2 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32649 DF PROTO=TCP SPT=31881 DPT=555 SEQ=953464685 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:02 ubuntu-768mb kernel: [480631.956750] TRACE: nat:PREROUTING:policy:1 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32649 DF PROTO=TCP SPT=31881 DPT=555 SEQ=953464685 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:02 ubuntu-768mb kernel: [480631.956773] TRACE: filter:INPUT:policy:6 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32649 DF PROTO=TCP SPT=31881 DPT=555 SEQ=953464685 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:02 ubuntu-768mb kernel: [480631.956783] TRACE: nat:INPUT:policy:1 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32649 DF PROTO=TCP SPT=31881 DPT=555 SEQ=953464685 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:02 ubuntu-768mb kernel: [480632.150981] TRACE: raw:PREROUTING:policy:2 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32651 DF PROTO=TCP SPT=31882 DPT=555 SEQ=137592908 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:02 ubuntu-768mb kernel: [480632.151061] TRACE: nat:PREROUTING:policy:1 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32651 DF PROTO=TCP SPT=31882 DPT=555 SEQ=137592908 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:02 ubuntu-768mb kernel: [480632.151095] TRACE: filter:INPUT:policy:6 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32651 DF PROTO=TCP SPT=31882 DPT=555 SEQ=137592908 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:02 ubuntu-768mb kernel: [480632.151113] TRACE: nat:INPUT:policy:1 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32651 DF PROTO=TCP SPT=31882 DPT=555 SEQ=137592908 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:03 ubuntu-768mb kernel: [480632.824527] TRACE: raw:PREROUTING:policy:2 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32653 DF PROTO=TCP SPT=31882 DPT=555 SEQ=137592908 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:03 ubuntu-768mb kernel: [480632.824583] TRACE: nat:PREROUTING:policy:1 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32653 DF PROTO=TCP SPT=31882 DPT=555 SEQ=137592908 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:03 ubuntu-768mb kernel: [480632.824614] TRACE: filter:INPUT:policy:6 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32653 DF PROTO=TCP SPT=31882 DPT=555 SEQ=137592908 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 08:09:03 ubuntu-768mb kernel: [480632.824633] TRACE: nat:INPUT:policy:1 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=244 ID=32653 DF PROTO=TCP SPT=31882 DPT=555 SEQ=137592908 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Editar 3:
Eu tenho o VPS para encaminhar pacotes recebidos na porta 555 para o dispositivo tun0, usando o seguinte comando:
iptables -t nat -A PREROUTING -p tcp --dport 555 -j DNAT --to 10.8.0.2
Isso funciona porque o servidor VPN já cria uma rota para a sub-rede 10.8.0 / 24 que direciona a interface tun0:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 185.181.9.1 0.0.0.0 UG 0 0 0 eth0
10.8.0.0 * 255.255.255.0 U 0 0 0 tun0
185.181.9.0 * 255.255.255.0 U 0 0 0 eth0
Agora, no log, posso ver as seguintes entradas:
Sep 13 11:55:03 ubuntu-768mb kernel: [ 6330.222757] TRACE: nat:PREROUTING:rule:2 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=189.182.5.5 LEN=52 TOS=0x00 PREC=0x00 TTL=117 ID=4482 DF PROTO=TCP SPT=38891 DPT=555 SEQ=3082300013 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 11:55:03 ubuntu-768mb kernel: [ 6330.222778] TRACE: mangle:INPUT:policy:1 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=10.8.0.1 LEN=52 TOS=0x00 PREC=0x00 TTL=117 ID=4482 DF PROTO=TCP SPT=38891 DPT=555 SEQ=3082300013 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 11:55:03 ubuntu-768mb kernel: [ 6330.222789] TRACE: filter:INPUT:policy:1 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=10.8.0.1 LEN=52 TOS=0x00 PREC=0x00 TTL=117 ID=4482 DF PROTO=TCP SPT=38891 DPT=555 SEQ=3082300013 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 11:55:03 ubuntu-768mb kernel: [ 6330.222799] TRACE: nat:INPUT:policy:1 IN=eth0 OUT= MAC=00:16:3e:0a:d3:67:80:71:1f:ca:8c:01:08:00 SRC=129.232.161.114 DST=10.8.0.1 LEN=52 TOS=0x00 PREC=0x00 TTL=117 ID=4482 DF PROTO=TCP SPT=38891 DPT=555 SEQ=3082300013 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Sep 13 11:52:28 ubuntu-768mb kernel: [ 6175.035548] TRACE: mangle:POSTROUTING:policy:1 IN= OUT=tun0 SRC=129.232.161.114 DST=10.8.0.2 LEN=52 TOS=0x00 PREC=0x00 TTL=116 ID=4265 DF PROTO=TCP SPT=38840 DPT=555 SEQ=3344052408 ACK=0 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405060103030801010402)
Você verá que estou usando uma regra DNAT para alterar o destino do pacote para 10.8.02, que é o endereço IP de tun0 no lado do cliente. Não tenho certeza se deveria ser 10.8.0.1, que é o endereço tun0 do servidor VPN?