Linux架构day02(案例)

案例

案例1:只允许22端口可以访问,其他端口全部无法访问。
iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

案例2:只允许22,80,443端口可以访问,其他端口全部无法访问。
iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 80 -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 443 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

案例3:只允许22,80,443端口可以访问,其他端口全部无法访问,但是本机可以访问百度。

案例4:要求使用192.168.15.81能够通过22端口链接,但是其他的不行
iptables -t filter -A INPUT -p TCP -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

案例5:只允许192.168.15.71能够通过22端口链接,其他的不行。
iptables -t filter -A INPUT -p TCP -s 192.168.15.71 -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

案例6:要求192.168.15.71对外部不可见
iptables -t filter -A INPUT -p TCP -d 192.168.15.71 -j DROP

案例7:要求使用eth0网卡的所有请求全部拒绝
iptables -t filter -A INPUT -p TCP -i etho -j DROP

使用172.16.1.71登录进来的窗口,不允许访问百度。
iptables -t filter -I OUTPUT -p TCP -o eth1 -j DROP

案例8:要求访问服务器的8080端口转发至80端口
iptables -t nat -A PREROUTING -p TCP --dport 8080 -j REDIRECT --to-port 80

案例9:要求只允许windows通过ssh连接192.168.15.81,其他的拒绝
iptables -t filter -A INPUT -p TCP -s 192.168.15.1 -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 22 -j DROP

  知识储备:
  查看本机端口占用的命令:
  netstat -nutlpA

模块

拓展iptables的功能的。

-m : 指定模块

1、连续匹配多个端口(multiport)

--dports : 指定多个端口(不同端口之间以逗号分割,连续的端口使用冒号分割)。

2、指定一段连续的ip地址范围(iprange)
--src-range from[-to]: 源地址范围
--dst-range from[-to] 目标地址范围

3、匹配指定字符串(string)
--string pattern # 指定要匹配的字符串
--algo {bm|kmp} # 匹配的查询算法

4、根据时间段匹配报文(time)
--timestart hh:mm[:ss] # 开始时间
--timestop hh:mm[:ss] # 结束时间
--monthdays day[,day...] # 指定一个月的某一天
--weekdays day[,day...] # 指定周 还是 周天

5、禁ping, 默认本机无法ping别人 、别人无法ping自己
--icmp-type {type[/code]|typename}
echo-request (8) 请求
echo-reply (0) 回应

6、限制链接数,并发连接数(connlimit)
--connlimit-upto n # 如果现有连接数小于或等于 n 则 匹配
--connlimit-above n # 如果现有连接数大于n 则匹配

7、针对 报文速率 进行限制。 秒、分钟、小时、天。

--limit rate[/second|/minute|/hour|/day] # 报文数量
--limit-burst number # 报文数量(默认:5)

案例

1、要求将22,80,443以及30000-50000之间所有的端口向外暴露,其他端口拒绝

iptables -t filter -A INPUT -p TCP -m multiport --dports 22,80,443,30000:50000 -j ACCEPT
iptables -f filter -A INPUT -p TCP -j DROP

2、要求访问数据包中包含HelloWorld的数据不允许通过。
iptables -t filter -A INPUT -p TCP -m string --string "HelloWorld" --algo kmp -j DROP

3、要求192.168.15.1 - 192.168.15.10之间的所有IP能够连接192.168.15.81,其他拒绝
iptables -t filter -A INPUT -p TCP -m iprange --src-range 192.168.15.1-192.168.15.10 -j ACCEPT
iptables -f filter -A INPUT -p TCP -j DROP

4、要求每天的12到13之间,不允许访问
iptables -t filter -A INPUT -p TCP -m time --timestart 4:00 --timestop 5:00 -j DROP

必须使用UTC时间

5、要求别人不能ping本机,但是本机可以ping别人
iptables -t filter -A INPUT -p TCP -m icmp --icmp-type "echo-request" -j DROP

6、要求主机连接最多有2个
iptables -t filter -A INPUT -p TCP --dport 22 -m connlimit --connlimit-above 2 -j DROP

7、要求限制速率在500k/s左右
iptables -t filter -A INPUT -p TCP -m limit 333/s -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

posted @ 2021-12-27 17:12  杰尼龟无所畏惧  阅读(106)  评论(0)    收藏  举报