강좌
클라우드/리눅스에 관한 강좌입니다.
리눅스 분류

Brute Forcing 공격에 대비하기

작성자 정보

  • 웹관리자 작성
  • 작성일

컨텐츠 정보

본문

Brute Forcing 공격에 대비하기

0.개요
우리가 서비의 로그를 확인할 경우
pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=xxx.xxx.xxx.xxx
pam_succeed_if(sshd:auth): error retrieving information about user developer
Invalid user developer from xxx.xxx.xxx.xxx
input_userauth_request: invalid user developer
pam_unix(sshd:auth): check pass; user unknown

이러한 형태의 로그를 자주 접했을 것이다 그리고
이러한 로그는 한번에 많게는 수십개씩 바로 붙어 확인이 될것이다
이러한 형태의 로그가 바로 패스워드 및 아이피를 무작위대입하여
공격을 하는 형태이다.

이러한 공격의 위험성은 패스워드 정책과 규칙을 재대로 관리 하는 곳이라면
패스워드가 풀릴 큰위험성은 없지만. 예를 들어 1111, password, 회사명 등
누구나 유추가능하고 쉬운 패스워드를 쓸경우 쉽게 풀려버리기도 한다.

그리고 순간적으로 너무 많은 접속을 하게되면 소켓 자원의 부족으로
정상적인 접근이 어렵게 되기도 한다

이러한 위험성에서 벗어날 수 있는 방법중 한기지를 소개하고자 한다


1. 소개 어플리케이션

소개할 어플리케이션은  fail2ban 이라는 프로그램이며
이는  모니터링할 어플리케이션의 로그파일과 iptables를 이용하여
접속 시도 확인과 차단을 한다
차단기준은 접속 실패 횟수와 횟수에 도달한 시간이 차단의 기준이다
예를 들어 5분안에 5회 이상 접속 실패일  경우 접속을 차단한다

2. 사이트 및 다운로드
fail2ban의 공식 사이트  http://www.fail2ban.org

다운로드 페이지에서
http://www.fail2ban.org/wiki/index.php/Downloads

사용하고 있는 종류위 배포판의 설치 파일을 다운받는다
참고로 필자는 CentOS6을 사요하고 있다
Red Hat/CentOS     RPMs are available through EPEL. 항목의
링크를 따라가면 epel 저장소를 추가할 수있는 설치파일을 다운 받아 설치한다

cd /usr/local/src/
wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
rpm -ivh epel-release-latest-6.noarch.rpm

여기까지 진행하면 fail2ban의 설치 준비가 완료 된 것이다

3. 설치하기

yum으로 설치하면된다
yum install fail2ban


4. 설정하기

/etc/fail2ban/jail.conf를 열어 다음 부분을 확인한다
필자는 sshd  모듈만 사용하도록 하였지만 직접 열어보고
필요한 부분은 enable 시켜 사용하면 된다

[DEFAULT]

# 차단하지 않을 화이트 아이피를 등록하는 곳이다
ignoreip = 127.0.0.1/8 xxx.xxx.xxx.xxx/32

# 차단되었을 경우 차단하고 있을 시간을을 설정한다
bantime  = 600

# 로그를 탐색할 시간을 설정한다
findtime  = 600

이상 실패시 차단을 할 횟수를 설정한다
maxretry = 5


아래와 같은 많은 차단 모듈 설정이 있지만 필자는 ssh 접속에 대해서만
fail2ban을 적용할 것이다  적용하기 위해 enabled = true를 추가 한다
[sshd]
enabled = true
port    = ssh
logpath = %(sshd_log)s

정장하고 fail2ban을 재시작 한다
chkconfig fail2ban on
service fail2ban restart

확인방법

iptables -nL -v
...
Chain fail2ban-SSH (1 references)
 pkts bytes target     prot opt in     out     source               destination         
 3570  402K RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0     

다음과 같은 항목이 추가 되었는지 확인하다

