'iptables'에 해당하는 글 6건

firewalld 방화벽

Server/CentOS 2021. 7. 11. 14:46

CentOS 7 부터 방화벽으로 firewalld 를 사용한다. port 나 대역 설정 등을 주로 한다. 아마도 기존 iptables 보다 설정이 간편할 것이다.

 

  • 여러 zone 생성 가능
  • zone 마다 각종 service, port, ip 등 설정 가능
  • 필요에 맞게 하나의 zone 을 활성화
  • default zone 은 public

 

가장 많이 사용하는 구문은 포트 열고 적용하고.

 

# firewall-cmd --zone=public --add-port=8080/tcp
success
# firewall-cmd --reload
success

 

 

1. 데몬 확인 (start/stop/status)

 

# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2021-07-10 23:31:53 EDT; 1h 29min ago
     Docs: man:firewalld(1)
 Main PID: 687 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─687 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

Jul 10 23:31:53 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Jul 10 23:31:53 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Jul 10 23:31:53 localhost.localdomain firewalld[687]: WARNING: AllowZoneDrifting is enabled. This is co...ow.
Hint: Some lines were ellipsized, use -l to show in full.

 

# firewall-cmd --state
running

 

 

2. 모든 zone 이름 확인

 

# firewall-cmd --get-zones
block dmz drop external home internal public trusted work

 

 

3. 기본 zone 확인

 

# firewall-cmd --get-default-zone
public

 

 

4. 기본 zone 을 home 으로 변경

 

# firewall-cmd --set-default-zone=home
# firewall-cmd --get-default-zone
home

 

 

5. public zone 의 설정 확인

 

# firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: dhcpv6-client ssh
  ports: 1521/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

 

현재 public zone 설정을 보면 ssh services 와 1521 포트가 열린 것을 확인할 수 있다. 알려진 포트들에 대해서는 services 로 등록을 해도 되고, 다른 포트를 사용하고 싶다 하면 직접 포트를 추가해도 된다. service 에 ssh 를 추가하나, 포트 22번을 여는 거나 동일한 결과란 뜻.

 

 

6. 서비스 추가/삭제 (즉시 적용)

 

# firewall-cmd --zone=public --add-service=mysql
# firewall-cmd --zone=public --remove-service=mysql

 

 

7. port 추가/삭제 (즉시 적용)

 

# firewall-cmd --zone=public --add-port=8081/tcp
# firewall-cmd --zone=public --remove-port=8081/tcp
# firewall-cmd --zone=public --add-port=8081-8090/tcp
# firewall-cmd --zone=public --remove-port=8081-8090/tcp

 

 

8. 영구 적용 (reload 후 적용)

 

# firewall-cmd --zone=public --add-service=mysql --permanent
# firewall-cmd --reload

 

--pormanent 를 제외하면 재부팅이나 데몬 재시작 할 경우 해당 설정은 사라진다.
또한 reload 로 설정을 재적용하지 않으면 반영되지 않는다.

 

 

9. IP 추가/삭제

 

# firewall-cmd --add-source=192.168.0.2 --permanent 
# firewall-cmd --remove-source=192.168.0.2 --permanent

 

 

10. IP 대역 추가/삭제

 

# firewall-cmd --add-source=192.168.0.0/24 --permanent 
# firewall-cmd --remove-source=192.168.0.0/24 --permanent

 

 

11. IP 대역 / 특정 포트 추가/삭제

 

# firewall-cmd --add-rich-rule='rule family="ipv4" source address=192.168.0.0/24 port port="8081" protocol="tcp" accept' --permanent
# firewall-cmd --remove-rich-rule='rule family="ipv4" source address=192.168.0.0/24 port port="8081" protocol="tcp" accept' --permanent

 


WRITTEN BY
손가락귀신
정신 못차리면, 벌 받는다.

,

Tomcat 80 to 443 port

Server/AWS 2018. 8. 8. 12:42

미션. http://mydomain.com -> https://mydomain.com


간만에 톰캣 단독 서버를 만들면서 포트 포워딩에 대한 노가다를 해보았다.

톰캣은 기본적으로 8080 포트와 8443(SSL) 포트를 사용하기 때문에 대부분의 경우 80 포트나 443 포트로 변경하기를 원할 것이다.

도커 등의 각종 컨테이너 서비스를 사용하면 포트 걱정할 일은 없지만, 포트 포워딩이 아닌 포트 변경이 가능한 방법을 찾아봤다.


> server.xml 


<Connector port="8080" protocol="HTTP/1.1" ... redirectPort="8443" ...
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" ...
cs


