awk结合iptables结合shell脚本实现的两个小案例,实现安全功能

SHELL封锁ip

扫描器封锁

可以解决恶意扫描器的扫描问题

(SHELL)
​
`#!/bin/bash`
​
`#找到你的日志路径`
​
`logfile=var/log/httpd`
​
`我要找一分钟之内的日志,找出ip访问最高的十个ip`
​
`last_mintues= 1`
​
`##开始时间的一分钟之前`
​
`start_time=$(date -d"$last_minutes minutes ago"+"%d%b%Y:%H:%M:%S")`
​
`echo $start_time`
​
`#结束时间`
​
`end_time =$(date="%d%b%Y:%H:%M:%S")`
​
`echo` `$end_time`
​
`##过滤单位时间内访问量最高的IP`,`并把它写入到一个文件中`
​
`tac $logfile/access_log | awk -v st="$start_time" -v et="end_time"'{t=substr($4)}'``;{if(t>=st&&t<+et){print $1}}' |sort | uniq -c|sort -nr| head -n 10 >$logfile/log-ip-top10` 
​
##ip-top10 十个ip
#单位时间内,我只允许单个ip10次访问
ips ='cat $Logfile/log-ip-top10 | awk '{if($1 > 10 print $2)}'
#由于ip筛选了十个,我们需要循环处理
for ip in ips 
do
echo $ip >> $logfile/getip.txt
echo $ip 
iptables -t filter -I INPUT -p tcp -m multiport --drop 80,443 -s $ip -j DROP
done
​
​
​

vim deny_scan.sh

./deny_scan.sh

./deny_Scan.sh

./deny_scan.sh

 

SSH封锁

shell封锁ssh远程连接恶意连续登录出错

#!/bin/bsah
​
​
#取出来访问失败的IP
logfile=/var/log/secure
IP=$(awk '/Failed password/{arr[($NF-3)]++}END{for (i in arr){if (arr[i]>=4)}}'$logfile)
​
#查看是否封堵过
for ip in ips
do
tmpIP='iptables -L -n | tr -s " " | awk /^DROP/ && /22$/{print $4}'
​
echo $ ={tmpIP[@]} | grep -qw $ip
​
if [ $? -ne 0]
then
iptables -t fileter -I INPUT -p tcp --dport 22 -s $ip -j DROP
fi
done
rpm -q iptables -services & > /dev/null
then 
yum -y install iptables-service & > /dev/null
systemctl enable --now iptables-service $ >/dev/null
fi
iptables-save > etc/sysconfig/iptables
unset tmpIP
​

此处已经被限制

总结 

awk结合iptables结合shell脚本实现的两个小案例,实现安全功能

补充

shell中的@符

@和星号是数组的特殊的索引,表示返回数组的所有成员

$ foo =(a b c d e f )
$ echo ${foo[@]}
a b c d e f
#这两个特殊的索引配合for循环,就可以遍历数组
for i in " ${name[@]}" ; do 
echo $i
done
⭐ 这个就相当于是一个遍览器
​
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rek'Sai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值