@@ -166,5 +166,247 @@ iptables -L -n --line-numbers
166166iptables -D INPUT 8
167167```
168168
169+ ** 开机启动网络转发规则**
170+
171+ 公网` 210.14.67.7 ` 让内网` 192.168.188.0/24 ` 上网
172+
173+ ``` bash
174+ iptables -t nat -A POSTROUTING -s 192.168.188.0/24 -j SNAT --to-source 210.14.67.127
175+ ```
176+
177+ ** 端口映射**
178+
179+ 本机的 2222 端口映射到内网 虚拟机的22 端口
180+
181+ ``` bash
182+ iptables -t nat -A PREROUTING -d 210.14.67.127 -p tcp --dport 2222 -j DNAT --to-dest 192.168.188.115:22
183+ ```
184+ iptables
185+ ===
186+
187+ Linux上常用的防火墙软件
188+
189+ ## 补充说明
190+
191+ ** iptables命令** 是Linux上常用的防火墙软件,是netfilter项目的一部分。可以直接配置,也可以通过许多前端和图形界面配置。
192+
193+ ### 语法
194+
195+ ```
196+ iptables(选项)(参数)
197+ ```
198+
199+ ### 选项
200+
201+ ```
202+ -t, --table table 对指定的表 table 进行操作, table 必须是 raw, nat,filter,mangle 中的一个。如果不指定此选项,默认的是 filter 表。
203+ -L, --list [chain] 列出链 chain 上面的所有规则,如果没有指定链,列出表上所有链的所有规则。
204+ -F, --flush [chain] 清空指定链 chain 上面的所有规则。如果没有指定链,清空该表上所有链的所有规则。
205+ -A, --append chain rule-specification 在指定链 chain 的末尾插入指定的规则,也就是说,这条规则会被放到最后,最后才会被执行。规则是由后面的匹配来指定。
206+ -D, --delete chain rule-specification -D, --delete chain rulenum 在指定的链 chain 中删除一个或多个指定规则。
207+ -I, --insert chain [rulenum] rule-specification 在链 chain 中的指定位置插入一条或多条规则。如果指定的规则号是1,则在链的头部插入。这也是默认的情况,如果没有指定规则号。
208+ -R, --replace chain rulenum rule-specification 用新规则替换指定链 chain 上面的指定规则,规则号从1开始。
209+ -Z, --zero [chain] :把指定链,或者表中的所有链上的所有计数器清零。
210+ -N, --new-chain chain 用指定的名字创建一个新的链。
211+ -p:指定要匹配的数据包协议类型;
212+ -X, --delete-chain [chain] :删除指定的链,这个链必须没有被其它任何规则引用,而且这条上必须没有任何规则。如果没有指定链名,则会删除该表中所有非内置的链。
213+ -E, --rename-chain old-chain new-chain :用指定的新名字去重命名指定的链。这并不会对链内部照成任何影响。
214+ -P, --policy chain target :为指定的链 chain 设置策略 target。注意,只有内置的链才允许有策略,用户自定义的是不允许的。
215+ -j, --jump target <指定目标> :即满足某条件时该执行什么样的动作。target 可以是内置的目标,比如 ACCEPT,也可以是用户自定义的链。
216+ -s, --source [!] address[/mask] :把指定的一个/一组地址作为源地址,按此规则进行过滤。当后面没有 mask 时,address 是一个地址,比如:192.168.1.1;当 mask 指定时,可以表示一组范围内的地址,比如:192.168.1.0/255.255.255.0。
217+ -d, --destination [!] address[/mask] :地址格式同上,但这里是指定地址为目的地址,按此进行过滤。
218+ -i, --in-interface [!] <网络接口name> :指定数据包的来自来自网络接口,比如最常见的 eth0 。注意:它只对 INPUT,FORWARD,PREROUTING 这三个链起作用。如果没有指定此选项, 说明可以来自任何一个网络接口。同前面类似,"!" 表示取反。
219+ -o, --out-interface [!] <网络接口name> :指定数据包出去的网络接口。只对 OUTPUT,FORWARD,POSTROUTING 三个链起作用。
220+ -h:显示帮助信息;
221+ ```
222+
223+ ** iptables命令选项输入顺序:**
224+
225+ ```
226+ iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作
227+ ```
228+
229+ 表名包括:
230+
231+ - ** raw** :高级功能,如:网址过滤。
232+ - ** mangle** :数据包修改(QOS),用于实现服务质量。
233+ - ** net** :地址转换,用于网关路由器。
234+ - ** filter** :包过滤,用于防火墙规则。
235+
236+ 规则链名包括:
237+
238+ - ** INPUT链** :处理输入数据包。
239+ - ** OUTPUT链** :处理输出数据包。
240+ - ** PORWARD链** :处理转发数据包。
241+ - ** PREROUTING链** :用于目标地址转换(DNAT)。
242+ - ** POSTOUTING链** :用于源地址转换(SNAT)。
243+
244+ 动作包括:
245+
246+ - ** ACCEPT** :接收数据包。
247+ - ** DROP** :丢弃数据包。
248+ - ** REDIRECT** :重定向、映射、透明代理。
249+ - ** SNAT** :源地址转换。
250+ - ** DNAT** :目标地址转换。
251+ - ** MASQUERADE** :IP伪装(NAT),用于ADSL。
252+ - ** LOG** :日志记录。
253+
254+ ``` bash
255+ ┏╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍┓
256+ ┌───────────────┐ ┃ Network ┃
257+ │ table: filter │ ┗━━━━━━━┳━━━━━━━┛
258+ │ chain: INPUT │◀────┐ │
259+ └───────┬───────┘ │ ▼
260+ │ │ ┌───────────────────┐
261+ ┌ ▼ ┐ │ │ table: nat │
262+ │local process│ │ │ chain: PREROUTING │
263+ └ ┘ │ └─────────┬─────────┘
264+ │ │ │
265+ ▼ │ ▼ ┌─────────────────┐
266+ ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ │ ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ │table: nat │
267+ Routing decision └───── outing decision ─────▶│chain: PREROUTING│
268+ ┅┅┅┅┅┅┅┅┅┳┅┅┅┅┅┅┅┅┅ ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ └────────┬────────┘
269+ │ │
270+ ▼ │
271+ ┌───────────────┐ │
272+ │ table: nat │ ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ │
273+ │ chain: OUTPUT │ ┌─────▶ outing decision ◀──────────────┘
274+ └───────┬───────┘ │ ┅┅┅┅┅┅┅┅┳┅┅┅┅┅┅┅┅
275+ │ │ │
276+ ▼ │ ▼
277+ ┌───────────────┐ │ ┌────────────────────┐
278+ │ table: filter │ │ │ chain: POSTROUTING │
279+ │ chain: OUTPUT ├────┘ └──────────┬─────────┘
280+ └───────────────┘ │
281+ ▼
282+ ┏╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍┓
283+ ┃ Network ┃
284+ ┗━━━━━━━━━━━━━━━┛
285+ ```
286+
287+
288+ ### 实例
289+
290+ #### 清除已有iptables规则
291+
292+ ```
293+ iptables -F
294+ iptables -X
295+ iptables -Z
296+ ```
297+
298+ #### 开放指定的端口
299+
300+ ```
301+ iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT #允许本地回环接口(即运行本机访问本机)
302+ iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #允许已建立的或相关连的通行
303+ iptables -A OUTPUT -j ACCEPT #允许所有本机向外的访问
304+ iptables -A INPUT -p tcp --dport 22 -j ACCEPT #允许访问22端口
305+ iptables -A INPUT -p tcp --dport 80 -j ACCEPT #允许访问80端口
306+ iptables -A INPUT -p tcp --dport 21 -j ACCEPT #允许ftp服务的21端口
307+ iptables -A INPUT -p tcp --dport 20 -j ACCEPT #允许FTP服务的20端口
308+ iptables -A INPUT -j reject #禁止其他未允许的规则访问
309+ iptables -A FORWARD -j REJECT #禁止其他未允许的规则访问
310+ ```
311+
312+ #### 屏蔽IP
313+
314+ ```
315+ iptables -A INPUT -p tcp -m tcp -s 192.168.0.8 -j DROP # 屏蔽恶意主机(比如,192.168.0.8
316+ iptables -I INPUT -s 123.45.6.7 -j DROP #屏蔽单个IP的命令
317+ iptables -I INPUT -s 123.0.0.0/8 -j DROP #封整个段即从123.0.0.1到123.255.255.254的命令
318+ iptables -I INPUT -s 124.45.0.0/16 -j DROP #封IP段即从123.45.0.1到123.45.255.254的命令
319+ iptables -I INPUT -s 123.45.6.0/24 -j DROP #封IP段即从123.45.6.1到123.45.6.254的命令是
320+ ```
321+
322+ #### 指定数据包出去的网络接口
323+
324+ 只对 OUTPUT,FORWARD,POSTROUTING 三个链起作用。
325+
326+ ``` bash
327+ iptables -A FORWARD -o eth0
328+ ```
329+
330+ #### 查看已添加的iptables规则
331+
332+ ```
333+ iptables -L -n -v
334+ Chain INPUT (policy DROP 48106 packets, 2690K bytes)
335+ pkts bytes target prot opt in out source destination
336+ 5075 589K ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
337+ 191K 90M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
338+ 1499K 133M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
339+ 4364K 6351M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
340+ 6256 327K ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
341+
342+ Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
343+ pkts bytes target prot opt in out source destination
344+
345+ Chain OUTPUT (policy ACCEPT 3382K packets, 1819M bytes)
346+ pkts bytes target prot opt in out source destination
347+ 5075 589K ACCEPT all -- * lo 0.0.0.0/0 0.0.0.0/0
348+ ```
349+
350+ #### 删除已添加的iptables规则
351+
352+ 将所有iptables以序号标记显示,执行:
353+
354+ ```
355+ iptables -L -n --line-numbers
356+ ```
357+
358+ 比如要删除INPUT里序号为8的规则,执行:
359+
360+ ``` bash
361+ iptables -D INPUT 8
362+ ```
363+
364+ #### 启动网络转发规则
365+
366+ 公网` 210.14.67.7 ` 让内网` 192.168.188.0/24 ` 上网
367+
368+ ``` bash
369+ iptables -t nat -A POSTROUTING -s 192.168.188.0/24 -j SNAT --to-source 210.14.67.127
370+ ```
371+
372+ #### 端口映射
373+
374+ 本机的 2222 端口映射到内网 虚拟机的22 端口
375+
376+ ``` bash
377+ iptables -t nat -A PREROUTING -d 210.14.67.127 -p tcp --dport 2222 -j DNAT --to-dest 192.168.188.115:22
378+ ```
379+
380+ #### 字符串匹配
381+
382+ 比如,我们要过滤所有TCP连接中的字符串` test ` ,一旦出现它我们就终止这个连接,我们可以这么做:
383+
384+ ``` bash
385+ iptables -A INPUT -p tcp -m string --algo kmp --string " test" -j REJECT --reject-with tcp-reset
386+ iptables -L
387+
388+ # Chain INPUT (policy ACCEPT)
389+ # target prot opt source destination
390+ # REJECT tcp -- anywhere anywhere STRING match "test" ALGO name kmp TO 65535 reject-with tcp-reset
391+ #
392+ # Chain FORWARD (policy ACCEPT)
393+ # target prot opt source destination
394+ #
395+ # Chain OUTPUT (policy ACCEPT)
396+ # target prot opt source destination
397+ ```
398+
399+ #### 阻止Windows蠕虫的攻击
400+
401+ ``` bash
402+ iptables -I INPUT -j DROP -p tcp -s 0.0.0.0/0 -m string --algo kmp --string " cmd.exe"
403+ ```
404+
405+ #### 防止SYN洪水攻击
406+
407+ ``` bash
408+ iptables -A INPUT -p tcp --syn -m limit --limit 5/second -j ACCEPT
409+ ```
410+
169411
170412<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ -->
0 commit comments