고고잉 2023. 1. 1. 19:10

포트미러링
-Local SPAN
1/1 에서 들어오는 프래임을 1/2로 보내기

- both : 양방향
- rx : 수신된
- tx : 송신된

ESW1(config)#monitor session 1 source  int fa 1/1 rx
ESW1(config)#monitor session 1 destination int fa1/2

- 모니터 세션을 설정하면 데이터 송수신 안된다. 
- 모니터 세션으로 만들기 전에 설치 해야 할 거 있으면 모니터 세션 설정 전에 미리 받아놔라


IDS-Snort 설치
yum install gcc flex bison zlib zlib-devel libpcap libpcap-devel pcre pcre-devel libdnet libdnet-devel tcpdump

daq-2.0.6.tar
snort-2.9.9.0.tar
snortrules-snapshot-2990.tar
바탕화면에 넣어두고
mv daq-2.0.6.tar.gz /usr/local/src/
mv snort-2.9.9.0.tar.gz /usr/local/src/
mv snortrules-snapshot-2990.tar.gz /usr/local/src/

압축해제

daq-2.0.6.tar 압축해제 하고 
[root@localhost]# tar -zxvf daq-2.0.6.tar.gz
[root@localhost]# cd daq-2.0.6
./configure -> make -> make install
확인은 
[root@localhost snort-2.9.9.0]# snort --daq-list

snort-2.9.9.0.tar 압축해제 하고
[root@localhost snort-2.9.9.0]# tar -zxvf snort-2.9.9.0.tar.gz
./configure --enable-sourcefire -> make -> make install
확인은 [root@localhost snort-2.9.9.0]# snort -V


snortrules-snapshot-2990.tar 압축 해제 하기전에
첫번째 /etc/snort 디렉토리 만들어주고
두번째 [root@localhost snort-2.9.9.0]목록에 있는 etc 안에 들어가 그안에 있는 파일 목록들을
세번째 /etc/snort/ 경로로 복사한다. 
[root@localhost src]# mkdir /etc/snort
[root@localhost snort-2.9.9.0]# cd etc/
[root@localhost etc]# cp * /etc/snort/

그리고 압축 해제
[root@localhost src]# tar -zxvf snortrules-snapshot-2990.tar.gz -C /etc/snort/
[root@localhost src]# cd /etc/snort/
[root@localhost snort]# ls
Makefile     attribute_table.dtd    file_magic.conf  reference.config  so_rules
Makefile.am  classification.config  gen-msg.map      rules             threshold.conf
Makefile.in  etc                    preproc_rules    snort.conf        unicode.map

[root@localhost snort]# cd etc/

[root@localhost etc]# ls
classification.config  reference.config  sid-msg.map  snort.conf  threshold.conf  unicode.map

[root@localhost etc]# pwd
/etc/snort/etc

[root@localhost etc]# cd ..

[root@localhost snort]# ls
Makefile     attribute_table.dtd    file_magic.conf  reference.config  so_rules
Makefile.am  classification.config  gen-msg.map      rules             threshold.conf
Makefile.in  etc                    preproc_rules    snort.conf        unicode.map

