python - Scapy - insert packet layer between two other layers -
i doing little networking project using scapy library python. project involves sniffing in packets, , shimming new layer between layers 3 , 4.
using guide, http://www.secdev.org/projects/scapy/doc/build_dissect.html
i able create new packet layer. can add layer on top of existing packet doing like,
packet = newlayer()/packet
and newlayer() layer placed below ip layer. want, however, sandwich new layer between layers 3 , 4 (instead of below ip). can't seem figure out easy way accomplish this.
i know can create new packet , like,
packet = ether()/ip()/newlayer()/tcp()
however since, want insert layer packets i've sniffed, i'd modify original packet instead of creating new packet scratch.
any appreciated!
here's example shows how inject dot1q()
header between layer 1 , layer 2 (counting ether()
layer 0):
>>> pkt = ether() / dot1q() / ip() / tcp() >>> payload = pkt.getlayer(1).payload >>> payload <ip frag=0 proto=tcp |<tcp |>> >>> pkt.getlayer(1).remove_payload() >>> pkt <ether type=n_802_1q |<dot1q |>> >>> newpkt = pkt / dot1q() / payload >>> newpkt <ether type=n_802_1q |<dot1q type=n_802_1q |<dot1q type=ipv4 |<ip frag=0 proto=tcp |<tcp |>>>>> >>>
there may easier way, above easy enough think.