Nie jesteś zalogowany.
Jeśli nie posiadasz konta, zarejestruj je już teraz! Pozwoli Ci ono w pełni korzystać z naszego serwisu. Spamerom dziękujemy!

Ogłoszenie

Prosimy o pomoc dla małej Julki — przekaż 1% podatku na Fundacji Dzieciom zdazyć z Pomocą.
Więcej informacji na dug.net.pl/pomagamy/.

#1  2017-11-02 15:51:00

  tenson - Użytkownik

tenson
Użytkownik
Zarejestrowany: 2017-07-08

VPS, problem z autostartem reguł iptables

Witam,

Posiadam VPS'a na którym chciałbym aby automatycznie ładowały się po restarcie następujące reguły iptables:

Kod:

iptables -t nat -I PREROUTING -p tcp -i eth0 --dport 9981 -j DNAT --to 10.1.100.99:9981
iptables -I FORWARD -p tcp -d 10.1.100.99 --dport 9981 -j ACCEPT

iptables -t nat -I PREROUTING -p tcp -i eth0 --dport 9080 -j DNAT --to 10.1.100.99:9080
iptables -I FORWARD -p tcp -d 10.1.100.99 --dport 9080 -j ACCEPT

iptables -t nat -I PREROUTING -p tcp -i eth0 --dport 80 -j DNAT --to 10.1.100.99:80
iptables -I FORWARD -p tcp -d 10.1.100.99 --dport 80 -j ACCEPT

W tym celu posłużyłem się iptables-persistent - czyli wywołałem powyższe reguły iptables a następnie zainstalowałem iptables-persistent. Po restarcie i wylistowaniu reguł (iptables -L) widzę, że są one załadowane, jednak nie działają. Muszę je ponownie wywołać bezpośrednio w terminalu i wówczas wszystko działa poprawnie (przekierowanie portów na komputer podłączony jako klient VPN).

CZy da się coś z tym poradzić, abym nie musiał za każdym razem po restarcie VPS'a wklejać tych reguł do terminala? I generalnie dlaczego nie działają skoro są ładowane po restarcie?

Offline

 

#2  2017-11-02 16:52:12

  arecki - Użytkownik

arecki
Użytkownik
Skąd: 44 Bronson Lane Hensonville
Zarejestrowany: 2016-03-03

Re: VPS, problem z autostartem reguł iptables

Kolejność regułek ma znaczenie. Być może te regułki są poprzedzone innymi.
Przydałby się jakiś pełny listing regułek fireewalla po restarcie.

Ostatnio edytowany przez arecki (2017-11-02 16:53:59)

Offline

 

#3  2017-11-02 17:05:19

  tenson - Użytkownik

tenson
Użytkownik
Zarejestrowany: 2017-07-08

Re: VPS, problem z autostartem reguł iptables

Dzięki za szybką odpowiedź.

Tak wygląda listing przed załadowaniem reguł:

Kod:

root@qwerty:~# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
AS0_ACCEPT  all  --  anywhere             anywhere             state RELATED,ESTABLISHED
AS0_ACCEPT  all  --  anywhere             anywhere            
AS0_IN_PRE  all  --  anywhere             anywhere             mark match 0x2000000/0x2000000
AS0_ACCEPT  udp  --  anywhere             qwerty               state NEW udp dpt:openvpn
AS0_ACCEPT  tcp  --  anywhere             qwerty               state NEW tcp dpt:https
AS0_WEBACCEPT  all  --  anywhere             anywhere             state RELATED,ESTABLISHED
AS0_WEBACCEPT  tcp  --  anywhere             qwerty               state NEW tcp dpt:943
f2b-ssh    tcp  --  anywhere             anywhere             multiport dports ssh
f2b-sshd   tcp  --  anywhere             anywhere             multiport dports ssh

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
AS0_ACCEPT  all  --  anywhere             anywhere             state RELATED,ESTABLISHED
AS0_IN_PRE  all  --  anywhere             anywhere             mark match 0x2000000/0x2000000
AS0_OUT_S2C  all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
AS0_OUT_LOCAL  all  --  anywhere             anywhere            

