手机版
你好,游客 登录 注册
背景:
阅读新闻

iptables 高级学习笔记

[日期:2012-12-24] 来源:Linux社区  作者:gzh0222 [字体:大 中 小]

对方没有回应的服务,回包 udp port-unreachable

 

RELATED

A------------->B port 12345(端口未开放) NEW

<-------------- icmp port-unreachable RELATED

 

nc (网络连接) -u (udp,不加-u,TCP) 自己 IP -l 端口 (不加-l,就写端口,连接别人端口)

不接收远端日志

[root@node1 ~]# tcpdump -i eth0 -nnhost node2

tcpdump: verbose output suppressed,use -v or -vv for full protocol decode

listening on eth0, link-type EN10MB(Ethernet), capture size 96 bytes

19:26:16.309934 IP 192.168.0.202.514> 192.168.0.201.514: SYSLOG user.notice, length: 18

19:26:16.419374 IP 192.168.0.201 >192.168.0.202: ICMP 192.168.0.201 udp port 514

unreachable, length 54

==============================================================================

ttl

[root@node1 ~]# iptables -t filter -AINPUT -s 192.168.0.202 -m ttl --ttl-eq64 -j DROP

[root@node1 ~]# cat/proc/sys/net/ipv4/ip_default_ttl

64

iptables -m ttl -h

[root@node2 ~]# echo 128 >/proc/sys/net/ipv4/ip_default_ttl

[root@node2 ~]# ping 192.168.0.201 -c1

[root@node1 ~]# tcpdump -i eth0 -nnvhost 192.168.0.202

tcpdump: listening on eth0, link-typeEN10MB (Ethernet), capture size 96 bytes

20:07:11.305825 IP (tos 0x0, ttl 128,id 0, offset 0, flags [DF], proto: ICMP (1), length: 84)

192.168.0.202 > 192.168.0.201:ICMP echo request, id 1806, seq 1, length 64

20:07:11.305880 IP (tos 0x0, ttl 64, id 47246, offset 0, flags [none], proto:ICMP (1), length: 84)

192.168.0.201 > 192.168.0.202:ICMP echo reply, id 1806, seq 1, length 64

===============================================================================

tos type of service

iptables -m tos -h

tos 0 一般服务(大多数,可以抓包看)

tos 2 最小开销服务 没有

tos 4 最大可靠性 没有

tos 8 最大吞吐量

下载 ftp 20

tos 16 最小延时 tcp pushtelnet ssh ftp 2

抓包发现

ssh tos 0

16

scp tos 0 8

scp 可以 ssh 不行

iptables -A INPUT -p tcp --dport 22-m tos 16 -j DROP

DROP/REJECT

===================================

[root@localhost ~]# iptables -tfilter -A INPUT -p tcp --dport 22 -j DROP

[root@localhost ~]# tcpdump -i eth0-nn host 172.16.1.1 and port 22

tcpdump: verbose output suppressed,use -v or -vv for full protocol decode

listening on eth0, link-type EN10MB(Ethernet), capture size 96 bytes

23:03:53.029197 IP 172.16.1.1.45313> 172.16.1.6.22: S 2666911189:2666911189(0) win 5840

<mss 1460,sackOK,timestamp165299736 0,nop,wscale 3>

23:03:56.110705 IP 172.16.1.1.45313> 172.16.1.6.22: S 2666911189:2666911189(0) win 5840

<mss 1460,sackOK,timestamp165302737 0,nop,wscale 3>

23:04:02.123340 IP 172.16.1.1.45313> 172.16.1.6.22: S 2666911189:2666911189(0) win 5840

<mss 1460,sackOK,timestamp165308737 0,nop,wscale 3>

23:04:14.139680 IP 172.16.1.1.45313> 172.16.1.6.22: S 2666911189:2666911189(0) win 5840

<mss 1460,sackOK,timestamp165320737 0,nop,wscale 3>

23:04:38.468138 IP 172.16.1.1.45313> 172.16.1.6.22: S 2666911189:2666911189(0) win 5840

<mss 1460,sackOK,timestamp165344738 0,nop,wscale 3>

23:05:27.726803 IP 172.16.1.1.45313> 172.16.1.6.22: S 2666911189:2666911189(0) win 5840

<mss 1460,sackOK,timestamp165392738 0,nop,wscale 3>

 


[root@localhost ~]# iptables -tfilter -A INPUT -p tcp --dport 22 -j REJECT --reject-with tcp-reset

[root@localhost ~]# tcpdump -i eth0-nn host 172.16.1.1 and port 22

tcpdump: verbose output suppressed,use -v or -vv for full protocol decode

listening on eth0, link-type EN10MB(Ethernet), capture size 96 bytes

23:10:05.690273 IP 172.16.1.1.44300> 172.16.1.6.22: S 3060689565:3060689565(0) win 5840

<mss 1460,sackOK,timestamp165668951 0,nop,wscale 3>

23:10:05.690645 IP 172.16.1.6.22 >172.16.1.1.44300: R 0:0(0) ack 3060689566 win 0

==============================================================================

网络地址转换(NAT,Network Address Translation)属接入广域网(WAN)技术,是一种将私有(保留)地址转化为合法 IP 地址的转换技术,它被广泛应用于各种类型 Internet 接入方式和各种类型的网络中。原因很简单,NAT 不仅完美地解决了 IP 地址不足的问题,而且还能够有效地避免来自网络外部的攻击,隐藏并保护网络内部的计算机。

透明转发

Client<------------------------------------->NAT Server <----------------------------------> WEB Server

192.168.1.1 192.168.1.2-----1.1.1.1 1.1.1.2

Clinet: route add default gw192.168.1.254 dev eth0

NAT Server: echo 1 >/proc/sys/net/ipv4/ip_forward

WEB Server: route add default gw1.1.1.1 dev eth0

SNAT 公司上网

Client<------------------------------------->NAT Server <------------------------------------> WEB Server

192.168.1.1 eth0:192.168.1.254 eth1:1.1.1.1 1.1.1.2

Clinet: route add default gw192.168.1.254 dev eth0

NAT Server: echo 1 >/proc/sys/net/ipv4/ip_forward

NAT Server: iptables -t nat -A POSTROUTING-p tcp --dport 80 -j SNAT --to-source 1.1.1.1

NAT Server: iptables -t nat -APOSTROUTING -o eth0 -j MASQUERADE (适用于 DHCP)

 


DNAT 发布内网的一台服务器

WEBServer<----------------------------------> NAT Server<--------------------------------> Client

192.168.1.1 eth0:192.168.1.254 eth1:1.1.1.1 1.1.1.2

WEB Server: route add default gw192.168.1.254 dev eth0

NAT Server: iptables -t nat -APREROUTING -d 1.1.1.1 -p tcp --dport 80 -j DNAT --to-des

192.168.1.1:80

iptables -t nat -A PREROUTING -p tcp--dport 80 -j REDIRECT --to-ports 3128

端口地址重定向 透明代理用的规则

===============================================================================

mark 打标记 用 mangle 表

iptables -t mangle -A PREROUTING -mttl --ttl-eq 64 -j MARK --set-mark 10

iptables -t mangle -A PREROUTING -mttl --ttl-eq 123 -j MARK --set-mark 20

iptables -t filter -A FORWARD -m mark--mark 10 -j ACCEPT

iptables -t filter -A FORWARD -m mark--mark 20 -j DROP

打标记的位置很重要

表优先级 mangle --------> nat ------>filter

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款