How to inject a raw L2 packet as an incoming packe

2019-05-15 12:09发布

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条回答
迷人小祖宗
2楼-- · 2019-05-15 12:43

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

查看更多
登录 后发表回答