본문 바로가기

공부/리눅스

IDS - snort 1

반응형
snort (http://www.snort.org)

참고 문서 : snort.pdf

네트워크기반 IDS

Ineternet ---- Router ------------ firewall ------------------ intranet
| | |
IDS | IDS
|----- IDS
|
DMZ

- open source NIDS
실시간 네트워크 트랙픽 분석과 로깅을 할수 있다.
프로토콜 분석, 다양한 공격을 탐지할 수 있다.
버퍼오버플로오 스탤스 스캔 , CGI 공격, OS 핑거프린터 시도를 포함한 다양한 공격을 탐지해 낼수 있다.

- 설치

snort 설치전에 필요한 패키지를 먼저 설치하여야 한다.

1. mysql 서버 및 클라이언트를 설치하여야 한다.
2. libpcap 을 설치해야 한다. (yum 으로 설치가능하다)
- snort 를 설치하기위해서는 libpcap을 컴파일해서 설치하는것이 더 편리한거
같습니다.
http://www.tcpdump.org/release (여기서 libpcap 소스파일을 다운로드 받는다)
다운받아서 압축을 풀고 ./configure ; make ; make install

4. http://www.pcre.org 에서
pcre(Perl Compatible Regular Expressions)최신 소스 패키지를 다운로드 받는다.
압축을 풀고 ./confiugre 스크립트를 옵션없이 실행하고 컴파일 한다.
./configure ; make ; make install

5. snort 설치
http://www.snort.org
http://www.snort.org/downloads 여기서 소스파일을 받는다.
6월14일 현재 최신버전은 snort-2.8.6.tar.gz 이다.

압축을 풀고
./configure --prefix=/usr/local/snort-2.8.6 --with-mysql=/usr/local/mysql

옵션설명
--prefix ; make install 이 끝났을때 snort 가 설치될 디렉토리를 명시하기 위한것.
--with-mysql ; snort 가 mysql 과 연동되기 위한 옵션.
에러없이 configure 가 완료되면
make && make install

*. 아래내용은 snort 가 패킷을 table 에 저장하기 위해서
필요한 테이블을 생성하는 작업이다.

[root@centos1 libpcap-0.9.4]# mysql -u root -p
mysql> create database snort
mysql> use snort
Database changed
mysql> source /tmp/snort-2.8.6/schemas/create_mysql
mysql> show tables;
+------------------+
| Tables_in_snort |
+------------------+
| data |
| detail |
| encoding |
| event |
| icmphdr |
| iphdr |
| opt |
| reference |
| reference_system |
| schema |
| sensor |
| sig_class |
| sig_reference |
| signature |
| tcphdr |
| udphdr |
+------------------+
16 rows in set (0.01 sec)

mysql>

mysql> grant insert,select on snort.* to snort@localhost
-> identified by 'pass1234'; <= snort 계정을 만들고 암호는 pass1234 로 지정.
그리고 snort db 에 대해서 insert 권한과 select 권한을 허락함.
Query OK, 0 rows affected (0.06 sec)

mysql>flush privileges; <= user table reload

세가지 주 사용방법.
- tcpdump 와 같은 packet sniffer 로 사용.
- 패킷 logger 로 사용(network traffic 디버깅에 유용하다)
- 네트워크 탐지 시스템으로 사용.

snort 로그패킷은 database 또는 아스키코드 포맷으로 변환할 수 있다.

-b tcpdump 패킷처럼 바이너리형태로 캡처한다. 이 옵션은 text 형태의 변환과정이 필요없으므로
빠르다. 디폴트 파일명은 snort.log.timestamp 형식이다.
다른이름으로 캡처하려면 -L 옵션이 필요하다.

-c configfile
config 파일이 있는 rule 을 사용한다.

-e Display/log the link layer packet headers.

-b Log packets in tcpdump format (much faster!)
-c Use Rules File
-d Dump the Application Layer
-D Run Snort in background (daemon) mode
-T Test and report on the current Snort configuration
-v Be verbose
-V Show version number
-X Dump the raw packet data starting at the link layer


packet sniffer 방식

[root@centos1 snort]# snort -v
Running in packet dump mode

--== Initializing Snort ==--
Initializing Output Plugins!
***
*** interface device lookup found: eth0
***
Initializing Network Interface eth0
Decoding Ethernet on interface eth0

--== Initialization Complete ==--

,,_ -*> Snort! <*-
o" )~ Version 2.8.6 (Build 38)
'''' By Martin Roesch & The Snort Team: http://www.snort.org/snort/snort-team
Copyright (C) 1998-2010 Sourcefire, Inc., et al.
Using PCRE version: 8.02 2010-03-19

Not Using PCAP_FRAMES
05/27-19:46:09.562589 ARP who-has 192.168.0.21 tell 192.168.0.1

05/27-19:46:09.565495 ARP reply 192.168.0.21 is-at 0:17:31:15:CD:E6

05/27-19:46:11.026841 192.168.0.1:3072 -> 239.255.255.250:1900
UDP TTL:1 TOS:0x0 ID:0 IpLen:20 DgmLen:293 DF
Len: 265
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
05/27-19:46:11.028662 192.168.0.1:3072 -> 239.255.255.250:1900
UDP TTL:1 TOS:0x0 ID:0 IpLen:20 DgmLen:365 DF
Len: 337
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+

05/27-19:46:11.030237 192.168.0.1:3072 -> 239.255.255.250:1900
UDP TTL:1 TOS:0x0 ID:0 IpLen:20 DgmLen:361 DF
Len: 333
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

- 패킷 logging 방식 (패킷을 파일에 저장하는 방식)

먼저 패킷을 저장할 디렉토리를 생성한다.
mkdir /var/log/snort


[root@centos1 snort]# snort -vde -l /vr/log/snort -K

-v : verbose
-d : 애플리케이션 계층의 데이터를 Hex code나 아스키 코드로 출력한다.
-e : MAC 주소 정보도 출력한다.
-l : log를 저장할 디렉토리
-K [logging-mode] : default logging mode is pcap.
유효한 옵션은 pcap, ascii, none.
pcap : pcap(tcpdump) format 으로 저장
ascii : 아스키포맷으로 저장
none : 로깅하지 않음.
디폴트 로깅디렉토리는 /var/log/snort 임.

*. 기타옵션
snot -vde port 80 ; 웹 서비스 관련 패킷만 캡처
snot -vd host 192.168.100.1 ; 특정 호스트 관련 패킷만 캡쳐
snot -vde udp ; udp 프로토콜 관련 패킷만 캡쳐


[root@centos1 snort]# snort -vd -l /var/log
-l : logging 디렉토리 지정.

[root@centos1 snort]# ls /var/log/snort.log.*
/var/log/snort.log.1274959324
[root@centos1 snort]#

root@centos1 snort]# snort -vd -l /var/log -K ascii

침입탐지모드(NIDS mode)
- rule 을 이용한 패킷캡처.

*. snort rule 설치후 test ( 에러가 나면 아래처럼 에러를 수정한다)
==========================================================
root@centos1 /usr/local/snort-2.8.6/etc]# snort -c ./snort.conf
Running in IDS mode

--== Initializing Snort ==--
Initializing Output Plugins!
Initializing Preprocessors!
Initializing Plug-ins!
Parsing Rules file "./snort.conf"
PortVar 'HTTP_PORTS' defined : [ 80 1220 2301 3128 7777 7779 8000 8008 8028 8080 8180 8888 9999 ]
PortVar 'SHELLCODE_PORTS' defined : [ 0:79 81:65535 ]
PortVar 'ORACLE_PORTS' defined : [ 1024:65535 ]
PortVar 'SSH_PORTS' defined : [ 22 ]
Detection:
Search-Method = AC-Full-Q
Split Any/Any group = enabled
Search-Method-Optimizations = enabled
Maximum pattern length = 20
ERROR: parser.c(5161) Could not stat dynamic module path "/usr/local/lib/snort_dynamicpreprocessor/": No such file or directory.
Fatal Error, Quitting..
[root@centos1 /usr/local/snort-2.8.6/etc]#
설정 파일안에 경로가 잘못되어 에러가 발생했다.
정확한 파일경로를 찾아서 설정파일에서 해당파일 경로를 수정한다.

[root@centos1 /usr/local/snort-2.8.6]# find . -name snort_dynamic*
./lib/snort_dynamicengine
./lib/snort_dynamicpreprocessor
./src/snort_dynamicsrc
[root@centos1 /usr/local/snort-2.8.6]#

[root@centos1 /usr/local/snort-2.8.6/etc]# snort -c ./snort.conf
Running in IDS mode

--== Initializing Snort ==--
Initializing Output Plugins!
Initializing Preprocessors!
Initializing Plug-ins!
Parsing Rules file "./snort.conf"
PortVar 'HTTP_PORTS' defined : [ 80 1220 2301 3128 7777 7779 8000 8008 8028 8080 8180 8888 9999 ]
PortVar 'SHELLCODE_PORTS' defined : [ 0:79 81:65535 ]
PortVar 'ORACLE_PORTS' defined : [ 1024:65535 ]
PortVar 'SSH_PORTS' defined : [ 22 ]
Detection:
Search-Method = AC-Full-Q
Split Any/Any group = enabled
Search-Method-Optimizations = enabled
Maximum pattern length = 20
ERROR: parser.c(5161) Could not stat dynamic module path "/usr/local/snort-2.8.6/lib/snort_dynamicrules": No such file or directory.
Fatal Error, Quitting..
에러가 발생했다.
[root@centos1 /usr/local/snort-2.8.6/etc]# find . -name snort_dynamicrules
[root@centos1 /usr/local/snort-2.8.6/etc]# cd ..
[root@centos1 /usr/local/snort-2.8.6]# find . -name snort_dynamicrules
[root@centos1 /usr/local/snort-2.8.6]# ; 해당 파일이 없다.
설정파일에서 주석처리해야 한다.

[root@centos1 /usr/local/snort-2.8.6/etc]# snort -c ./snort.conf
...
...
...
161 client (Footprint)
443 client (Footprint) server (Footprint)
Stream5 UDP Policy config:
Timeout: 180 seconds
ERROR: ./snort.conf(193) => Invalid keyword 'compress_depth' for 'global' configuration.
Fatal Error, Quitting..
[root@centos1 /usr/local/snort-2.8.6/etc]# ; 193라인에서 에러 발생

파일열어서 주석처리한다. compress_depth 2048
========================================================



snort 를 실행하고 패킷이 수집되는 동안 잠시기다렸다가 끝내고 확인해보면
아래와 같이 패킷이 저장된 파일을 확인할 수 있다.

[root@centos1 snort]# pwd
/var/log/snort
[root@centos1 snort]# file snort.log.1274957588
snort.log.1274957588: tcpdump capture file (little-endian) - version 2.4 (Ethernet, capture length 1514)

[root@centos1 snort]# ls -F
111.67.211.105/ 192.168.0.21/ PACKET_NONIP snort.log.1274947830 snort.log.1274949793
192.168.0.1/ 192.168.100.100/ snort.log.1274943849 snort.log.1274949769 snort.log.1274957588
[root@centos1 snort]#

* alert 파일 내용
[**] [1:1002:13] WEB-IIS cmd.exe access [**]
[Classification: Web Application Attack] [Priority: 1]
05/28-09:03:28.187553 192.168.100.100:35845 -> 192.168.100.1:80
TCP TTL:64 TOS:0x10 ID:7084 IpLen:20 DgmLen:66 DF
***AP*** Seq: 0x2AF77A2 Ack: 0x610DE601 Win: 0x5C TcpLen: 32
TCP Options (3) => NOP NOP TS: 247890360 87338255

[**] [119:19:1] (http_inspect) LONG HEADER [**]
[Priority: 3]
05/28-09:05:22.604075 192.168.0.29:1402 -> 202.131.27.89:80
TCP TTL:128 TOS:0x0 ID:38217 IpLen:20 DgmLen:1454 DF
***A**** Seq: 0xAC6BD2D Ack: 0x9CA28C12 Win: 0x4470 TcpLen: 20

snort 가 남긴 alert 파일 내용이다.

snort.conf 파일에 보면
include $RULE_PATH/web-iis.rules

그리고 rule 파일은 rule 을 설치한 디렉토리에
web-iis.rules 파일에 보면

alert tcp $EXTERNAL_NET any -> $HTTP_SERVERS $HTTP_PORTS (msg:"WEB-IIS cmd.exe access"; flow:to_server,established; uricontent:" cmd.exe"; fast_pattern; nocase; metadata:policy balanced-ips drop, policy connectivity-ips drop, policy security-ips drop, servi ce http; classtype:web-application-attack; sid:1002; rev:13;)

이 rule 설정에 의해 위에 출력된것과 같은 alert 메세지가 남는다.


*. mysql db 와 연동이 되게 하기위해서는
운영중인 snort 를 종료시키고

=> killall snort

snort.conf 파일에서 아래내용을 확인하고 수정한다.

# database
# output database: alert, , user= password= test dbname= host=
# output database: log, , user= password= test dbname= host=

아래처럼 수정한다.

# database
output database: alert, mysql, user=snort password=pass1234 dbname=snort host=localhost
output database: log, mysql, user=snort password=pass1234 dbname=snort host=localhost

그런다음

다시 snort 를 구동한다.

snort -c 경로명/snort.conf

snort 가 구동될때 에러가 발생하면 에러를 잡고 다시 구동한다.


그리고 snort 계정으로 db 접속후 아래와 같이 확인해보면
table 에 로그가 잘 저장되어 있는것을 확인할 수 있다.

mysql> select * from signature
-> ;
+--------+----------------------------------------------------------------------+--------------+--------------+---------+---------+---------+
| sig_id | sig_name | sig_class_id | sig_priority | sig_rev | sig_sid | sig_gid |
+--------+----------------------------------------------------------------------+--------------+--------------+---------+---------+---------+
| 1 | (portscan) TCP Portscan | 0 | 3 | NULL | 1 | 122 |
| 2 | (portscan) Open Port | 0 | 3 | NULL | 27 | 122 |
| 3 | Consecutive TCP small segments exceeding threshold | 0 | 3 | 1 | 12 | 129 |
| 4 | WEB-IIS cmd.exe access | 1 | 1 | 13 | 1002 | 1 |
| 5 | ICMP Destination Unreachable Fragmentation Needed and DF bit was set | 2 | 3 | 7 | 396 | 1 |
| 6 | (http_inspect) LONG HEADER | 0 | 3 | 1 | 19 | 119 |
| 7 | Bad segment, adjusted size <= 0 | 0 | 3 | 1 | 5 | 129 |
+--------+----------------------------------------------------------------------+--------------+--------------+---------+---------+---------+
7 rows in set (0.03 sec)

mysql> select * from sig_class;
+--------------+------------------------+
| sig_class_id | sig_class_name |
+--------------+------------------------+
| 1 | web-application-attack |
| 2 | misc-activity |
+--------------+------------------------+
2 rows in set (0.02 sec)

mysql>

반응형