forked from scy-phy/scapy-cip-enip
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenip_tcp.py
90 lines (81 loc) · 4.67 KB
/
enip_tcp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Copyright (c) 2015 Nicolas Iooss, SUTD
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Ethernet/IP over TCP scapy dissector"""
import struct
from scapy import all as scapy_all
import enip
import enip_cpf
# Moved all ENIP classes to file enip.py --> Keeps all EtherNet/IP Level
# processing in the same file rather than splitting based on upper layer - MED
scapy_all.bind_layers(scapy_all.TCP, enip.ENIP_PACKET, dport=44818)
scapy_all.bind_layers(scapy_all.TCP, enip.ENIP_PACKET, sport=44818)
scapy_all.bind_layers(enip_cpf.CPF_AddressDataItem, enip.ENIP_ConnectionAddress, type_id=0xA1)
scapy_all.bind_layers(enip_cpf.CPF_DataItem, enip.ENIP_ConnectionPacket, type_id=0xB1)
scapy_all.bind_layers(enip_cpf.CPF_Item, enip.ENIP_ConnectionAddress, type_id=0x00A1)
scapy_all.bind_layers(enip_cpf.CPF_Item, enip.ENIP_ConnectionPacket, type_id=0x00B1)
if __name__ == '__main__':
# Test building/dissecting packets
# Build a raw packet over ENIP
pkt = scapy_all.Ether(src='01:23:45:67:89:ab', dst='ba:98:76:54:32:10')
pkt /= scapy_all.IP(src='192.168.1.1', dst='192.168.1.42')
pkt /= scapy_all.TCP(sport=10000, dport=44818)
# Modified to reflect changes in code names and locations - MED
pkt /= enip.ENIP_PACKET()
pkt /= enip.ENIP_SendUnitData(Encapsulated_CPF_packet = enip_cpf.ENIP_CPF(items=[
enip_cpf.CPF_AddressDataItem() / enip.ENIP_ConnectionAddress(connection_id=1337),
enip_cpf.CPF_DataItem() / enip.ENIP_ConnectionPacket(sequence=4242) / scapy_all.Raw(load='test'),
]))
# pkt /= enip_cpf.ENIP_CPF('', Address_Item = enip_cpf.CPF_AddressDataItem() / enip.ENIP_ConnectionAddress(connection_id=1337),
# Data_Item = enip_cpf.CPF_DataItem() / enip.ENIP_ConnectionPacket(sequence=4242) / scapy_all.Raw(load='test'))
# pkt /= enip_cpf.ENIP_CPF(items=[
# enip_cpf.CPF_AddressDataItem() / enip.ENIP_ConnectionAddress(connection_id=1337),
# enip_cpf.CPF_DataItem() / enip.ENIP_ConnectionPacket(sequence=4242) / scapy_all.Raw(load='test'),
# ])
# Build!
data = str(pkt)
print ' '.join("{:02x}".format(ord(c)) for c in data)
pkt = scapy_all.Ether(data)
pkt.show()
# Test the value of some fields; new test setup due to moving classes - MED
assert pkt[enip.ENIP_PACKET].session == 0
assert pkt[enip.ENIP_PACKET].status == 0
assert pkt[enip.ENIP_PACKET].command_id == 0x70
assert pkt[enip.ENIP_PACKET].length == 26
assert pkt[enip_cpf.ENIP_CPF].count == 2
assert pkt[enip_cpf.ENIP_CPF].items[0].type_id == 0x00a1
assert pkt[enip_cpf.ENIP_CPF].items[0].length == 4
assert pkt[enip_cpf.ENIP_CPF].items[0].payload == pkt[enip.ENIP_ConnectionAddress]
assert pkt[enip.ENIP_ConnectionAddress].connection_id == 1337
assert pkt[enip_cpf.ENIP_CPF].items[1].type_id == 0x00b1
assert pkt[enip_cpf.ENIP_CPF].items[1].length == 6
assert pkt[enip_cpf.ENIP_CPF].items[1].payload == pkt[enip.ENIP_ConnectionPacket]
assert pkt[enip.ENIP_ConnectionPacket].sequence == 4242
assert pkt[enip.ENIP_ConnectionPacket].payload.load == 'test'
# assert pkt[enip.ENIP_SendUnitData].items[0].type_id == 0x00a1
# assert pkt[enip.ENIP_SendUnitData].items[0].length == 4
# assert pkt[enip.ENIP_SendUnitData].items[0].payload == pkt[enip.ENIP_ConnectionAddress]
# assert pkt[enip.ENIP_ConnectionAddress].connection_id == 1337
# assert pkt[enip.ENIP_SendUnitData].items[1].type_id == 0x00b1
# assert pkt[enip.ENIP_SendUnitData].items[1].length == 6
# assert pkt[enip.ENIP_SendUnitData].items[1].payload == pkt[enip.ENIP_ConnectionPacket]
# assert pkt[enip.ENIP_ConnectionPacket].sequence == 4242
# assert pkt[enip.ENIP_ConnectionPacket].payload.load == 'test'