Chain AS0_ACCEPT (5 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            

Chain AS0_IN (3 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             172.27.224.1        
AS0_IN_POST  all  --  anywhere             anywhere            

Chain AS0_IN_NAT (0 references)
target     prot opt source               destination         
MARK       all  --  anywhere             anywhere             MARK or 0x8000000
ACCEPT     all  --  anywhere             anywhere            

Chain AS0_IN_POST (1 references)
target     prot opt source               destination         
AS0_OUT    all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere            

Chain AS0_IN_PRE (2 references)
target     prot opt source               destination         
AS0_IN     all  --  anywhere             192.168.0.0/16      
AS0_IN     all  --  anywhere             172.16.0.0/12       
AS0_IN     all  --  anywhere             10.0.0.0/8          
ACCEPT     all  --  anywhere             anywhere            

Chain AS0_IN_ROUTE (0 references)
target     prot opt source               destination         
MARK       all  --  anywhere             anywhere             MARK or 0x4000000
ACCEPT     all  --  anywhere             anywhere            

Chain AS0_OUT (2 references)
target     prot opt source               destination         
AS0_OUT_POST  all  --  anywhere             anywhere            

Chain AS0_OUT_LOCAL (1 references)
target     prot opt source               destination         
DROP       icmp --  anywhere             anywhere             icmp redirect
ACCEPT     all  --  anywhere             anywhere            

Chain AS0_OUT_POST (1 references)
target     prot opt source               destination         
DROP       all  --  anywhere             anywhere            

Chain AS0_OUT_S2C (1 references)
target     prot opt source               destination         
AS0_OUT    all  --  anywhere             anywhere            

Chain AS0_WEBACCEPT (2 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            

Chain f2b-ssh (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere            

Chain f2b-sshd (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere

A tak po załadowaniu reguł:

Kod:

root@qwerty:~# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
AS0_ACCEPT  all  --  anywhere             anywhere             state RELATED,ESTABLISHED
AS0_ACCEPT  all  --  anywhere             anywhere            
AS0_IN_PRE  all  --  anywhere             anywhere             mark match 0x2000000/0x2000000
AS0_ACCEPT  udp  --  anywhere             qwerty               state NEW udp dpt:openvpn
AS0_ACCEPT  tcp  --  anywhere             qwerty               state NEW tcp dpt:https
AS0_WEBACCEPT  all  --  anywhere             anywhere             state RELATED,ESTABLISHED
AS0_WEBACCEPT  tcp  --  anywhere             qwerty               state NEW tcp dpt:943
f2b-ssh    tcp  --  anywhere             anywhere             multiport dports ssh
f2b-sshd   tcp  --  anywhere             anywhere             multiport dports ssh

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
[*] ACCEPT     tcp  --  anywhere             10.1.100.99          tcp dpt:http
[*] ACCEPT     tcp  --  anywhere             10.1.100.99          tcp dpt:9080
[*] ACCEPT     tcp  --  anywhere             10.1.100.99          tcp dpt:9981
AS0_ACCEPT  all  --  anywhere             anywhere             state RELATED,ESTABLISHED
AS0_IN_PRE  all  --  anywhere             anywhere             mark match 0x2000000/0x2000000
AS0_OUT_S2C  all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
AS0_OUT_LOCAL  all  --  anywhere             anywhere            

Chain AS0_ACCEPT (5 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            

Chain AS0_IN (3 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             172.27.224.1        
AS0_IN_POST  all  --  anywhere             anywhere            

Chain AS0_IN_NAT (0 references)
target     prot opt source               destination         
MARK       all  --  anywhere             anywhere             MARK or 0x8000000
ACCEPT     all  --  anywhere             anywhere            

Chain AS0_IN_POST (1 references)
target     prot opt source               destination         
AS0_OUT    all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere            

Chain AS0_IN_PRE (2 references)
target     prot opt source               destination         
AS0_IN     all  --  anywhere             192.168.0.0/16      
AS0_IN     all  --  anywhere             172.16.0.0/12       
AS0_IN     all  --  anywhere             10.0.0.0/8          
ACCEPT     all  --  anywhere             anywhere            

Chain AS0_IN_ROUTE (0 references)
target     prot opt source               destination         
MARK       all  --  anywhere             anywhere             MARK or 0x4000000
ACCEPT     all  --  anywhere             anywhere            

Chain AS0_OUT (2 references)
target     prot opt source               destination         
AS0_OUT_POST  all  --  anywhere             anywhere            

Chain AS0_OUT_LOCAL (1 references)
target     prot opt source               destination         
DROP       icmp --  anywhere             anywhere             icmp redirect
ACCEPT     all  --  anywhere             anywhere            

Chain AS0_OUT_POST (1 references)
target     prot opt source               destination         
DROP       all  --  anywhere             anywhere            

Chain AS0_OUT_S2C (1 references)
target     prot opt source               destination         
AS0_OUT    all  --  anywhere             anywhere            

Chain AS0_WEBACCEPT (2 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            

Chain f2b-ssh (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere            

Chain f2b-sshd (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere

Ostatnio edytowany przez tenson (2017-11-02 18:19:05)

Offline

 

#4  2017-11-02 17:11:45

  arecki - Użytkownik

arecki
Użytkownik
Skąd: 44 Bronson Lane Hensonville
Zarejestrowany: 2016-03-03

Re: VPS, problem z autostartem reguł iptables

To jeszcze wypadałoby wskazać w tym powyższym listingu te nowe regułki.
Inna sprawa, że nie pokazałeś analogicznych listingów dla tablicy NAT.

A już poza tematem, ten twój "firewall" jest bez sensu (polityka ACCEPT w powiązaniu z obecnymi regułkami).

Ostatnio edytowany przez arecki (2017-11-02 17:17:10)

Offline

 

#5  2017-11-02 18:19:32

  tenson - Użytkownik

tenson
Użytkownik
Zarejestrowany: 2017-07-08

Re: VPS, problem z autostartem reguł iptables

To, że wygląda bez sensu to inna sprawa, ale takie są defaultowe zaraz po utworzeniu nowego świeżego VPS. Jeżeli zrobię flush wszystkiego to uwalę VPS'a (już próbowałem, na szczęście zrobiłem snapshota).

Natomiast w drugim listingu zaznaczyłem gwiazdką [*] nowe reguły które pojawiły się po ich dodaniu (edytowałem post przed chwilą).

Ostatnio edytowany przez tenson (2017-11-02 18:20:43)

Offline

 

#6  2017-11-02 18:41:30

  arecki - Użytkownik

arecki
Użytkownik
Skąd: 44 Bronson Lane Hensonville
Zarejestrowany: 2016-03-03

Re: VPS, problem z autostartem reguł iptables

tenson napisał(-a):

w drugim listingu zaznaczyłem gwiazdką [*] nowe reguły które pojawiły się po ich dodaniu (edytowałem post przed chwilą).

Wciąż brak tablicy nat.

tenson napisał(-a):

To, że wygląda bez sensu to inna sprawa,

Dlatego napisałem "poza tematem".

Offline

 

#7  2017-11-02 20:31:18

  tenson - Użytkownik

tenson
Użytkownik
Zarejestrowany: 2017-07-08

Re: VPS, problem z autostartem reguł iptables

Ok, już poprawiam. Mam nadzieję, że o to chodzi.

Tablice NAT
Przed wywołaniem reguł z pierwszego posta:

Kod:

root@qwerty:~# iptables -t nat -v -L -n --line-number
Chain PREROUTING (policy ACCEPT 17 packets, 1262 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 AS0_NAT_PRE_REL_EST  all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED

Chain INPUT (policy ACCEPT 17 packets, 1262 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

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

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 AS0_NAT_POST_REL_EST  all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
2        0     0 AS0_NAT_PRE  all  --  *      *       0.0.0.0/0            0.0.0.0/0            mark match 0x2000000/0x2000000

Chain AS0_NAT (3 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 SNAT       all  --  *      eth0    0.0.0.0/0            0.0.0.0/0            to:[ZEWNĘTRZNY IP SERWERA VPS]
2        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain AS0_NAT_POST_REL_EST (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain AS0_NAT_PRE (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 AS0_NAT    all  --  *      *       0.0.0.0/0            0.0.0.0/0            mark match 0x8000000/0x8000000
2        0     0 AS0_NAT_TEST  all  --  *      *       0.0.0.0/0            192.168.0.0/16      
3        0     0 AS0_NAT_TEST  all  --  *      *       0.0.0.0/0            172.16.0.0/12       
4        0     0 AS0_NAT_TEST  all  --  *      *       0.0.0.0/0            10.0.0.0/8          
5        0     0 AS0_NAT    all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain AS0_NAT_PRE_REL_EST (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain AS0_NAT_TEST (3 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      as0t+   0.0.0.0/0            0.0.0.0/0           
2        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            mark match 0x4000000/0x4000000
3        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            10.1.100.0/24       
4        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            172.27.224.0/20     
5        0     0 AS0_NAT    all  --  *      *       0.0.0.0/0            0.0.0.0/0

Po wywołaniu reguł z pierwszego posta:

Kod:

root@qwerty:~# iptables -t nat -v -L -n --line-number
Chain PREROUTING (policy ACCEPT 24 packets, 2770 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        6   360 DNAT       tcp  --  eth0   *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80 to:10.1.100.99:80
2        0     0 DNAT       tcp  --  eth0   *       0.0.0.0/0            0.0.0.0/0            tcp dpt:9080 to:10.1.100.99:9080
3        0     0 DNAT       tcp  --  eth0   *       0.0.0.0/0            0.0.0.0/0            tcp dpt:9981 to:10.1.100.99:9981
4        0     0 AS0_NAT_PRE_REL_EST  all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED

Chain INPUT (policy ACCEPT 24 packets, 2770 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 6 packets, 422 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 12 packets, 782 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 AS0_NAT_POST_REL_EST  all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
2        0     0 AS0_NAT_PRE  all  --  *      *       0.0.0.0/0            0.0.0.0/0            mark match 0x2000000/0x2000000

Chain AS0_NAT (3 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 SNAT       all  --  *      eth0    0.0.0.0/0            0.0.0.0/0            to:[ZEWNĘTRZNY IP SERWERA VPS]
2        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain AS0_NAT_POST_REL_EST (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain AS0_NAT_PRE (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 AS0_NAT    all  --  *      *       0.0.0.0/0            0.0.0.0/0            mark match 0x8000000/0x8000000
2        0     0 AS0_NAT_TEST  all  --  *      *       0.0.0.0/0            192.168.0.0/16      
3        0     0 AS0_NAT_TEST  all  --  *      *       0.0.0.0/0            172.16.0.0/12       
4        0     0 AS0_NAT_TEST  all  --  *      *       0.0.0.0/0            10.0.0.0/8          
5        0     0 AS0_NAT    all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain AS0_NAT_PRE_REL_EST (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain AS0_NAT_TEST (3 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      as0t+   0.0.0.0/0            0.0.0.0/0           
2        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            mark match 0x4000000/0x4000000
3        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            10.1.100.0/24       
4        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            172.27.224.0/20     
5        0     0 AS0_NAT    all  --  *      *       0.0.0.0/0            0.0.0.0/0

Offline

 

#8  2017-11-03 06:46:31

  arecki - Użytkownik

arecki
Użytkownik
Skąd: 44 Bronson Lane Hensonville
Zarejestrowany: 2016-03-03

Re: VPS, problem z autostartem reguł iptables

Jak widzisz po restarcie serwera nie ma tych regułek w żadnej z tablic, więc nie wiem gdzie ty je widziałeś.

Offline

 

#9  2017-11-03 10:57:13

  tenson - Użytkownik

tenson
Użytkownik
Zarejestrowany: 2017-07-08

Re: VPS, problem z autostartem reguł iptables

No ok, a linie 1,2,3,4 na samej górze w drugim kodzie w poście #7 ???

Sorry za lamerskie pytania, ale kompletnie nie znam się na iptables więc proszę mi wybaczyć :)

Ostatnio edytowany przez tenson (2017-11-03 10:57:47)

Offline

 

#10  2017-11-03 12:13:07

  arecki - Użytkownik

arecki
Użytkownik
Skąd: 44 Bronson Lane Hensonville
Zarejestrowany: 2016-03-03

Re: VPS, problem z autostartem reguł iptables

tenson napisał(-a):

No ok, a linie 1,2,3,4 na samej górze w drugim kodzie w poście #7 ???

Proszę, oto te linie.

Kod:

1        6   360 DNAT       tcp  --  eth0   *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80 to:10.1.100.99:80
2        0     0 DNAT       tcp  --  eth0   *       0.0.0.0/0            0.0.0.0/0            tcp dpt:9080 to:10.1.100.99:9080
3        0     0 DNAT       tcp  --  eth0   *       0.0.0.0/0            0.0.0.0/0            tcp dpt:9981 to:10.1.100.99:9981
4        0     0 AS0_NAT_PRE_REL_EST  all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED

Tylko nie rozumiem o co pytasz, skoro są to linijki już po "ręcznym" dodaniu.

Pisałeś w pierwszej wiadomości, że:

tenson napisał(-a):

Po restarcie i wylistowaniu reguł (iptables -L) widzę, że są one załadowane, jednak nie działają

jednak przedstawione listingi temu przeczą.

Problem tkwi najprawdopodobniej w poniższym.

tenson napisał(-a):

W tym celu posłużyłem się iptables-persistent - czyli wywołałem powyższe reguły iptables a następnie zainstalowałem iptables-persistent.

Tu mogłeś popełnić błąd.
iptables-persistent trzyma swoje regułki w określonym miejscu, nie napisałeś jak je tam umieszczałeś.
Można tak

Kod:

iptables-save > /sciezka/do/regulek

aby zapisać bieżący stan regułek iptables.
A tak:

Kod:

iptables-restore < /sciezka/do/regulek

aby zaciągnąć regułki z wcześniej przygotowanego pliku.

No i jak wszystko będzie prawidłowo działać to wtedy trzeba zadbać o uruchamianie przy starcie umieszczając np. w /etc/network/if-pre-up.d/ skrypt uruchmiający

Kod:

iptables-restore < /sciezka/do/regulek

W dobie systemd rozwiązanie powyższego jest zapewne inne.

Ostatnio edytowany przez arecki (2017-11-03 14:35:58)

Offline

 

Stopka forum

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson
Nas ludzie lubią po prostu, a nie klikając w przyciski ;-)