IPTABLES-LAB> 방화벽 점검도구/보안도구 (hping) 사용하기 

--> 해보기

방화벽을 테스트하는 툴 : hping 

http://hping.org/download.php

http://hping.org/hping2.0.0-rc3.tar.gz


# wget http://hping.org/hping2.0.0-rc3.tar.gz

# tar xzf hping2.0.0-rc3.tar.gz

# cd hping2-rc3

# ./configure

# make

# mkdir -p /usr/local/man/man8

# make install  <-- /usr/sbin 에 설치된다.

# ./hping2 --help


(이번엔 hping3 설치해봄.. git에서 받을 수 있었음 그래서 git부터 설치)


(hping3 다운로드)





(hping3 설치 시 필요한 라이브러리 설치 libpcap과 libpcap-devel 과 ncurses 그리고 tcl

rpm -qa | grep pcap / rpm -qa | grep ncurses 로 설치 여부 확인 가능 )


그리고 find로 bpf.h 를 찾는다 find /usr -name *bpf.h -ls

아마 /usr/local/include/bpf.h 나 /usr/include/pcap-bpf.h 라는 식으로 있을 것이다

이걸 /usr/local/include/net/bpf.h 하고 /usr/include/net/bpf.h으로 심볼릭 링크를 걸어 준다.

(pcap-bpf.h이 원본 bpf.h이 심볼릭링크)


ex) mkdir /usr/local/include/net

ex) ln -sf /usr/include/pcap-bpf.h /usr/local/include/net/bpf.h



(hping3 설치)

man8 디렉터리는 안만들어도 되는거 같다.


(/usr/sbin/hping3 에 실행파일위치)


# iptables -F

# iptables -N DROP-IP

# iptables -N ACCEPT-IP


# iptables -A DROP-IP -s 192.168.56.55 -j DROP

# iptables -A INPUT -j ACCEPT-IP

# iptables -A ACCEPT-IP -j DROP-IP


# iptables-save > /etc/sysconfig/iptables


# hping -a 192.168.56.55 192.168.56.200 -S -c 1 -i u1000

-a 는 src IP 지정 그냥 IP쓰는건 dst IP 지정 -S 는 SYN 패킷

-c 는 몇개 보낼지 지정 -i u1000 은 보내는 시간 지정 수가 클수록 빨리 보냄


# iptables -nvL DROP-IP

Chain DROP-IP (1 references)

 pkts bytes target     prot opt in     out     source               destination         

    1    40 DROP       all  --  *      *       192.168.56.55        0.0.0.0/0    



# hping -a 192.168.56.55 192.168.56.200 -S -c 5 -i u1000

# iptables -nvL DROP-IP

Chain DROP-IP (1 references)

 pkts bytes target     prot opt in     out     source               destination         

    6   240 DROP       all  --  *      *       192.168.56.55        0.0.0.0/0     

======================================================================


======================================================================

IPTABLES-LAB> 서버의 서비스를 스캔하는 보안도구 (nmap) 사용하기 

--> 해보길

# iptables -F

# wget http://nmap.org/dist/nmap-6.46.tar.bz2

# tar xjf nmap-6.46.tar.bz2

# cd nmap-6.46

# ./configure && make && make install



# ls /usr/local/bin/

nmap nampfe nping



# nmap -sT -p1-65535 localhost

-sT 는 TCP open 스캔 -p 1-65535는 스캔할 포트 범위 지정

localhost 부분에 타겟 IP

  :

  :

PORT      STATE SERVICE

22/tcp    open  ssh

25/tcp    open  smtp

53/tcp    open  domain

110/tcp   open  pop3

111/tcp   open  rpcbind

737/tcp   open  unknown

953/tcp   open  rndc

33750/tcp open  unknown

40285/tcp open  unknown

45990/tcp open  unknown



- 특정 IP만 접근하게 설정하고 포트스캔을 해본다.

# iptables -A INPUT -p tcp --dport 22 -s 192.168.56.1 -j ACCEPT

# iptables -A INPUT -p tcp --dport 22 -j DROP

# nmap -sT -p 22 localhost

-p 22 (22번 포트만 스캔한다)

  :

  :

PORT   STATE    SERVICE

22/tcp filtered ssh         <-- 필터링 되었다는걸 의미한다.

  :

  :


# nmap -sT -p 23 localhost

  :

  :

PORT   STATE  SERVICE

23/tcp closed telnet        <-- 닫힌 포트를 의미한다.

  :

  :

======================================================================


======================================================================

실습> iptables 를 이용하여 아래 서비스들을 추가하여 보자.

--> 해보길

/etc/services  <-- 서비스명이 있다.


서비스명 : www , ssh, smtp, rsync   <-- ACCEPT  

룰순서   : www , ssh, smtp, rsync   

