千家信息网

如何进行iptables的实战分析

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章将为大家详细讲解有关如何进行iptables的实战分析,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。案例公司有三个部门工程部 2.10-2.2
千家信息网最后更新 2026年11月02日如何进行iptables的实战分析

这篇文章将为大家详细讲解有关如何进行iptables的实战分析,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

案例

公司有三个部门

工程部 2.10-2.20

软件部 2.21-2.30

经理办 2.31-2.40

上班时间 (周一___周五 08:20:00)

工程部 ftp 下班后无限制

软件部 http 不允许非法站点sina ,不允许使用迅雷,连接数最多3个,不允许qq,不允许下载电影,不允许浏览图片,下班后无限制

经理办公室 http qq 都可以,下班后无限制

dmz区域www服务器进行发布

使用iptable+l7 +squid+透明代理实现

分析:

对于本实验可在上班时使用squid透明代理以方便设置用户上网行为控制,同时使用iptables辅助限制用户上网行为. 下班时候采用NAT转换实现无限制. Squid使用透明代理并设置监听地址和端口,各种上网规则. Iptables 使用较为严格的控制默认设置为DROP

实验拓补图如下:

基本设置

设置 SSHD 和 loopback 接口的通过

[root@localhost ~]# iptables -t filter -A INPUT -s 192.168.101.130 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

[root@localhost ~]# iptables -t filter -A OUTPUT -d 192.168.101.130 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

[root@localhost ~]# iptables -t filter -A INPUT -i lo -j ACCEPT

[root@localhost ~]# iptables -t filter -A OUTPUT -o lo -j ACCEPT

[root@localhost ~]# iptables -P INPUT DROP

[root@localhost ~]# iptables -P OUTPUT DROP

[root@localhost ~]# iptables -P FORWARD DROP

[root@localhost ~]# ping 127.0.0.1

测试 ping 127.0.0.1

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.436 ms

64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.045 ms

工程部配置如下 :

配置NAT eth0 出口的NAT 转换

[root@localhost ~]# iptables -t nat -A POSTROUTING -m iprange --src-range 192.168.2.10-192.168.2.20 -m time --weekdays Mon,Tue,Wed,Thu,Fri --timestart 08:00 --timestop 20:00 -p tcp --dport 21 -o eth0 -j MASQUERADE

设置FORWARD 为ACCEPT

