防火墙(iptables)

防火墙(iptables)

 

防火墙
三种链:
INPUT
OUTPUT
FORWARDING
PREROUTING
POSTROUTING
防火墙。默认有两层OUTPUT 链表。
INPUT---OUTPUT
PREROUTING--- FORWARDING ---POSTROUTING
表nat,filter
COMMANDS
These options specify the specific action to perform. Only one of them
can be specified on the command line unless otherwise specified below.
For all the long versions of the command and option names, you need t
o
use only enough letters to ensure that iptables can differentiate it
from all other options.
-A, --append chain rule-specification
Append one or more rules to the end of the selected chain. When
the source and/or destination names resolve to more than one
address, a rule will be added for each possible address combina-
tion.
追加一条或者多条规则到指定的链尾部,如果源ip地址或者目标ip地址有多个域名,则规
则应用所有域名。
-D, --delete chain rule-specification
-D, --delete chain rulenum
Delete one or more rules from the selected chain. There are two
versions of this command: the rule can be specified as a number
in the chain (starting at 1 for the first rule) or a rule to
match.
-I, --insert chain [rulenum] rule-specification
Insert one or more rules in the selected chain as the given rule
number. So, if the rule number is 1, the rule or rules are
inserted at the head of the chain. This is also the default if
no rule number is specified.-R, --replace chain rulenum rule-specification
Replace a rule in the selected chain. If the source and/or des-
tination names resolve to multiple addresses, the command will
fail. Rules are numbered starting at 1.
-L, --list [chain]
List all rules in the selected chain. If no chain is selected,
all chains are listed. As every other iptables command, it
applies to the specified table (filter is the default), so NAT
rules get listed by
iptables -t nat -n -L
Please note that it is often used with the -n option, in order
to avoid long reverse DNS lookups. It is legal to specify the
-Z (zero) option as well, in which case the chain(s) will be
atomically listed and zeroed. The exact output is affected by
the other arguments given. The exact rules are suppressed until
you use
iptables -L -v
-F, --flush [chain]
Flush the selected chain (all the chains in the table if none is
given). This is equivalent to deleting all the rules one by
one.
-Z, --zero [chain]
Zero the packet and byte counters in all chains. It is legal to
specify the -L, --list (list) option as well, to see the coun-
ters immediately before they are cleared. (See above.)
-N, --new-chain chain
Create a new user-defined chain by the given name. There must
be no target of that name already.
-X, --delete-chain [chain]
Delete the optional user-defined chain specified. There must be
no references to the chain. If there are, you must delete or
replace the referring rules before the chain can be deleted.
The chain must be empty, i.e. not contain any rules. If no
argument is given, it will attempt to delete every non-builtin
chain in the table.
-P, --policy chain target
Set the policy for the chain to the given target. See the sec-
tion TARGETS for the legal targets. Only built-in (non-user-defined) chains can have policies, and neither built-in nor
user-defined chains can be policy targets.
-E, --rename-chain old-chain new-chain
Rename the user specified chain to the user supplied name. This
is cosmetic, and has no effect on the structure of the table.
-h Help. Give a (currently very brief) description of the command
syntax.
PARAMETERS
The following parameters make up a rule specification (as used in the
add, delete, insert, replace and append commands).
-p, --protocol [!] protocol
The protocol of the rule or of the packet to check. The speci-
fied protocol can be one of tcp, udp, icmp, or all, or it can be
a numeric value, representing one of these protocols or a dif-
ferent one. A protocol name from /etc/protocols is also
allowed. A "!" argument before the protocol inverts the test.
The number zero is equivalent to all. Protocol all will match
with all protocols and is taken as default when this option is
omitted.
-s, --source [!] address[/mask]
Source specification. Address can be either a network name, a
hostname (please note that specifying any name to be resolved
with a remote query such as DNS is a really bad idea), a network
IP address (with /mask), or a plain IP address. The mask can be
either a network mask or a plain number, specifying the number
of 1’s at the left side of the network mask. Thus, a mask of 24
is equivalent to 255.255.255.0. A "!" argument before the
address specification inverts the sense of the address. The flag
--src is an alias for this option.
-d, --destination [!] address[/mask]
Destination specification. See the description of the -s
(source) flag for a detailed description of the syntax. The
flag --dst is an alias for this option.
-j, --jump target
This specifies the target of the rule; i.e., what to do if the
packet matches it. The target can be a user-defined chain
(other than the one this rule is in), one of the special builtintargets which decide the fate of the packet immediately, or an
extension (see EXTENSIONS below). If this option is omitted in
a rule (and -g is not used), then matching the rule will have no
effect on the packet’s fate, but the counters on the rule will
be incremented.
-g, --goto chain
This specifies that the processing should continue in a user
specified chain. Unlike the --jump option return will not con-
tinue processing in this chain but instead in the chain that
called us via --jump.
-i, --in-interface [!] name
Name of an interface via which a packet was received (only for
packets entering the INPUT, FORWARD and PREROUTING chains).
When the "!" argument is used before the interface name, the
sense is inverted. If the interface name ends in a "+", then
any interface which begins with this name will match. If this
option is omitted, any interface name will match.
-o, --out-interface [!] name
Name of an interface via which a packet is going to be sent (for
packets entering the FORWARD, OUTPUT and POSTROUTING chain
s).
When the "!" argument is used before the interface name, the
sense is inverted. If the interface name ends in a "+", then
any interface which begins with this name will match. If this
option is omitted, any interface name will match.
[!] -f, --fragment
This means that the rule only refers to second and further frag-
ments of fragmented packets. Since there is no way to tell the
source or destination ports of such a packet (or ICMP type),
such a packet will not match any rules which specify them. When
the "!" argument precedes the "-f" flag, the rule will only
match head fragments, or unfragmented packets.
-c, --set-counters PKTS BYTES
This enables the administrator to initialize the packet and byte
counters of a rule (during INSERT, APPEND, REPLACE operations).
OTHER OPTIONS
The following additional options can be specified:-v, --verbose
Verbose output. This option makes the list command show the
interface name, the rule options (if any), and the TOS masks.
The packet and byte counters are also listed, with the suffix
’K’, ’M’ or ’G’ for 1000, 1,000,000 and 1,000,000,000 multipli-
ers respectively (but see the -x flag to change this). For
appending, insertion, deletion and replacement, this causes
detailed information on the rule or rules to be printed.
-n, --numeric
Numeric output. IP addresses and port numbers will be printed
in numeric format. By default, the program will try to display
them as host names, network names, or services (whenever appli-
cable).
-x, --exact
Expand numbers. Display the exact value of the packet and byte
counters, instead of only the rounded number in K’s (multiples
of 1000) M’s (multiples of 1000K) or G’s (multiples of 1000M).
This option is only relevant for the -L command.
--line-numbers
When listing rules, add line numbers to the beginning of each
rule, corresponding to that rule’s position in the chain.
--modprobe=command
When adding or inserting rules into a chain, use command to load
any necessary modules (targets, match extensions, etc).
[root@uplooking linux-2.6.29-rc5]# iptables -t filter -L
[root@uplooking linux-2.6.29-rc5]# iptables -t filter -F
ACCEPT all — anywhere 192.168.122.0/24 state RELATED,ESTAB
LISHED
Tcp
Udp
Icmp
8 ---请求
0 ---回应
[root@uplooking linux-2.6.29-rc5]iptable -t filter -A INPUT -p icmp —icmp-ty
pe 8 -j DROP
[root@uplooking linux-2.6.29-rc5]iptable -t filter -A INPUT -p tcp --dport 80
-s 192.168.0.0/24 -j DROP[root@uplooking linux-2.6.29-rc5]# iptables -t filter -vnL
[root@uplooking linux-2.6.29-rc5]# iptables -t filter -D INPUT 1
[root@uplooking linux-2.6.29-rc5]# iptables -t filter -D INPUTR -s 192.168.0.0/
24 -j DROP
防火墙规则从上到下读取,匹配则执行该规则动作。
NAT网络地址转换
[root@uplooking linux-2.6.29-rc5]# iptables -t nat -A POSTROUTING -s 192.16
8.0.0/24 -o ppp0 -j SNAT —to 202.106.0.1 ---固定公网ip 地址
-j MASQUERADE ---自动帮定动态 ip 地址
[root@uplooking linux-2.6.29-rc5]# echo 1 > /proc/sys/net/ipv4/ip_forward
---系统内核开启ip 地址转换功能。
[root@uplooking linux-2.6.29-rc5]#iptables -t filter -A INPUT -I eth0 -j DROP
[root@uplooking linux-2.6.29-rc5]#iptables -t filter -A INPUT -I eth0 -j REJECT
[root@uplooking linux-2.6.29-rc5]# cat /proc/net/ip* ---查看网络相关的东西
Winhex


