首页 > 系统相关 >CentOS / Redhat Iptables Firewall Configuration Tutorial

CentOS / Redhat Iptables Firewall Configuration Tutorial

时间:2023-03-28 10:01:00浏览次数:57  
标签:Iptables CentOS Redhat -- tcp state PUB IPT INPUT


显示代码

#!/bin/bash
# A sample firewall shell script
IPT= "/sbin/iptables"
SPAMLIST= "blockedip"
SPAMDROPMSG= "BLOCKED IP DROP"
SYSCTL= "/sbin/sysctl"
BLOCKEDIPS= "/root/scripts/blocked.ips.txt"
 
# Stop certain attacks
echo "Setting sysctl IPv4 settings..."
$SYSCTL net.ipv4.ip_forward=0
$SYSCTL net.ipv4.conf.all.send_redirects=0
$SYSCTL net.ipv4.conf.default.send_redirects=0
$SYSCTL net.ipv4.conf.all.accept_source_route=0
$SYSCTL net.ipv4.conf.all.accept_redirects=0
$SYSCTL net.ipv4.conf.all.secure_redirects=0
$SYSCTL net.ipv4.conf.all.log_martians=1
$SYSCTL net.ipv4.conf.default.accept_source_route=0
$SYSCTL net.ipv4.conf.default.accept_redirects=0
$SYSCTL net.ipv4.conf.default.secure_redirects=0
$SYSCTL net.ipv4.icmp_echo_ignore_broadcasts=1
#$SYSCTL net.ipv4.icmp_ignore_bogus_error_messages=1
$SYSCTL net.ipv4.tcp_syncookies=1
$SYSCTL net.ipv4.conf.all.rp_filter=1
$SYSCTL net.ipv4.conf.default.rp_filter=1
$SYSCTL kernel. exec -shield=1
$SYSCTL kernel.randomize_va_space=1
 
echo "Starting IPv4 Firewall..."
$IPT -F
$IPT -X
$IPT -t nat -F
$IPT -t nat -X
$IPT -t mangle -F
$IPT -t mangle -X
 
# load modules
modprobe ip_conntrack
 
[ -f "$BLOCKEDIPS" ] && BADIPS=$( egrep - v -E "^#|^$" "${BLOCKEDIPS}" )
 
# interface connected to the Internet
PUB_IF= "eth0"
 
#Unlimited traffic for loopback
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A OUTPUT -o lo -j ACCEPT
 
# DROP all incomming traffic
$IPT -P INPUT DROP
$IPT -P OUTPUT DROP
$IPT -P FORWARD DROP
 
if [ -f "${BLOCKEDIPS}" ];
then
# create a new iptables list
$IPT -N $SPAMLIST
 
for ipblock in $BADIPS
do
$IPT -A $SPAMLIST -s $ipblock -j LOG --log-prefix "$SPAMDROPMSG "
$IPT -A $SPAMLIST -s $ipblock -j DROP
done
 
$IPT -I INPUT -j $SPAMLIST
$IPT -I OUTPUT -j $SPAMLIST
$IPT -I FORWARD -j $SPAMLIST
fi
 
# Block sync
$IPT -A INPUT -i ${PUB_IF} -p tcp ! --syn -m state --state NEW -m limit --limit 5 /m --limit-burst 7 -j LOG --log-level 4 --log-prefix "Drop Sync"
$IPT -A INPUT -i ${PUB_IF} -p tcp ! --syn -m state --state NEW -j DROP
 
# Block Fragments
$IPT -A INPUT -i ${PUB_IF} -f -m limit --limit 5 /m --limit-burst 7 -j LOG --log-level 4 --log-prefix "Fragments Packets"
$IPT -A INPUT -i ${PUB_IF} -f -j DROP
 
# Block bad stuff
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags ALL ALL -j DROP
 
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags ALL NONE -m limit --limit 5 /m --limit-burst 7 -j LOG --log-level 4 --log-prefix "NULL Packets"
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags ALL NONE -j DROP # NULL packets
 
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
 
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags SYN,FIN SYN,FIN -m limit --limit 5 /m --limit-burst 7 -j LOG --log-level 4 --log-prefix "XMAS Packets"
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP #XMAS
 
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags FIN,ACK FIN -m limit --limit 5 /m --limit-burst 7 -j LOG --log-level 4 --log-prefix "Fin Packets Scan"
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags FIN,ACK FIN -j DROP # FIN packet scans
 
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
 
# Allow full outgoing connection but no incomming stuff
$IPT -A INPUT -i ${PUB_IF} -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A OUTPUT -o ${PUB_IF} -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
 
# Allow ssh
$IPT -A INPUT -i ${PUB_IF} -p tcp --destination-port 22 -j ACCEPT
 
