6

防止服务器被攻击_李超超超超的技术博客_51CTO博客

 1 year ago
source link: https://blog.51cto.com/chao/5333967
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

防止服务器被攻击

原创

李超超超超 2022-05-26 14:42:25 ©著作权

文章标签 linux vim bash 文章分类 Linux 系统/运维 阅读数147

一、编写脚步自动添加添加暴力破解IP地址。

1、编写shell脚本查看登录失败记录添加到/etc/hosts.deny下。

#!/bin/bash

cat /var/log/wtmp >> log-wtmp.back #备份失败记录,下面会清空

lastb >> lastb.bak #备份系统登入失败的用户相关信息

lastb | awk '{print $3}' | sort | uniq -c >> black.ip

list=$(lastb | awk '{print $3}'| sort | uniq -c | awk '{if ($1 > 2) print $2}')

for ip in ${list}

echo ALL: ${ip} >> /etc/hosts.deny #加入黑名单

echo > /var/log/btmp #清空失败记录,防止脚本下次执行重复统计IP

防止服务器被攻击_bash

2、执行lastb列出登入系统失败的用户相关信息。

lastb (可以看到登录2次失败的用户信息)

防止服务器被攻击_vim_02

3、登入失败次数大于1次的IP追加到/etc/hosts/deny下。

#!/bin/bash

cat /var/log/wtmp >> log-wtmp.back #备份失败记录,下面会清空

lastb >> lastb.bak #备份系统登入失败的用户相关信息

lastb | awk '{print $3}' | sort | uniq -c >> black.ip

list=$(lastb | awk '{print $3}'| sort | uniq -c | awk '{if ($1 > 1) print $2}')

for ip in ${list}

echo ALL: ${ip} >> /etc/hosts.deny #加入黑名单

echo > /var/log/btmp #清空失败记录,防止脚本下次执行重复统计IP

防止服务器被攻击_linux_03

4、执行lastb列出登入系统失败的用户相关信息和etc/hosts/deny信息。

防止服务器被攻击_linux_04

5、设置计划任务,并模拟ssh登录失败。

防止服务器被攻击_linux_05

防止服务器被攻击_bash_06

二、开启防火墙和selinux

1、开启防火墙并设置开机自启动

systemctl start firewalld

systemctl enable firewalld

防止服务器被攻击_bash_07

2、开启selinux

vim /etc/selinux/config

防止服务器被攻击_linux_08

SELINUX=enforcing

防止服务器被攻击_vim_09修改完selinux配置文件后要重启服务器。

三、更改ssh端口禁用ping

1、更改ssh端口

vim /etc/ssh/sshd_config

防止服务器被攻击_linux_10

原配置文件 Port 22这行默认是注释的,添加一个新的端口

防止服务器被攻击_bash_11

重启ssd服务 systemctl restart sshd

防止服务器被攻击_bash_12

2、禁用ping

echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all

防止服务器被攻击_bash_13

防止服务器被攻击_linux_14

echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all

echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all

注意,icmp_echo_ignore_all 不是真实文件,因此不能通过 vim 来编辑修改。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK