iptable的安全设置
基础知识:
一、iptables的表格:
1、Nat表:用户网络地址转换;
包括:POSTROUTING【路由判断之后】、PREROUTING【路由判断之前】
2、Filter表:
包括:INPUT、OUTPUT、FORWARD
3、Mangle表:用于实现QOS时使用;
二、iptables的指令、选项和动作
iptables -t 表格名称 指令(大写) 链名称 选项 参数
-t nat
filter
mangle
指令:
-A --append 追加
-R --replace 替换
-D --delete 删除
-I --insert 插入
-N --new 用户自定义链
-X 删除用户自定义的空链
-F --flush 清空链
-P --policy ACCEPT DROP REJECT
链名称 chain
nat POSTROUTING --->SNAT PREROUTING --->DNAT OUTPUT
filter INPUT OUTPUT FORWARD
选项:
选项 参数
来源 -s --source 地址 子网 网段
-i 【-i eth0】
目标 -d --destnation 地址 子网 网段
-o 【-o eth1】
协议 tcp --src-port --dst-port
udp --src-port --dst-port
icmp --icmp-type [!] typename echo-reply (pong) echo-request
动作 -j --jump
SNAT DNAT MASQUERADE
ACCEPT DROP REJECT
REDIRECT
案例--实现iptables对网络流量的过滤
需要的软件包:
一、重新编译内核
1、合并kernel+layer7补丁
tar -jxvf linux-2.6.28.tar\(1\).bz2 -C /usr/src/
tar -zxvf netfilter-layer7-v2.20.tar.gz -C /usr/src/
cd /usr/src/linux-2.6.28/
# patch -p1 < /usr/src/netfilter-layer7-v2.20/kernel-2.6.25-layer7-2.20.patch
2、配置新内核
[root@localhost ~]# cd /usr/src/linux-2.6.28/
[root@localhost linux-2.6.28]# cp /boot/config-2.6.18-164.el5 .config
#沿用旧的内核配置
[root@localhost linux-2.6.28]#make menuconfig
//配置内核时,在“Networking ---> Networking Options ---> Network Packet filtering framework (Netfilter) ”处主要注意两个地方:
1) ---> Core Netfilter Configuration
//将“Netfilter connection tracking suport (NEW)”选择编译为模块(M),需选取此项才能看到layer7支持的配置。
//将layer7、string、state、time、IPsec、iprange、connlimit、ftp……等编译成模块,根据需要自行选择。
2) ---> IP: Netfilter Configuration
//将“IPv4 connection tracking support (require for NAT)”编译成模块。
//将“Full NAT”下的“MASQUERADE target support”和“REDIRECT target support”编译成模块。
3、编译及安装模块、新内核
shell> make && make modules_install && make install
#编译安装成后后,重启选择使用新的内核(2.6.25.19)引导系统
二、重新编译iptables
卸载原来的iptables
[root@localhost ~]# cd /etc/init.d/
[root@localhost init.d]# cp iptables iptables.bak
[root@localhost ~]# rpm -e iptables –nodeps
2、合并iptables+layer7补丁
tar -jxvf iptables-1.4.2.tar.bz2 -C /usr/src/
# cd /usr/src/netfilter-layer7-v2.20/iptables-1.4.1.1-for-kernel-2.6.20forward/
#cp libxt_layer7.c libxt_layer7.man /usr/src/iptables-1.4.2/extensions/
3、编译安装
#cd /usr/src/iptables-1.4.2/
./configure --prefix=/ --with-ksource=/usr/src/linux-2.6.25.19
[root@localhost iptables-1.4.2]# make && make install
4、安装l7-protocols模式包
[root@localhost ~]# tar zxvf l7-protocols-2008-10-04.tar.gz -C /etc/
[root@localhost ~]# mv /etc/l7-protocols-2008-10-04 /etc/l7-protocols
[root@localhost ~]# mv /etc/init.d/iptables.bak /etc/init.d/iptables
重启iptables服务
[root@localhost ~]# service iptables restart
案例
某公司有三个部门
工程部门 192.168.10.11—192.168.10.20
技术部门 192.168.10.21—192.168.10.30
经理办 192.168.10.31—192.168.10.40
公司上班时间 (周一---周五 08:20:00)
为了提高员工工作效率,现将公司网络做如下整改:
1、工程部门 上班时间可以访问ftp和http
不允许 qq聊天和迅雷下载,下班后无限制
2、软件部门 可以访问http
不允许非法站点sina ,不允许使用迅雷 ,连接数最多3个
不允许聊天 ,下班后无限制
3、经理办公室 可以访问http资源 qq聊天,下班后无限制
4、dmz区域www服务器进行发布
拓扑图
实验步骤
[root@localhost ~]# vim /etc/sysctl.conf
7 net.ipv4.ip_forward = 1
[root@localhost ~]# sysctl -p
[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth1 -j SNAT --to 192.168.101.234
[root@localhost ~]# modprobe ip_nat_ftp #加上ftp模块
修改为iptablse默认拒绝所有,增加安全性能
[root@localhost ~]# iptables -A INPUT -s 192.168.10.15 -p tcp --dport 22 -j ACCEPT
[root@localhost ~]# iptables -A OUTPUT -d 192.168.10.15 -p tcp --sport 22 -j ACCEPT
[root@localhost ~]# iptables -P INPUT DROP
[root@localhost ~]# iptables -P OUTPUT DROP
[root@localhost ~]# iptables -P FORWARD DROP
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.10.11-192.168.10.20 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 21 -j ACCEPT
[root@localhost ~]# iptables -t filter -A FORWARD -p tcp --sport 21 -j ACCEPT
[root@localhost ~]# iptables -t filter -A FORWARD -p tcp --sport 20 -j ACCEPT
[root@localhost ~]# iptables -t filter -A FORWARD -p tcp --dport 20 -j ACCEPT
或者
基于状态的访问方式,记录数据流出的轨迹,自动返回
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.10.11-192.168.10.20 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 21 -j ACCEPT
[root@localhost ~]# iptables -t filter -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
上班时间工程部可以访问ftp
[root@localhost ~]# iptables -t filter -A FORWARD -s 192.168.10.0/24 -m time --timestart 20:01 --timestop 07:59 -o eth1 -j ACCEPT
下班无限制
软件部
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 80 -j ACCEPT
DNS
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p udp --dport 53 -j ACCEPT
可以访问http的资源
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m string --string "sina" --algo bm -j DROP
[root@localhost ~]# iptables -t filter -I FORWARD 1 -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m string --string "sina" --algo bm -j DROP
不允许使用迅雷
[root@localhost ~]# iptables -I FORWARD 2 -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq -j DROP
[root@localhost ~]# iptables -I FORWARD 2 -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto xunlei -j DROP
[root@localhost ~]# iptables -I FORWARD 2 -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto msnmessenger -j DROP
连接数目不超过三个
[root@localhost ~]# iptables -I FORWARD 2 -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --syn --dport 80 -m connlimit --connlimit-above 3 -j DROP
经理办公室
[root@localhost ~]# iptables -I FORWARD 2 -m iprange --src-range 192.168.10.31-192.168.10.40 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p udp --dport 53 -j ACCEPT
#放行任何主机访问31——40主机上的TCP协议的53端口上的DNS传输
[root@localhost ~]# iptables -I FORWARD 2 -m iprange --src-range 192.168.10.31-192.168.10.40 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 80 -j ACCEPT
#放行任何主机访问31——40主机上的TCP协议的80端口上的已经建立的传输
[root@localhost ~]# iptables -I FORWARD 2 -m iprange --src-range 192.168.10.31-192.168.10.40 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq -j ACCEPT
#允许QQ在工作时间的使用
[root@localhost ~]# iptables -I FORWARD 2 -m iprange --src-range 192.168.10.31-192.168.10.40 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto xunlei -j ACCEPT
#允许QQ在工作时间的使用
可以上网下载
可以聊天
可以浏览新浪站点
上班时间不可以访问FTP
DMZ区域的服务器发布
[root@localhost ~]# iptables -t nat -A PREROUTING -d 192.168.101.234 -p tcp --dport 80 -i eth1 -j DNAT --to 192.168.2.100
#将外网的web访问请求(80端口)通过DNAT转发至内网web服务器192.168.2.100
[root@localhost ~]# iptables -t filter -A FORWARD -d 192.168.2.100 -p tcp --dport 80 -j ACCEPT
转载于:https://blog.51cto.com/shdw1999/996943
iptable的安全设置相关推荐
- gitlab安装配置、备份恢复
安装前提:内存不少于2G,安装在centos7上 若是此方式不行,就用官网的 官方安装文档 https://about.gitlab.com/installation/?version=ce#cent ...
- Android智能手机网络防火墙开发的经验心得
首届 Google 暑期大学生博客分享大赛--2010 Andriod 篇 Android 智能手机网络防火墙开发的经验心得 这篇博客主要讲述我实现一款Android手机上网防火墙的一个经验心得分享. ...
- Android网络框架(二)——策略路由与常用命令
目录 一. 策略路由 1.1 默认路由表 1.2 默认规则 1.3 路由 二. 命令 2.1 ip route 2.2 ip rule 一. 策略路由 传统路由:在Android4.4之前是使用的传统 ...
- Linux之防火墙策略
linux之防火墙策略 什么是防火墙 众所周知,相较于企业内网,外部的公网环境更加恶劣,罪恶丛生.在公网与企业内网之间充当保护屏障的防火墙虽然有软件或硬件之分,但主要功能都是依据策略对穿越防火墙自身的 ...
- 玩转「Wi-Fi」系列之Connman剖析(六)
原文地址 译者:程序手艺人 转载请注明出处: http://blog.csdn.net/z2066411585 连接管理器(ConnMan)是一个连接管理守护进程(connmand),用于管理运行Li ...
- linux6.6 ip 设置,centos 6.6默认iptable规则详解
今天在自己电脑上新装了centos6.6虚拟机,然后装了nginx,没有进行任何其他设置,然后就发现只能在centos上面可以访问nginx,看了下iptable的规则 [root@centos6 ~ ...
- iptable设置 备忘
显示当前iptable设置: iptables -L -n 关闭所有接入端口: iptables -I INPUT DROP 关闭tcp22准入端口: iptables -I INPUT -p tcp ...
- CentOS7防火墙firewalld和iptable的设置和使用
再部署了服务器环境之后,服务器正常启动,日志也正常打印,但是服务不能访问,对问题逐一进行排查,最后发现是服务器防火墙设置的问题.于是将防火墙的一些设置方法总结记录如下: 1.firewalld的基本使 ...
- Android设置iptable实现外网访问
支持WiFi热点访问外网 iptables -t nat -A POSTROUTING -s 192.168.43.1/24 -o ccmni0 -j MASQUERADE 支持USB共享网络访问外网 ...
- Linux中Iptable防火墙规则的应用
在没有硬件防火墙的前提下,Linux系统也提供了很完善的防火墙策略Iptable,同样能胜任防火墙的策略,但由于规则负责的原因很少被使用.我总结一下iptable的使用方法. 通常防火墙策略配置文件所 ...
最新文章
- ubuntu16.04 cuda9.0 cudnn Tensorflow GPU 1.10.0
- python编程工具p-python Gui开发工具选择
- 组策略禁止自动锁定计算机,如何在Windows 10中禁用自动锁定
- Unity3D 游戏引擎之平面小球重力感应详解【转】
- php 重复写入数据,完美解决Thinkphp3.2中插入相同数据的问题
- [渝粤题库]西北工业大学复变函数与积分变换
- base64还原_冰蝎3.0流量分析与还原
- 原生DOM操作方法小结
- Image Processing --- Gaussian Pyramid Laplacian Pyramid
- 完整的 .NET Core 目标框架的预处理器符号列表
- ASP.NET中常用功能代码总结(1)——发送邮件篇
- 巴斯勒相机外部触发接线_PLC控制柜的设计原理,电装布局、接线图和原理图
- ORACLE有EXCEL中trend函数,EXCEL【TREND】函数,你知道如何使用吗?
- 我的Java传承名单(不知为何以前的又没有了,幸亏有备份才可以又贴出来)
- 火狐,Firefox浏览器怎么设置主页
- 正版win10如何重装系统|win10正版重装系统教程
- 国内最火的10款Java开源项目
- 15 个有趣的 JS 和 CSS 库
- openGauss社区理事会正式成立!云和恩墨与3大运营商、7大头部银行等18家理事单位加入,共建、共享、共治优质社区...
- Aaron blog 摘抄