打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
用scapy在python中编写一个以太网桥

我想做这样的事情:

            10.1.1.0/24          10.1.2.0/24 ------------         ------------         ------------ |            |       |            |       |            ||            |       |            |       |            ||     A    d  -------  e   B    f  -------  g   C      ||            |       |            |       |            ||            |       |            |       |            | ------------         ------------         ------------     d              e           f           g    10.1.1.1       10.1.1.2    10.1.2.1    10.1.2.2

这样Acan就可以通过B向C发送数据包了.

我尝试通过在B上运行一个可以嗅探端口e和f的scapy程序来构建这个东西,并且在每种情况下修改数据包中的目标IP和MAC地址,然后通过另一个接口发送它.就像是:

my_macs = [get_if_hwaddr(i) for i in get_if_list()]pktcnt = 0dest_mac_address = discover_mac_for_ip(dest_ip) # output_mac = get_if_hwaddr(output_interface)def process_packet(pkt):    # ignore packets that were sent from one of our own interfaces    if pkt[Ether].src in my_macs:        return    pktcnt  = 1    p = pkt.copy()    # if this packet has an IP layer, change the dst field    # to our final destination    if IP in p:        p[IP].dst = dest_ip    # if this packet has an ethernet layer, change the dst field    # to our final destination. We have to worry about this since    # we're using sendp (rather than send) to send the packet.  We    # also don't fiddle with it if it's a broadcast address.    if Ether in p        and p[Ether].dst != 'ff:ff:ff:ff:ff:ff':        p[Ether].dst = dest_mac_address        p[Ether].src = output_mac    # use sendp to avoid ARP'ing and stuff    sendp(p, iface=output_interface)sniff(iface=input_interface, prn=process_packet)

然而,当我运行这个东西(完整源码here)时,各种疯狂的事情开始发生……一些数据包通过,我甚至得到一些响应(用ping测试)但是有一些类型的反馈循环导致要发送一堆重复的数据包……

有什么想法在这里发生了什么?尝试这样做是否很疯狂?

我有点怀疑反馈循环是由于B正在对数据包进行自己的一些处理这一事实…有什么方法可以阻止操作系统在我嗅探后处理数据包?

解决方法:

这样做有点疯狂,但花费你的时间并不是一种糟糕的方式.你会学到很多有趣的东西.然而,您可能想要考虑将数据包挂得更低 – 我不认为scapy能够实际拦截数据包 – 所有libpcap都设置为promisc并让你看到一切,所以你和内核都得到了相同的东西.如果您转身并重新发送,那可能是您的数据包风暴的原因.

但是,您可以设置一些创意防火墙规则,将每个接口彼此分开并以此方式处理数据包,或者使用类似divert sockets的内容来实际将数据包从内核中分离出来,以便您可以使用它们.

来源:https://www.icode9.com/content-1-435101.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
盘点一款Python发包收包利器——scapy
linux下网络监听与发送数据包的方法(即libpcap、libnet两种类库的使用方法)
Linux下抓包工具tcpdump使用介绍
python scapy网络嗅探
学习tcpdump
tcpdump 使用详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服