우분투의 기본적인 방화벽은 UFW입니다. 이는 iptables를 좀 더 쉽게 설정할 수 있도록 한 것인데 간단한 방화벽 구성에는 문제가 없지만 수준 높은 방화벽 구성에는 iptables 룰을 직접 사용해야 합니다.

참고 : 우분투 UFW help 가이드

UFW 사용법

UFW 기본 설정법에 대하여 알아보자.

UFW 활성화/비활성화

UFW는 기본 비활성화 상태이기에 이를 활성화 한다.

BASH

sudo ufw enable

UFW 비활성화

BASH

sudo ufw disable

UFW 상태 확인

BASH

sudo ufw status verbose

UFW 기본 룰

UFW에 설정되어 있는 기본 룰은 아래와 같다.

  • 들어오는 패킷에 대해서는 전부 거부(deny)

  • 나가는 패킷에 대해서는 전부 허가(allow)

기본 룰 확인

BASH

sudo ufw show raw

기본 정책 차단

BASH

sudo ufw default deny

기본 정책 허용

BASH

sudo ufw default allow

UFW 허용과 차단

UFW 허용

sudo ufw allow <port>/<optional: protocal>

예) SSH 포트 22번 허용(tcp/udp 22번 포트를 모두 허용)

BASH

sudo ufw allow 22

tcp 22번 포트만을 허용 - SSH는 tcp 22번 포트만 허용하는게 정답

BASH

sudo ufw allow 22/tcp

udp 22번 포트만을 허용

BASH

sudo ufw allow 22/udp

UFW 거부

sudo ufw deny <port>/<optional: protocol>

예) ssh 포트 22번 거부(tcp/udp 22번 포트를 모두 거부)

BASH

sudo ufw deny 22

tcp 22번 포트만을 거부

BASH

sudo ufw deny 22/tcp

udp 22번 포트만을 거부

BASH

sudo ufw deny 22/udp

UFW 룰의 삭제

ufw deny 22/tcp 설정이 되어있다고 가정

BASH

sudo ufw delete deny 22/tcp

service 명을 이용한 설정

/etc/services에 지정되어 있는 서비스명과 포트를 이용해 UFW를 설정할 수 있다.

서비스명 보기

BASH

less /etc/services

서비스명으로 허용

sudo ufw allow <service name>

예) SSH 서비스

BASH

sudo ufw allow sshsudo ufw deny ssh

UFW 로그 기록

BASH

sudo ufw logging on

sudo ufw logging off

Advanced Syntax

문법을 확장하여 목적지 주소와 포트, 프로토콜등을 지정할 수 있다.

특정한 IP 주소 허가/거부

특정한 IP주소 허용

sudo ufw allow from <ip address>

예) 192.168.0.100 주소 허용(IP 주소192.168.0.100 에서만 접속이 가능해진다)

BASH

sudo ufw allow from 192.168.0.100

네트워크 단위로 지정하여 같은 네트워크 상에 있는 컴퓨터들은 접속가능해진다.

BASH

sudo ufw allow from 192.168.0.0/24

특정 IP 주소와 일치하는 포트 허용

sudo ufw allow from <ip address> to <protocol> port <port number>

예) 192.168.0.100 주소와 포트, 프로토콜 허용

BASH

sudo ufw allow from 192.168.0.100 to any port 22

특정 IP 주소와 프로토콜, 포트 허용

$ sudo ufw allow from <ip address> to <protocol> port <port number> proto <protocol name>

예) 192.168.0.100 주소와 tcp 프로토콜 22번 포트 허용

BASH

sudo ufw allow from 192.168.0.100 to any port 22 proto tcp

위의 예제들에서 allow 대신 deny를 입력하면 거부가 된다.

ping (icmp) 허용/거부

UFW 기본설정은 ping 요청을 허용하도록 되어있다.

BASH

sudo vi /etc/ufw/before.rules

 

# ok icmp codes

-A ufw-before-input -p icmp --icmp-type destination-unreachable -j ACCEPT

-A ufw-before-input -p icmp --icmp-type source-quench -j ACCEPT

-A ufw-before-input -p icmp --icmp-type time-exceeded -j ACCEPT

-A ufw-before-input -p icmp --icmp-type parameter-problem -j ACCEPT

-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT

위 코드들의 ACCEPT 부분을 모두 DROP으로 변경하거나 삭제하면 ping 요청을 거부하게 된다.

ufw numbered rules

UFW 룰들에 숫자를 붙여서 볼 수 있다. 이를 이용해 룰에 수정이나 삭제, 추가를 할 수 있다.

ufw number 보기

BASH

sudo ufw status numbered

ufw numbered 수정

BASH

sudo ufw delete 1

sudo ufw insert 1 allow from 192.168.0.100

추천 방화벽 정책

BASH

sudo ufw enablesudo ufw allow from 192.168.0.3 to any port 22 proto tcp

sudo ufw allow 123/udp

sudo ufw allow 80/tcp

sudo ufw allow 3306/tcp

sudo ufw status

반응형
블로그 이미지

조이풀 라이프

Lift is short, enjoy the life

,

