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爆破字典了,从而可以反扫回去. ...
随机推荐
- Android开发之recycleView详解代码,看完包你熟练掌握recycleView的用法。转自网络经典文章
来源 http://jinyudong.com/2014/11/13/Introduce-RecyclerView-%E4%B8%80/ 编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在 ...
- Transform与Vector3 的API
Transform.InverseTransformDirection(Vector3 direction) Vector3.ProjectOnPlane(Vector3 vector, Vector ...
- OneDrive Weblist
OneIndex-Serverless 教程:https://zhuanlan.zhihu.com/p/74538287 https://github.com/LiuChangFreeman/OneI ...
- SSM框架中添加写日志功能
前提:要导入log4j的jar包 在web.xml中输入: <!--日志加载--> <context-param> <param-name>log4jConfigL ...
- 部署zabbix监控服务器,部署主动监控
1.1部署服务运行环境 LNMP#yum -y install gcc pcre-devel zlib-devel openssl-devel #tar -zxvf nginx-1.12.2.ta ...
- 2020年 .NET ORM 完整比较、助力选择
.NET ORM 前言 为什么要写这篇文章? 希望针对 SEO 优化搜索引擎,让更多中国人知道并且使用.目前百度搜索 .NET ORM 全是 sqlsugar,我个人是无语的,每每一个人进群第一件事就 ...
- [Binder深入学习二]Binder驱动——基础数据结构二
Userspace和KernelSpace进行交互时,大部分命令是通过 ioctl 实现的,在这个过程中,最重要的一个便是 BINDER_WRITE_READ 命令了. #define BINDER_ ...
- vue | 基于vue的城市选择器和搜索城市对应的小区
城市选择器应该是比较常用的一个组件,用户可以去选择自己的城市,选择城市后返回,又根据自己选择的城市搜索小区. 功能展示 这是选择结果 这是选择城市 这是搜索小区 这是搜索小区接口,key为城市名字,i ...
- Spring中同一个service中方法相互调用事务不生效问题解决方案
问题描述: 我们在用Spring框架开发Web项目过程中,经常需要用同一个service中的一个方法调用另一个方法,如果此时调用方没有添加事务注解@Transactional,而在被调用方添加事务注解 ...
- 云计算openstack核心组件——neutron网络服务(9)
一.虚拟机获取 ip: 用 namspace 隔离 DHCP 服务 Neutron 通过 dnsmasq 提供 DHCP 服务,而 dnsmasq 通过 Linux Network Namespa ...