Pull to refresh

Настройка UFW в Ubuntu Server 16.04 на примере BigBlueButton

Reading time8 min
Views22K
UFW (Uncomplicated Firewall) – это интерфейс iptables, предназначенный для упрощения процесса настройки брандмауэра. Инструмент iptables надёжный и гибкий, но новичку будет непросто настроить его самостоятельно. Я как раз новичок в этом деле.

По умолчанию в iptables включена политика ACCEPT и выглядет это так:



В нашем примере нет никаких ограничений. Политика по умолчанию — ACCEPT.

INPUT – ВХОДЯЩИЕ СОЕДИНЕНИЯ.
OUTPUT – ИСХОДЯЩИЕ СОЕДИНЕНИЯ.
FORWARD – МАРШРУТИЗАЦИЯ (НАПРИМЕР, НЕОБХОДИМО ПРОПИСАТЬ МАРШРУТ к компьютеру за шлюзом).

Включить ufw можно командой sudo ufw enable
И сразу же после этой команды политика iptables поменяется.
Вот как она выглядит на BigBlueButton после включения ufw:

Chain INPUT (policy DROP)
target     prot opt source               destination
ufw-before-logging-input  all  --  anywhere             anywhere
ufw-before-input  all  --  anywhere             anywhere
ufw-after-input  all  --  anywhere             anywhere
ufw-after-logging-input  all  --  anywhere             anywhere
ufw-reject-input  all  --  anywhere             anywhere
ufw-track-input  all  --  anywhere             anywhere

Chain FORWARD (policy DROP)
target     prot opt source               destination
ufw-before-logging-forward  all  --  anywhere             anywhere
ufw-before-forward  all  --  anywhere             anywhere
ufw-after-forward  all  --  anywhere             anywhere
ufw-after-logging-forward  all  --  anywhere             anywhere
ufw-reject-forward  all  --  anywhere             anywhere
ufw-track-forward  all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ufw-before-logging-output  all  --  anywhere             anywhere
ufw-before-output  all  --  anywhere             anywhere
ufw-after-output  all  --  anywhere             anywhere
ufw-after-logging-output  all  --  anywhere             anywhere
ufw-reject-output  all  --  anywhere             anywhere
ufw-track-output  all  --  anywhere             anywhere

Chain ufw-after-forward (1 references)
target     prot opt source               destination

Chain ufw-after-input (1 references)
target     prot opt source               destination
ufw-skip-to-policy-input  udp  --  anywhere             anywhere             udp dpt:netbios-ns
ufw-skip-to-policy-input  udp  --  anywhere             anywhere             udp dpt:netbios-dgm
ufw-skip-to-policy-input  tcp  --  anywhere             anywhere             tcp dpt:netbios-ssn
ufw-skip-to-policy-input  tcp  --  anywhere             anywhere             tcp dpt:microsoft-ds
ufw-skip-to-policy-input  udp  --  anywhere             anywhere             udp dpt:bootps
ufw-skip-to-policy-input  udp  --  anywhere             anywhere             udp dpt:bootpc
ufw-skip-to-policy-input  all  --  anywhere             anywhere             ADDRTYPE match dst-type BROADCAST

Chain ufw-after-logging-forward (1 references)
target     prot opt source               destination
LOG        all  --  anywhere             anywhere             limit: avg 3/min burst 10 LOG level warning prefix "[UFW BLOCK] "

Chain ufw-after-logging-input (1 references)
target     prot opt source               destination
LOG        all  --  anywhere             anywhere             limit: avg 3/min burst 10 LOG level warning prefix "[UFW BLOCK] "

Chain ufw-after-logging-output (1 references)
target     prot opt source               destination

Chain ufw-after-output (1 references)
target     prot opt source               destination

Chain ufw-before-forward (1 references)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere             icmp destination-unreachable
ACCEPT     icmp --  anywhere             anywhere             icmp source-quench
ACCEPT     icmp --  anywhere             anywhere             icmp time-exceeded
ACCEPT     icmp --  anywhere             anywhere             icmp parameter-problem
ACCEPT     icmp --  anywhere             anywhere             icmp echo-request
ufw-user-forward  all  --  anywhere             anywhere

Chain ufw-before-input (1 references)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ufw-logging-deny  all  --  anywhere             anywhere             ctstate INVALID
DROP       all  --  anywhere             anywhere             ctstate INVALID
ACCEPT     icmp --  anywhere             anywhere             icmp destination-unreachable
ACCEPT     icmp --  anywhere             anywhere             icmp source-quench
ACCEPT     icmp --  anywhere             anywhere             icmp time-exceeded
ACCEPT     icmp --  anywhere             anywhere             icmp parameter-problem
ACCEPT     icmp --  anywhere             anywhere             icmp echo-request
ACCEPT     udp  --  anywhere             anywhere             udp spt:bootps dpt:bootpc
ufw-not-local  all  --  anywhere             anywhere
ACCEPT     udp  --  anywhere             224.0.0.251          udp dpt:mdns
ACCEPT     udp  --  anywhere             239.255.255.250      udp dpt:1900
ufw-user-input  all  --  anywhere             anywhere

