1# SPDX-License-Identifier: GPL-2.0-only 2# This file is part of Scapy 3# See https://scapy.net/ for more information 4# Copyright (C) Nils Weiss <nils@we155.de> 5 6# scapy.contrib.description = UDS EcuState modifications 7# scapy.contrib.status = library 8from scapy.contrib.automotive.uds import UDS_DSCPR, UDS_ERPR, UDS_SAPR, \ 9 UDS_RDBPIPR, UDS_CCPR, UDS_TPPR, UDS_RDPR, UDS 10from scapy.packet import Packet 11from scapy.contrib.automotive.ecu import EcuState 12 13 14__all__ = ["UDS_DSCPR_modify_ecu_state", "UDS_CCPR_modify_ecu_state", 15 "UDS_ERPR_modify_ecu_state", "UDS_RDBPIPR_modify_ecu_state", 16 "UDS_TPPR_modify_ecu_state", "UDS_SAPR_modify_ecu_state", 17 "UDS_RDPR_modify_ecu_state"] 18 19 20@EcuState.extend_pkt_with_modifier(UDS_DSCPR) 21def UDS_DSCPR_modify_ecu_state(self, req, state): 22 # type: (Packet, Packet, EcuState) -> None 23 state.session = self.diagnosticSessionType # type: ignore 24 try: 25 del state.security_level # type: ignore 26 except AttributeError: 27 pass 28 29 30@EcuState.extend_pkt_with_modifier(UDS_ERPR) 31def UDS_ERPR_modify_ecu_state(self, req, state): 32 # type: (Packet, Packet, EcuState) -> None 33 state.reset() 34 state.session = 1 # type: ignore 35 36 37@EcuState.extend_pkt_with_modifier(UDS_SAPR) 38def UDS_SAPR_modify_ecu_state(self, req, state): 39 # type: (Packet, Packet, EcuState) -> None 40 if self.securityAccessType % 2 == 0 and \ 41 self.securityAccessType > 0 and len(req) >= 3: 42 state.security_level = self.securityAccessType # type: ignore 43 elif self.securityAccessType % 2 == 1 and \ 44 self.securityAccessType > 0 and \ 45 len(req) >= 3 and not any(self.securitySeed): 46 state.security_level = self.securityAccessType + 1 # type: ignore 47 48 49@EcuState.extend_pkt_with_modifier(UDS_CCPR) 50def UDS_CCPR_modify_ecu_state(self, req, state): 51 # type: (Packet, Packet, EcuState) -> None 52 state.communication_control = self.controlType # type: ignore 53 54 55@EcuState.extend_pkt_with_modifier(UDS_TPPR) 56def UDS_TPPR_modify_ecu_state(self, req, state): 57 # type: (Packet, Packet, EcuState) -> None 58 state.tp = 1 # type: ignore 59 60 61@EcuState.extend_pkt_with_modifier(UDS_RDBPIPR) 62def UDS_RDBPIPR_modify_ecu_state(self, req, state): 63 # type: (Packet, Packet, EcuState) -> None 64 state.pdid = self.periodicDataIdentifier # type: ignore 65 66 67@EcuState.extend_pkt_with_modifier(UDS_RDPR) 68def UDS_RDPR_modify_ecu_state(self, req, state): 69 # type: (Packet, Packet, EcuState) -> None 70 oldstr = getattr(state, "req_download", "") 71 newstr = str(req.fields) 72 state.req_download = oldstr if newstr in oldstr else oldstr + newstr # type: ignore # noqa: E501 73 74 75@EcuState.extend_pkt_with_modifier(UDS) 76def UDS_modify_ecu_state(self, req, state): 77 # type: (Packet, Packet, EcuState) -> None 78 if self.service == 0x77: # UDS RequestTransferExitPositiveResponse 79 try: 80 state.download_complete = state.req_download # type: ignore 81 except (KeyError, AttributeError): 82 pass 83 state.req_download = "" # type: ignore 84