Skip to content

Instantly share code, notes, and snippets.

@jossef
Created June 1, 2017 12:55
Show Gist options
  • Select an option

  • Save jossef/651f49400347fa59ac3d615b826d9c15 to your computer and use it in GitHub Desktop.

Select an option

Save jossef/651f49400347fa59ac3d615b826d9c15 to your computer and use it in GitHub Desktop.
GRE ERSPAN Scapy pcap encapsulator
#! /usr/bin/python
from scapy.layers.inet import *
from scapy.layers.l2 import *
from scapy.all import *
class ERSPAN(Packet):
name = "ERSPAN"
fields_desc = [BitField("version", 1, 4),
BitField("vlan", 0, 12),
BitField("priority", 0, 3),
BitField("unknown2", 0, 1),
BitField("direction", 0, 1),
BitField("truncated", 0, 1),
BitField("span_id", 0, 10),
XIntField("unknown7", 0x00000000)]
def main():
pcap_file_path = '/tmp/input.pcap'
output_file_path = '/tmp/output.pcap'
output_file = PcapWriter(output_file_path, append=False, sync=True)
packets = rdpcap(pcap_file_path)
for packet in packets:
fixed_packet = Ether() / \
IP() / \
GRE(flags=0x1000, proto=0x88be, seqence_number=1796, seqnum_present=1) / \
ERSPAN(version=1, vlan=3900, priority=0, unknown2=0, direction=1, truncated=0, span_id=666, unknown7=0) / \
packet
output_file.write(fixed_packet)
if __name__ == '__main__':
main()
@maulikmaheta18
Copy link

Hey, can you also help me to generate sample script for ERSPAN Type III

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment