原文:http://www.right.com.cn/forum/thread-124429-1-1.html

原理:1. snort做入侵检测是很好,但是太大太复杂,我们需要轻量化的操作。当对方进行SSH 端口的穷举攻击的时候,dropbear会在系统log中记录下特定信息如:

Login attempt for nonexistent user

Bad password attempt for

通过后台crontab当特定时间段内检测到一定频率的攻击,则将此IP所有数据包全部DROP忽略。

2. 安装:

1)复制脚本DropBrute.sh 到任意目录如 /etc/ 下,授予执行权限如0755. 在crontab(LUCI WEB -->system - scheduled task)中加入一行,每隔一段时间执行:

*/10 * * * * /etc/DropBrute.sh 2>&1 >> /tmp/DropBrute.log

关于如何配置定期任务cron: http://en.wikipedia.org/wiki/Cron

本例是10分钟执行一次扫描log

3. 脚本配置:

见脚本中说明,如看不懂英文请补习英语。

20170321更新:

a)好久以来一直以为这个代码能好好的干活,不过最近调试其它脚本时发现iptables里并没有DropBrute该有的Drop IP,这很不科学,所以仔细看了代码,发现当中有几个需要修改的地方:

1、today=`date -'%a %b %d`

2、日志中记录登陆失败的表达首字母为大写,如Login\Bad

b)还有一点要注意的:要把下面三行添加到/etc/firewall.user里面去

iptables -N DropBrute

iptables -I input_rule -i br-wan -p tcp --dport 22 -j DropBrute

iptables -I input_rule -i br-wan -p tcp --dport 22 -m state --state NEW -m limit --limit 6/min --limit-burst 6 -j ACCEPT

(下面主修改后的正确代码,修改的地方就不说明了)

#!/bin/sh

#

# DropBrute.sh @20130516

#

# minimalist OpenWRT/dropbear ssh brute force attack banning script

#

# Installation steps:

#

# 1) Optionally edit the variables in the header of this script to customise

#    for your environment

#

# 2) Insert a reference for this rule in your firewall script before you

#    accept ssh, something like:

#

#    iptables -N DropBrute

#    iptables -I input_rule -i br-wan -p tcp --dport 22 -j DropBrute

#    iptables -I input_rule -i br-wan -p tcp --dport 22 -m state --state NEW -m limit --limit 6/min --limit-burst 6 -j ACCEPT

#

# 3) Run the script periodically out of cron:

#

#    echo '*/10 * * * * /usr/sbin/DropBrute.sh 2>&1 >> /tmp/DropBrute.log' >> /etc/crontabs/root

#

# 4) If cron is not enabled, you'll also need to run the following:

#

#    /etc/init.d/cron enable && /etc/init.d/cron start

#

#

# To whitelist hosts or networks, simply add a manual entry to the lease

# file with a leasetime of -1.  This can be done with the following syntax:

#

#    echo -1 192.168.1.0/24 >> /tmp/DropBrute.leases

#

# A static, or non-expiring blacklist of a host or network can also be

# added, just use a lease time of 0.  This can be done with the following syntax:

#

#    echo 0 1.2.3.0/24 >> /tmp/DropBrute.leases

# How many bad attempts before banning. Only the log entries from the

# current day are checked.

allowedAttempts=5

# How long IPs are banned for after the current day ends.

# default is 1 days

secondsToBan=$((7*60*60*24))

# the "lease" file - defaults to /tmp which does not persist across reboots

leaseFile=/tmp/DropBrute.leases

# This is the iptables chain that drop commands will go into.

# you will need to put a reference in your firewall rules for this

iptChain=DropBrute

# the IP Tables drop rule

iptDropRule='-j DROP'

# the IP Tables whitelist rule

iptWhiteRule='-j RETURN'

# You can put default leasefile entries in the following space.

# Syntax is simply "leasetime _space_ IP_or_network".  A leasetime of -1 is a

# whitelist entry, and a leastime of 0 is a permanent blacklist entry.

[ -f $leaseFile ] || cat <<__EOF__>>$leaseFile

-1 192.168.0.0/24

-1 192.168.1.0/24

__EOF__

# End of user customizable variables (unless you know better )

ipt='/usr/sbin/iptables'

[ `date +'%s'` -lt 1320000000 ] && echo System date not set, aborting. && exit -1

$ipt -N $iptChain >&/dev/null

today=`date +'%a %b %d'`

now=`date +'%s'`

nowPlus=$((now + secondsToBan))

echo Running DropBrute on `date` \($now\)

# find new badIPs

for badIP in `logread|egrep "^$today"|fgrep dropbear|egrep 'Login attempt for nonexistent user'\|'Bad password attempt for'|sed 's/^.*from //'|sed 's/:.*$//'|sort -u` ; do

  found=`logread|egrep "^$today"|fgrep dropbear|egrep 'Login attempt for nonexistent user'\|'Bad password attempt for'|sed 's/^.*from //'|sed 's/:.*$//'|fgrep $badIP|wc -l`

  if [ $found -gt $allowedAttempts ] ; then

    if [ `egrep \ $badIP\$ $leaseFile|wc -l` -gt 0 ] ; then

       [ `egrep \ $badIP\$ $leaseFile|cut -f1 -d\ ` -gt 0 ] && sed -i 's/^.* '$badIP\$/$nowPlus\ $badIP\/ $leaseFile

    else

       echo $nowPlus $badIP >> $leaseFile

    fi

  fi

done

# now parse the leaseFile

while read lease ; do

  leaseTime=`echo $lease|cut -f1 -d\ `

  leaseIP=`echo $lease|cut -f2 -d\ `

  if [ $leaseTime -lt 0 ] ; then

    if [ `$ipt -S $leaseChain|egrep \ $leaseIP/32\ \|\ $leaseIP\ |fgrep -- "$iptWhiteRule"| wc -l` -lt 1 ] ; then

      echo Adding new whitelist rule for $leaseIP

      $ipt -I $iptChain -s $leaseIP $iptWhiteRule

    fi

  elif [ $leaseTime -ge 1 -a $now -gt $leaseTime ] ; then

    echo Expiring lease for $leaseIP

    $ipt -D $iptChain -s $leaseIP $iptDropRule

    sed -i /$leaseIP/d $leaseFile

  elif [ $leaseTime -ge 0 -a `$ipt -S $leaseChain|egrep \ $leaseIP/32\ \|\ $leaseIP\ |wc -l` -lt 1 ] ; then

    echo Adding new rule for $leaseIP

    $ipt -A $iptChain -s $leaseIP $iptDropRule

  fi

done < $leaseFile

openwrt中防暴力破解shell的脚本的更多相关文章

  1. centos7防暴力破解五种方法

    什么是暴力破解,简单来说就是对一个服务器进行无数次尝试登陆,并用不同的密码进行登陆直到可以登陆成功.暴力破解的基本步骤可以分为以下几步: 1. 找到对应的linux服务器    Ip地址 2.扫描端口 ...

  2. fail2ban的使用以及防暴力破解与邮件预警

    fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是防火墙),而且可以发送e-mail通知系统管理员! fail2ban运行机制:简单来说其功能就 ...

  3. openssh安装、设置指定端口号、免密码登录、变量传递、防暴力破解

    首先确保机器挂在好光盘镜像,然后查看软件包信息 [root@xuegod63 ~]# df -hFilesystem      Size  Used Avail Use% Mounted on/dev ...

  4. WordPress防暴力破解:安全插件和用.htpasswd保护WordPress控制面板

    正在用Wordpress的博主们一定知道最近全球兴起的一波黑客锁定Wordpress暴力破解控制面板密码的风波了,据CloudFlare执行长Matthew Prince所说,所谓的暴力密码攻击是输入 ...

  5. fail2ban 防暴力破解总结

    公司服务器安全问题一直是个令人头疼的问题,许多运维的小伙伴一直在用脚本来监控服务器登录状态,然而脚本编写比较麻烦,今天就给大家推荐一款小而精致的防暴力破解工具 fail2ban ,他可以监控系统日志, ...

  6. Centos6.4 安装fail2ban防暴力破解

    Centos6.4 安装fail2ban防暴力破解 一. 安装 curl -O https://codeload.github.com/fail2ban/fail2ban/tar.gz/0.9.0 m ...

  7. Odoo14 防暴力破解登录密码

    1 # Odoo14 防暴力破解登录密码 2 # 主要工具:redis 3 # 实现思路:限制每个用户24小时内登录失败次数.连续超过5次失败后,需要等待一定时间后才能再次尝试登录 4 # 配置:在你 ...

  8. [典型漏洞分享]YS的防暴力破解设计存在缺陷

    YS使用的防暴力破解机制存在缺陷,该缺陷可被用于暴力破解其它用户密码[高] 问题描述: YS在用户登录页面设置了验证码机制,当用户输入密码错误次数达到3次时,再次登录需要验证码以防止攻击者进行暴力破解 ...

  9. 【Linux系统】防暴力破解

    在日志文件/var/log/secure 中可以看到涉及到安全方面的日志,可以查看是否有人尝试暴力破解及是否成功,对于肉鸡行为有一定帮助 思路基本上都是加强密码的复杂度,增加iptables配置黑名单 ...

随机推荐

  1. ef-codefirst方式配置实体类,生成数据库

    做项目的时候,如果我们如果用orm方式来做数据库持久化操作的话.微软官方首先会向我们推荐ef,而我们用ado.net的话,似乎也需要建立实体类来接传值,那么我们用codefirst就有一举两得的效果了 ...

  2. python中转义符&str格式化

    转义字符: 1.将有意义的字符变的无意义 2.将无意义的字符变的有意义 语法: \ + 某个字符 \n, \r\n :    代表换行 \t :      代表一个缩进, (水平制表符) \r :   ...

  3. win10配置labelImg

    [引言]在目标检测中,需要用图像标注工具标注图像,如Labelme . labelImg等,本文使用的是LabelImg ,LabelImg在Ubuntu下很好部署, 在win10中有些地方要注意下, ...

  4. HBase 集群部署

     前提条件:hadoop及zookeeper机群已经搭建好. 配置hbase集群步骤: 1.配置hbase集群,要修改3个文件 注意:要把hadoop的hdfs-site.xml和core-site. ...

  5. 把一串数字表示成千位分隔形式——JS正则表达式的应用

    梳理思路 要先明白的是,我们将要转换成的数字格式是这样:从个位往左数起,每三位前插入一个千位分隔符,,即可以想象成我们要把每三位数字前面的那个空""匹配出来,并替换成千位分隔符,. ...

  6. Intorduction To Computer Vision

    本文将主要介绍图像分类问题,即给定一张图片,我们来给这张图片打一个标签,标签来自于预先设定的集合,比如{people,cat,dog...}等,这是CV的核心问题,图像分类在实际应用中也有许多变形,而 ...

  7. ~Vue实现简单答题功能,主要包含单选框和复选框

    内容 实现简单答题效果 环境 Vue,webpack(自行安装) 实现方式 页面将答题列表传递给调用组件,组件将结果返回给调用页面(其它模式也ok,这只是例子) ------------------- ...

  8. C#提取双引号中的字符串

    public static void Main(string[] args) { string strtmp = "123\"456\"qqq\"789\&qu ...

  9. CF 966E May Holidays

    /* 考虑对于询问分块, 每根号n个询问做一次 考虑一次询问, 我们建立出虚树来每条链上的更改一定是一样的, 然后会有根号条链 对于每条链上的点按照w基数排序并且合并相同, 然后每次更改 就是一个指针 ...

  10. 2-Zookeeper、HA安装

    1.Zookeeper安装 1.解压 zookeeper 到安装目录中/opt/app/zookeeper 中. 2.在安装目录下创建data和logs两个目录用于存储数据和日志: cd /opt/a ...