DenyHosts SSH 폭파 방지

Deny Hosts 공식 홈 페이지 는:http://denyhosts.sourceforge.net
1. 설치 조건 검사
1. 시스템 에 설 치 된 sshd 가 tcp 를 지원 하 는 지 먼저 판단 합 니 다wrappers (기본 값 지원)
# ldd /usr/sbin/sshd
libwrap.so.0 => /usr/lib/libwrap.so.0 (0x0046e000)

2. 기본 설 치 된 Python 버 전 판단
[root@localhost 03]# python -V
Python 2.4.3
Centos5.5 python 2.4.3

2. Python 2.3 이상 버 전이 설 치 된 경우
1. DenyHosts 설치
# cd /usr/local/src
# wget http://jaist.dl.sourceforge.net/sourceforge/denyhosts/DenyHosts-2.6.tar.gz
# tar zxf DenyHosts-2.6.tar.gz
# cd DenyHosts-2.6
# python setup.py install

프로그램 스 크 립 트 가 / usr / share / denyhosts 에 자동 으로 설 치 됩 니 다.
라 이브 러 리 파일 은 / usr / lib / python 2.3 / site - packages / DenyHosts 에 자동 으로 설 치 됩 니 다.
denyhosts. py 자동 설치 / usr / bin 
2. 시작 스 크 립 트 설정
# cd /usr/share/denyhosts/
# cp daemon-control-dist daemon-control
# chown root daemon-control
# chmod 700 daemon-control
# grep -v "^#" denyhosts.cfg-dist > denyhosts.cfg
# vi denyhosts.cfg

자기 의 필요 에 따라 상응하는 수정 을 진행 하 다
----------------denyhosts.cfg------------------------
SECURE_LOG = /var/log/secure
#RedHat/Fedora Core
# linux denyhosts.cfg-dist 。
PURGE_DENY = 30m
#
DENY_THRESHOLD_INVALID = 1
# (/etc/passwd )
DENY_THRESHOLD_VALID = 5
# ( )
DENY_THRESHOLD_ROOT = 3
# root
HOSTNAME_LOOKUP=NO
#
----------------denyhosts.cfg------------------------

DenyHosts 가 시스템 재 부팅 에 따라 자동 으로 시작 하려 면 다음 설정 을 해 야 합 니 다.
# vi /etc/rc.local 

아래 명령 에 가입 하 십시오.
/usr/share/denyhosts/daemon-control start 

3. 시동
# /usr/share/denyhosts/daemon-control start

Deny Hosts 를 다시 시작 할 때마다 자동 으로 시작 하려 면 다음 설정 을 해 야 합 니 다.
# cd /etc/init.d
# ln -s /usr/share/denyhosts/daemon-control denyhosts
# chkconfig --add denyhosts
# chkconfig --level 345 denyhosts on

그리고 시작 할 수 있 습 니 다:
service denyhosts start
DenyHosts 프로필:
vi /etc/denyhosts.cfg
SECURE_LOG = /var/log/secure #ssh , 。
HOSTS_DENY = /etc/hosts.deny #
PURGE_DENY = 5m#
BLOCK_SERVICE   = sshd#
DENY_THRESHOLD_INVALID = 1#
DENY_THRESHOLD_VALID = 10#
DENY_THRESHOLD_ROOT = 5# root
HOSTNAME_LOOKUP=NO#
DAEMON_LOG = /var/log/denyhosts#
ADMIN_EMAIL = [email protected]# ,

이 installdenyhosts. sh 는 설 치 된 작은 스 크 립 트 를 자동 으로 다운로드 합 니 다. 물론 설치 후 설정 파일 을 수 동 으로 조정 해 야 합 니 다.비 생산 서버 에 적합;생산 서버 라면 제 뒤의 ssh 를 사용 하 시 는 것 을 권장 합 니 다.deny. sh 스 크 립 트.
본 installdenyhosts. sh 스 크 립 트 는 다음 과 같 습 니 다.
#!/bin/bash
cd /usr/local/src
wget http://jaist.dl.sourceforge.net/sourceforge/denyhosts/DenyHosts-2.6.tar.gz
tar zxf DenyHosts-2.6.tar.gz
cd DenyHosts-2.6
python setup.py install
cd /usr/share/denyhosts/
cp daemon-control-dist daemon-control
chown root daemon-control
chmod 700 daemon-control
grep -v "^#" denyhosts.cfg-dist > denyhosts.cfg
echo "/usr/share/denyhosts/daemon-control start" >>/etc/rc.local
cd /etc/init.d
ln -s /usr/share/denyhosts/daemon-control denyhosts
chkconfig --add denyhosts
chkconfig --level 345 denyhosts on
service denyhosts start

다음은 hostsdeny 의 예제 입 니 다.

Connection to 192.168.0.154 closed.
[root@autolemp ~]# ssh 192.168.0.154
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied (publickey,gssapi-with-mic,password)


사실 이 원리 도 간단 하 다. 내 가 조금 설명 할 게.루트 로그 인 실패 횟수 가 10 회 를 넘 으 면 / etc / hosts. deny 파일 에 기록 합 니 다.사실 우 리 는 셸 로 이런 일 을 할 수 있 습 니 다. 스 크 립 트 제어 학생 들 은 다음 과 같은 스 크 립 트 를 주목 할 수 있 습 니 다. 저 는 온라인 서버 에서 테스트 에 성 공 했 습 니 다.저 는 주로 안전 을 위해 온라인 서버 를 크게 바 꾸 고 싶 지 않 기 때문에 셸 방식 으로 이런 수요 가 있 는 친구 들 도 주목 할 수 있 습 니 다.

vim /root/ssh_deny.sh
#! /bin/bash
cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}'  >/root/black.txt
DEFINE="10"

for i in `cat  /root/black.txt`
do
        IP=`echo $i |awk -F= '{print $1}'`
        NUM=`echo $i|awk -F= '{print $2}'`
        if [ $NUM -gt $DEFINE ];
        then
         grep $IP /etc/hosts.deny > /dev/null
          if [ $? -gt 0 ];
          then
          echo "sshd:$IP" >> /etc/hosts.deny
          fi
        fi
done

vim /etc/crontab
* */1 * * * root sh /root/ssh_deny.sh


이 스 크 립 트 를 쓸 때 제 가 시간 을 두 고 수집 하고 싶 었 던 문제, 즉 시간 을 두 고 한 번 만 / var / log / secure 의 로그 정 보 를 수집 하려 고 했 는데 나중에 이 방법 이 복잡 하 다 고 생각 했 습 니 다.나 는 위의 대본 을 조금 설명 했다.이것 은 사실 crontab 에 놓 여 있 습 니 다. 한 시간 마다 / var / log / secure 의 로그 정 보 를 읽 고 루트 를 연결 하 는 IP 의 밸브 값 을 10 으로 정의 합 니 다. 이 IP 가 / etc / hosts. deny 에 있 으 면 아무것도 하지 않 습 니 다.($? 값 이 0 이 아 닌 상태 에서) 없 으 면 / etc / hosts. deny 에 추가 합 니 다. 이 스 크 립 트 는 온라인 환경 에 두 어도 한동안 효과 가 있 습 니 다.
from http://netsecurity.51cto.com

좋은 웹페이지 즐겨찾기