Linux/iptables

[Linux] iptables 방화벽을 이용한 IP 주소 차단하기

GGkeeper 2021. 11. 13. 05:05

실습> 방화벽을 이용한 IP주소 차단하기

/etc/services : 서비스 포트 번호의 목록 저장된 파일

http            80/tcp          www www-http    # WorldWideWeb HTTP
https           443/tcp                         # http protocol over TLS/SSL

HTTP  : 80(TCP)
HTTPS : 443(TCP)

   Client           Server
+-----------+    +-----------+
|    OUTPUT ------> INPUT    |
| 8000      |    | 80        |
|           |    |           |
|     INPUT <------ OUTPUT   |
| 8000      |    | 80        |
+-----------+    +-----------+
     WEB             WEB
    Client          Server    
192.168.108.1    192.168.108.3
192.168.108.3

192.168.108.1 ---> X
192.168.108.3 ---> O

1. 웹 서버 설치
httpd : 웹 서버 패키지 (아파치)
[root@www ~]# iptables -F
[root@www ~]# yum -y install httpd

2. 웹 서버 시작
웹 서버를 활성화시키고 시작한다.
[root@www ~]# systemctl enable httpd
[root@www ~]# systemctl start httpd
[root@www ~]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      980/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1128/master
tcp6       0      0 :::80                   :::*                    LISTEN      16730/httpd
tcp6       0      0 :::22                   :::*                    LISTEN      980/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      1128/master

3. 웹 문서 작성
/var/www/html : 웹 페이지가 제공되는 웹 문서 디렉터리
[root@www ~]# cd /var/www/html
[root@www html]# echo Hello Linux webserver > index.html
[root@www html]# cat index.html
Hello Linux webserver

4. 웹 서버 접속
192.168.108.1 에서 192.168.108.3으로 접속한다.
http://192.168.108.3/
Hello Linux webserver

192.168.108.3 에서 192.168.108.3으로 접속한다.
[root@www html]# yum -y install lynx
[root@www html]# lynx --dump 192.168.108.3
   Hello Linux webserver

5. 방화벽 룰 설정
192.168.108.1에서 80번 포트로 접속을 못하게 설정한다.
[root@www html]# iptables -A INPUT -p tcp --dport 80 -s 192.168.108.1 -j DROP
[root@www html]# iptables -nL

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       tcp  --  192.168.108.1        0.0.0.0/0            tcp dpt:80

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination


[root@www html]# lynx --dump 192.168.108.3
   Hello Linux webserver

[root@www html]# iptables -nvL
Chain INPUT (policy ACCEPT 146 packets, 11260 bytes)
 pkts bytes target     prot opt in     out     source               destination
   39  2028 DROP       tcp  --  *      *       192.168.108.1        0.0.0.0/0            tcp dpt:80

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

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