Centos Denyhosts 一鍵安裝配置指令碼

來源:互聯網
上載者:User

標籤:暴力破解   ssh   denyhosts   hosts.deny   

Centos Denyhosts 一鍵安裝配置指令碼

 

一鍵安裝denyhosts指令碼並配置為常用配置。放置Linux伺服器被暴力破解

由於不能上傳tar檔案。所以改為zip壓縮。

將附件的zip壓縮包解壓後將整個檔案夾上傳到伺服器。

然後進入denyhost檔案夾

賦予指令碼執行許可權。

chmod 700 denyhosts.sh./ denyhosts.sh

指令碼內容是:

denyhosts.sh

#!/bin/bash # Copyright (c) 2015.6  Fenei# BY:封飛# 2015年6月29日23點# denyhosts auto install and configure shell# VER. 1.1.0 - http://www.fenei.net# 自動安裝配置denyhosts指令碼# 2015年6月29日23點# # 本程式是專為Redhat、Centos等系統製作的自動安裝配置denyhosts的指令碼。# 本程式為西普網路專用,預設添加西普網路IP到"/usr/share/denyhosts/allowed-hosts"檔案。# 其他人員如果使用必須將自己的遠程辦公IP手動添加至"/usr/share/denyhosts/allowed-hosts"檔案。# 本指令碼為開源,初次製作,如有不足之處,可以聯絡修改。# # Copyright (c) 2015.6  Fenei# BY:封飛# 2015年6月29日23點## [email protected] Sinpul Network# VER. 1.1.0 - http://www.fenei.net## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.#=====================================================================stty erase ^h stty erase ^H# 將shell運行過程中讀取退格鍵 替換為退格命令,否則會輸出 ^H! chmod 700 denyhosts_removeip.shcp -a * /tmpcd /tmptar zxf DenyHosts-2.6.tar.gzcd DenyHosts-2.6python setup.py installcd /usr/share/denyhosts/cp daemon-control-dist daemon-controlchown root daemon-controlchmod 700 daemon-control# 複製我已經配置好的設定檔,預設為登入5次拒絕登入10分鐘。cp /tmp/denyhosts.cfg .cp -a /tmp/denyhosts_removeip.sh .# 配置denyhosts的開機啟動echo "/usr/share/denyhosts/daemon-control start" >> /etc/rc.localcd /etc/init.dln -s /usr/share/denyhosts/daemon-control denyhostschkconfig --add denyhostschkconfig --level 345 denyhosts on# 添加遠程登入IP至"/etc/hosts.allow"檔案。echo "127.0.0.1" >> /usr/share/denyhosts/allowed-hosts# 這裡輸入您的管理IP地址,將您的地址添加到白名單。 echo "你的IP" >> /usr/share/denyhosts/allowed-hosts# 重啟denyhosts服務service denyhosts restart

 如果IP被禁止後想解鎖IP請使用一下指令碼

denyhosts_removeip.sh

#!/bin/bashHOST=$1if [ -z "${HOST}" ]; then    echo "Usage:$0 IP"    exit 1fi/etc/init.d/denyhosts stopecho ‘/etc/hosts.deny/usr/share/denyhosts/data/hosts/usr/share/denyhosts/data/hosts-restricted/usr/share/denyhosts/data/hosts-root/usr/share/denyhosts/data/hosts-valid/usr/share/denyhosts/data/users-hosts‘ | grep -v "^$" | xargs sed -i "/${HOST}/d"/etc/init.d/denyhosts start

我指令碼內配置好的denyhosts.cfg檔案

