정보보안기사/linux

실습 iptables

멋쟁이천재사자 2023. 7. 22. 21:29

질문

 

 

 

혹시 iptables룰 설정할때 순서도 중요한가요?? 예를들어서
-A INPUT -m state --state NEW -p tcp --dport 80 이것과
-A INPUT -p tcp --dport 80 -m state --state NEW  
아시는 분은 조언 부탁드립니다 

 

답안(연구중)

 

한 줄만 생기면 똑 같은 것일텐데 두 줄이 생겨서 같은 것인지 다른 것인지 더 연구해봐야겠어요. 그런데 예상과 달리 완전 동일한 정책도 2번 입력하면 2줄 생기네요. 한 줄 생기는지 두 줄 생기는지로는 판단할 수 없습니다. 의미상 같아 보이고 실제 traffic 을 통해 테스트를 해보아야 할 것 같은데 테스트를 어떻게 하면 되려나.

 

 

실습 세부 로그 (7월 23일)

 

https://linux.die.net/man/8/iptables

 

iptables(8) - Linux man page

iptables(8) - Linux man page Name iptables - administration tool for IPv4 packet filtering and NAT Synopsis iptables [-t table] -[AD] chain rule-specification [options] iptables [-t table] -I chain [rulenum] rule-specification [options] iptables [-t table]

linux.die.net

 

This module, when combined with connection tracking, allows access to the connection tracking state for this packet.

--state state

Where state is a comma separated list of the connection states to match. Possible states are INVALID meaning that the packet could not be identified for some reason which includes running out of memory and ICMP errors which don't correspond to any known connection, ESTABLISHED meaning that the packet is associated with a connection which has seen packets in both directions, NEW meaning that the packet has started a new connection, or otherwise associated with a connection which has not seen packets in both directions, and RELATED meaning that the packet is starting a new connection, but is associated with an existing connection, such as an FTP data transfer, or an ICMP error.

 

 

갑자기 방화벽 설정을 iptables -nL 로 살펴보니 길고 복잡해졌다? xined finger 등 설치를 시도한 것이 영향을 주었나보다.

현재 설정을 백업해두고, clear 한 다음에 -m state 옵션에 대해 공부해보려고 한다. 매뉴얼에 안보이기 때문이다.

 

 

[root@centos xinetd.d]# cd /root
[root@centos ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg
[root@centos ~]# mkdir iptables
[root@centos ~]# cd iptables
[root@centos iptables]# iptables-save > 20230723.rules
[root@centos iptables]# ls -al
합계 8
drwxr-xr-x. 2 root root   28  7월 23 15:38 .
dr-xr-x---. 7 root root  233  7월 23 15:36 ..
-rw-r--r--. 1 root root 6727  7월 23 15:38 20230723.rules
[root@centos iptables]# tail -3 20230723.rules
-A IN_public_allow -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW,UNTRACKED -j ACCEPT
COMMIT
# Completed on Sun Jul 23 15:38:29 2023
[root@centos iptables]# wc -l 20230723.rules
195 20230723.rules
[root@centos iptables]# iptables -F
[root@centos iptables]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD_IN_ZONES (0 references)

...

 

iptables -F 해도 라인수는 여전히 겁나 길다.

 

 

[root@centos iptables]# iptables -A INPUT -m state --state NEW -p tcp --dport 80
[root@centos iptables]# iptables -A INPUT -m state --state NEW -p tcp --dport 80
[root@centos iptables]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
           tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80
           tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80

 

 

똑같은 규칙을 두번 입력하면 한줄만 생성되는 줄 알았는데 2줄이 보인다

 

 

[root@centos iptables]# iptables -A INPUT -m1 state --state NEW -p tcp --dport 80
iptables v1.4.21: Couldn't load match `1':No such file or directory

Try `iptables -h' or 'iptables --help' for more information.
[root@centos iptables]# iptables -A INPUT -m state1 --state NEW -p tcp --dport 80
iptables v1.4.21: Couldn't load match `state1':No such file or directory

Try `iptables -h' or 'iptables --help' for more information.

 

-m state 이 유효한 옵션인지 확인하기 위해 일부러 비틀어서 m1 state1 이랃고 하면 오류를 뿌려준다. 오류가 없이 들어간다는 것은 예약어에 해당한다는 뜻이다.

 

[root@centos iptables]# iptables -nL | head -7
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
           tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80
           tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
[root@centos iptables]# iptables -A INPUT --state NEW -p tcp --dport 80
iptables v1.4.21: unknown option "--state"
Try `iptables -h' or 'iptables --help' for more information.

 

state NEW tcp dpt:80 에 m state 라는 표현이 없어서 그부분을 빼고 iptables 명령을 실행해보았다. 그랬더니 unknown option "--state".  --state 옵션을 쓰기위해 사전에 필요한 옵션이었던 것이군

 

-m state 대신에 --match state 라고 해도 된다. -match 는 오류난다.

 

[root@centos iptables]# iptables -F
[root@centos iptables]# iptables -nL | head -5
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
[root@centos iptables]# iptables -A INPUT --match state --state NEW -p tcp --dport 80
[root@centos iptables]# iptables -nL | head -5
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
           tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80

Chain FORWARD (policy ACCEPT)
[root@centos iptables]# iptables -A INPUT -m state --state NEW -p tcp --dport 80
[root@centos iptables]# iptables -nL | head -5
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
           tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80
           tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80

 

 

실습 세부 로그 (7월 22일)

 

iptables -nL
iptables -nL --line-numbers
iptables -A INPUT -m state --state NEW -p tcp --dport 80
iptables -A INPUT -p tcp --dport 80 -m state --state NEW


-D(--delete) : DELETE : 정책을 삭제합니다.
-F(--flush) : FLUSH : 체인으로부터 모든 정책 삭제합니다.
iptables -D INPUT -p tcp --dport 80 -j DROP

iptables -D INPUT -m state --state NEW -p tcp --dport 80
iptables -D INPUT -p tcp --dport 80 -m state --state NEW


[root@centos ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@centos ~]# iptables A INPUT -m state --state NEW -p tcp --dport 80
Bad argument `A'
Try `iptables -h' or 'iptables --help' for more information.
[root@centos ~]# iptables -A INPUT -m state --state NEW -p tcp --dport 80
[root@centos ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
           tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@centos ~]# iptables -A INPUT -p tcp --dport 80 -m state --state NEW
[root@centos ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
           tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80
           tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 state NEW

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@centos ~]# iptables -nL --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1               tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80
2               tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 state NEW

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

'정보보안기사 > linux' 카테고리의 다른 글

[실습] xinetd finger클라  (0) 2023.07.23
[실습] DNS 서버 설치  (0) 2023.07.22
CentOS 7 설치 #4  (0) 2023.07.21
CentOS 7 설치 #3  (0) 2023.07.20
리눅스 실습  (0) 2023.07.20