본문 바로가기
정보보안

정보보안5 7차시

by IT매니절 2024. 8. 24.

환경구성

1. 리눅스1 (NIDS)

2. 리눅스2 (DMZ)

ip a

NIDS 역할의 리눅스에 Network adapter를 추가하여 인터페이스를 두 개로 만든다

nmtui 에서 ens224 추가
ens160 고정ip 셋팅
ens224 고정 셋팅
vi /etc/hosts에 추가

reboot 리부팅

확인

 

또는 systemctl restart NetworkManager

 

짤막보안
sudo vi /etc/ssh/sshd_config
=> PermitRootLogin no 로 설정
sudo systemctl reload sshd.service
root 로그인 원격 접속 차단

 

DMZ의 경우 VMnet1로 설정
DNZ nmtui 셋팅

둘 다 방화벽 삭제 : sudo dnf -y remove firewalld
(yum도 가능)

 

NIDS 리눅스에

sudo dnf -y install iptables-services
sudo systemctl enable --now iptables
systemctl status iptables

iptables 설치 및 활성화

enabled 영구적 실행

sudo cp /dev/null /etc/sysconfig/iptables

iptables 룰 초기화

 

$ sudo 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

 

reboot 후 룰 확인

 

방화벽을 삭제한 이유: 보통 속도를 위해 DMZ 내부에서는 방화벽을 꺼놓는다

NIDS는 iptables를 이용함

 

참고 : https://cafe.naver.com/linuxmasternet/587

 

iptables
CLI 명령어로 ipv4 패킷을 필터링 하거나 nat를 제어하는 관리도구
리눅스에 기본적으로 설치되는 패키지
table-> chain -> rule의 구조

1) table
방화벽에서 기능을 제공하는 것. 소문자
- filter(필터링)
- nat(필터링x 소스주소나 목적지주소를 변환하는 기능)
- mangle(패킷의 특성을 변경하거나 지정하는 기능)
- raw(테이블의 연결추적기능을 더 상세하게 처리가능)
- security(SELinux의 강제접근통제를 지원하는 테이블)


2) chain
패킷이 이동하는 경로. 각각 table마다 chain 존재. 대문자
빌트인체인(INPUT, OUTPUT, FORWARD, PREROUTING, POSTROUTING)은 삭제, 수정 불가

INPUT : 외부 -> 내부

OUTPUT : 내부 -> 외부

FORWARD : 최종목적지에 도달하기 위해 통과하는 패킷

PREROUTING : (INPUT 체인 전) nat테이블을 사용하여 주로 도착지 주소 변경

POSTROUTING : (OUTPUT 체인 후) nat테이블을 사용하여 주로 출발지 주소 변경


3) rule
각 chain에 설정하는 방화벽 정책. 사용자가 지정하여 설정하는 룰.

https://cafe.naver.com/linuxmasternet/587

 

룰 조회

–nL 현재 설정 확인
–nvL 현재 설정 더 자세히
–nL --line 현재 설정 확인+ 줄번호 출력

$ sudo iptables -L               (-L은 List의 약자) $ sudo iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  one.one.one.one      anywhere
ACCEPT     all  --  2.2.2.2              anywhere
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  1.1.1.1              0.0.0.0/0
ACCEPT     all  --  2.2.2.2              0.0.0.0/0
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
$ sudo iptables -nvL --line $ sudo iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 

 

-t filter는 생략할 수 있다 (기본 옵션이기 때문)

nat 테이블 조회시 -t nat 추가
mangle 테이블 조회시 -t mangle 추가

 

( + 오픈소스 방화벽 pfsense

공식사이트 : https://pfsense.org/

웹 기반 GUI를 통해 설정가능
Stateful 패킷 필터링, Block/pass rules, 로깅, L4 기반 상용방화벽 및 스위치의 대부분의 기능들이 제공됨 )

 

-N 옵션으로 사용자정의체인 추가

$ sudo iptables -N CHAN1

$ sudo iptables -N CHAN2

$ sudo iptables -N CHAN3

$ sudo 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 CHAN1 (0 references)
target     prot opt source               destination
Chain CHAN2 (0 references)
target     prot opt source               destination
Chain CHAN3 (0 references)
target     prot opt source               destination

 

빌트인 체인은 (policy ACCEPT) 가 붙어있다

-X로 삭제가능 ( sudo iptables -X CHAN1 )

-E로 이름 수정가능 ( sudo iptables -E CHAN1 CHANMOD1 )

 

방화벽 룰 추가하기 -A
세부옵션
-s : 출발지 주소
-d : 목적지 주소
-p tcp --sport : TCP의 출발지 포트
-p tcp --dport : TCP의 목적지 포트
-p udp --sport : UDP의 출발지 포트
-p udp --dport : UDP의 목적지 포트

 

ex)

iptables -A INPUT -s 1.1.1.1 -j DROP

=> INPUT 체인에 -s 출발지 주소가 1.1.1.1인 패킷이 -j 맞다면 DROP 차단하는 룰을 -A 추가

( 1.1.1.1 은 1.1.1.1/32 로 쓸 수 있다 )

iptables -A INPUT -s 1.1.1.1 -j ACCEPT

=> 조건은 같지만 패킷을 허용한다

iptables -A INPUT -s 1.1.1.1 -j REJECT

=> 조건은 같지만 상대방에게 패킷을 버린 것을 알려준다

iptables -A INPUT -s 1.1.1.1 -j LOG

=> 조건은 같지만 로그를 기록한다

그외 ( -RETURN 패킷검사중지나 사용자정의체인으로 지정하여 이동시킬 수 있다 )

 

순서는 변경되어도 상관없다

iptables -j DROP -A INPUT -s 1.1.1.1 이런식으로...

 

칼리 리눅스에서
scapy를 이용해
packet = Ether()/IP(dst='192.168.100.6', src='1.1.1.1')/ICMP(seq=8888)
sendp(packet)

전송하면

 

$ sudo iptables -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 
    1    28 DROP       all  --  *      *       1.1.1.1              0.0.0.0/0   
    0     0 ACCEPT     all  --  *      *       2.2.2.2              0.0.0.0/0 

INPUT 체인에 추가한 DROP 룰의 pkts 카운트가 1이 된 것을 확인할 수 있다

 

packet = Ether()/IP(src='2.2.2.2', dst='192.168.100.6')/ICMP(seq=8888)
sendp(packet, count=2)

카운트를 지정하여 전송

 

$ sudo iptables -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 
    1    28 DROP       all  --  *      *       1.1.1.1              0.0.0.0/0   
    2    56 ACCEPT     all  --  *      *       2.2.2.2              0.0.0.0/0

 

2.2.2.2 ACCEPT 룰에 2개 추가된 것이 확인됨

 

룰삭제

iptables -D INPUT -s 1.1.1.1 -j DROP

또는

 

$ sudo iptables -nvL INPUT --line
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        1    28 DROP       all  --  *      *       1.1.1.1              0.0.0.0/0
2        2    56 ACCEPT     all  --  *      *       2.2.2.2              0.0.0.0/0
3        2    56 DROP       all  --  *      *       3.3.3.3              0.0.0.0/0

 

--line 옵션으로 룰번호를 확인해서

iptables -D INPUT 1

이렇게 룰번호로 삭제할 수 있다

 

룰수정

iptables -R INPUT 2

$ sudo iptables -R INPUT 2 -s 2.2.2.2 -j DROP
$ sudo iptables -nvL INPUT --line
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        1    28 DROP       all  --  *      *       1.1.1.1              0.0.0.0/0
2        0     0 DROP       all  --  *      *       2.2.2.2              0.0.0.0/0
3        2    56 DROP       all  --  *      *       3.3.3.3              0.0.0.0/0

=> 2번룰이 ACCEPT였는데 DROP으로 변경됨

 

 

NIDS에 DNAT 설정하기 (목적지 주소 변경)

1) IP 주소 전체 사용

2) 포트번호만 사용

 

패킷 포워딩 설정
/etc/sysctl.conf 파일에 커널 파라미터값 설정 (vi 사용)
net.ipv4.ip_forward = 1

 

cat /proc/sys/net/ipv4/ip_forward

해보면 1로 나오는 것 확인

 

(+ comment 모듈로 주석처럼 comment를 달 수 있다 )

 

$ sudo iptables -t nat -A PREROUTING -i ens160 -p tcp --dport 80  -d 192.168.100.10 -j DNAT --to 10.10.11.10:80

( 해석

nat 테이블에 -A PREROUTING 룰을 추가한다. -i ens160 인터페이스로 들어오는 패킷이

tcp프로토콜이고 목적지 주소가 192.168.100.10에 포트가 80일 경우 ---to 10.10.11.10:80 으로 변경 )

$ sudo iptables -t nat -A PREROUTING -i ens160 -p tcp --dport 443 -d 192.168.100.10 -j DNAT --to 10.10.11.10:443

 

$ sudo iptables -t nat -nvL PREROUTING
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 
    0     0 DNAT       tcp  --  ens160 *       0.0.0.0/0            192.168.100.10       tcp dpt:80 to:10.10.11.10:80
    0     0 DNAT       tcp  --  ens160 *       0.0.0.0/0            192.168.100.10       tcp dpt:443 to:10.10.11.10:443

 

iptables-save > /etc/sysconfig/iptables

방화벽 룰 저장. 일반유저는 불가능하여 잠깐 root로 변경하여 저장함.

 

 

NIDS에 SNAT 설정하기

* IP주소를 통째로 넘기는 경우

 

$ sudo iptables -t nat -A POSTROUTING -o ens160 -s 10.10.11.10 -j SNAT --to 192.168.100.10

 

설정 후 DMZ에서 ping 테스트 해보면 NIDS를 경유하여 잘된다 

'정보보안' 카테고리의 다른 글

정보보안6 1차시  (0) 2024.08.31
정보보안5 8차시  (0) 2024.08.25
정보보안5 6차시  (0) 2024.08.18
정보보안5 5차시  (0) 2024.08.17
정보보안5 4차시  (0) 2024.08.11