서비스명 : icmp <-- REJECT

포트 : 6667  <-- DROP

나머지 : DROP


# iptables -F

# iptables -A INPUT -p tcp --dport www -j ACCEPT

# iptables -A INPUT -p tcp --dport ssh -j ACCEPT

# iptables -A INPUT -p tcp --dport 25  -j ACCEPT 

# iptables -A INPUT -p tcp --dport rsync -j ACCEPT

# iptables -A INPUT -p icmp -j REJECT

# iptables -A INPUT -p tcp --dport 6667 -j DROP

# iptables -A INPUT -j DROP



# iptables -nvL --line-numbers   <-- 룰번호를 출력



# alias it='iptables -nvL INPUT --line-numbers'

(명령어가 길어지니 alias로 단축시킴)

# it



- 위에서 설정된 방화벽 룰에 포트번호 8000을 추가해보자.

# iptables -A INPUT -p tcp --dport 8000 -j ACCEPT


(-A 로 추가하면 맨 아래 추가됨 all DROP 밑에 있어서 안됨)


# iptables -D INPUT 8


( -D 에서 line 번호로 룰 삭제)


# iptables -I INPUT 7 -p tcp --dport 8000 -j ACCEPT


(-I (대문자아이) 옵션으로 all DROP 위인 7번 라인에 룰 추가 함으로써 새로 추가한 룰이 all DROP 위에 위치)


- 위에서 설정한 방화벽 룰을 저장해보자.

# iptables-save > /root/iptables


- 위에서 저장한 방화벽 룰을 이용해서 복구해보자.

# iptables -F INPUT

# iptables-restore /root/iptables

# iptables -nvL



# cat /root/iptables






RAW 소켓을 이용하면 프로그램을 사용자가 조작할 수 있다.


# hping localhost -S -p 80 -c 3  <-- 80번 포트로 연결패킷을 날린다.

HPING localhost (lo 127.0.0.1): S set, 40 headers + 0 data bytes

len=44 ip=127.0.0.1 flags=SA DF seq=0 ttl=128 id=0 win=32792 rtt=0.0 ms

len=44 ip=127.0.0.1 flags=SA DF seq=1 ttl=128 id=0 win=32792 rtt=0.3 ms

len=44 ip=127.0.0.1 flags=SA DF seq=2 ttl=128 id=0 win=32792 rtt=0.2 ms


--- localhost hping statistic ---

3 packets tramitted, 3 packets received, 0% packet loss


(flags가 R A 가 왔으므로 80번 포트가 닫혀있다는 뜻 리셋(Rst), 에크(Ack))


# hping localhost -S -p 25 -c 30 -i u1000

HPING localhost (lo 127.0.0.1): S set, 40 headers + 0 data bytes

len=44 ip=127.0.0.1 flags=SA DF seq=0 ttl=128 id=0 win=32792 rtt=0.0 ms

len=44 ip=127.0.0.1 flags=SA DF seq=1 ttl=128 id=0 win=32792 rtt=0.0 ms

                    ~~~~~~~~~  <-- SYN 패킷에 대한 응답패킷이다.

응답 패킷이 왔다는 것은 서버에서 25번 포트가 오픈되어 있다는걸 의미한다.


(flags가 S A -> 25번 포트가 열려있다는 의미 씬(Syn), 에크(Ack))


# hping localhost -S -p 6667 -c 3 -i u1000

HPING localhost (lo 127.0.0.1): S set, 40 headers + 0 data bytes

     <-- 응답 패킷이 안온 이유는 서버에서 모두 DROP 되었기 때문이다.

또한 클라이언트 입장에서 응답 패킷이 안왔다는 것은 서버가 이 포트를 

오픈하지 않다는걸 의미한다.

--- localhost hping statistic ---

3 packets tramitted, 0 packets received, 100% packet loss

round-trip min/avg/max = 0.0/0.0/0.0 ms


(응답이 안옴 -> DROP 되었다. 방화벽일 가능성 생김)


# it | grep 6667

7   3 120 DROP    tcp  --  *  *  0.0.0.0/0   0.0.0.0/0  tcp dpt:6667 

   ~~~ <-- 3개의 패킷이 왔고 이 패킷 모두가 DROP 룰에 걸렸다.



실습> iptables 의 설정룰을 변경하고 hping 에서 테스트해보기


DROP  : 방화벽에서 패킷을 버린다.

REJECT : 방화벽에서 패킷을 버리지만 클라이언트로 결과를 알려준다.


# iptables -nvL --line-numbers

...

4   30  1200 ACCEPT   tcp  --  *   *  0.0.0.0/0  0.0.0.0/0   tcp dpt:25 

...



# iptables -D INPUT 4

# iptables -I INPUT 4 -p tcp --dport 25 -j REJECT



