arptables工具的使用及举例

1 arptables 简介

arptables 可以当作是 linux 下的 ARP 防火墙。

arptables 是一个用户空间,用于管理内核中的 ARP 规则表,规则检查处理的是 ARP 数据帧。

arptables 类似 iptables,但比 iptables 简单,它需要载入内核模块 arptable_filter。

正常情况下,arptable_filter 只有一个表 filter ,不指定 -t 表名 时默认就是 filter 表。filter 表有两个链,一个是INPUT,表示外面发进来的 ARP 包;另外一个是 OUTPUT,表示本机发出的 ARP 包。

内建的动作:

  • ACCEPT 放行 ARP 包
  • DROP 丢掉 ARP 包
  • CONTINUE 继续下一规则
  • RETURN 不在这个链中继续进行匹配,返回到上一条链的下一条规则

2 使用方法

$ arptables -h
arptables v0.0.3.4

Usage: arptables -[AD] chain rule-specification [options]
       arptables -[RI] chain rulenum rule-specification [options]
       arptables -D chain rulenum [options]
       arptables -[LFZ] [chain] [options]
       arptables -[NX] chain
       arptables -E old-chain-name new-chain-name
       arptables -P chain target [options]
       arptables -h (print this help information)

Commands:
Either long or short options are allowed.
  --append  -A chain		Append to chain								追加规则
  --delete  -D chain		Delete matching rule from chain				删除指定规则
  --delete  -D chain rulenum
				Delete rule rulenum (1 = first) from chain				删除指定位置的规则
  --insert  -I chain [rulenum]
				Insert in chain as rulenum (default 1=first)			插入规则
  --replace -R chain rulenum
				Replace rule rulenum (1 = first) in chain				替换规则
  --list    -L [chain]		List the rules in a chain or all chains		列出规则
  --flush   -F [chain]		Delete all rules in  chain or all chains	清空规则
  --zero    -Z [chain]		Zero counters in chain or all chains		清空所有计数
  --new     -N chain		Create a new user-defined chain				新建用户自定义链
  --delete-chain
            -X [chain]		Delete a user-defined chain 				删除用户自定义链
  --policy  -P chain target
				Change policy on chain to target						将链上的策略更改为target
  --rename-chain
            -E old-chain new-chain
				Change chain name, (moving any references)				重命名链

Options:
  --source-ip	-s [!] address[/mask]
				source specification									源地址
  --destination-ip -d [!] address[/mask]
				destination specification								目的地址
  --source-mac [!] address[/mask]										源MAC
  --destination-mac [!] address[/mask]									目的MAC
  --h-length   -l   length[/mask] hardware length (nr of bytes)			硬件长度
  --opcode code[/mask] operation code (2 bytes)							操作码
  --h-type   type[/mask]  hardware type (2 bytes, hexadecimal)			硬件类型
  --proto-type   type[/mask]  protocol type (2 bytes)					协议类型
  --in-interface -i [!] input name[+]
				network interface name ([+] for wildcard)				收到数据包的网络接口
  --out-interface -o [!] output name[+]
				network interface name ([+] for wildcard)				发送数据包的网络接口
  --jump	-j target
				target for rule (may load target extension)				规则的目标
  --match	-m match
				extended match (may load extension)
  --numeric	-n		numeric output of addresses and ports 				ip地址和端口号使用数字输出
  --table	-t table	table to manipulate (default: `filter')			要操作的表,默认为 filter
  --verbose	-v		verbose mode										详细模式
  --line-numbers		print line numbers when listing					打印行号
  --exact	-x		expand numbers (display exact values)
  --modprobe=<command>		try to insert modules using this command 	尝试使用此命令插入模块
  --set-counters PKTS BYTES	set the counter during insert/append
[!] --version	-V		print package version.							打印版本号

 opcode strings: 
 1 = Request
 2 = Reply
 3 = Request_Reverse
 4 = Reply_Reverse
 5 = DRARP_Request
 6 = DRARP_Reply
 7 = DRARP_Error
 8 = InARP_Request
 9 = ARP_NAK
 hardware type string: 1 = Ethernet
 protocol type string: 0x800 = IPv4

mangle target v0.0.3.4 options:
--mangle-ip-s IP address
--mangle-ip-d IP address
--mangle-mac-s MAC address
--mangle-mac-d MAC address
--mangle-target target (DROP, CONTINUE or ACCEPT -- default is ACCEPT)

Standard v0.0.3.4 options:
(If target is DROP, ACCEPT, RETURN or nothing)

3 举例

  1. arptables -F:清除 filter 所有规则

  2. arptables -L -n:列表 filter 所有规则

  3. 防止某些 ARP 欺骗

    用作 ARP 防火墙,防止某些 ARP 欺骗,要完全防止 ARP 欺骗只能在网关和本机上都互相绑定 MAC。

    假设网关 MAC 是 00:0C:29:C7:01:13,IP 是 192.168.1.1

    发进来的 ARP 包中,如果源 IP 是 192.168.1.1,源 MAC 不是 00:0C:29:C7:01:13 的,丢弃。规则如下:

    $ sudo arptables -A INPUT --src-ip 192.168.1.1 --src-mac ! 00:0C:29:C7:01:13 -j DROP
    

    另外,如果源 MAC 是 00:0C:29:C7:01:13,但是源 IP 不是 192.168.1.1,丢弃,规则如下:

    $ sudo arptables -A INPUT --src-ip ! 192.168.1.1 --src-mac 00:0C:29:C7:01:13 -j DROP
    

    这两条规则很好解决了某些 ARP 欺骗,还有很多灵活的规则,如可以设置只允许网关和某些主机的 ARP 包通过,可以根据源 IP、MAC 或者两结合判断。

    注意:arptables 有一个缺点就是没有日志记录功能,所以在我的另一篇文章中有介绍可以使用 ebtables 来替代 arptables 进行 arp 欺骗的防御。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半砖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值