0

I have setup a DNS server on a machine. I want to capture the DNS replies before the machine sends out, and change some fields in it and then send the packet.

I am only able to change fields in the packet my pcap code(written in C) captures, which seems like a copy, as the original packet is also transmitted.

I tried iptables to drop packets originating from the machine, but it drops the pcap injected packets as well.

Is there any way out of this?

thank you

4

1 回答 1

1

如果您正在寻找仅 pcap 的解决方案,您将不得不拦截 DNS 请求数据包,对其进行检查,并在 DNS 服务器回复之前组装正确的回复。这似乎并不可靠,因为如果 DNS 服务器缓存了一个条目,它可能会在您的自定义代码组装数据包并将其发送出去之前进行回复。

最可靠的方法是编写一个作为 netfilter 钩子的内核模块。Netfilter 钩子能够检查数据包并在数据包离开机器之前的几个点影响对它的处理。将其挂接到 NF_IP_LOCAL_OUT 级别。然后,您可以检查传出数据包,看看它是否是符合您标准的 DNS 回复。下一部分我还没有完成,但是由于您可以直接访问 skb(套接字缓冲区)作为自定义挂钩函数的输入参数,因此您可以在此处修改数据包并返回 NF_ACCEPT 以将响应传递给客户端. 如果您需要对请求本身进行一些处理,您可以连接到 NF_IP_LOCAL_IN 并以多种方式处理它,包括将其传递给用户空间程序。

Google 上有很多 Linux 内核编程示例(搜索:Linux Kernel Module Programming)以及 netfilter hook 示例。

于 2010-04-21T15:42:38.077 回答