# hping localhost -S -p 25 -c 3 -i u1000




실습> iptables 의 설정룰을 파일로 저장해보자.

-->복습

!!! 저장은 iptables-save 라는 명령어를 이용한다.


# iptables-save > iptables_testrule.txt

# cat iptables_testrule.txt 

# Generated by iptables-save v1.3.5 on Wed Aug  1 10:30:08 2012

*filter

:INPUT ACCEPT [12536:1511210]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [6117:660585]

-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT 

-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT 

-A INPUT -p tcp -m tcp --dport 2200 -j ACCEPT 

-A INPUT -p tcp -m tcp --dport 25 -j REJECT --reject-with icmp-port-unreachable 

-A INPUT -p tcp -m tcp --dport 873 -j ACCEPT 

-A INPUT -p icmp -j REJECT --reject-with icmp-port-unreachable 

-A INPUT -p tcp -m tcp --dport 6667 -j DROP 

-A INPUT -j DROP 

COMMIT

# Completed on Wed Aug  1 10:30:08 2012


o iptables 의 모듈디렉토리 

# ls /lib/iptables

...

libipt_tcp.so



!!! 중요한 사항 !!!

인터넷에서 iptables 에 대한 강좌가 있는데 어떤 강좌를 보니까

이런 내용의 룰 설정이 있었다... 그런데 내가 가지고 있는 서버에서 

해보니까 안되더라...~~~~  


예)

# iptables -A INPUT ... -m abc ...  


해결책 : abc 모듈이 현재 서버에 존재하는지를 /lib/iptables 디렉토리에서

체크해야 한다. 만약 abc 모듈이 없다면 위의 예에 있는 룰은 실행이 

안된다..


실습> iptables 의 설정룰을 복구해보자.


iptables-restore 를 이용한다.


# iptables-restore iptables_testrule.txt 


실습> iptables 의 설정룰을 복구하고 iptables 의 기본룰로 저장해보자.


# iptables-restore iptables_testrule.txt 

# iptables-save > /etc/sysconfig/iptables 



# /etc/init.d/iptables stop  

# /etc/init.d/iptables start

# iptables -nvL

    <-- /etc/sysconfig/iptables 의 룰이 그대로 설정되어 있다.



/etc/init.d/iptables 스크립트에 /etc/sysconfig/iptables 파일을 

restore 하는 부분이 있다.


실습> iptables 에서 접근하는 클라이언트의 IP 를 막아보자.


클라이언트 1 : 192.168.56.100

클라이언트 2 : 192.168.56.200


# iptables -F

# iptables -A INPUT -s 192.168.56.100 -j DROP

# iptables -A INPUT -s 192.168.56.200 -j DROP

# iptables -nvL INPUT

Chain INPUT (policy ACCEPT 5327 packets, 449K bytes)

 pkts bytes target     prot opt in     out     source               destination         

    0     0 DROP       all  --  *      *       192.168.56.100       0.0.0.0/0           

    0     0 DROP       all  --  *      *       192.168.56.200       0.0.0.0/0  


# hping localhost -a 192.168.56.100 -S -c 5 -i u1000

HPING localhost (lo 127.0.0.1): S set, 40 headers + 0 data bytes

     <-- 이 부분에 응답이 없는 이유는 ???  DROP 이기 때문에

--- localhost hping statistic ---

5 packets tramitted, 0 packets received, 100% packet loss

round-trip min/avg/max = 0.0/0.0/0.0 ms


# iptables -nvL INPUT  <-- 패킷 카운트 5 확인

Chain INPUT (policy ACCEPT 5973 packets, 496K bytes)

 pkts bytes target     prot opt in     out     source               destination         

    5   200 DROP       all  --  *      *       192.168.56.100       0.0.0.0/0           

    0     0 DROP       all  --  *      *       192.168.56.200       0.0.0.0/0      


# hping localhost -a 192.168.56.200 -S -c 3 -i u1000

HPING localhost (lo 127.0.0.1): S set, 40 headers + 0 data bytes

     <-- 패킷에 응답이 없는 이유는 ???  DROP 이기 때문에

--- localhost hping statistic ---

3 packets tramitted, 0 packets received, 100% packet loss

round-trip min/avg/max = 0.0/0.0/0.0 ms


# iptables -nvL INPUT   <-- 패킷 카운트 3 확인

Chain INPUT (policy ACCEPT 6698 packets, 554K bytes)

 pkts bytes target     prot opt in     out     source               destination         

    5   200 DROP       all  --  *      *       192.168.56.100       0.0.0.0/0           

    3   120 DROP       all  --  *      *       192.168.56.200       0.0.0.0/0      



# hping localhost -a 192.168.56.111 -S -c 5 -i u1000

      <-- 패킷이 응답이 없는 이유는 ???