[root@10.10.10.10 ~]# curl -o /dev/null -w “Connect: %{time_connect} TTFB: %{time_starttransfer} Total time: %{time_total} \n” -s https://internal-test.bbunbro.com

 

Connect: 0.002 TTFB: 0.069 Total time: 0.069

 

외부는 그냥 webpagetest.org 에서 테스트 하시오. 좋습니다.

 

참고 :

(로컬 호스트 톰켓 http listener 체크)

[root@10.10.10.10 ~]# curl -o /dev/null -w “Connect: %{time_connect} TTFB: %{time_starttransfer} Total time: %{time_total} \n” -s http://127.0.0.1:8080/www

 

Connect: 0.000 TTFB: 0.026 Total time: 0.026

 

A friendly formatter for curl requests to help with debugging.

Raw (디버깅을 위한 스니프 포멧 생성 후 테스트 하는 법)

sniff.txt

\n

============= HOST: ==========\n

\n

local_ip: %{local_ip}\n

local_port: %{local_port}\n

remote_ip: %{remote_ip}\n

remote_port: %{remote_port}\n

\n

======= CONNECTION: ==========\n

\n

http_code: %{http_code}\n

http_connect: %{http_connect}\n

num_connects: %{num_connects}\n

num_redirects: %{num_redirects}\n

redirect_url: %{redirect_url}\n

\n

============= FILE: ==========\n

\n

content_type: %{content_type}\n

filename_effective: %{filename_effective}\n

ftp_entry_path: %{ftp_entry_path}\n

size_download: %{size_download}\n

size_header: %{size_header}\n

size_request: %{size_request}\n

size_upload: %{size_upload}\n

speed_download: %{speed_download}\n

speed_upload: %{speed_upload}\n

ssl_verify_result: %{ssl_verify_result}\n

url_effective: %{url_effective}\n

\n

=== TIME BREAKDOWN: ==========\n

\n

time_appconnect: %{time_appconnect}\n

time_connect: %{time_connect}\n

time_namelookup: %{time_namelookup}\n

time_pretransfer: %{time_pretransfer}\n

time_redirect: %{time_redirect}\n

time_starttransfer: %{time_starttransfer}\n

———-\n

time_total: %{time_total}\n

 

\n

 

Steps to Use:

#1. Make a file named sniff.txt and paste the contents of this gist into it

#2. Make an alias in your .bash_profile or .zshrc ( or whatever you use ) that looks like this ( make sure to source .bash_profile the file afterwards ):

alias sniff=’curl -w “@/path/to/sniff.txt” -o /dev/null -s ‘

#3. Now you can use your alias to get some fun data back:

sniff https://api.twitter.com/1.1/search/tweets.json?q=@mrmidi

You will get a response that looks like this:

============= HOST: ==========

local_ip: 192.168.0.67

local_port: 49469

remote_ip: 199.16.156.231

remote_port: 443

======= CONNECTION: ==========

http_code: 400

http_connect: 000

num_connects: 1

num_redirects: 0

redirect_url:

============= FILE: ==========

content_type: application/json; charset=utf-8

filename_effective: /dev/null

ftp_entry_path:

size_download: 62

size_header: 380

size_request: 110

size_upload: 0

speed_download: 57.000

speed_upload: 0.000

ssl_verify_result: 0

url_effective: https://api.twitter.com/1.1/search/tweets.json?q=mrmidi

=== TIME BREAKDOWN: ==========

time_appconnect: 0.724

time_connect: 0.566

time_namelookup: 0.526

time_pretransfer: 0.724

time_redirect: 0.000

time_starttransfer: 1.078

———-

 

time_total: 1.078

반응형
블로그 이미지

조이풀 라이프

Lift is short, enjoy the life

,

Network Interface 우선 순위 변경하기

Ubuntu에서 랜카드를 여러 개 꽂아서 네트워크 인터페이스(Network Interface)가 여러 개 존재할 경우 우선 순위를 바꾸는 방법입니다.

 

먼저 ifmetric를 설치합니다.

 

sudo apt install ifmetric

 

이후 다음 명령어를 이용해 Routing Table을 확인합니다.

 

$ route -n

 

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 10.51.0.1 0.0.0.0 UG 100 0 0 eth0

0.0.0.0 192.168.0.1 0.0.0.0 UG 600 0 0 wlan0

맨 뒤의 Iface 항목이 각 네트워크 인터페이스 이름이며 Metric 항목이 우선 순위라고 생각하면 됩니다. Metric 값이 낮을 수록 우선 순위가 높습니다.

 

ifmetric 명령어를 이용해서 다음과 같이 우선 순위를 변경할 수 있습니다.

 

sudo ifmetric wlan0 50

 

다시 route -n 명령어로 Routing Table을 확인해봅니다.

 

$ route -n

 

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.0.1 0.0.0.0 UG 50 0 0 wlan0

0.0.0.0 10.51.0.1 0.0.0.0 UG 100 0 0 eth0

우선 순위가 바뀐 것을 확인할 수 있습니다.

반응형
블로그 이미지

조이풀 라이프

Lift is short, enjoy the life

,