Linux网络安全配置

常见入侵流程

在这里插入图片描述


总结出来四点:

  • 防火墙
  • 服务权限
  • SELinux
  • 系统权限
    在这里插入图片描述

防火墙推荐配置

在这里插入图片描述

# 设置默认允许策略
iptables -P INPUT ACCEPT

# 清空现有的所有策略
iptables -F

# 允许业务需要的端口
iptables -A INPUT -p tcp -m tcp--dport 80 -j ACCEPT

# 允许远程登陆的端口
iptables -A INPUT -s 1.1.1.1 -p tcp -m tcp--dport 22 -j ACCEPT

# 允许内网网卡全通--如果服务器有内网ip和外网ip,一般内网那张卡是全开的
iptables -A INPUT -i eth1 -j ACCEPT

# 回环会话一定要打开
iptables -A INPUT -i lo -j ACCEPT

# 安全防护措施
照抄图片就行

禁用不需要的服务

https://blog.51cto.com/dlican/3741859
在这里插入图片描述

# 安装ntsysv
yum install -y ntsysv

# 使用ntsysv管理服务自启
ntsysv

在这里插入图片描述


系统登陆安全与ssh配置

在这里插入图片描述


1.授权用户登陆与sudo设定

  • 禁止root用户登陆
  • 配置普通用户 允许所有来源 所有程序都不需要密码
  • /etc/sudoers 权限必须是440

在这里插入图片描述


2.ssh安全登陆经验

https://eulixos.com/docs/2.0/SecHarden/%E7%B3%BB%E7%BB%9F%E6%9C%8D%E5%8A%A1.html#%E5%8A%A0%E5%9B%BA-ssh-%E6%9C%8D%E5%8A%A1

  • 需要默认ssh端口为4位端口(数字建议大一点)
  • ssh不使用dns反查,提高ssh连接速度
  • ssh关闭GSSAPI验证,提高ssh连接速度
  • 禁止root账号登陆
    • 登陆普通用户后使用sudo su- 完全切换到root账户
      在这里插入图片描述

3.锁定重要系统文件

在这里插入图片描述

#!/usr/bin/env bash

## 删除内置的多余账号
function delete_redundant_account(){
# 理论上adm,lp,sync,shutdown,halt,news,uucp,operator,games,ftp,postfix,dovecot这些账号都可以删除,但是删了以后想加回去会很麻烦
redundant_user_lists=(operator
    lp
    shutdown
    halt
    games
)

for user in "${redundant_user_lists[@]}"; do
    # 这里使用-r参数可能会把/root目录给删除掉
    userdel -f "$user"
done
}

## 删除内置的多余用户组
function delete_redundant_group(){
# 可以删除的组adm,lp,mail,games,ftp,audio
redundant_group_lists=(lp
    uucp
    games
    dip
    games)

for group_name in "${redundant_group_lists[@]}"; do
    # 删除用户组
    groupdel -f "$group_name"
done
}

## 锁定重要系统文件
function Lock_important_system_files(){
# 重要系统文件列表
important_system_files=("/etc/passwd"
    "/etc/shadow"
    "/etc/group"
    "/etc/inittab"
    "/root/.ssh/authorized_keys")

# 锁定重要系统文件
for file in "${important_system_files[@]}"; do
    (ls "$file" && chattr +i "$file" && lsattr "$file") || echo "$file"文件不存在
done
}

# 清空/etc/issue,/etc/issue.net 去除系统内核版本登陆的屏幕显示
function clear_login_prompt(){
sysinfo_files=("/etc/issue"
    "/etc/issue.net")

for file in "${sysinfo_files[@]}"; do
    echo >"$file"
done
}

main() {
    # 1.删除内置的多余账号
    delete_redundant_account
    # 2.删除内置的多余用户组
    delete_redundant_group
    # 3.锁定重要系统文件
    Lock_important_system_files
    # 4.清空登录提示
    clear_login_prompt
}