- 목록 안에 있는 etc안에 있는 파일들을 꺼내 덮어 씌운다
[root@localhost snort]# cp ./etc/* .
cp: overwrite `./classification.config'? y
cp: overwrite `./reference.config'? y
cp: overwrite `./snort.conf'? y
cp: overwrite `./threshold.conf'? y
cp: overwrite `./unicode.map'? y


[root@localhost snort]# touch rules/white_list.rules
[root@localhost snort]# touch rules/black_list.rules

[root@localhost snort]# vim snort.conf 

상대경로를 절대경로로 변경
104 var RULE_PATH /etc/snort/rules
105 var SO_RULE_PATH /etc/snort/so_rules
106 var PREPROC_RULE_PATH /etc/snort/preproc_rules
107 
108 # If you are using reputation preprocessor set these
109 var WHITE_LIST_PATH /etc/snort/rules
110 var BLACK_LIST_PATH /etc/snort/rules

계정 추가
[root@localhost snort]# useradd snort -u 40000 -d /var/log/snort -s /sbin/nologin -c SNORT_IDS

[root@localhost snort]# tail /etc/passwd
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
test:x:500:500:test:/home/test:/bin/bash
arpwatch:x:77:77::/var/lib/arpwatch:/sbin/nologin
ftpuser:x:501:501::/home/ftpuser:/bin/bash
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
ftptest:x:502:502::/home/ftptest:/bin/bash
snort:x:40000:40000:SNORT_IDS:/var/log/snort:/sbin/nologin


[root@localhost snort]# cd /etc/
[root@localhost etc]# chown -R snort:snort snort
[root@localhost etc]# chmod -R 700 snort

[root@localhost etc]# cd /usr/local/src
[root@localhost src]# chown -R snort:snort daq-2.0.6 snort-2.9.9.0 snort_dynamicsrc
[root@localhost src]# chmod -R 700 daq-2.0.6 snort-2.9.9.0 snort_dynamicsrc

[root@localhost src]# cd /usr/local/lib
[root@localhost lib]# mkdir snort_dynamicrules
[root@localhost lib]# chown -R snort:snort snort* pkgconfig
[root@localhost lib]# chmod -R 700 snort* pkgconfig

[root@localhost lib]# cd /usr/local/bin/
[root@localhost bin]# chown -R snort:snort daq-modules-config u2*
[root@localhost bin]# chmod -R 700 daq-modules-config u2*

설정 확인
[root@localhost bin]# snort -T -i eth0 -u snort -g snort -c /etc/snort/snort.conf 

-설정 확인 했을 때 맨 아래에 에러가 뜨면 snort.conf 들어가서 lzma를 지워준다.
ERROR: /etc/snort/snort.conf(326) => Invalid keyword '}' for server configuration.


[root@localhost ~]# vim /etc/snort/snort.conf 

> decompress : 압축해제
> swf : 영상 인코딩 관련 타입
> lzma : 현재 지원이 안돼서 지워야함

325     decompress_swf { deflate lzma } \
326     decompress_pdf { deflate }
                    
↓↓↓

325     decompress_swf { deflate } \
326     decompress_pdf { deflate }


스위치에 모니터 세션 설정하기

snort 실행

기본실행 : snort

실행종료 :  ctrl + c

초기화된 설정파일 구동
[root@localhost ~]# snort -c /etc/snort/snort.conf 

옵션붙혀서 실행
> -v : 자세히
> -e : 데이터링크계층 포함해서 표시
> -d :  어플리케이션 계층 포함해서 표시
> -l : 로그기록
[root@localhost snort]# snort -ved -c /etc/snort/snort.conf -l /var/log/snort

[root@localhost snort]# cd /etc/snort/rules/


[root@localhost rules]# vim local.rules 안에 아레내용 작성
alert icmp any any <> any any (sid:1000000; msg:"icmp test";)
저장 후

[root@localhost rules]# snort -v -c /etc/snort/snort.conf 
실행

[root@localhost ~]# tail -f /var/log/snort/alert 로 로그 찍히는지 확인

연습
tcp 패킷 탐지, "tcp test" 메시지 출력
udp 패킷 탐지, "udp test" 메시지 출력

> sid 중복되지 않도록 다음 번호 할당 


풀이

alert tcp any any <> any any (sid:1000001; msg:"tcp test";)
alert udp any any <> any any (sid:1000002; msg:"udp test";)

Snort Rule 구성 중 content 사용
alert tcp any any <> 192.168.0.100 23 (sid:1000003; msg:"telnet root detect"; content:"root";)


와이어샤크 리눅스에 설치
[root@localhost rules]# yum install wireshark wireshark-gnome

Snort Rule 구성 중 nocaset 사용
> 기본적으로 대소문자 구별함
> nocase : 대소문자 구별 안함
alert tcp any any <> 192.168.0.100 23 (sid:1000003; msg:"telnet root detect"; content:"root"; nocase;)

payload detection rule option
alert tcp any any -> 192.168.0.100 80 (sid:1000004; msg:"HTTP GET Method detect"; content:"GET"; http_method;)


연습2
주소 지정하지 않고 출발지와 목적지가 같은 패킷이 수신 시 
"Dos - Land Attack Detected" 메시지 출력하시오
> 칼리에서 웹서버 쪽으로 land attack 공격을 수행 후 확인

samip 
- land attack 막을 수 있다.

alert icmp any any <> 192.168.0.100  any (sid:1000005; msg:"DoS - Land Attack Detect";  sameip;)

hping3 -a 192.168.0.100 34.0.0.100 --icmp --flood 


flags
> S : SYN
> A : ACK
alert tcp any any <> any any (sid:1000006; msg:"tcp flags test"; flags: S;)
alert tcp any any <> any any (sid:1000006; msg:"tcp flags test"; flags: SA;)

연습3
TCP Scanning
FIN, NULL, X-mas Scan 탐지 룰 작성 테스트
msg : FIN Scan
msg : NULL Scan
msg : X-mas Scan

풀이3
alert tcp any any <> any any (sid:1000007; msg:"FIN Scan"; flags: F;)
alert tcp any any <> any any (sid:1000008; msg:"NULL Scan"; flags:0;)
alert tcp any any <> any any (sid:1000009; msg:"X-mas Scan"; flags:fpu ;)

threshold
> type limit
> 10초에 1개씩 로그 기록
alert icmp any any -> 192.168.0.100 any (sid:1000010; msg:"threshold type limit"; threshold: type limit, track by_dst, count 3, seconds 10;)

> type threshold
> seconds는 상관없이 카운트 수만큼 하나씩 매치
alert icmp any any -> 192.168.0.100 any (sid:1000010; msg:"threshold type threshold"; threshold: type limit, track by_dst, count 3, seconds 10;)

> type both
> 카운트 일때 주기까지 적용되어 로그 기록
alert icmp any any -> 192.168.0.100 any (sid:1000010; msg:"threshold type both"; threshold: type limit, track by_dst, count 3, seconds 10;)


연습4
"TCP SYN Flooding Attack"

- 출발지는 랜던 목적지는 고정으로 해서 SYN를 보내는 것으로 
정상과 비정상을 구분하기 힘든데 구분 할 수 있는건 속도이다.(얼마나 빠르게 보내는가)

- 이상탐지 -> 기준을 잘 정해야 함 , 잘못 정하면 정상도 비정상으로 판단하게 됨

칼리에서 공격
# hping3 --rand-source 34.0.0.100 -p 80 -S --fast

IDS 서버에서 로그 확인
alert tcp any any -> 192.168.0.100 any (sid:1000011; msg:"TCP SYN Flooding Attack"; \
                                        flags:S;\
                                        threshold: type both,\
                                         track by_dst,\
                                         count 10, seconds 1;)


> rules 설정할 때 \를 붙히면 다음칸에 써도 명령이 이어진다.


연습1
"Ping of Death Attack Detected" 룰 작성 후 테스트

풀이1
> 정답은 여러개가 될 수 있다.
alert ip any any -> 192.168.0.100 any (sid:1000012; msg:"Ping of Death Attack Detected"; \
                                        fragbits:M;\
                                        threshold: type both,\
                                         track by_src, \
                                        count 5,\
                                         seconds 1;)