우분투의 SW 기반 방화벽 - UFW

이 글은 데비안 계열(우분투 등)에서 사용되는 ufw에 대해 다룬다. ufw는 iptables라는 유틸을 활용하는 유틸이고, iptables는 리눅스 커널의 netfilter라는 기능의 인터페이스(CLI)이다. 많은 사람들이 간단한 보안 수요에 대해서는 iptables의 복잡성 때문에 ufw를 사용하는 것을 권장한다.

iptables is a command line interface of netfilter, which is the underlying mechanics in the kernel for all of the work related. It has been (mostly) replaced by nftables (netfilter-tables) which provides more (advanced) features and a unified interface for IPv4/IPv6/ARP/Ethernet Bridges. But both iptables and nftables requires manually writing rules since they are kind of low-level.

UFW wraps around nftables (used to be iptables&ip6tables) and provides a much-easier-to-use command line interface for managing basic firewall functionalities, hence its name.

I personally recommend using UFW directly if all you want is a simple firewall. Otherwise you may use firewalld or write iptables/nftables rules directly.

출처: Quora


iptables는 아래 도표 중 hook API를 활용하는 인터페이스이다.

img


아래는 Netfilter와 리눅스 내, 외부의 처리 흐름이라고 한다.

img


Netfilter와 Iptables에 대해 더 자세히 알고 싶은 경우 iptables와 netfilter 정리 | 리눅스, 클라우드, IT 관련 기술 블로그를 참고하기 바란다.


1. 사용해야 할 때

ufw를 언제 써야 할까? 해당 Host의 앞 단에 보안 장비가 없고, 포트 접근 제어가 필요할 때이다. 리눅스를 클라이언트로 사용하는 경우 열린 포트가 없어 비교적 안전하지만 서버 단의 경우 그렇지 않다.


2. 간단한 사용법

ufw는 기본적으로 비활성화돼있는데, sudo ufw enable 로 활성화할 수 있다. 활성화 시 기본 설정만으로도 iptables의 NAT Table 설정이 자동 생성된다. 이는 ufw가 iptables의 wrapper이자 간단한 설정을 상세한 설정으로 변환시키는 역할을 담당함을 보여준다. (출처: 초보 시스템 관리자의 일기 | 우분투 방화벽 ufw로 시스템 보호하기)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
administrator@test02:~$ sudo iptables -L
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

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)
...
Chain ufw-after-input (1 references)
...
Chain ufw-after-logging-forward (1 references)
...

ufw의 사용법은 매우 간단하다. ufw {action} {port | serviceName}를 입력하면 된다. 이 때 서비스의 목록은 cat /etc/services로 확인할 수 있고, ufw deny ssh와 같이 사용할 수 있다. 아래와 같이 ufw도 status 명령을 지원한다. (iptables -L에 비해 가독성이 훨씬 낫다.)

ufw_deny_status


ufw는 기본적으로 whitelisting이지만, ufw default allow를 실행하면 blacklisting으로 전환할 수 있다.


ufw의 규칙을 제거하려면 ufw delete {규칙 내용} 으로 가능하다.

ufw_delete_deny


3. ‘우선 규칙’ 적용하기

보안 설정의 경우 우선 정의한 규칙이 적용된다고 한다. 즉, 순차적으로 ALLOW를 check하고, ALLOW되는 경우 통과하는 것이다. 이 경우 넓은 범위의 규칙은 뒤에 놓고, 좁은 범위의 (IP 차단 등) 규칙은 앞에 놓는 게 맞다.

‘우선 규칙’은 sudo nano /etc/ufw/before.rules에서 작성할 수 있다. End required lines 주석 아래에 -A ufw-before-input -s {Target_IP_Address, ...} -j DROP 을 추가하면 해당 IP를 모든 포트에 대해 접근 제한할 수 있다. (출처)


TODO

Logging에 대해 더 알아보기. (리눅스 로깅은 분량이 많고 어려운 것 같다.)

우분투의 SW 기반 방화벽 - UFW

https://jsqna.com/linux-ufw/

Author

Seongbin Kim

Posted on

21-06-06

Updated on

21-06-06

Licensed under

댓글