내 IP가 111이 아니기 때문이겠징


실습> iptables 에서 정상적인 패킷이 아닌 경우 DROP 하는 룰을 추가해보자.


# iptables -F

# iptables -A INPUT -m state --state INVALID -j DROP

# iptables -nvL

Chain INPUT (policy ACCEPT 14843 packets, 1280K bytes)

 pkts bytes target     prot opt in     out     source               destination         

    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           state INVALID 



# ls /lib/iptables  <-- state 에 관련된 모듈이 존재해야 사용할 수 있다.

... libipt_state.so  ...  

(64비트 OS일 경우 ls /lib64/iptables)


실습> iptables 에서 udp 포트를 모두 막아보자. (단! DNS 쿼리는 허용!)


# iptables -F

# iptables -A INPUT -p udp -m udp --dport 53 -j ACCEPT

# iptables -A INPUT -p udp -m udp  -j DROP

# iptables -nvL INPUT


# hping localhost -S -2 -p 53 -c 3 -i u1000

# iptables -nvL INPUT

 3    84 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:53 



실습> iptables 에서 애플리케이션 부분의 특정 문자열을 막아보자.


# iptables -F

# telnet nahs.or.kr 80

Trying 218.38.12.95...

Connected to nahs.or.kr.

Escape character is '^]'.

GET / HTTP/1.0

Host: nahs.or.kr    <-- 특정 문자열을 잡아보자


... <-- 소스가 출력



지울 때 Ctrl + Backspace

GET ~

Host ~ 쓰고 엔터 2번 (Head 끝)


# iptables -F

# iptables -A OUTPUT -m string --string "Host: nahs.or.kr" --algo kmp -j DROP



# telnet nahs.or.kr 80

Trying 218.38.12.95...

Connected to nahs.or.kr.

Escape character is '^]'.

GET / HTTP/1.0

Host: nahs.or.kr    <-- 특정 문자열을 잡아보자


^]

telnet> quit


# lynx nahs.or.kr <-- 막힌다.


(lynx 설치는 이렇게)


(요청을 보냈지만 방화벽에서 DROP 당해서 응답은 오지 않음)


# lynx www.nahs.or.kr <-- 요건 못막는다. 이걸 막을려면 아래를 실행한다.


# iptables -A OUTPUT -m string --string "Host: www.nahs.or.kr" --algo kmp -j DROP


# iptables -nvL OUTPUT




실습> 서버에서 악성코드(a.exe) 파일을 다운받지 못하게 설정하자.


-- 클라이언트에서 실행 (방화벽에 설정이 안되어 있을때) --

# wget nahs.or.kr/a.exe

--2012-08-01 12:27:53--  http://nahs.or.kr/a.exe

Resolving nahs.or.kr... 218.38.12.95

Connecting to nahs.or.kr|218.38.12.95|:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 95116 (93K) [application/x-msdownload]

Saving to: `a.exe'


100%[======================>] 95,116      --.-K/s   in 0.07s   


2012-08-01 12:27:53 (1.21 MB/s) - `a.exe' saved [95116/95116]


(nahs.or.kr 에 a.exe가 없고 밑에 서버에서 방화벽 설정도 해야하니 다른 Kali OS를 하나 준비하여 서버로 사용함)


-- 서버에서 설정 --

# iptables -A INPUT -m string --string "a.exe" --algo kmp -j DROP


(서버는 Kali 이용했음)


-- 클라이언트에서 실행 (서버에 방화벽이 설정되어 있을때) --

# wget nahs.or.kr/a.exe <-- 방화벽에 의해서 다운로드를 못함.

--2012-08-01 12:31:42--  http://nahs.or.kr/a.exe

Resolving nahs.or.kr... 218.38.12.95

Connecting to nahs.or.kr|218.38.12.95|:80... connected.

HTTP request sent, awaiting response...   




o iptables 패키지 확인


# rpm -qa | grep iptables

iptables-1.3.5-9.1.el5

# rpm -ql iptables | grep bin

/sbin/iptables              <-- 방화벽 룰을 설정하는 명령어

/sbin/iptables-restore      <-- 방화벽 룰을 복구하는 명령어

/sbin/iptables-save         <-- 방화벽 룰을 저장하는 명령어

# rpm -qc iptables           

/etc/rc.d/init.d/iptables

/etc/sysconfig/iptables-config

# rpm -qd iptables

/usr/share/doc/iptables-1.3.5/COPYING

/usr/share/doc/iptables-1.3.5/INCOMPATIBILITIES

/usr/share/doc/iptables-1.3.5/INSTALL

/usr/share/man/man8/iptables-restore.8.gz

/usr/share/man/man8/iptables-save.8.gz

/usr/share/man/man8/iptables.8.gz


==================================================================