forked from IdahoLabResearch/AcCCS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEVSE.py
762 lines (642 loc) · 26.8 KB
/
EVSE.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
"""
Copyright 2023, Battelle Energy Alliance, LLC, ALL RIGHTS RESERVED
This class is used to emulate a EVSE when talking to an PEV. Handles level 2 SLAC communications
and level 3 UDP and TCP communications to the electric vehicle.
"""
# need to do this to import the custom SECC and V2G scapy layer
import sys, os
sys.path.append("./external_libs/HomePlugPWN")
sys.path.append("./external_libs/V2GInjector/core")
from threading import Thread
from layers.SECC import *
from layers.V2G import *
from layerscapy.HomePlugGP import *
from XMLBuilder import XMLBuilder
from EXIProcessor import EXIProcessor
from EmulatorEnum import *
from NMAPScanner import NMAPScanner
import xml.etree.ElementTree as ET
import binascii
from scapy.layers.l2 import Ether # adjustment MicroNova
from scapy.layers.inet6 import IPv6, UDP, TCP, ICMPv6ND_NS, ICMPv6ND_NA, ICMPv6NDOptDstLLAddr # adjustment MicroNova
#from smbus import SMBus # adjustment MicroNova, to disable HardWare J1772 Pilot Signal
import argparse
import socket
from scapy.all import StreamSocket
class EVSE:
def __init__(self, args):
self.mode = RunMode(args.mode[0]) if args.mode else RunMode.FULL
self.iface = args.interface[0] if args.interface else "eth1"
self.sourceMAC = args.source_mac[0] if args.source_mac else get_if_hwaddr(self.iface) #"00:1e:c0:f2:6c:a0" # adjustment MicroNova
#ip6_addr = [addr for addr, _, iface in in6_getifaddr() if iface == self.iface and not addr.startswith("fe80")][0] # ip address # adjustment MicroNova
self.ip6_addr_ll = [addr for addr, _, iface in in6_getifaddr() if iface == self.iface and addr.startswith("fe80")][0] # ip local link address # adjustment MicroNova
self.sourceIP = args.source_ip[0] if args.source_ip else self.ip6_addr_ll #"fe80::21e:c0ff:fef2:6ca0" # adjustment MicroNova
# adjustment MicroNova
# A port number in the range of Dynamic Ports (49152-65535) as defined in IETF RFC 6335 are allowed for TCP.
self.sourcePort = args.source_port[0] if args.source_port else 49153 # 25565 # adjustment MicroNova
self.NID = args.NID[0] if args.NID else b"\x9c\xb0\xb2\xbb\xf5\x6c\x0e"
self.NMK = args.NMK[0] if args.NMK else b"\x48\xfe\x56\x02\xdb\xac\xcd\xe5\x1e\xda\xdc\x3e\x08\x1a\x52\xd1"
self.protocol = Protocol(args.protocol[0]) if args.protocol else Protocol.DIN
self.nmapMAC = args.nmap_mac[0] if args.nmap_mac else ""
self.nmapIP = args.nmap_ip[0] if args.nmap_ip else ""
self.nmapPorts = []
if args.nmap_ports:
for arg in args.nmap_port[0].split(','):
if "-" in arg:
i1,i2 = arg.split("-")
for i in range(int(i1), int(i2)+1):
self.nmapPorts.append(i)
else:
self.nmapPorts.append(int(arg))
if args.modified_cordset:
self.modified_cordset = True
else:
self.modified_cordset = False
self.destinationMAC = None
self.destinationIP = None
self.destinationPort = None
self.exi = EXIProcessor(self.protocol)
self.slac = _SLACHandler(self)
self.tcp = _TCPHandler(self)
# I2C bus for relays
# self.bus = SMBus(1) # adjustment MicroNova
# Constants for i2c controlled relays
self.I2C_ADDR = 0x20
self.CONTROL_REG = 0x9
self.EVSE_CP = 0b1
self.EVSE_PP = 0b1000
self.ALL_OFF = 0b0
# Start the emulator
def start(self):
# Initialize the I2C bus for wwrite
# self.bus.write_byte_data(self.I2C_ADDR, 0x00, 0x00)
self.toggleProximity()
self.doSLAC()
self.tcp.sourcePort = self.sourcePort # adjustment MicroNova
self.tcp.destinationIP = self.slac.destinationIP # adjustment MicroNova
self.tcp.destinationPort = self.destinationPort # adjustment MicroNova
self.tcp.destinationMAC = self.slac.destinationMAC # adjustment MicroNova
self.doTCP()
# If NMAP is not done, restart connection
if not self.tcp.finishedNMAP:
print("INFO (EVSE): Attempting to restart connection...")
self.start()
# Close the circuit for the proximity pins
def closeProximity(self):
if self.modified_cordset:
print("INFO (EVSE): Closing CP/PP relay connections")
# self.bus.write_byte_data(self.I2C_ADDR, self.CONTROL_REG, self.EVSE_PP | self.EVSE_CP)
else:
print("INFO (EVSE): Closing CP relay connection")
# self.bus.write_byte_data(self.I2C_ADDR, self.CONTROL_REG, self.EVSE_CP)
# Close the circuit for the proximity pins
def openProximity(self):
print("INFO (EVSE): Opening CP/PP relay connections")
# self.bus.write_byte_data(self.I2C_ADDR, self.CONTROL_REG, self.ALL_OFF)
# Opens and closes proximity circuit with a delay
def toggleProximity(self, t: int = 5):
self.openProximity()
time.sleep(t)
self.closeProximity()
# Starts TCP/IPv6 thread that handles layer 3 comms
def doTCP(self):
self.tcp.start()
print("INFO (EVSE): Done TCP")
# Starts SLAC thread that handles layer 2 comms
def doSLAC(self):
self.slac.start()
self.slac.sniffThread.join()
print("INFO (EVSE): Done SLAC")
# Handles all SLAC communications
class _SLACHandler:
def __init__(self, evse: EVSE):
self.evse = evse
self.iface = self.evse.iface
self.sourceMAC = self.evse.sourceMAC
self.sourceIP = self.evse.sourceIP
self.sourcePort = self.evse.sourcePort
self.NID = self.evse.NID
self.NMK = self.evse.NMK
self.timeout = 8
self.stop = False
# Starts SLAC process
def start(self):
self.stop = False
print("INFO (EVSE): Sending SET_KEY_REQ")
sendp(self.buildSetKey(), iface=self.iface, verbose=0)
self.sniffThread = Thread(target=self.startSniff)
self.sniffThread.start()
self.timeoutThread = Thread(target=self.checkForTimeout)
self.timeoutThread.start()
def checkForTimeout(self):
self.lastMessageTime = time.time()
while True:
if self.stop:
break
if time.time() - self.lastMessageTime > self.timeout:
print("INFO (EVSE): SLAC timed out, resetting connection...")
self.evse.toggleProximity()
self.lastMessageTime = time.time()
def startSniff(self):
sniff(iface=self.iface, prn=self.handlePacket, stop_filter=self.stopSniff)
def stopSniff(self, pkt):
if pkt.haslayer("SECC_RequestMessage"):
self.handshake() # adjustment MicroNova
print("INDO (EVSE): Recieved SECC_RequestMessage")
self.evse.destinationMAC = pkt[Ether].src # adjustment MicroNova
self.destinationMAC = pkt[Ether].src # adjustment MicroNova
# use this to send 3 secc responses incase car doesnt see one
self.destinationIP = pkt[IPv6].src
self.destinationPort = pkt[UDP].sport
Thread(target=self.sendSECCResponse).start()
self.stop = True
return self.stop
# adjustment MicroNova
def handshake(self, syn = None):
print("INFO (PEV): TCP Handshake start")
full_host_address = self.sourceIP + f"%{self.iface}"
resolve = socket.getaddrinfo(full_host_address, self.sourcePort, socket.AF_INET6, socket.SOCK_STREAM, 0, 0)
sockaddr = ()
(family, socktype, proto, _, sockaddr) = resolve[0]
print(f"INFO (PEV): TCP Socket is {sockaddr}")
self.conn = socket.socket(family, socktype)
try:
self.conn.bind(sockaddr)
self.conn.listen(1)
except socket.error as e:
print("INFO (PEV): TCP Handshake failed")
raise e
print("INFO (PEV): TCP Handshake successed")
def sendSECCResponse(self):
time.sleep(0.2)
for i in range(3):
print("INFO (EVSE): Sending SECC_ResponseMessage")
sendp(self.buildSECCResponse(), iface=self.iface, verbose=0)
def handlePacket(self, pkt):
if pkt[Ether].type != 0x88E1 or pkt[Ether].src == self.sourceMAC:
return
self.lastMessageTime = time.time()
if pkt.haslayer("CM_SLAC_PARM_REQ"):
print("INFO (EVSE): Recieved SLAC_PARM_REQ")
self.destinationMAC = pkt[Ether].src
self.runID = pkt[CM_SLAC_PARM_REQ].RunID
print("INFO (EVSE): Sending CM_SLAC_PARM_CNF")
sendp(self.buildSlacParmCnf(), iface=self.iface, verbose=0)
if pkt.haslayer("CM_MNBC_SOUND_IND") and pkt[CM_MNBC_SOUND_IND].Countdown == 0:
print("INFO (EVSE): Recieved last MNBC_SOUND_IND")
print("INFO (EVSE): Sending ATTEN_CHAR_IND")
sendp(self.buildAttenCharInd(), iface=self.iface, verbose=0)
if pkt.haslayer("CM_SLAC_MATCH_REQ"):
print("INFO (EVSE): Recieved SLAC_MATCH_REQ")
print("INFO (EVSE): Sending SLAC_MATCH_CNF")
sendp(self.buildSlacMatchCnf(), iface=self.iface, verbose=0)
def buildSlacParmCnf(self):
ethLayer = Ether()
ethLayer.src = self.sourceMAC
ethLayer.dst = self.destinationMAC
homePlugAVLayer = HomePlugAV()
homePlugAVLayer.version = 0x01
# Parameters copied from packet #13 in BMW-i3-Plugin-ChargeStart-UserStop.pcapng
homePlugLayer = CM_SLAC_PARM_CNF()
homePlugLayer.MSoundTargetMAC = "ff:ff:ff:ff:ff:ff"
homePlugLayer.NumberMSounds = 0x0A
homePlugLayer.TimeOut = 0x06
homePlugLayer.ResponseType = 0x01
homePlugLayer.ForwardingSTA = self.destinationMAC
homePlugLayer.RunID = self.runID
# padding?
rawLayer = Raw()
rawLayer.load = b"\x00" * 16
responsePacket = ethLayer / homePlugAVLayer / homePlugLayer / rawLayer
return responsePacket
def buildAttenCharInd(self):
ethLayer = Ether()
ethLayer.src = self.sourceMAC
ethLayer.dst = self.destinationMAC
homePlugAVLayer = HomePlugAV()
homePlugAVLayer.version = 0x01
# Parameters copied from packet #29 in BMW-i3-Plugin-ChargeStart-UserStop.pcapng
homePlugLayer = CM_ATTEN_CHAR_IND()
homePlugLayer.ApplicationType = 0x00
homePlugLayer.SecurityType = 0x00
homePlugLayer.SourceAdress = self.destinationMAC
homePlugLayer.RunID = self.runID
homePlugLayer.NumberOfSounds = 0x0A
# TODO: deal with number of groups and average attenuations
# Does the number of groups change?
homePlugLayer.NumberOfGroups = 58
attens = [
26,
25,
26,
28,
25,
27,
34,
33,
33,
36,
31,
31,
31,
31,
30,
29,
29,
28,
27,
26,
25,
23,
22,
22,
21,
20,
24,
27,
31,
36,
41,
45,
45,
38,
32,
29,
29,
31,
32,
32,
32,
34,
35,
35,
35,
35,
35,
35,
34,
38,
39,
39,
40,
40,
39,
41,
42,
57,
]
groups = []
for e in attens:
g = HPGP_GROUP()
g.group = e
groups.append(g)
homePlugLayer.Groups = groups
responsePacket = ethLayer / homePlugAVLayer / homePlugLayer
return responsePacket
def buildSlacMatchCnf(self):
ethLayer = Ether()
ethLayer.src = self.sourceMAC
ethLayer.dst = self.destinationMAC
homePlugAVLayer = HomePlugAV()
homePlugAVLayer.version = 0x01
slacVars = SLAC_varfield_cnf()
slacVars.EVMAC = self.destinationMAC
slacVars.EVSEMAC = self.sourceMAC
slacVars.RunID = self.runID
slacVars.NetworkID = self.NID
slacVars.NMK = self.NMK
homePlugLayer = CM_SLAC_MATCH_CNF()
homePlugLayer.MatchVariableFieldLen = 0x5600
homePlugLayer.VariableField = slacVars
responsePacket = ethLayer / homePlugAVLayer / homePlugLayer
return responsePacket
def buildSetKey(self):
ethLayer = Ether()
ethLayer.src = self.sourceMAC
ethLayer.dst = "00:b0:52:00:00:01" # Some AtherosC MAC for some reason
homePlugAVLayer = HomePlugAV()
homePlugAVLayer.version = 0x01
homePlugLayer = CM_SET_KEY_REQ()
homePlugLayer.KeyType = 0x1
homePlugLayer.MyNonce = 0xAAAAAAAA
homePlugLayer.YourNonce = 0x00000000
homePlugLayer.PID = 0x4
homePlugLayer.NetworkID = self.NID
homePlugLayer.NewEncKeySelect = 0x1
homePlugLayer.NewKey = self.NMK
responsePacket = ethLayer / homePlugAVLayer / homePlugLayer
return responsePacket
def buildSECCResponse(self):
e = Ether()
e.src = self.sourceMAC
e.dst = self.destinationMAC
ip = IPv6()
ip.src = self.sourceIP
ip.dst = self.destinationIP
udp = UDP()
udp.sport = 15118
udp.dport = self.destinationPort
secc = SECC()
secc.SECCType = 0x9001
secc.PayloadLen = 20
seccRM = SECC_ResponseMessage()
seccRM.SecurityProtocol = 16
seccRM.TargetPort = self.sourcePort
seccRM.TargetAddress = self.sourceIP # eno1
responsePacket = e / ip / udp / secc / seccRM
return responsePacket
class _TCPHandler:
def __init__(self, evse: EVSE):
self.evse = evse
self.iface = self.evse.iface
self.sourceMAC = self.evse.sourceMAC
self.sourceIP = self.evse.sourceIP
self.sourcePort = self.evse.sourcePort
self.destinationMAC = self.evse.destinationMAC
self.destinationIP = self.evse.destinationIP
self.destinationPort = self.evse.destinationPort
self.seq = 10000
self.ack = 0
self.exi = self.evse.exi
self.xml = XMLBuilder(self.exi)
self.msgList = {}
self.finishedNMAP = False
self.stop = False
self.scanner = None
self.timeout = 5
def start(self):
self.msgList = {}
self.running = True
print("INFO (EVSE): Starting TCP")
self.startSniff = False
self.got_client_info = False
# adjustment MicroNova
self.handshakeThread = AsyncSniffer(
count=1, iface=self.iface, lfilter=lambda x: x.haslayer("IPv6") and x.haslayer("TCP") and x[TCP].flags == "S", prn=self.handshake
)
self.handshakeThread.start()
while not self.got_client_info:
continue
self.recvThread = AsyncSniffer(
iface=self.iface,
lfilter=lambda x: x.haslayer("TCP") and x[TCP].sport == self.destinationPort and x[TCP].dport == self.sourcePort,
prn=self.handlePacket,
started_callback=self.setStartSniff,
)
self.recvThread.start()
while not self.startSniff:
continue
# adjustment MicroNova
# self.handshakeThread = AsyncSniffer(
# count=1, iface=self.iface, lfilter=lambda x: x.haslayer("IPv6") and x.haslayer("TCP") and x[TCP].flags == "S", prn=self.handshake
# )
# self.handshakeThread.start()
self.neighborSolicitationThread = AsyncSniffer(
iface=self.iface, lfilter=lambda x: x.haslayer("ICMPv6ND_NS") and x[ICMPv6ND_NS].tgt == self.sourceIP, prn=self.sendNeighborSoliciation
)
self.neighborSolicitationThread.start()
self.timeoutThread = Thread(target=self.checkForTimeout)
self.timeoutThread.start()
while self.running:
time.sleep(1)
def checkForTimeout(self):
print("INFO (EVSE): Starting timeout thread")
self.lastMessageTime = time.time()
while True:
# if self.stop: break
if time.time() - self.lastMessageTime > self.timeout or self.running == False:
print("INFO (EVSE): TCP timed out, resetting connection...")
self.killThreads()
break
time.sleep(1)
# Need this so the sniff thread is actually running when the handshake is sent
def setStartSniff(self):
self.startSniff = True
# print("INFO (EVSE): Starting recv sniff")
def recv(self):
print("EVSE (INFO): Starting recv thread")
sniff(
iface=self.iface,
lfilter=lambda x: x.haslayer("TCP") and x[TCP].sport == self.destinationPort and x[TCP].dport == self.sourcePort,
prn=self.handlePacket,
started_callback=self.setStartSniff,
)
def fin(self):
print("INFO (EVSE): Recieved FIN")
self.running = False
self.ack = self.ack + 1
ethLayer = Ether()
ethLayer.src = self.sourceMAC
ethLayer.dst = self.destinationMAC
ipLayer = IPv6()
ipLayer.src = self.sourceIP
ipLayer.dst = self.destinationIP
tcpLayer = TCP()
tcpLayer.sport = self.sourcePort
tcpLayer.dport = self.destinationPort
tcpLayer.flags = "A"
tcpLayer.seq = self.seq
tcpLayer.ack = self.ack
ack = ethLayer / ipLayer / tcpLayer
sendp(ack, iface=self.iface, verbose=0)
tcpLayer.flags = "FA"
finAck = ethLayer / ipLayer / tcpLayer
print("INFO (EVSE): Sending FINACK")
sendp(finAck, iface=self.iface, verbose=0)
def killThreads(self):
print("INFO (EVSE): Killing sniffing threads")
self.running = False
# adjustment MicroNova
try:
self.conn.close()
except:
pass
if self.scanner:
self.scanner.stop()
if self.recvThread.running:
self.recvThread.stop()
if self.handshakeThread.running:
self.handshakeThread.stop()
if self.neighborSolicitationThread.running:
self.neighborSolicitationThread.stop()
def handlePacket(self, pkt):
self.last_recv = pkt
self.seq = self.last_recv[TCP].ack
self.ack = self.last_recv[TCP].seq + len(self.last_recv[TCP].payload)
if "F" in self.last_recv.flags:
#self.fin() # adjustment MicroNova
return
if "P" not in self.last_recv.flags:
return
self.lastMessageTime = time.time()
data = self.last_recv[Raw].load
v2g = V2GTP(data)
payload = v2g.Payload
# Save responses to decrease load on java webserver
if payload in self.msgList.keys():
exi = self.msgList[payload]
else:
exi = self.getEXIFromPayload(payload)
if exi == None:
return
self.msgList[payload] = exi
sendp(self.buildV2G(binascii.unhexlify(exi)), iface=self.iface, verbose=0)
def buildV2G(self, payload):
ethLayer = Ether()
ethLayer.src = self.sourceMAC
ethLayer.dst = self.destinationMAC
ipLayer = IPv6()
ipLayer.src = self.sourceIP
ipLayer.dst = self.destinationIP
tcpLayer = TCP()
tcpLayer.sport = self.sourcePort
tcpLayer.dport = self.destinationPort
tcpLayer.seq = self.seq
tcpLayer.ack = self.ack
tcpLayer.flags = "PA"
v2gLayer = V2GTP()
v2gLayer.PayloadLen = len(payload)
v2gLayer.Payload = payload
return ethLayer / ipLayer / tcpLayer / v2gLayer
def getEXIFromPayload(self, data):
data = binascii.hexlify(data)
xmlString = self.exi.decode(data)
# print(f"XML String: {xmlString}")
root = ET.fromstring(xmlString)
if root.text is None:
if root[0].tag == "AppProtocol":
self.xml.SupportedAppProtocolResponse()
return self.xml.getEXI()
name = root[1][0].tag
print(f"Request: {name}")
if "SessionSetupReq" in name:
self.xml.SessionSetupResponse()
elif "ServiceDiscoveryReq" in name:
self.xml.ServiceDiscoveryResponse()
elif "ServicePaymentSelectionReq" in name:
self.xml.ServicePaymentSelectionResponse()
elif "ContractAuthenticationReq" in name:
self.xml.ContractAuthenticationResponse()
if self.evse.mode == RunMode.STOP:
self.xml.EVSEProcessing.text = "Ongoing"
elif self.evse.mode == RunMode.SCAN:
self.xml.EVSEProcessing.text = "Ongoing"
# Start nmap scan while connection is kept alive
if self.scanner == None:
nmapMAC = self.evse.nmapMAC if self.evse.nmapMAC else self.destinationMAC
nmapIP = self.evse.nmapIP if self.evse.nmapIP else self.destinationIP
self.scanner = NMAPScanner(EmulatorType.EVSE, self.evse.nmapPorts, self.iface, self.sourceMAC, self.sourceIP, nmapMAC, nmapIP)
self.scanner.start()
elif "ChargeParameterDiscoveryReq" in name:
self.xml.ChargeParameterDiscoveryResponse()
# self.xml.MinCurrentLimitValue.text = "0"
self.xml.MaxCurrentLimitValue.text = "5"
elif "CableCheckReq" in name:
self.xml.CableCheckResponse()
elif "PreChargeReq" in name:
self.xml.PreChargeResponse()
self.xml.Multiplier.text = root[1][0][1][0].text
self.xml.Value.text = root[1][0][1][2].text
elif "PowerDeliveryReq" in name:
self.xml.PowerDeliveryResponse()
elif "CurrentDemandReq" in name:
self.xml.CurrentDemandResponse()
self.xml.CurrentMultiplier.text = root[1][0][1][0].text
self.xml.CurrentValue.text = root[1][0][1][2].text
self.xml.VoltageMultiplier.text = root[1][0][8][0].text
self.xml.VoltageValue.text = root[1][0][8][2].text
self.xml.CurrentLimitValue.text = "5"
elif "SessionStopReq" in name:
self.running = False
self.xml.SessionStopResponse()
# elif "WeldingDetectionReq" in name:
# self.xml.WeldingDetectionResponse()
else:
raise Exception(f'Packet type "{name}" not recognized')
return self.xml.getEXI()
def startNeighborSolicitationSniff(self):
sniff(iface=self.iface, prn=self.sendNeighborSoliciation)
def sendNeighborSoliciation(self, pkt):
# if self.stop: exit()
# if not (pkt.haslayer("ICMPv6ND_NS") and pkt[ICMPv6ND_NS].tgt == self.sourceIP): return
self.destinationMAC = pkt[Ether].src
self.destinationIP = pkt[IPv6].src
print("INFO (EVSE): Sending Neighor Advertisement")
sendp(self.buildNeighborAdvertisement(), iface=self.iface, verbose=0)
def handshake(self, syn):
self.destinationMAC = syn[Ether].src
self.destinationIP = syn[IPv6].src
self.destinationPort = syn[TCP].sport
self.ack = syn[TCP].seq + 1
self.got_client_info = True
# ethLayer = Ether()
# ethLayer.src = self.sourceMAC
# ethLayer.dst = self.destinationMAC
# ipLayer = IPv6()
# ipLayer.src = self.sourceIP
# ipLayer.dst = self.destinationIP
# tcpLayer = TCP()
# tcpLayer.sport = self.sourcePort
# tcpLayer.dport = self.destinationPort
# tcpLayer.flags = "SA"
# tcpLayer.seq = self.seq
# tcpLayer.ack = self.ack
# synAck = ethLayer / ipLayer / tcpLayer
# print("INFO (EVSE): Sending SYNACK")
# sendp(synAck, iface=self.iface, verbose=0)
def buildNeighborAdvertisement(self):
ethLayer = Ether()
ethLayer.src = self.sourceMAC
ethLayer.dst = self.destinationMAC
ipLayer = IPv6()
ipLayer.src = self.sourceIP
ipLayer.dst = self.destinationIP
ipLayer.plen = 32
ipLayer.hlim = 255
icmpLayer = ICMPv6ND_NA()
icmpLayer.type = 136
icmpLayer.R = 0
icmpLayer.S = 1
icmpLayer.tgt = self.sourceIP
optLayer = ICMPv6NDOptDstLLAddr()
optLayer.type = 2
optLayer.len = 1
optLayer.lladdr = self.sourceMAC
responsePacket = ethLayer / ipLayer / icmpLayer / optLayer
return responsePacket
if __name__ == "__main__":
# Parse arguements from command line
parser = argparse.ArgumentParser(description="EVSE emulator for AcCCS")
parser.add_argument(
"-M",
"--mode",
nargs=1,
type=int,
help="Mode for emulator to run in: 0 for full conversation, 1 for stalling the conversation, 2 for portscanning (default: 0)",
)
parser.add_argument("-I", "--interface", nargs=1, help="Ethernet interface to send/recieve packets on (default: eth1)")
parser.add_argument("--source-mac", nargs=1, help="Source MAC address of packets (default: 00:1e:c0:f2:6c:a0)")
parser.add_argument("--source-ip", nargs=1, help="Source IP address of packets (default: fe80::21e:c0ff:fef2:72f3)")
parser.add_argument("--source-port", nargs=1, type=int, help="Source port of packets (default: 25565)")
parser.add_argument("--NID", nargs=1, help="Network ID of the HomePlug GreenPHY AVLN (default: \\x9c\\xb0\\xb2\\xbb\\xf5\\x6c\\x0e)")
parser.add_argument(
"--NMK",
nargs=1,
help="Network Membership Key of the HomePlug GreenPHY AVLN (default: \\x48\\xfe\\x56\\x02\\xdb\\xac\\xcd\\xe5\\x1e\\xda\\xdc\\x3e\\x08\\x1a\\x52\\xd1)",
)
parser.add_argument("-p", "--protocol", nargs=1, help="Protocol for EXI encoding/decoding: DIN, ISO-2, ISO-20 (default: DIN)")
parser.add_argument("--nmap-mac", nargs=1, help="The MAC address of the target device to NMAP scan (default: EVCC MAC address)")
parser.add_argument("--nmap-ip", nargs=1, help="The IP address of the target device to NMAP scan (default: EVCC IP address)")
parser.add_argument("--nmap-ports", nargs=1, help="List of ports to scan seperated by commas (ex. 1,2,5-10,19,...) (default: Top 8000 common ports)")
parser.add_argument("--modified-cordset", action="store_true", help="Set this option when using a modified cordset during testing of a target vehicle. The AcCCS system will provide a 150 ohm ground on the proximity line to reset the connection. (default: False)")
args = parser.parse_args()
evse = EVSE(args)
try:
evse.start()
except KeyboardInterrupt:
print("INFO (EVSE): Shutting down emulator")
except Exception as e:
print(e)
finally:
evse.openProximity()
del evse