-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcapgen.py
executable file
·317 lines (283 loc) · 8.64 KB
/
pcapgen.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
# Generate dummy ETI/EOBI PCAP files
#
# Examples:
#
# ETI:
#
# ./pcapgen.py | text2pcap -T 1337,19043 -4 192.168.0.23,192.168.0.60 - foo.pcap
#
# EOBI:
#
# ./pcapgen.py | text2pcap -u 1337,59001 -4 192.168.0.23,192.168.0.60 - bar.pcap
#
# SPDX-FileCopyrightText: © 2021 Georg Sauthoff <mail@gms.tf>
# SPDX-License-Identifier: GPL-3.0-or-later
import argparse
# NB: imported via main()
#import eti.v9_1 as eti
#import xti.v9_1 as xti
#import eobi.v9_1 as eobi
import sys
def mk_reject(text, seq, bs):
m = eti.Reject()
m.NRResponseHeaderME.MsgSeqNum = seq
m.SessionRejectReason = eti.SessionRejectReason.OTHER
m.VarText = text.encode()
m.VarTextLen = len(m.VarText)
m.update_length()
n = m.pack_into(bs)
return memoryview(bs)[:n]
def mk_unaligned(text, seq, bs):
m = eti.Reject()
m.NRResponseHeaderME.MsgSeqNum = seq
m.SessionRejectReason = eti.SessionRejectReason.PRICE_NOT_REASONABLE
m.VarText = text.encode()
m.VarTextLen = len(m.VarText)
m.MessageHeaderOut.BodyLen += m.VarTextLen
n = m.pack_into(bs)
return memoryview(bs)[:m.MessageHeaderOut.BodyLen]
def mk_exec_report(bs):
m = eti.OrderExecReportBroadcast()
m.RBCHeaderME.PartitionID = 23
m.OrderID = 815
m.ClOrdID = 1337
# m.SecurityID unset although required
m.ExecID = 1633861270328920429
m.LeavesQty = 7
m.CumQty = 8
m.CxlQty = 9
m.OrderQty = 10
m.MarketSegmentID = 36
m.NoLegExecs = 3
m.NoFills = 1
m.NoLegs = 2
c = eti.LegOrdGrpComp()
c.LegPositionEffect = eti.LegPositionEffect.OPEN
m.LegOrdGrp.append(c)
c = eti.LegOrdGrpComp()
c.LegPositionEffect = eti.LegPositionEffect.CLOSE
m.LegOrdGrp.append(c)
c = eti.FillsGrpComp()
c.FillExecID = 666
m.FillsGrp.append(c)
m.InstrmntLegExecGrp.append(eti.InstrmntLegExecGrpComp())
c = eti.InstrmntLegExecGrpComp()
c.LegSide = eti.LegSide.BUY
c.LegLastQty = 1234
m.InstrmntLegExecGrp.append(c)
m.InstrmntLegExecGrp.append(eti.InstrmntLegExecGrpComp())
m.update_length()
n = m.pack_into(bs)
return memoryview(bs)[:n]
def mk_xti_mod_order(bs):
# i.e. malformed when parsing as Derivatives-ETI
m = xti.ModifyOrderSingleRequest()
m.FreeText1 = b'cash rules'
m.FreeText2 = b'everything'
m.FreeText4 = b'around'
m.FIXClOrdID = b'me'
n = m.pack_into(bs)
return memoryview(bs)[:n]
def mk_mod_order(bs, freetext1):
m = eti.ModifyOrderSingleRequest()
m.RequestHeader.MsgSeqNum = 5
m.RequestHeader.SenderSubID = 2323
m.OrderQty = 99000
m.MarketSegmentID = 8989
m.SimpleSecurityID = 232
m.FreeText1 = freetext1.encode()
m.FreeText3 = b'\0lol'
n = m.pack_into(bs)
return memoryview(bs)[:n]
def mk_logon(bs):
m = eti.LogonRequest()
m.RequestHeader.MsgSeqNum = 1
m.PartyIDSessionID = 2323
m.Password = b'geheim23'
m.ApplicationSystemName = b'skynet'
m.ApplicationSystemVersion = b'1.2.3'
m.ApplicationSystemVendor = b'ACME'
n = m.pack_into(bs)
return memoryview(bs)[:n]
def mk_login(bs):
m = eti.UserLoginRequest()
m.RequestHeader.MsgSeqNum = 2
m.Username = 1337
m.Password = b'besttrader'
n = m.pack_into(bs)
return memoryview(bs)[:n]
def mk_invalid_template(bs, tid):
m = eti.UserLoginRequest()
m.MessageHeaderIn.TemplateID = tid
m.RequestHeader.MsgSeqNum = 2
m.Username = 1234
m.Password = b'geheim'
n = m.pack_into(bs)
return memoryview(bs)[:n]
def mk_invalid_enum(bs):
m = eti.DeleteAllOrderRequest()
m.RequestHeader.SenderSubID = 88888
m.SecurityID = 815
m.Side = 23 # NB: Invalid enumeration value!
n = m.pack_into(bs)
return memoryview(bs)[:n]
# i.e. check the message whose usage information in the usages
# array comes last in eti2wireshark generated code
def mk_user_logout_response(bs):
m = eti.UserLogoutResponse()
m.ResponseHeader.RequestTime = 1637015805000000000
m.ResponseHeader.SendingTime = 1637015806000000000
m.ResponseHeader.MsgSeqNum = 711
n = m.pack_into(bs)
return memoryview(bs)[:n]
def mk_heartbeat(bs):
ph = eobi.PacketHeader()
ph.ApplSeqNum = 4712
ph.MarketSegmentID = 1337
ph.PartitionID = 7
ph.CompletionIndicator = eobi.CompletionIndicator.COMPLETE
ph.DSCP = 0
ph.TransactTime = 1633864284123456789
n = ph.pack_into(bs)
m = eobi.Heartbeat()
m.LastMsgSeqNumProcessed = 4711
n = m.pack_into(bs, n)
return memoryview(bs[:n])
def mk_summary(bs):
ph = eobi.PacketHeader()
ph.ApplSeqNum = 4713
ph.MarketSegmentID = 1337 # product id
ph.PartitionID = 7
ph.CompletionIndicator = eobi.CompletionIndicator.COMPLETE
ph.DSCP = 0x3c # exclude ECN field
ph.TransactTime = 1633864284123456789
n = ph.pack_into(bs)
m = eobi.ExecutionSummary()
m.MessageHeader.MsgSeqNum = 23
m.SecurityID = 23 # instrument id
m.ExecID = 1633864284123000789
m.LastQty = 400123
m.AggressorSide = eobi.AggressorSide.BUY
m.LastPx = 9902422
# NB: m.RestingCxlQty unset although it's required
n = m.pack_into(bs, n)
return memoryview(bs[:n])
def mk_empty_inssum(bs):
ph = eobi.PacketHeader()
ph.ApplSeqNum = 4713
ph.MarketSegmentID = 1337 # product id
ph.PartitionID = 7
ph.CompletionIndicator = eobi.CompletionIndicator.COMPLETE
ph.DSCP = 0x4c
ph.TransactTime = 1633864284123480230
n = ph.pack_into(bs)
m = eobi.InstrumentSummary()
m.MessageHeader.MsgSeqNum = 4711
m.SecurityID = 42 # instrument id
m.SoldOutIndicator = eobi.SoldOutIndicator.SOLDOUT
assert m.NoMDEntries == 0
# NB: in contrast to ETI, EOBI messages with variable-length arrays are
# never shortened
m.MessageHeader.BodyLen = m.sizes[1]
m.pack_into(bs, n)
n += m.sizes[1]
m.NoMDEntries = 0xff # NO_VALUE
m.MarketCondition = eobi.MarketCondition.STRESSED
m.pack_into(bs, n)
n += m.sizes[1]
return memoryview(bs[:n])
def mk_counter_overflow(bs):
bs.__init__(1024)
ph = eobi.PacketHeader()
ph.ApplSeqNum = 4714
ph.MarketSegmentID = 1337 # product id
ph.PartitionID = 7
ph.CompletionIndicator = eobi.CompletionIndicator.COMPLETE
ph.DSCP = 0x5c
ph.TransactTime = 1633864284123480238
n = ph.pack_into(bs)
m = eobi.MassInstrumentStateChange()
# NB: m.MessageHeader.MsgSeqNum unset although it's required
m.SecurityMassTradingStatus = eobi.SecurityMassTradingStatus.INTRADAYAUCTION
m.NoRelatedSym = 25 # NB: max: 24 => overflow
m.MessageHeader.BodyLen = m.sizes[1]
m.pack_into(bs, n)
n += m.MessageHeader.BodyLen
return memoryview(bs[:n])
def dump(u):
print(f'000000 {u.hex(sep=" ", bytes_per_sep=1)}\n')
def gen_eti():
buf = bytearray(1024)
tail = bytearray(1024)
u = mk_reject('Invalid login credentials!', 23, buf)
dump(u)
u = mk_exec_report(buf)
dump(u)
# test tcp packet reassembly
v = mk_logon(buf)
w = mk_login(tail)
dump(v[:123])
dump(bytearray(v[123:]) + w[:23])
dump(w[23:])
u = mk_xti_mod_order(buf)
dump(u)
u = mk_mod_order(buf, 'test stray chars')
dump(u)
u = mk_invalid_template(buf, 23)
dump(u)
u = mk_invalid_template(buf, 10105)
dump(u)
u = mk_invalid_enum(buf)
dump(u)
u = mk_user_logout_response(buf)
dump(u)
u = mk_unaligned('show unaligned', 666, buf)
dump(u)
def mk_overused(bs):
ph = eobi.PacketHeader()
ph.MessageHeader.MsgSeqNum = 0x23
ph.ApplSeqNum = 4733
ph.MarketSegmentID = 90211
ph.PartitionID = 6
ph.DSCP = 0
n = ph.pack_into(bs)
m = eobi.Heartbeat()
m.LastMsgSeqNumProcessed = 14141
n = m.pack_into(bs, n)
return memoryview(bs[:n])
def gen_eobi():
buf = bytearray(1024)
u = mk_heartbeat(buf)
dump(u)
u = mk_summary(buf)
dump(u)
u = mk_counter_overflow(buf)
dump(u)
u = mk_empty_inssum(buf)
dump(u)
u = mk_overused(buf)
dump(u)
def parse_args():
p = argparse.ArgumentParser(description='Generate dummy ETI/EOBI PCAP files')
p.add_argument('--eobi', action='store_true', help='generate EOBI PCAP instead of ETI')
p.add_argument('--release', '-r', default='9.1', help='parse eti/eobi v9.0 (default: %(default)s)')
args = p.parse_args()
return args
def main():
args = parse_args()
if args.release:
global eti
global xti
global eobi
import importlib
rel = args.release.replace('.', '_')
eobi = importlib.import_module(f'eobi.v{rel}')
eti = importlib.import_module(f'eti.v{rel}')
xti = importlib.import_module(f'xti.v{rel}')
if args.eobi:
gen_eobi()
else:
gen_eti()
if __name__ == '__main__':
sys.exit(main())