톰캣이 실행중일 때 netstat 로 listen 중인 포트를 확인해 보면 8080, 8443 포트가 열려 있는 것을 확인할 수 있다.

server.xml 파일에 설정되어 있는 포트 때문인데, 이 포트들을 원하는 포트로 변경한다고 해서 모두 동작하지는 않는다.

1024 이하의 TCP/UDP 포트(well-known) 로 변경하려 할 경우 root 권한을 필요로 하기 때문에 기본 세팅으로는 well-known 포트로 변경 할 수 없다.

(이를 가능하게 하는 것이 authbind 이다.)


이러한 이유로 우리는 tomcat 유저로 톰캣을 실행시킬 경우 포트를 80 이나 443 등으로 바꾸려는 무모한 짓으로 시간을 낭비하지 말아야 한다.

이를 확인하려면 tomcat8.conf 파일의 TOMCAT_USER 를 root 로 설정하면 server.xml 에 설정한 대로 포트가 변경되는 것을 확인할 수 있다.

하지만 운용시 생성되는 모든 파일, 배포 디렉토리 및 log 파일까지 모두 root 로 설정되기 때문에 일반 유저로는 접근할 수 없어 좋은 방법이 될 수 없다.

그 어떤 서비스도 해당 그룹과 권한을 설정하여 사용하지, root 로 실행하라고 권하는 서비스는 없다.


결국 깔끔한 방법은 포트 포워딩이다.



80->8080, 443->8443 포트 포워딩


$ sudo vi /etc/init.d/iptables 
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8443
 
$ sudo /etc/init.d/iptables restart
cs


이렇게 iptables 로 포트 포워딩을 하면 80 과 443 을 사용할 수 있다.


http://mydomain.com (80)   -> http://mydomain.com (8080)

https://mydomain.com (443) -> https://mydomain.com (8443)


iptables 대신 firewalld 를 사용할 수도 있다.


$ sudo firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toport=8080 --permanent
$ sudo firewall-cmd --zone=public --add-forward-port=port=443:proto=tcp:toport=8443 --permanent $ sudo firewall-cmd --reload
cs



80->443 포트 포워딩


이 상태에서 80 포트으로 접속했을 때 무조건 443 포트로 포워딩하려면 web.xml 파일을 수정한다.


