Использование iptables для блокировки всего интернет-трафика, кроме определенного порта

У меня есть сервер Emby, работающий на Ubuntu Server 20.04 LTS, и я хотел бы настроить iptables для блокировки всех входящих подключений из Интернет, за исключением порта 8920, но разрешает обычные входящие соединения (ssh и т. д.) от узлов в локальной сети. Это возможно? (Я делаю это, потому что мой маршрутизатор Zyxel, EMG3425-Q10A, неправильно выполняет переадресацию портов. Все еще работаю над решением этой проблемы.)

0
задан 10 May 2021 в 18:13

1 ответ

Вот сценарий iptables. LAN (локальная сеть) и определения интерфейсов предназначены для моего тестового компьютера и должны быть изменены в соответствии с требованиями пользователей:

doug@s19:~/iptables/misc$ cat ask1337350
#!/bin/sh
FWVER=0.01
#
# ask1337350 Smythies 2021.05.10 Ver:0.01
#       See here:
#       https://askubuntu.com/questions/1337350/using-iptables-to-block-all-internet-originating-traffic-except-for-a-specific-p
#       run as sudo on s19.
#
#       Note: These rules likely need to be merged with
#       any existing iptables rules set.

echo "Loading ask1337350 rule set version $FWVER..\n"

# The location of the iptables program
#
IPTABLES=/sbin/iptables

#Setting the EXTERNAL and INTERNAL interfaces and addresses for the network
#
# Set for Smythies s19 computer (for testing). Edit for ask1337350's computer.
# EXTIF="enp3s0" no,no,no use the bridge br0, or everything breaks, big time.
EXTIF="br0"
EXTIP="192.168.111.136"
NETWORK="192.168.111.0/24"
UNIVERSE="0.0.0.0/0"

# Clearing any previous configuration
# Be careful here. I can do this on s18, but do not know
# about vxsa4's computer.
#
echo "  Clearing any existing rules and setting default policies.."
$IPTABLES -P INPUT DROP
$IPTABLES -F INPUT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -F OUTPUT
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -F FORWARD
$IPTABLES -t nat -F

# Delete user defined chains
$IPTABLES -X
# Reset all IPTABLES counters
$IPTABLES -Z
# Smythies: While my references do not have it, I think this is needed.
$IPTABLES -t nat -Z

# loopback interfaces are valid.
#
$IPTABLES -A INPUT -i lo -s $UNIVERSE -d $UNIVERSE -j ACCEPT

# Allow any related traffic coming back to the server in.
#
$IPTABLES -A INPUT -i $EXTIF -s $UNIVERSE -d $EXTIP -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allow in any LAN traffic
#
$IPTABLES -A INPUT -s $NETWORK -i $EXTIF -j ACCEPT

# Also allow in port 8920 traffic from anywhere
# The question does not specify a protocol. Do both.
#
$IPTABLES -A INPUT --protocol udp --destination-port 8920 -i $EXTIF -j ACCEPT
$IPTABLES -A INPUT --protocol tcp --destination-port 8920 -i $EXTIF -j ACCEPT

# Do not allow in anything else
# Could also just fall through to default policy here, but sometimes a logging rule is also desired.
#
$IPTABLES -A INPUT -i $EXTIF -j DROP

# At this point carry on. This might need to be merged into whatever existing iptables rule set.
#
echo ask1337350 rule set version $FWVER done.

Даю это через короткое время:

doug@s19:~/iptables/misc$ sudo iptables -xvnL
Chain INPUT (policy DROP 0 packets, 0 bytes)
    pkts      bytes target     prot opt in     out     source               destination
       8      616 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
     133     7916 ACCEPT     all  --  br0    *       0.0.0.0/0            192.168.111.136      state RELATED,ESTABLISHED
      51     3355 ACCEPT     all  --  br0    *       192.168.111.0/24     0.0.0.0/0
       0        0 ACCEPT     udp  --  br0    *       0.0.0.0/0            0.0.0.0/0            udp dpt:8920
       0        0 ACCEPT     tcp  --  br0    *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8920
      19     5880 DROP       all  --  br0    *       0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    pkts      bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 90 packets, 12122 bytes)
    pkts      bytes target     prot opt in     out     source               destination
1
ответ дан 28 July 2021 в 11:42

Другие вопросы по тегам:

Похожие вопросы: