一聚教程网:一个值得你收藏的教程网站

热门教程

Nginx 拒绝指定IP访问网站的配置

时间:2022-06-30 18:24:24 编辑:袖梨 来源:一聚教程网

Nginx 拒绝指定IP访问的方法其实非常的简单,我们来看简单的配置

下面的这个例子将拒绝掉所有的连接:
location / {
  #这里将永远输出403错误。
  deny all;
  #这些指令不会被启用,因为到达的连接在第一条已经被拒绝
  deny    192.168.1.1;
  allow   192.168.1.0/24;
  allow   10.1.1.0/1
}

当然我们可以有一部份合法的IP可以访问

控制规则按照声明的顺序进行检查,首条匹配IP的访问规则将被启用。

location / {
  deny    192.168.1.1;
  allow   192.168.1.0/24;
  allow   10.1.1.0/16;
  deny    all;
}
上面的例子中仅允许192.168.1.0/24和10.1.1.0/16网络段访问这个location字段,但192.168.1.1是个例外。

例子

下面就是我的思路和操作步骤:
通过 Nginx 的错误日志(为什么不使用访问日志)来获取攻击者的IP。之前没有对 Nginx 的错误日志进行定时切割,为了方便统计攻击者的IP所以,编写脚本并加入定时任务,使错误日志每小时切割一次,并且每小时对黑名单文件进行清空。
错误日志切割、清空黑名单脚本:

[root@z-dig scripts]# cat rotate-nginx-error-logs.sh
#!/bin/bash
# Rotate nginx error logs and clean block ip 's configure file
# Nginx pid file : /application/nginx/logs/nginx.pid
# Nginx error logs directory : /data/logs/nginx
# Block Ip 's configure file : /application/nginx/conf/website/blockip.conf
# Default log name : error.log
# Author : Mr.Zhou
# E-mail : zhou@111com.net

NGX_PID=/application/nginx/logs/nginx.pid
NGINX_CMD=/application/nginx/sbin/nginx
LOGS_DIR=/data/logs/nginx
LOG_NAME=error.log
BLOCK_IP_FILE=/application/nginx/conf/website/blockip.conf

cd $LOGS_DIR &&
/usr/bin/rename $LOG_NAME $(/bin/date +%F-%H -d "last hour").$LOG_NAME $LOG_NAME &&
/bin/kill -USR1 $(cat $NGX_PID)
>$BLOCK_IP_FILE &&
$($NGINX_CMD -s reload)
[root@z-dig scripts]#


获取攻击者IP脚本:
该脚本从 Nginx 的错误日志中统计出超过20次试图猜测路径或上传文件的IP,并将这些IP加入到 Nginx 的配置文件。若有新增加的IP则 reload Nginx 使配置文件生效,若没有新增IP则不进行reload。

[root@z-dig scripts]# cat block-ip.sh
#!/bin/bash
# Author  : Mr.Zhou
# Email   : zhou@111com.net
# Website : http://www.111com.net
# block ip

ERR_LOG=/data/logs/nginx/error.log
BLOCK_IP_FILE=/application/nginx/conf/website/blockip.conf
BLOCKED_IP=/dev/shm/blocked-ip.txt
BLOCK_IP=/dev/shm/block-ip.txt
NGINX_CMD=/application/nginx/sbin/nginx

/bin/cp $BLOCK_IP_FILE $BLOCKED_IP &&
/bin/sed -nr 's#.*[^0-9](([0-9]+.){3}[0-9]+).*#1#p' $ERR_LOG |/bin/awk '{IP[$1]++}END{for (i in IP) print IP[i],i}'|/bin/awk '{if($1>20)print "deny "$2";"}' >$BLOCK_IP &&
/bin/grep -v -f $BLOCK_IP_FILE $BLOCK_IP >>$BLOCK_IP_FILE &&
$($NGINX_CMD -s reload)
[root@z-dig scripts]#

将拒绝指定IP访问的配置文件(黑名单)单独存放,并在 nginx 主配置文件中 include 进去。

[root@z-dig conf]# grep blockip.conf nginx.conf
  include website/blockip.conf;
[root@z-dig conf]#

blockip.conf 文件格式如下:

[root@z-dig website]# cat blockip.conf
deny 195.154.211.220;
deny 195.154.188.28;
deny 195.154.188.186;
deny 180.97.106.161;
deny 180.97.106.162;
deny 180.97.106.36;
deny 195.154.180.69;
deny 195.154.211.26;
deny 221.229.166.247;
deny 180.97.106.37;
deny 195.154.216.164;
deny 195.154.216.165;
[root@z-dig website]#

将脚本放入定时任务执行:
每小时对 Nginx 的错误日志进行切割并且清空一次被拒绝访问IP的配置文件,若不清空的话,此IP将终生不能访问,若它再次攻击则会再次进入黑名单,>_<。 清空命令放在了切割脚本的尾部。
可以自己决定统计频率,根据指定的频率执行脚本,获取攻击者的IP,若此IP已经在黑名单中,则会忽略掉(由于错误日志一小时切割一次,所以在一小时内会出现重复的IP)。然后把剩下的新攻击者的IP追加到黑名单。并 reload Nginx 。若没有新增的攻击者IP则什么都不做。

[root@z-dig ~]# crontab -l
# rotate nginx log everyday
00 00 * * * /bin/bash /application/scripts/rotate-nginx-logs.sh &>/dev/null
# rotate nginx error log every hour and clean the block ip file
00 */1 * * *  /bin/bash /application/scripts/rotate-nginx-error-logs.sh &>/dev/null
# check hacker's ip every ten minutes
*/10 * * * * /bin/bash /application/scripts/block-ip.sh &>/dev/null
[root@z-dig ~]#

以下是脚本运行一段时间的攻击者IP

[root@z-dig ~]# cat /application/nginx/conf/website/blockip.conf
deny 195.154.211.220;
deny 195.154.188.28;
deny 195.154.188.186;
deny 180.97.106.161;
deny 180.97.106.162;
deny 180.97.106.36;
deny 195.154.180.69;
deny 195.154.211.26;
deny 221.229.166.247;
deny 180.97.106.37;
deny 195.154.216.164;
deny 195.154.216.165;
[root@z-dig ~]#

过段时间,再列出一份黑名单IP,看是否有变化。

[root@z-dig ~]# cat /application/nginx/conf/website/blockip.conf
deny 195.154.188.224;
[root@z-dig ~]# curl ipinfo.io/195.154.188.224;echo ''
{
  "ip": "195.154.188.224",
  "hostname": "195-154-188-224.rev.poneytelecom.eu",
  "city": "",
  "region": "",
  "country": "FR",
  "loc": "48.8600,2.3500",
  "org": "AS12876 ONLINE S.A.S."
}
[root@z-dig ~]# grep '195.154.188.224' /data/logs/nginx/error.log |wc -l
102
[root@z-dig ~]# grep '195.154.188.224' /data/logs/nginx/error.log |grep -v 'access forbidden' |wc -l
24
[root@z-dig ~]#
[root@z-dig ~]# tail -n 1 /data/logs/nginx/error.log
2015/11/30 10:47:53 [error] 30754#0: *37828 access forbidden by rule, client: 195.154.188.224, server: www.111com.net, request: "GET / HTTP/1.1", host: "www.111com.net", referrer: "http://www.111com.net"
[root@z-dig ~]#

看来多少还是管点用的。一共 access forbidden by rule 了 102-24=78 次。
适当的改改脚本,保存黑名单的历史数据,定期将大于1000的IP直接放入iptables!

热门栏目