그리고 로그에서
 fail2ban.server[6751]: INFO Stopping all jails
 fail2ban.jail[6751]: INFO Jail 'sshd' stopped
 fail2ban.jail[6751]: INFO Jail 'ssh-iptables' stopped
 fail2ban.server[6751]: INFO Exiting Fail2ban
 fail2ban.server[6953]: INFO Changed logging target to SYSLOG (/dev/log) for Fail2ban v0.9.3
 fail2ban.database[6953]: INFO Connected to fail2ban persistent database '/var/lib/fail2ban/fail2ban.sqlite3'
 fail2ban.jail[6953]: INFO Creating new jail 'sshd'
 fail2ban.jail[6953]: INFO Jail 'sshd' uses pyinotify
 fail2ban.filter[6953]: INFO Set jail log file encoding to UTF-8
 fail2ban.jail[6953]: INFO Initiated 'pyinotify' backend
 fail2ban.filter[6953]: INFO Added logfile = /var/log/secure
 fail2ban.filter[6953]: INFO Set maxRetry = 5
 fail2ban.filter[6953]: INFO Set jail log file encoding to UTF-8
 fail2ban.actions[6953]: INFO Set banTime = 600
 fail2ban.filter[6953]: INFO Set findtime = 600
 fail2ban.filter[6953]: INFO Set maxlines = 10
 fail2ban.server[6953]: INFO Jail sshd is not a JournalFilter instance
 fail2ban.jail[6953]: INFO Jail 'sshd' started

이러한 로그를 확인 하면 정상적으로 설치 및 설정이 된것이다


차단 및 해제를 는 로그에서 ban은 차단  unban은 해제를 나타낸다
 fail2ban.filter[8536]: INFO [sshd] Found 158.69.207.180
 fail2ban.filter[8536]: INFO [sshd] Found 158.69.207.180
 fail2ban.filter[8536]: INFO [sshd] Found 158.69.207.180
 fail2ban.filter[8536]: INFO [sshd] Found 158.69.207.180
 fail2ban.filter[8536]: INFO [sshd] Found 158.69.207.180
 fail2ban.actions[8536]: NOTICE [sshd] Ban 158.69.207.180
 fail2ban.filter[8536]: INFO [sshd] Found 108.0.11.43
 fail2ban.actions[8536]: NOTICE [sshd] Unban 158.69.207.180


5. 트러블슈팅
fail2ban.filter[32750]: WARNING Determined IP using DNS Lookup: 185a25b149c226.greendata.pl = ['185.25.149.226']
위와 같은 로그가 쉴세없이 남는다면

/etc/fail2ban/jail.conf에서  usedns=no로 설정을 하면 해결된다


아래와 같은 로그가 남고 아이피테이블에 체인생성이 되지않으면
fail2ban.action[32750]: ERROR iptables -w -N f2b-SSH#012iptables -w -A f2b-SSH -j RETURN#012iptables -w -I INPUT -p tcp --dport ssh -j f2b-SSH -- stdout: ''
fail2ban.action[32750]: ERROR iptables -w -N f2b-SSH#012iptables -w -A f2b-SSH -j RETURN#012iptables -w -I INPUT -p tcp --dport ssh -j f2b-SSH -- stderr: "iptables v1.4.7: option `-w' requires an argument\nTry `iptables -h' or 'iptables --help' for more information.\niptables v1.4.7: option `-w' requires an argument\nTry `iptables -h' or 'iptables --help' for more information.\niptables v1.4.7: option `-w' requires an argument\nTry `iptables -h' or 'iptables --help' for more information.\n" fail2ban.action[32750]: ERROR iptables -w -N f2b-SSH#012iptables -w -A f2b-SSH -j RETURN#012iptables -w -I INPUT -p tcp --dport ssh -j f2b-SSH -- returned 2
fail2ban.actions[32750]: ERROR Failed to start jail 'ssh-iptables' action 'iptables': Error starting action

 /etc/fail2ban/action.d/iptables-common.conf 파일에서 lockingopt = -w 설정을 lockingopt = 바꿔주면 된다 

관련자료

댓글 0
등록된 댓글이 없습니다.

공지사항


뉴스광장


  • 현재 회원수 :  60,034 명
  • 현재 강좌수 :  35,783 개
  • 현재 접속자 :  245 명