# Allow http / https (open port 80 / 443)
$IPT -A INPUT -i ${PUB_IF} -p tcp --destination-port 80 -j ACCEPT
#$IPT -A INPUT -o ${PUB_IF} -p tcp --destination-port 443 -j ACCEPT
 
# allow incomming ICMP ping pong stuff
$IPT -A INPUT -i ${PUB_IF} -p icmp --icmp- type 8 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
#$IPT -A OUTPUT -o ${PUB_IF} -p icmp --icmp-type 0 -m state --state ESTABLISHED,RELATED -j ACCEPT
 
# Allow port 53 tcp/udp (DNS Server)
$IPT -A INPUT -i ${PUB_IF} -p udp --dport 53 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
#$IPT -A OUTPUT -o ${PUB_IF} -p udp --sport 53 -m state --state ESTABLISHED,RELATED -j ACCEPT
 
$IPT -A INPUT -i ${PUB_IF} -p tcp --destination-port 53 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
#$IPT -A OUTPUT -o ${PUB_IF} -p tcp --sport 53 -m state --state ESTABLISHED,RELATED -j ACCEPT
 
# Open port 110 (pop3) / 143
$IPT -A INPUT -i ${PUB_IF} -p tcp --destination-port 110 -j ACCEPT
$IPT -A INPUT -i ${PUB_IF} -p tcp --destination-port 143 -j ACCEPT
 
##### Add your rules below ######
#
#
##### END your rules ############
 
# Do not log smb/windows sharing packets - too much logging
$IPT -A INPUT -p tcp -i ${PUB_IF} --dport 137:139 -j REJECT
$IPT -A INPUT -p udp -i ${PUB_IF} --dport 137:139 -j REJECT
 
# log everything else and drop
$IPT -A INPUT -j LOG
$IPT -A FORWARD -j LOG
$IPT -A INPUT -j DROP
 
exit 0

标签:Iptables,CentOS,Redhat,--,tcp,state,PUB,IPT,INPUT
From: https://blog.51cto.com/u_2650279/6153835

相关文章

  • CentOS常用操作记录
    1.查看Kernel等版本信息============================hostnamectl  2.配置DNS ===================================1)编辑配置文件vi/etc/resolv.confsearch......
  • Centos系统服务器怎么查看端口是否开放?
    不同的linux系统查看端口是否开放的不同,在某些环境下,需要依赖于服务,如果centos中并没有装相关的服务,就不一定能生效。所以具体使用哪种方法,取决于环境,可以每一种都试试。......
  • CentOS 7 设置IP
     ####1、查看网卡信息```ipaddr```####2、修改网卡配置```vi/etc/sysconfig/network-scripts/ifcfg-ens192``````propertiesTYPE=EthernetPROXY_METHOD=noneBROW......
  • centos系统 修改ssh默认端口号
    前提:关闭防火墙、关闭SELinux#关闭防火墙systemctlstopfirewalld&&systemctldisablefirewalld#关闭SElinux#永久关闭sed-i's/SELINUX=enforcing/SELIN......
  • centos添加永久静态路由
    CentOS7系统双网卡环境下添加静态路由的几种方法发布于2019-11-2111:11:44阅读 9.4K0 如下拓扑图所示(点击图片查看清晰大图)场景简单描述一下:CentOS7......
  • [GAUSS-50201]:The /opt/software/openGauss/xxxx-RedHat-64bit.tar.bz2 does not exi
    问题描述:使用redhat7.9来安装opengauss集群,预安装过不去。opengauss官方只支持centos版本,最好是centos7.6。[root@db01script]#./gs_preinstall-Uomm-Gdbgrp-X/o......
  • CentOS/Ubuntu 安装 Docker和Portainer
    在两个环境下安装了Docker及Portainer,记录下以供日后查阅CentOS7部分1.清理旧版本$sudoyumremovedocker\docker-client\docker-c......
  • 如何将CentOS Stream退回为CentOS 8.5
    CentOS8已于2021年年底正式停止维护,因业务需要,老大说,换Steam吧,后面环境有问题果然反悔了,哈哈,怎么办,没降级工具哦,那就喊我吃后悔药了。说明:原来版本是8.5.2111,换Steam以......
  • Centos7 安装 mysql 8
    安装1.配置yum仓库#更新密钥rpm-importhttps:/repo.mysql.com/RPM-GPG-KEYmysql-202#安装Mysql8.x版本yum库rpm-Uvhhttps:/dev.mysql.com/get/mysql80-commu......
  • Centos7安装Python3.7
    说明:全部操作都在root用户下执行,python3.7安装在/root/python3目录下1.安装编译相关工具yum-ygroupinstall"Developmenttools"yum-yinstallzlib-develbzip2-dev......