denyhosts.cfg

       ############ THESE SETTINGS ARE REQUIRED ############SECURE_LOG = /var/log/secure#ssh記錄檔HOSTS_DENY = /etc/hosts.deny#將阻止IP寫入到hosts.denyPURGE_DENY = 10m#過多久後清除已經禁止的,其中w代表周,d代表天,h代表小時,s代表秒,m代表分鐘BLOCK_SERVICE = sshd#阻止服務名DENY_THRESHOLD_INVALID = 5#允許無效使用者(在/etc/passwd未列出)登入失敗次數,允許無效使用者登入失敗的次數.DENY_THRESHOLD_VALID = 5#允許普通使用者登入失敗的次數DENY_THRESHOLD_ROOT = 5#允許root登入失敗的次數DENY_THRESHOLD_RESTRICTED = 1#設定 deny host 寫入到該資料夾WORK_DIR = /usr/share/denyhosts/data#將deny的host或ip紀錄到Work_dir中SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS = YESHOSTNAME_LOOKUP=YES#是否做網域名稱反解LOCK_FILE = /var/lock/subsys/denyhosts#將DenyHOts啟動的pid紀錄到LOCK_FILE中,已確保服務正確啟動,防止同時啟動多個服務。ADMIN_EMAIL = [email protected]   ############ THESE SETTINGS ARE OPTIONAL #############設定管理員郵件地址SMTP_HOST = localhostSMTP_PORT = 25SMTP_FROM = DenyHostsSMTP_SUBJECT = DenyHosts ReportALLOWED_HOSTS_HOSTNAME_LOOKUP=YES#啟用allow-hosts 白名單檔案功能AGE_RESET_VALID=1d#有效使用者登入失敗計數歸零的時間AGE_RESET_ROOT=1d#root使用者登入失敗計數歸零的時間AGE_RESET_RESTRICTED=5d#使用者的失敗登入計數重設為0的時間(/usr/share/denyhosts/data/restricted-usernames)AGE_RESET_INVALID=10d#無效使用者登入失敗計數歸零的時間DAEMON_LOG = /var/log/denyhosts#自己的記錄檔DAEMON_SLEEP = 30s   ######### THESE SETTINGS ARE SPECIFIC TO DAEMON MODE  ##########DAEMON_PURGE = 5m#該項與PURGE_DENY 設定成一樣,也是清除hosts.deniedssh 使用者的時間

 

 

運行成功以後,提示將目前存在的暴力破解IP自動添加到hosts.deny檔案

650) this.width=650;" title="運行成功" style="float:none;" alt="wKiom1WSRw-DlXNDAAEnQkaGkfg568.jpg" src="http://s3.51cto.com/wyfs02/M01/6F/20/wKiom1WSRw-DlXNDAAEnQkaGkfg568.jpg" />

hosts.deny檔案內的內容,顯示封鎖IP列表(我這裡配置的是10分鐘後清空此檔案)

650) this.width=650;" title="hosts.deny" style="float:none;" alt="wKioL1WSSNHyKLgzAAJ9_2UXI_8996.jpg" src="http://s3.51cto.com/wyfs02/M00/6F/1D/wKioL1WSSNHyKLgzAAJ9_2UXI_8996.jpg" />

 

/var/log/denyhosts 檔案的記錄

 

650) this.width=650;" title="3.png" style="float:none;" alt="wKioL1WSTXSBJSIBAAdAkAFY8pE323.jpg" src="http://s3.51cto.com/wyfs02/M00/6F/1D/wKioL1WSTXSBJSIBAAdAkAFY8pE323.jpg" />

/var/log/secure 檔案的登陸記錄

650) this.width=650;" title="5.png" style="float:none;" alt="wKioL1WSTXSzn9ukAAvJeVIb978207.jpg" src="http://s3.51cto.com/wyfs02/M01/6F/1D/wKioL1WSTXSzn9ukAAvJeVIb978207.jpg" />

 

使用

cat /var/log/secure | awk ‘/Failed/{print $(NF-3)}‘| sort| uniq -c| awk ‘{print $2"="$1;}‘

 查看secure檔案內統計的失敗登陸IP地址

650) this.width=650;" title="6.png" src="http://s3.51cto.com/wyfs02/M00/6F/1F/wKioL1WSXGXzPF9IAADV4T-pcFM195.jpg" alt="wKioL1WSXGXzPF9IAADV4T-pcFM195.jpg" />

本文出自 “www.fenei.com” 部落格,請務必保留此出處http://453861.blog.51cto.com/443861/1669436

Centos Denyhosts 一鍵安裝配置指令碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.