Назад | Перейти на главную страницу

iptables: статические карты эфемерных портов

У меня есть VPN на моем сервере Linux, который выполняет IPv4 NAT, например:

iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -s 10.8.0.0/24 -j ACCEPT
iptables -t nat -A POSTROUTING  -s 10.8.0.0/24 -o venet0 -j SNAT --to-source 1.2.3.4

Однако я хотел бы установить статические временные диапазоны портов для каждого внутреннего IP-адреса 10.8.0.0/24, чтобы упростить регистрацию того, какой клиент подключился к чему, возможно ли это с помощью iptables?

Например, порты 1000-2500 будут временными портами, используемыми 10.8.0.10, 10000-20000 для 10.8.0.20 и т. Д.

Я искал в Google, но ничего не нашел.

Отметьте цель MASQUERADE вместо SNAT: он поддерживает параметр:

--to-ports port[-port]
              This specifies a range of source ports to  use,  overriding  the
              default SNAT source port-selection heuristics (see above).  This
              is only valid if the rule also specifies one  of  the  following
              protocols: tcp, udp, dccp or sctp.

Никогда не пользуюсь, но тестирую! Но будет работать только по tcp, udp. У других вроде ICMP невезение, порта нет ...

Я знаю, что этот вопрос очень старый, поэтому я поделюсь своими выводами: Согласно справочной странице iptables по адресу https://linux.die.net/man/8/iptables

SNAT

This target is only valid in the nat table, in the POSTROUTING chain. It specifies that the source address of the packet should be modified (and all future packets in this connection will also be mangled), and rules should cease being examined. It takes one type of option:
--to-source ipaddr[-ipaddr][:port-port]
which can specify a single new source IP address, an inclusive range of IP addresses, and optionally, a port range (which is only valid if the rule also specifies -p tcp or -p udp). If no port range is specified, then source ports below 512 will be mapped to other ports below 512: those between 512 and 1023 inclusive will be mapped to ports below 1024, and other ports will be mapped to 1024 or above. Where possible, no port alteration will occur.
In Kernels up to 2.6.10, you can add several --to-source options. For those kernels, if you specify more than one source address, either via an address range or multiple --to-source options, a simple round-robin (one after another in cycle) takes place between these addresses. Later Kernels (>= 2.6.11-rc1) don't have the ability to NAT to multiple ranges anymore.

В --to-source ipaddr[-ipaddr][:port-port] опция позволяет указать порты. Хотя я не тестировал его, и буду рад, если кто-нибудь меня поправит, я думаю, теперь вы можете использовать что-то похожее на ответ @chicks, но с портами.

iptables -t nat -A POSTROUTING  -s 10.8.0.10/32 -o venet0 -j SNAT --to-source 1.2.3.4:1000-1500
iptables -t nat -A POSTROUTING  -s 10.8.0.20/32 -o venet0 -j SNAT --to-source 1.2.3.4:10000-20000

и т.п.

Конечно, это действительно только в том случае, если правило также указывает -p tcp или -p udp.

Я не думаю, что вы можете контролировать, какие порты используются для каких IP-адресов NAT. Однако, если у вас есть какие-либо дополнительные IP-адреса в публичном пространстве, вы можете воспользоваться этим, сопоставив те, которые вы хотите отслеживать:

iptables -t nat -A POSTROUTING  -s 10.8.0.10/32 -o venet0 -j SNAT --to-source 1.2.3.1
iptables -t nat -A POSTROUTING  -s 10.8.0.20/32 -o venet0 -j SNAT --to-source 1.2.3.2
iptables -t nat -A POSTROUTING  -s 10.8.0.0/24 -o venet0 -j SNAT --to-source 1.2.3.4