-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathgoose.py
More file actions
193 lines (155 loc) · 5.49 KB
/
Copy pathgoose.py
File metadata and controls
193 lines (155 loc) · 5.49 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import struct
import binascii
from scapy.all import *
import BER
class ASNType(object):
tag = ''
def __init__(self, data='', length=0):
pass
def unpack(self, data):
raise NotImplemented()
def pack(self, data):
raise NotImplemented()
def __str__(self):
return str(self.data)
def __repr__(self):
return str(self.data)
class Integer(ASNType):
def __init__(self, data='', length=0):
self.data = BER.unpack_varint(data, length)
def pack(self):
if isinstance(self.data, int):
if self.data <= 255:
return struct.pack('!B', self.data)
elif self.data <= 65535:
return struct.pack('!h', self.data)
else:
return struct.pack('!i', self.data)
if isinstance(self.data, long):
return struct.pack('!l', self.data)
class VisibleString(ASNType):
def __init__(self, data='', length=0):
self.data = data
def __repr__(self):
return "'" + self.data + "'"
def pack(self):
return self.data
class Boolean(ASNType):
ID = 3
def __init__(self, data='', length=0):
self.data = struct.unpack('!b', data)[0]
def __repr__(self):
if self.data:
return "True"
else:
return "False"
def pack(self):
return struct.pack('!b', self.data)
class UTCTime(ASNType):
def __init__(self, data='', length=0):
self.data = struct.unpack('!d', data)[0]
def pack(self):
return struct.pack('!d', self.data)
class UnsignedInteger(ASNType):
def __init__(self, data='', length=0):
self.data = struct.unpack()
class Float(ASNType):
def __init__(self, data='', length=0):
self.data = struct.unpack('!f', data)[0]
def pack(self):
return struct.data('!f', data)
class Real(Float):
pass
class OctetString(ASNType):
def __init__(self, data='', length=0):
self.data = struct.unpack('!d', data)[0]
class BitString(ASNType):
ID = 4
def __init__(self, data='', length=0):
c = {'0': '0000', '1': '0001', '2': '0010',
'3':'0011', '4':'0100', '5':'0101',
'6':'0110', '7':'0111', '8':'1000',
'9':'1001', 'a':'1010', 'b':'1011',
'c':'1100', 'd':'1101', 'e':'1110',
'f':'1111'}
self.padding = struct.unpack('!h', '\x00'+data[:1])[0]
h = binascii.b2a_hex(data[1:])
self.data = ''
for i in h:
self.data += c[i]
def pack(self):
packed_padding = struct.pack('!B', self.padding)
packed_data = struct.pack('!h', int(self.data, 2))
return packed_padding + packed_data
class ObjectID(ASNType):
pass
class BCD(ASNType):
pass
class BooleanArray(ASNType):
pass
class UTF8String(ASNType):
pass
class Data(object):
tag = ''
tagmap = {(128,0,3):('boolean', Boolean),
(128,0,4):('bitstring', BitString),
(128,0,5):('integer', Integer),
(129,0,6):('unsigned', UnsignedInteger),
(128,0,7):('float', Float),
(128,0,8):('real', Real),
(128,0,9):('octetstring', OctetString),
(129,0,10):('visiblestring', VisibleString),
(128,0,12):('binarytime', UTCTime),
(128,0,13):('bcd', BCD),
(129,0,14):('booleanarray', BooleanArray),
(128,0,15):('objID', ObjectID),
(128,0,16):('mMSString', UTF8String),
(128,0,17):('utcstring', UTCTime)}
def __init__(self, data=None, length=0):
self.tagmap[(128,32,1)] = ('array', Data)
self.tagmap[(128,32,2)] = ('structure', Data)
self.data = BER.decoder(data, self.tagmap, decode_as_list=True)
def __getitem__(self, index):
return self.data[index]
def __repr__(self):
return repr(self.data)
def pack(self):
""" This is a hack, and should probably be integrated in to
the BER encoder at some point.
"""
packed_data = ''
for i in self.data:
tag = i.tag[0] + i.tag[1] + i.tag[2]
tag = struct.pack('!B', tag)
package = i.pack()
if len(package) < 128:
length = struct.pack('!B', len(package))
else: # HACK.. this will only support lengths up to 254.
length = struct.pack('!BB', 129, len(package))
packed_data += tag + length + package
return packed_data
class GOOSEPDU(object):
ID = 97
tagmap = {(128,0,0):('gocbRef', VisibleString),
(128,0,1):('timeAllowedToLive', Integer),
(128,0,2):('datSet', VisibleString),
(128,0,3):('goID', VisibleString),
(128,0,4):('t', UTCTime),
(128,0,5):('stNum', Integer),
(128,0,6):('sqNum', Integer),
(128,0,7):('test',Boolean),
(128,0,8):('confRev', Integer),
(128,0,9):('ndsCom', Boolean),
(128,0,10):('numDataSetEntries', Integer),
(128,32,11):('allData', Data)}
def __init__(self, data=None, length=0):
self.__dict__ = BER.decoder(data, self.tagmap)
def pack(self):
return BER.encoder(self.__dict__, self.tagmap)
class GOOSE(Packet):
name = "GOOSE"
fields_desc = [ ShortField("APPID", 3),
ShortField("Length", None),
ShortField("Reserved1", 0),
ShortField("Reserved2", 0),
]