2007. 2. 23. 22:23

portsentry를 사용해서 포트스캔 탐지 및 tcp-wrapper 에 자동 기록

portsentry를 사용해서 포트스캔 탐지 및 tcp-wrapper 에 자동 기록.

portsentry는 누군가 내 시스템의 포트를 스캔 하는지 검사하여,

tcp_wrapper에서 자동으로 ip를 막아주는 프로그램이다.

일반적으로 포트를 스캔 하는 경우는 해킹에 대비하기 위해서

내 시스템의 열려있는 포트를 스캔 하거나,

다른 서버를 해킹할 목적으로 열려 있는 포트를 스캔 하는 경우가

대부분 이기 때문에 누군가 내 시스템의 포트를 스캔했다면,

해킹을 위한 시도라고 생각하면 된다.

portsentry를 설치하면, 누군가 내 시스템의 포트를 스캔 했을때

아래와 같은 형식으로 /etc/hosts.deny 파일에 기록이 되어,

해당 아이피의 접근을 차단하게된다.

 ALL: 123.23.123.3

하지만 자신의 /etc/hosts.deny 에 ALL:ALL 로 설정해 놓았다면

단순히 로그분석용으로만 사용하는것과 같을 것이다.

- 설치 과정

tar xvzf portsentry*

우선 MakeFile 을 열어 중요부분을 살펴보자.

INSTALLDIR = /usr/local/psionic
CHILDDIR=/portsentry

설치되는 디렉토리를 정의하고 있다.

linux:
SYSTYPE=linux
@echo "Making $(SYSTYPE)"
$(CC) $(CFLAGS) -DLINUX -DSUPPORT_STEALTH -o ./portsentry ./portsentry.c
./portsentry_io.c ./portsentry_util.c $(LIBS)

debian-linux:
SYSTYPE=debian-linux
@echo "Making $(SYSTYPE)"
$(CC) $(CFLAGS) -DLINUX -DDEBIAN -DSUPPORT_STEALTH -o ./portsentry ./portsentry.c
./portsentry_io.c ./portsentry_util.c $(LIBS)


bsd:
SYSTYPE=bsd
@echo "Making $(SYSTYPE)"
$(CC) $(CFLAGS) -DBSD44 -o ./portsentry ./portsentry.c
./portsentry_io.c ./portsentry_util.c


openbsd:
SYSTYPE=openbsd
@echo "Making $(SYSTYPE)"
$(CC) $(CFLAGS) -DBSD44 -o ./portsentry ./portsentry.c
./portsentry_io.c ./portsentry_util.c


freebsd:
SYSTYPE=freebsd
@echo "Making $(SYSTYPE)"
$(CC) $(CFLAGS) -DBSD44 -o ./portsentry ./portsentry.c
./portsentry_io.c ./portsentry_util.c

 

이하 생략...

make , 즉 make 명령어 뒤에 자신의 시스템 타입을 적어 주어야 한다.

예)
make linux
make install

위와 같은 순서로 컴파일, 인스톨 하면 된다.


cd /usr/local/psionic/portsentry

세개의 파일이 보일것이다. portsentry.conf 파일을 보자.

# Use these if you just want to be aware: 검사하고 싶은 대상 포트를 적어줌
(각종 트로이 포트들을 적어주면 검사를 잘 해줄 것이다.)


TCP_PORTS="1,11,15,79,111,119,143,540,635,1080,1524,2000,5742,6667,12345,12346,20034,27665,31337,32771,32772,32773,32774,40421,49724,54320"
UDP_PORTS="1,7,9,69,161,162,513,635,640,641,700,37444,34555,31335,32770,32771,32772,32773,32774,31337,54321"

# Default TCP ident and NetBIOS service: 검사에서 제외될 tcp/udp 포트들이다.
ADVANCED_EXCLUDE_TCP="113,139"
# Default UDP route (RIP), NetBIOS, bootp broadcasts.
ADVANCED_EXCLUDE_UDP="520,138,137,67"

 

아래와 같은 방법으로 portsentry 데몬을 띄운후에...

# /usr/local/psionic/portsentry/portsentry -tcp
# /usr/local/psionic/portsentry/portsentry -stcp
# /usr/local/psionic/portsentry/portsentry -udp
# /usr/local/psionic/portsentry/portsentry -sudp

# ps -ef | grep portsentry 로 데몬이 떠 있는지확인 하고, 로그파일을 확인 하면, 포트스캔의 흔적이 로그에 남게된다.

# tail -100 /var/log/messages

Mar 1 23:38:59 myserver portsentry[28771]: attackalert: TCP SYN/Normal scan from host: 21x.11x.11x.13x/21x.11x.11x.13x to TCP port: 12345

위 로그는 누군가가 내 시스템의 12345 번 포트를 검사하고 있다는 로그이다.

그리고 /etc/hosts.deny 파일을 확인 하면.. 아래와 같이 추가되어 있을 것이다.

ALL: 21x.11x.11x.13x

portsentry는 내 시스템의 포트를 스캔한 아이피를 차단만 하는 것이기 때문에, 스캔후에 다른 아이피로 침입을 시도한다면, 무용지물이 될것이다.
nmap 등의 프로그램을 이용해서 내 시스템의 포트를 주기적으로 스캔 해보고, 불필요한 포트는 미리미리 막아 놓는 습관이 더 중요 할 것이다.