Ubuntu通过iptables防止ssh暴力破解
步骤
- 安装iptables-persistent用于保存iptables规则
- 配置iptables规则
- 实时更新iptables规则以拦截IP访问
安装iptables-persistent
sudo aptitude install iptables-persistent
配置iptables规则
# 先查看网卡的名称,修改脚本中的$EXTIF变量
ip addr
sudo mkdir -p /usr/local/feng/iptables/
cat << 'EOF' | sudo tee /usr/local/feng/iptables/iptables.rule
#!/bin/bash
EXTIF="ens4"
INIF=""
INNET=""
export EXTIF INIF INNET
# 设置核心的网络功能:
echo "1" > /proc/sys/net/ipv4/tcp_syncookies
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
# 清除规则,设定预设政策以及开放lo与相关的设定值
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin; export PATH
iptables -F ; iptables -X ; iptables -Z
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# 启动额外的防火墙script
if [ -f /usr/local/feng/iptables/iptables.deny ]; then
sh /usr/local/feng/iptables/iptables.deny
fi
if [ -f /usr/local/feng/iptables/iptables.allow ]; then
sh /usr/local/feng/iptables/iptables.allow
fi
if [ -f /usr/local/feng/httpd-err/iptables.http ]; then
sh /usr/local/feng/httpd-err/iptables.http
fi
AICMP="0 3 3/4 4 11 12 14 16 18"
for tyicmp in $AICMP
do
iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT
done
iptables -A INPUT -p TCP -i $EXTIF --dport 22 --sport 1024:65534 -j ACCEPT
iptables -A INPUT -p TCP -i $EXTIF --dport 80 --sport 1024:65534 -j ACCEPT
iptables -A INPUT -p TCP -i $EXTIF --dport 443 --sport 1024:65534 -j ACCEPT
iptables -A INPUT -p TCP -i $EXTIF --dport 2333 --sport 1024:65534 -j ACCEPT
# ubuntu
/usr/sbin/netfilter-persistent save
EOF
## 执行脚本
sudo bash /usr/local/feng/iptables/iptables.rule
实时更新iptables
编写脚本
cat << 'EOF' | sudo tee /usr/local/feng/iptables/secure_iptables.sh
cat /var/log/auth.log|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' | sort -g -t '=' -k2 > /tmp/black.txt #尝试登录的次数和ip
DEFINE="3" #单个ip尝试登录最大值
for i in `cat /tmp/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
grep "$IP" /usr/local/feng/iptables/iptables.deny > /dev/null
if [ $? -gt 0 ];then
if [ ! "$IP" == "127.0.0.1" ]; then
echo "iptables -A INPUT -s $IP -j DROP" >> /usr/local/feng/iptables/iptables.deny
sh /usr/local/feng/iptables/iptables.rule
fi
fi
fi
fi
done
EOF
配置crontab
# 涉及防火墙,需要root权限
echo "* * * * * root bash /usr/local/feng/iptables/secure_iptables.sh" | sudo tee -a /etc/crontab
查看被拦截IP
# 通过iptables
sudo iptables-save
# 查看iptables.deny文件
sudo cat /usr/local/feng/iptables/iptables.deny
释放被拦截IP
# 设置IP变量
IP=112.27.168.236
# 把IP从iptables.deny移除
sudo sed -i "/${IP}/d" /usr/local/feng/iptables/iptables.deny
# 把IP从auth.log移除
sudo sed -i "/${IP}/d" /var/log/auth.log
# 更新iptables规则
sudo bash /usr/local/feng/iptables/iptables.rule
# 检查防火墙规则,检查auth.log文件
sudo iptables-save | grep $IP
cat /var/log/auth.log|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' | grep ${IP}
# 由于 rsyslogd 的登录文件(auth.log)只要“被编辑过”就无法继续记录,需要重启rsyslog服务
sudo systemctl restart rsyslog.service
Ubuntu通过iptables防止ssh暴力破解的更多相关文章
- 利用iptables防止ssh暴力破解和控制网速
iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --setiptables -I INPUT -p ...
- Linux应急响应(一):SSH暴力破解
0x00 前言 SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议,主要用于给远程登录会话数据进行加密,保证数据传输的安全.SSH口令长度太短或者复杂度不够,如仅包含数字,或仅包 ...
- Linux用root强制踢掉已登录用户;用fail2ban阻止ssh暴力破解root密码
Linux用root强制踢掉已登录用户 首先使用w命令查看所有在线用户: [root@VM_152_184_centos /]# w 20:50:14 up 9 days, 5:58, 3 use ...
- SSH 暴力破解趋势——植入的恶意文件属 DDoS 类型的恶意文件最多,接近70%,包括 Ganiw、 Dofloo、Mirai、 Xarcen、 PNScan、 LuaBot、 Ddostf等家族。此外挂机、比特币等挖矿程序占5.21%
SSH 暴力破解趋势:从云平台向物联网设备迁移 | 云鼎实验室出品 from: http://www.freebuf.com/articles/paper/177473.html 导语:近日,腾讯云发 ...
- 防止ssh暴力破解的小工具denyhosts
DenyHosts 简介 DenyHosts 是 Python 语言写的一个程序软件,运行于 Linux 上预防 SSH 暴力破解的,它会分析 sshd 的日志文件(/var/log/secure), ...
- Linux CentOS 防止SSH暴力破解
一. 问题的发现 昨晚苦逼加班完后,今早上班继续干活时,SSH连接服务器发现异常的提示,仔细看了一下吓一小跳,昨晚9点钟到现在,一夜之间被人尝试连接200+,慌~~~ 1. 速度查一下log [roo ...
- ssh暴力破解解决方案(Centos7更改端口)
服务器默认ssh远程连接端口为22端口,通常通过22远程连接的话,容易有ssh暴力破解的风险,给我们造成一定的损失.下面是更换ssh端口过程: 1.添加ssh端口 vim /etc/ssh/sshd_ ...
- 树莓派保卫战--防止SSH暴力破解
自己用树莓派搭建了个小server,用了很长时间了,最近查看log发现有很多SSH登陆失败,瞬间心就碎了,一直没关心小派的安全问题,怪我咯! 马上行动,首先研究下log:/var/log/auth.l ...
- 记录ssh暴力破解的密码字典
之前我已经在wooyun和91ri上发表了这篇文章,今天转到51cto上... 默认的ssh日志是不带密码记录功能的,现在想把登录密码记录下来,这样就可以搜集黑客的ssh爆破字典了,从而可以反扫回去. ...
随机推荐
- Hacker101 CTF 学习记录(一)
前言 苦力挖洞,靠运气赚点小钱.看着大佬严重,高危,再看看自己手上的低危,无危害默默流下了菜鸡的泪水 思路受局限,之前听学长推荐和同事聊到hacker101,因此通过hacker101拓展下漏洞利用思 ...
- 【Nginx】面试官:给我讲讲Nginx如何实现四层负载均衡?
写在前面 这次又被问到Nginx四层负载均衡的问题了,别慌,我们一起来细细分析这个看似简单的问题. 如果文章对你有点帮助,请关注 冰河技术 微信公众号,点赞.在看.留言和转发,大家的四连是我持续创作的 ...
- Ubuntu修改默认Python版本,你了解多少~
目录 1 查看Python版本 1.1 查看装有哪些版本 1.2 查看默认版本 2 修改Python默认版本 2.1 基于文件修改 2.2 基于软链接修改 1 查看Python版本 1.1 查看装有哪 ...
- selenium常用api之切换:table切换、alert弹框切换、iframe框架切换
10.查看浏览器打开了多少个table和当前页面在哪个table 测试:打开了浏览器后,打开了一个新的标签页之后,显示此时有2个table,浏览器中当前页面展示的是第2个页面,但是代码打印显示的仍然是 ...
- oracle之DML和DDL语句的其他用法
DML和DDL语句的其他用法 17.1 DML语句-MERGE 作用:把数据从一个表复制到另一个表,插入新数据或替换掉老数据. Oracle 10g中MERGE有如下一些改进: 1.UPDATE或IN ...
- oldboy edu python full stack s22 day16 模块 random time datetime os sys hashlib collections
今日内容笔记和代码: https://github.com/libo-sober/LearnPython/tree/master/day13 昨日内容回顾 自定义模块 模块的两种执行方式 __name ...
- PooledByteBuf内存池-------这个我现在不太懂
转载自:http://blog.csdn.net/youaremoon/article/details/47910971 http://blog.csdn.net/youar ...
- archaius(3) 配置管理器
基于上一节介绍的配置源,我们来继续了解配置管理器.配置源只是抽象了配置的获取来源,配置管理器是基于配置源的基础上对这些配置项进行管理.配置管理器的主要功能是将配置从目标位置加载到内存中,并且管理内存配 ...
- hystrix总结之请求批量执行
hystrix可以将同一个命令的多次执行合并到一起执行. public class HelloWorldCommandCollapser extends HystrixCollapser<Lis ...
- Jackson精解第4篇-@JacksonInject与@JsonAlias注解
Jackson是Spring Boot(SpringBoot)默认的JSON数据处理框架,但是其并不依赖于任何的Spring 库.有的小伙伴以为Jackson只能在Spring框架内使用,其实不是的, ...