Chain ufw-before-logging-forward (1 references)
target     prot opt source               destination

Chain ufw-before-logging-input (1 references)
target     prot opt source               destination

Chain ufw-before-logging-output (1 references)
target     prot opt source               destination

Chain ufw-before-output (1 references)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ufw-user-output  all  --  anywhere             anywhere

Chain ufw-logging-allow (0 references)
target     prot opt source               destination
LOG        all  --  anywhere             anywhere             limit: avg 3/min burst 10 LOG level warning prefix "[UFW ALLOW] "

Chain ufw-logging-deny (2 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere             ctstate INVALID limit: avg 3/min burst 10
LOG        all  --  anywhere             anywhere             limit: avg 3/min burst 10 LOG level warning prefix "[UFW BLOCK] "

Chain ufw-not-local (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere             ADDRTYPE match dst-type LOCAL
RETURN     all  --  anywhere             anywhere             ADDRTYPE match dst-type MULTICAST
RETURN     all  --  anywhere             anywhere             ADDRTYPE match dst-type BROADCAST
ufw-logging-deny  all  --  anywhere             anywhere             limit: avg 3/min burst 10
DROP       all  --  anywhere             anywhere

Chain ufw-reject-forward (1 references)
target     prot opt source               destination

Chain ufw-reject-input (1 references)
target     prot opt source               destination

Chain ufw-reject-output (1 references)
target     prot opt source               destination

Chain ufw-skip-to-policy-forward (0 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere

Chain ufw-skip-to-policy-input (7 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere

Chain ufw-skip-to-policy-output (0 references)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere

Chain ufw-track-forward (1 references)
target     prot opt source               destination

Chain ufw-track-input (1 references)
target     prot opt source               destination

Chain ufw-track-output (1 references)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             ctstate NEW
ACCEPT     udp  --  anywhere             anywhere             ctstate NEW

Chain ufw-user-forward (1 references)
target     prot opt source               destination

Chain ufw-user-input (1 references)
target     prot opt source               destination
<b>ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT     udp  --  anywhere             anywhere             udp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:1935
ACCEPT     udp  --  anywhere             anywhere             udp dpt:1935
ACCEPT     udp  --  anywhere             anywhere             multiport dports 16384:32768
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
ACCEPT     udp  --  anywhere             anywhere             udp dpt:https
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     udp  --  anywhere             anywhere             udp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:5090
ACCEPT     udp  --  anywhere             anywhere             udp dpt:5090
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:sip
ACCEPT     udp  --  anywhere             anywhere             udp dpt:sip
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:5066
ACCEPT     udp  --  anywhere             anywhere             udp dpt:5066
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:tproxy
ACCEPT     udp  --  anywhere             anywhere             udp dpt:8081
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:8082
ACCEPT     udp  --  anywhere             anywhere             udp dpt:8082
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:3000
ACCEPT     udp  --  anywhere             anywhere             udp dpt:3000
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:2855
ACCEPT     udp  --  anywhere             anywhere             udp dpt:2855
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:2856
ACCEPT     udp  --  anywhere             anywhere             udp dpt:2856</b>

Chain ufw-user-limit (0 references)
target     prot opt source               destination
LOG        all  --  anywhere             anywhere             limit: avg 3/min burst 5 LOG level warning prefix "[UFW LIMIT BLOCK] "
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable

Chain ufw-user-limit-accept (0 references)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere

Chain ufw-user-logging-forward (0 references)
target     prot opt source               destination

Chain ufw-user-logging-input (0 references)
target     prot opt source               destination

Chain ufw-user-logging-output (0 references)
target     prot opt source               destination

Chain ufw-user-output (1 references)
target     prot opt source               destination

Выделенная область – это место для правил пользователя, они прописываются командами:

Для порта:

sudo ufw allow ssh (по названию порта)
sudo ufw allow 22 (конкретно указать номер порта)
sudo ufw allow 80/udp (конкретно с указанием номера порта и протокола tcp или udp)

Для диапазона портов:

sudo ufw allow 16384:32768/udp

Удаление правил:

sudo ufw delete allow 80

Выкладываю порядок действий по настройке ufw на примере bbb.

Во-первых, смотрим какие сервисы какие порты слушают:

netstat -ntlp | grep LISTEN

tcp 0 0 xxx.xxx.xxx.x:5090 0.0.0.0:* LISTEN 1258/freeswitch
tcp 0 0 127.0.0.1:8100 0.0.0.0:* LISTEN 1720/soffice.bin
tcp 0 0 xxx.xxx.xxx.x:5060 0.0.0.0:* LISTEN 1258/freeswitch
tcp 0 0 127.0.0.1:8101 0.0.0.0:* LISTEN 1766/soffice.bin
tcp 0 0 127.0.0.1:8102 0.0.0.0:* LISTEN 1811/soffice.bin
tcp 0 0 127.0.0.1:8103 0.0.0.0:* LISTEN 1856/soffice.bin
tcp 0 0 xxx.xxx.xxx.x:2855 0.0.0.0:* LISTEN 1258/freeswitch
tcp 0 0 127.0.0.1:8104 0.0.0.0:* LISTEN 1902/soffice.bin
tcp 0 0 xxx.xxx.xxx.x:2856 0.0.0.0:* LISTEN 1258/freeswitch
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1186/mongod
tcp 0 0 xxx.xxx.xxx.x:5066 0.0.0.0:* LISTEN 1258/freeswitch
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1227/redis-server 1
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1359/nginx -g daemo
tcp 0 0 xxx.xxx.xxx.x:8081 0.0.0.0:* LISTEN 1258/freeswitch
tcp 0 0 xxx.xxx.xxx.x:8082 0.0.0.0:* LISTEN 1258/freeswitch
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1176/sshd
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 1195/node
tcp6 0 0 ::1:5090 :::* LISTEN 1258/freeswitch
tcp6 0 0 ::1:5060 :::* LISTEN 1258/freeswitch
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1341/java
tcp6 0 0 :::5070 :::* LISTEN 1461/java
tcp6 0 0 :::9999 :::* LISTEN 1461/java
tcp6 0 0 :::1935 :::* LISTEN 1461/java
tcp6 0 0 :::8080 :::* LISTEN 1341/java
tcp6 0 0 :::80 :::* LISTEN 1359/nginx -g daemo
tcp6 0 0 ::1:8081 :::* LISTEN 1258/freeswitch
tcp6 0 0 ::1:8082 :::* LISTEN 1258/freeswitch
tcp6 0 0 :::8021 :::* LISTEN 1258/freeswitch
tcp6 0 0 :::22 :::* LISTEN 1176/sshd
tcp6 0 0 :::5080 :::* LISTEN 1461/java


Нас интересуют строчки с xxx.xxx.xxx.x (xxx.xxx.xxx.x – ip-адрес вашего сервера), так как это указывает что сервис слушает сетевую карту на наличие входящих запросов из всех сетей (0.0.0.0:*). Строчки с 127.0.0.1 нас не интересуют так как это внутренний интерфейс, так же нас интересует сточки с 0.0.0.0 в 4 столбце (выделен жирным), например

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1359/nginx -g daemo

так как это говорит, что 80 порт открыт для всех интерфейсов сервера и принимает запросы со всех сетей.

Итак, согласно этому составляем правила:

sudo ufw allow 22 для ssh
sudo ufw allow 80 для вебсервера nginx или apache

Целый ряд правил для freeswitch:

sudo ufw allow 5060
sudo ufw allow 5090
sudo ufw allow 2855
sudo ufw allow 2856
sudo ufw allow 5066
sudo ufw allow 8081
sudo ufw allow 8082

для сервиса node:

sudo ufw allow 3000
Далее необходимо посмотреть все-таки документацию, в которой мы увидим что необходимо открыть следующие порты:

sudo ufw allow 16384:32768/udp — для работы WebRTC
sudo ufw allow 443
sudo ufw allow 1935

На этом базовая настройка ufw для сервера BigBlueButton 2.0 закончена.

Добавлю только то, что желательно или отключать ssh или менять порт, или разрешить этот порт только для определенной сети, например для сети вашего предприятия.

К примеру, ваш шлюз имеет два сетевых интерфейса один виртуальный (к пользователям) 192.168.3.2, а другой реальный 105.ххх.ххх.ххх (к провайдеру), вот для правила ufw берем реальный ip, а точнее указываем сеть, которой принадлежит этот ip.

Сначала меняем порт в конфигурационном файле SSH сервера, воспользоваться можно любым текстовым редактором:

vim /etc/ssh/sshd_config

Затем удаляем правило, разрешающее 22 порт:

Sudo ufw delete allow 22 – иногда называется OpenSSH.

Создаем правило разрешающее подключение к новому порту только из сети предприятия:

sudo ufw allow from 105.ххх.ххх.ххх/26 to any port 4321

Затем перезагружаем ssh сервис:

/etc/init.d/ssh restart

И смотрим результат

netstat -ntlp | grep LISTEN

tcp   0    0.0.0.0:4321    0.0.0.0:*     listen   5676/sshd

а в правилах ufw увидим, кто имеет доступ к порту:

ufw status

4321      ALLOW       105.xxx.xxx.xxx/26

Уважаемые читатели прошу Вас писать дополнения в комментариях: Как вы считаете, все ли правильно здесь описано, или же необходимо что-то добавить или убрать?
Tags:
Hubs:
+6
Comments8

Articles