2011-02-20 39 views
3

我在内核模块中使用netfilter钩子。我希望能够捕获由scapy创建的数据包。Netfilter和Scapy

挂钩以及通过scapy生成的数据包都在同一台物理主机上运行。似乎没有可用的netfilter钩子能够捕获数据包。

我也尝试从虚拟机内发送相同的数据包,但这也不起作用。

我怀疑问题与环回iterface上的所有内容有关,因为它全部在同一个盒子上。

我当然可以用两个物理主机去,但这是不幸的是不可能的现在:(

static unsigned int out_hook(unsigned int hooknum, 
       struct sk_buff *skb, 
       const struct net_device *in, 
       const struct net_device *out, 
       int (*okfn)(struct sk_buff *)) 
{ 
    sock_buff = skb; 

    if (!sock_buff) { 
     return NF_ACCEPT; 
    } else { 
      ip_header = (struct iphdr *)skb_network_header(sock_buff); 
     if (!ip_header) { 
      return NF_ACCEPT; 
     } else { 
      if (ip_header->protocol == IPPROTO_TCP) { 
       th = (struct tcphdr *)(skb_transport_header(sock_buff)+sizeof(struct iphdr)); 

       printk(KERN_INFO "[LOCAL_OUT] %d.%d.%d.%d:%d -> %d.%d.%d.%d:%d\n", ip_header->saddr & 0x000000FF, (ip_header->saddr & 0x0000FF00) >> 8,(ip_header->saddr & 0x00FF0000) >> 16,(ip_header->saddr & 0xFF000000) >> 24, th->source, ip_header->daddr & 0x000000FF, (ip_header->daddr & 0x0000FF00) >> 8,(ip_header->daddr & 0x00FF0000) >> 16,(ip_header->daddr & 0xFF000000) >> 24, th->dest); 

       unsigned int len = sock_buff->len - sizeof(struct tcphdr) - sizeof(struct iphdr); 
       printk(KERN_INFO "\t [skbuf->len]=%d", sock_buff->len); 
       printk(KERN_INFO "\t [skbuf->data_len]=%d", sock_buff->data_len); 


       return NF_ACCEPT; 

      } else { 
       return NF_ACCEPT; 
      } 
     } 
    } 
} 

以上是钩。

#!/usr/bin/env python 

import sys 
sys.path.append('/usr/local/bin') 

import time 
from threading import Thread 
from scapy.all import * 
from hashlib import sha1, md5 
import random 
import crypt 

conf.iface='wlan0' 


packet = IP(dst="192.168.0.104")/TCP(sport=1234, dport=2222)/Raw("testtest") 
send(packet) 

以上是发送。 PY

+1

如果您没有使用AF_LOCAL套接字进行传输,则回送没有任何关系。得到的代码? – user611775

+0

上面显示的代码 – eeknay

回答

1

Sniffing with Scapy

[[email protected] ~]$ sudo python 
Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14) 
[GCC 4.3.2] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from scapy.all import sniff 
>>> from scapy.all import wrpcap 
>>> foo = sniff(filter="icmp", count=3) 
>>> wrpcap("icmppaks.pcap", foo) 
>>> quit() 
[[email protected] ~]$ tshark -r icmppaks.pcap 
    1 0.000000 192.0.2.178 -> 192.0.2.6 ICMP Echo (ping) request 
    2 0.000065 192.0.2.6 -> 192.0.2.178 ICMP Echo (ping) reply 
    3 1.004224 192.0.2.178 -> 192.0.2.6 ICMP Echo (ping) request 
[[email protected] ~]$