$ sudo /etc/tomcat8/web.xml
      ...
      <security-constraint>
            <web-resource-collection>
                  <web-resource-name>Automatic SSL Forward</web-resource-name>
                  <url-pattern>/*</url-pattern>
            </web-resource-collection>
            <user-data-constraint>
                  <transport-guarantee>
                        CONFIDENTIAL
                  </transport-guarantee>
            </user-data-constraint>
      </security-constraint>
 
</web-app>
cs



http://mydomain.com -> https://mydomain.com:8443


여기서 server.xml 파일의 redirectPort 를 443 으로 바꿔주면,


http://mydomain.com -> https://mydomain.com


firewalld 를 사용할 때는 http 접속시 https 로 자동변경 되지 않아 server.xml 파일에 추가 설정이 필요했다.


<Connector port="8080" redirectPort="443" ... />
<Connector port="8443" proxyPort="443" ... />
cs



8443 포트 막기


마지막으로 https://mydomain.com:8443 같은 식의 8443 포트로 직접 접근을 막기 위해 iptables 를 한번 더 수정한다.


$ sudo vi /etc/init.d/iptables 
iptables -A PREROUTING -t mangle -p tcp --dport 8443 -j DROP
 
$ sudo /etc/init.d/iptables restart
cs




WRITTEN BY
손가락귀신
정신 못차리면, 벌 받는다.

,

tomcat 80 port

Server/CentOS 2016. 6. 9. 00:49

톰캣의 8080 포트를 80 포트로 변경하는 방법들은 운영체제별로, 배포판 별로 조금씩 다르다.

레드햇 계열에서는 다음의 방법들이 있다.

  1. apache 연동
  2. authbind
  3. iptables


이 중에서 아무 설치없이 바로 사용할 수 있는 iptables 를 사용해 봤다.


# vi /etc/init.d/iptables
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
 
# /etc/init.d/iptables save
# /etc/init.d/iptables restart
# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
REDIRECT   tcp  --  anywhere             anywhere             tcp dpt:http redir ports 8080
cs


엄밀히 말하자면 tomcat 의 기본 포트를 변경한 것이 아니라,

서버로 들어오는 80 포트를 8080 포트로 리다이렉트 시킨 것이다. ^^




WRITTEN BY
손가락귀신
정신 못차리면, 벌 받는다.

,

iptables FTP

Server/CentOS 2009. 12. 4. 10:58
FTP 서비스는 일반적인 인터넷 서비스와는 달리 두 가지의 port를 사용합니다.
명령어와 응답을 주고받는 command(control) port(일반적으로 21번) 와 실제 데이터를 주고받는 data port(일반적으로 20번) 로 구성된 TCP 기반 서비스입니다.
여기서 Active / Passive 모드는 data port의 접속 방식에 따라서 구분합니다.
  • Active 모드
    서버가 클라이언트의 data 포트에 접속 시도
    클라이언트의 방화벽에서 차단될 가능성이 큼
  • Passive 모드
    클라이언트가 직접 서버의 data 포트에 접속 시도
    클라이언트에서 서버에 직접 접근하는 방식이므로 보안상 문제점이 존재


FTP 모드에 따른 iptables 설정

# vi /etc/init.d/iptables
#!/bin/sh
#
# flush all chain

/sbin/iptables -F

# accept service port :::::: Active mode
/sbin/iptables -A INPUT -p tcp --sport 1024: --dport 20 -m state --state NEW -j ACCEPT
/sbin/iptables -A INPUT -p tcp --sport 1024: --dport 21 -m state --state NEW -j ACCEPT

# accept service port :::::: Passive mode
/sbin/iptables -A INPUT -p tcp --sport 1024: --dport 21 -m state --state NEW -j ACCEPT
/sbin/iptables -A INPUT -p tcp -m tcp --dport 60001:60020 -j ACCEPT

passive 모드의 경우, 사용하는 ftp 설정 파일에서도 사용할 passive 포트를 지정해 줘야 함.
iptable 적용 및 ftp 재실행.

WRITTEN BY
손가락귀신
정신 못차리면, 벌 받는다.

,

SSH brute force

Server/CentOS 2009. 12. 4. 10:10
SSH brute force(무차별 공격) 란 사전적 계정이름과 단순한 패스워드를 노리는 대입공격 입니다.
혹시나 해킹에 성공한다면 사용자 계정을 획득하여 또 다른 해킹을 시도할 것입니다.
본인의 서버에 SSH brute force 공격이 있었는지를 확인해 봅니다.

# vi /var/log/secure
Nov 30 10:46:57 localhost sshd[32438]: Address 210.21.225.202 maps to sym.gdsz.cncnet.net, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
Nov 30 10:46:57 localhost sshd[32438]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=210.21.225.202  user=root
Nov 30 10:46:59 localhost sshd[32438]: Failed password for root from 210.21.225.202 port 63852 ssh2
Nov 30 10:46:59 localhost sshd[32439]: Received disconnect from 210.21.225.202: 11: Bye Bye
Nov 30 10:47:02 localhost sshd[32440]: Address 210.21.225.202 maps to sym.gdsz.cncnet.net, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
Nov 30 10:47:02 localhost sshd[32440]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=210.21.225.202  user=root
Nov 30 10:47:04 localhost sshd[32440]: Failed password for root from 210.21.225.202 port 64468 ssh2
Nov 30 10:47:05 localhost sshd[32441]: Received disconnect from 210.21.225.202: 11: Bye Bye
Nov 30 10:47:08 localhost sshd[32444]: Address 210.21.225.202 maps to sym.gdsz.cncnet.net, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
Nov 30 10:47:08 localhost sshd[32444]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=210.21.225.202  user=root
Nov 30 10:47:10 localhost sshd[32444]: Failed password for root from 210.21.225.202 port 65319 ssh2
Nov 30 10:47:10 localhost sshd[32445]: Received disconnect from 210.21.225.202: 11: Bye Bye
이것이 바로 SSH brute force 공격입니다.


SSH brute force prevent

1. 패스워드를 누구도 상상할 수 없는 엄청난 조합으로 만드는 것입니다. ^^
2. prevent 같은 방어 툴을 사용합니다.
3. iptables 로 수시로 접속을 시도하는 패킷에 대해 차단합니다.
4. ssh 포트를 변경하고, iptables 로 포트 스캔도 막습니다. (포트 변경 만으로도 방어가 가능한 것 같습니다.)


ssh port 변경

1. ssh 설정 파일 수정
# vi /etc/ssh/sshd_config
Port
32
PermitRootLogin no

2. iptables 수정
# dport 32 accept
/sbin/iptables -A INPUT -p tcp --sport 1024: --dport 32 -m state --state NEW -j
ACCEPT

3. iptables 적용 / ssh 재시작
# /etc/init.d/iptables save && /etc/init.d/sshd restart


WRITTEN BY
손가락귀신
정신 못차리면, 벌 받는다.

,