Python – How to create a sample IPSec packet using python scapy

How to create a sample IPSec packet using python scapy… here is a solution to the problem.

How to create a sample IPSec packet using python scapy

I’ll create a VPN tunnel between the two routers. So I need to send some raw packets generated by scapy through the VPN tunnel. Basically I need to generate some raw IPSec packets.

Solution

This is scapy’s IPSec test file https://github.com/secdev/scapy/blob/master/test/ipsec.uts

It provides many examples, such as

:

import socket

p = IP(src='1.1.1.1', dst='2.2.2.2')
p /= TCP(sport=45012, dport=80)
p /= Raw('testdata')
p = IP(raw(p))
p

sa = SecurityAssociation(ESP, spi=0x222,
                         crypt_algo='NULL', crypt_key=None,
                         auth_algo='NULL', auth_key=None)

e = sa.encrypt(p)
e

assert(isinstance(e, IP))
assert(e.src == '1.1.1.1' and e.dst == '2.2.2.2')
assert(e.chksum != p.chksum)
assert(e.proto == socket. IPPROTO_ESP)
assert(e.haslayer(ESP))
assert(not e.haslayer(TCP))
assert(e[ESP].spi == sa.spi)
assert(b'testdata' in e[ESP].data)

d = sa.decrypt(e)
d

Related Problems and Solutions