[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.10-192.168.2.20 -m time --weekdays Mon,Tue,Wed,Thu,Fri --timestart 08:00 --timestop 20:00 -p tcp --dport 21 -j ACCEPT

[root@localhost ~]# iptables -t filter -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

载入模块

[root@localhost ~]# modprobe ip_nat_ftp

软件部配置如下 :

配置80 端口的重定向为3128

[root@localhost ~]# iptables -t nat -A PREROUTING -m iprange --src-range 192.168.2.21-192.168.2.40 -m time --weekdays Mon,Tue,Wed,Thu,Fri --timestart 08:00 --timestop 20:00 -p tcp --dport 80 -j REDIRECT --to-ports 3128

设置3128 INPUT 为ACCEPT

[root@localhost ~]# iptables -t filter -A INPUT -p tcp --dport 3128 -j ACCEPT

[root@localhost ~]# iptables -t filter -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

允许HTTP 和DNS 出去

[root@localhost ~]# iptables -t filter -I OUTPUT 3 -p tcp --dport 80 -j ACCEPT

[root@localhost ~]# iptables -t filter -I OUTPUT 3 -p udp --dport 53 -j ACCEPT

[root@localhost ~]# iptables -t filter -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

设置客户机DNS 的NAT 转换

[root@localhost ~]# iptables -t nat -A POSTROUTING -m iprange --src-range 192.168.2.21-192.168.2.40 -m time --weekdays Mon,Tue,Wed,Thu,Fri --timestart 08:00 --timestop 20:00 -p udp --dport 53 -o eth0 -j MASQUERADE

设置客户机的DNS 查询可通过

[root@localhost ~]# iptables -t filter -I FORWARD 2 -m iprange --src-range 192.168.2.21-192.168.2.40 -p udp --dport 53 -j ACCEPT

限制迅雷和QQ 聊天

[root@localhost ~]# iptables -t filter -I FORWARD 3 -m iprange --src-range 192.168.2.21-192.168.2.30 -m time --weekdays Mon,Tue,Wed,Thu,Fri --timestart 08:00 --timestop 20:00 -m layer7 --l7proto xunlei -j DROP

[root@localhost ~]# iptables -t filter -I FORWARD 3 -m iprange --src-range 192.168.2.21-192.168.2.30 -m time --weekdays Mon,Tue,Wed,Thu,Fri --timestart 08:00 --timestop 20:00 -m layer7 --l7proto qq -j DROP

设置 squid 代理

修改 squid 的配置文档 设置监听地址和端口并添加如下规则

927 http_port 192.168.2.1:3128 transparent

591 acl badip src 192.168.2.21-192.168.2.30/255.255.255.255

592 acl worktime time 08:00-20:00

593 acl badsite url_regex -i sina

594 acl badcont urlpath_regex -i \.jpg$

595 acl conn3 maxconn 3

596 http_access deny badip worktime badsite

597 http_access deny badip worktime badcont

598 http_access deny badip conn3

设置下班无限制 使用NAT 并允许iptables 规则为通过

[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -m time --weekdays Mon,Tue,Wek,Thu,Fri --timestart 20:01 --timestop 23:59 -o eth0 -j MASQUERADE

[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -m time --weekdays Mon,Tue,Wek,Thu,Fri --timestart 00:00 --timestop 07:59 -o eth0 -j MASQUERADE

[root@localhost ~]# iptables -t filter -I FORWARD 5 -s 192.168.2.0/24 -m time --weekdays Mon,Tue,Wek,Thu,Fri --timestart 00:00 --timestop 07:59 -j ACCEPT

[root@localhost ~]# iptables -t filter -I FORWARD 5 -s 192.168.2.0/24 -m time --weekdays Mon,Tue,Wek,Thu,Fri --timestart 20:01 --timestop 23:59 -j ACCEPT

服务器设置

使用DNAT 并设置iptables 规则为通过防火墙

[root@localhost ~]# iptables -t nat -A PREROUTING -d 192.168.101.25 -p tcp --dport 80 -j DNAT --to 192.168.3.100

[root@localhost ~]# iptables -t filter -I FORWARD 8 -d 192.168.3.100 -p tcp --dport 80 -j ACCEPT

配置 192.68.3.100 的客户机的Apache

[root@localhost ~]# yum -y install httpd

[root@localhost ~]# vim /var/www/html/index.html

Hi ~~ This is my web!

[root@localhost ~]# service httpd start

配置截图 :

关于如何进行iptables的实战分析就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

配置 无限制 规则 代理 分析 客户 客户机 工程 工程部 端口 软件 实战 实战分析 内容 地址 文章 更多 服务器 用户 知识 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 合肥互联网科技公司 互联网科技排名 撒么互联网科技公司 成都仁贤领冠互联网科技有限公司 互联网巨头投资的硬核科技 互联网 科技馆 苏州五指互联网络科技有限 成都互联网科技公司工作好不好 锡山区互联网智能科技产品供应 南京一网一红互联网科技有限公司 新氧科技和美国互联网有关系吗 银川新华互联网科技学校是国营吗 广西泛欧互联网科技有限公司 互联网科技博主张洲 山东蛋芒互联网科技有限公司 陕西彩京云互联网科技有限公司 网互联网络科技 现代科技的标志之一的互联网 科技美学互联网 艺术 互联网 科技节活动 微视频大赛 喔噻互联网科技有限公司 河南真二互联网科技有限公司 期待互联网科技 互联网金融科技平台 天扬科技互联网广告 卓越科技互联网 招聘 河南住房之家互联网科技 上海京穆互联网科技有限公司 江苏互联网智能科技软件 互联网金融金融科技
0