How to inject a raw L2 packet as an incoming packet to an interface on Linux?

帅比萌擦擦* 提交于 2019-12-22 10:50:46

问题


I need to inject some L2 packets(customized) to an specific interface as incoming packets in Linux to test some applications running on it.

Is there any libraries(python preferred)/examples that can help? I was skimming through the Scrapy library, but then it looks like it can only inject packets to the network as outgoing packets?


回答1:


If you have the native linux bridge module available then you can use it in this way. Create a bridge

brctl addbr <brname>

Now create a virtual eth pair (default names veth0, veth1). veths are connected L2 devices

ip link add type veth
ifconfig veth0 up
ifconfig veth1 <some_ip> up

Now add your specific interface, lets say eth0, and one side of the veth pair to this bridge.

brctl addif <brname> eth0
brctl addif <brname> veth0

This adds both these interfaces to the bridge. Now when you send traffic on veth0, you should be able to get it on eth0 also(based on normal L2 switch functionality). To send traffic on veth0 you simply need to pump traffic into veth1 since both of them are connected internally. So lets say you want to use tcpreplay, just do

tcpreply -i veth1 yourpcap.pcap

Let us know how it goes



来源:https://stackoverflow.com/questions/14407400/how-to-inject-a-raw-l2-packet-as-an-incoming-packet-to-an-interface-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!