iptables
# iptables -L
filter:过滤器
nat:地址转换
mangle:对包进行修改(属性,数值等)
注:表下是链,链负责在不同位置起作用
# iptables -t mangle -L (5条链)
显示其下所有链的规则
顺序:mangle,nat,filter
# ip ro sh ta all
查看全部路由表
默认指定filter 表
# iptables -t table -A INPUT,FORWARD,OUTPUT -s 192.168.0.0/24 -j DROP
-t:表格式(filter,nat,mangle)
-A:指定参数(input,forword,output,pre~,post~)
-s:包描述(源地址)
-j:操作目标
-I:网卡进入
-o:网卡出口
-p:运行协议 tcp udp icmp
-D
-R
-n:关闭解析
-P:指定默认规则(默认为全部报允许)
# iptables -P DROP (改变默认规则,兜底规则)
-Z:计数器清零
# iptables -Z INPUT 计数清零
# iptables -L INPUT -nv 查看详细数据
匹配:match
-s
-d
-i
-o
-p
注:icmp --icmp-type
tcp,udp --drop --sport目标:
-j DROP ACCEPT
匹配扩展规则:match extensions
account:统计规则(统计流量,规则数量等,默认不能用)
# ls /lib/modules/2.6.18-53.el5/kernel/net/netfilter/
防火墙内核模块,匹配扩展参数需要有对应模块支持
注:小写的是匹配扩展模块,大写的是目标扩展模块
ping包长度:56(84)字节,84:ping 加ip 报头字节总合(length看的)
# ping -s 72 192.168.0.254(ping包看的 72)
PING 192.168.0.254 (192.168.0.254) 72(100) bytes of data.
length:匹配包长度(看的是84)用来防御小包攻击
# iptables -A INPUT 192.168.0.100-192.168.0.254/24 -m length ! --length 100:200 -j DROP
除了 100-200 的包外不能访问
注:防火墙只能防御小包攻击,大包要在内存中(组装包部分)时设定
limit:pps频率限制(每秒钟通过多少个包)
# iptables -A INPUT -m -limit --limit 10/minute -p icmp --icmp-type 8 -j ACCEPT 每分钟只
能通过10个包
# iptables -A INPUT -p icmp --icmp-type 8 -j DROP
超过的丢弃
注:ping自己查看丢包量(平均6 秒过一个)
限速:
pps*mtu 网卡速度
令牌筒流量限速:可发生爆发流量(有筒来存储令牌,如遇爆发流量可用筒中的存储令牌进
行一次通过,此流量)
筒的容量也要限定
limit-burst 限定令牌筒的容量(默认为 5个令牌)
频率限制:
# iptables -A OUTPUT -s 192.168.0.254 -p tcp --sport 80 -m -limit --limit 5000/second -j
ACCEPT 流量允许0.254ip,5000个包每秒通过
# iptables -A INTPUT -s 192.168.0.254 -p tcp --dport 80 -m -limit --limit 5000/second -j DROP
拒绝超过5000 的0.254ip
注:connlimit:限制连接个数(tcp的连接数,不可直接使用)iprange:匹配网段范围
# iptables -A FORWARD -m iprange --src-range 192.168.0.100-192.168.0.254 -j DROP
length:匹配包长度
mangle 打标记:
# iptables -t mangle -A PREROUTING -s 192.168.0.254 -j MARK --set-mark 1
打标记(1为数字,可随意设定)
# iptables -t mangle -A PREROUTING -s 127.0.0.1 -j MARK --set-mark 2
# iptables -A INPUT -m mark --mark 2 -j ACCEPT 匹配标记
标记为2的可通过,其他的不行
owner:对本地产生的数据包进行设定,但此模块不稳定(只可在 output 设定)
state:连接状态匹配(连接状态防火墙)
# iptables -A INPUT -s 192.168.0.254 -m state --state ESTABLISHED -j ACCEPT
# iptables -A INPUT -s 192.168.0.254 -j DROP
拒绝 254ping到123,但123可以 ping 到他
intables -A INPUT -p icmp --icmp-type 8 -j DROP
连接状态跟踪列表:
# cat /proc/net/ip_conntrack (只记录tcp,udp,记录与我连接的ip)
string:检索匹配包中的某些字符串(在squid中设定此功能更好,在此会相当慢)
tos:服务类型,可设定服务处理的优先级(如ssh,sendmail 谁先后)
在ip报头中,8bit 字符,
# iptables -A INPUT -s 192.168.0.254 -p tcp --dport 22 -m tos --tos 16 -j DROP
只允许22传输,不允许22的ssh 登录
目标扩展:
-j DROP ACCEPT MARK REJECT
DROP (丢弃)
REJECT(回绝)
# iptables -A INPUT -s 192.168.0.254 -j REJECT --reject-with icmp-host-unreachable 无
法ping到0.254
log(可把访问记录写到日志里)# iptables -A INPUT -s 0.0.0.0/0 -d 192.168.0.254 --dport 22 -j LOG -p tcp --log-level info
--log-prefix "hjf:"
对象为内核对象的info 级别写入message 日之中
--log-prefix "hjf:" 打标记(为了以后方便查看)
在log/message 日志之中
# awk '{for (i=1;i<NF;i++) {if ($i ~ /^kernel/ && $(i+1) ~ /iptables/) { print $i,$(i+10)}}}'
/var/log/messages | sort | uniq
# awk '{for (i=1;i<NF;i++) {if ($i ~ /^SRC/) { print $i,$(i+10)}}}' /var/log/messages | sort | uniq
iptables监控流量(-Z计数参数)
# nmap -sP 192.168.0.0/24 -n | grep ^Host 检索网络中的活动ip
# for i in `nmap -sP 192.168.0.0/24 -n | awk '{if ($1 ~ /^Host/) {print $2} }'`; do if iptables -A
INPUT -s $i -j ACCEPT; fi;done
网络安全:
传输层:tcp/udp
tcp 的Dos攻击(拒绝服务攻击,针对协议的攻击)
网络层:iptables
链路层:ebtables (用不多)
othernet(以太网协议,用 MAC) ATM ppp FEEI(光纤) x.25(骨干网祯中继) :
协议
注:mac 通过ip使用时,arp负责给ip进行解析(通过 mac地址进行所绑定的 ip
进行解析)
用pppoe 可解决 arp欺骗
Dos:拒绝访问
# echo 1 > /proc/sys/net/ipv4/tcp_synack_retries 修改后生效(重试次数)
# vim /etc/sysctl.conf 下写入
net.ipv4.tcp_synack_retries = 1 即可开启(此为不用半连接池改用cpu来计算,可杜绝 Dos
攻击)
# sysctl -p 查看列表
DDos:分布式拒绝访问(海量鸡肉一起进攻)
DrDos:分布式反射拒绝攻击(多在lan 中实现,洪水ping包,占用对方带宽)
# cat /proc/sys/net/ipv4/icmp_echo_ignore_all 允许ping
# cat /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts 允许广播ping
注:这个特殊,改为0 后启用
ping -b 192.168.0.254 广播 ping
ping -f 洪水ping

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值