main

4.自动拉黑密码错误次数多的IP

源码:https://github.com/omaidb/qiaofei_notes/blob/main/shell_code/infosec/block_ssh_error_ip.sh

 #!/bin/bash

#  本脚本适用于rhel8+iptables
# 自动拉黑ssh密码登录错误大于7次的ip

# 开启debug模式
# set -ex

function check() {
    # 初始化IP列表
    block_ip_list=""

    # 获取最近ssh密码错误登录的IP列表和次数
    failed_logins=$(journalctl _SYSTEMD_UNIT=sshd.service | grep 'Failed password' | awk '{print $(NF-3)}' | sort | uniq -c)

    # 遍历错误登录事件
    while read -r line; do
        # ssh登录错误次数 
        count=$(echo "$line" | awk '{print $1}')
        # ssh登录错误源ip
        ip=$(echo "$line" | awk '{print $2}')
        # ssh登录错误大于等于7次,就添加到黑名单IP列表中
        if [ "$count" -ge 7 ]; then
            # echo "IP $ip failed $count times. Adding to block list."
            block_ip_list="$block_ip_list $ip"
        fi
    done <<< "$failed_logins"
}

# 方法:拉黑IP
function block_ip() {
    local ip="$1"
    echo "Blocking IP: $ip"
    # 检查是否已有该IP的防火墙规则,存在则什么都不显示,不存在则报错
    if sudo iptables -C INPUT -s "$ip" -j DROP >/dev/null 2>&1; then
        # echo "IP $ip 已在被iptables封锁中。"
        :
    else
        # 将IP添加到防火墙规则中
        sudo iptables -A INPUT -s "$ip" -j DROP
        echo "拉黑$ip"
    fi
}

# 脚本退出前保存防火墙规则
function ipt_save(){
    # 退出前保存iptabels规则
    service iptables save || iptables-save > /etc/sysconfig/iptables
    echo "防火墙规则已保存"
}

# 捕获脚本退出信号
# trap 退出时绑定的函数名 EXIT
# trap cleanup EXIT
# 捕获CTRL+C信号
# trap ipt_save INT
# 捕获所有信号
trap ipt_save SIGINT SIGTERM SIGHUP

# 设置退出信号处理方式
trap ipt_save EXIT

# 设置INT信号处理方式
# 在接收到INT信号时执行exit 2命令,即以退出状态码2退出当前脚本。
trap 'exit 2' INT

main() {
    # 间隔10s无限循环检查函数
    while true; do
        # 检查ssh登录错误的恶意IP
        check
        # 循化拉黑检查到的ip
        for ip in $block_ip_list; do
            block_ip "$ip"
        done
        # 每隔10s检查一次,时间可根据需要自定义
        sleep 10
    done
}

# 执行主方法
main

5. 使用fail2ban防止ssh穷举爆破

https://blog.csdn.net/omaidb/article/details/120231345


yum源配置建议


1.配置第三方源

Centos7配置yum国内源(BaseOS+epel+ELRepo+SCL+IUS+REMI)


2.升级系统内核及更新软件

上线之前必须更新内核和系统补丁

https://blog.csdn.net/omaidb/article/details/127122959

在这里插入图片描述

# 清空yum缓存
yum clean all

# 生成缓存
yum makecache

# 更新内核
yum install kernel -y

# 更新系统及内核
yum upgrade

# 必备软件
yum install ntpdate wget -y

服务器定期对时

chrony时间服务
ntp时间服务

# ntp时间服务器
ntp.aliyun.com

# 用crontab计划任务对时--ntp
/usr/sbin/ntpdate ntp.aliyun.com>>/var/log/ntp.log 2>&1;/sbin/hwclock -w

Linux系统加固

参考:
https://eulixos.com/docs/2.0/SecHarden/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

识途老码

赞赏是第一生产力

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

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

打赏作者

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

抵扣说明:

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

余额充值