Ejemplo n.º 1
0
 def control_auth(self):
     uds_msg = UDSMessage()
     self.frames.extend(
         uds_msg.add_request(
             self._status2['id_uds'],  # ID
             0x27,  # Service
             0x1,  # Sub function
             []))
     self.wait_for_auth = True
     return ""
Ejemplo n.º 2
0
 def write_param(self, key):
     uds_msg = UDSMessage()
     (ids, data) = key.split(',')
     ids = list(bytes.fromhex(ids.strip()))
     data = list(bytes.fromhex(data.strip()))
     self.frames.extend(
         uds_msg.add_request(
             self._status2['id_uds'],  # ID
             0x2e,  # Service
             ids[0],  # Sub function
             ids[1:] + data))
     return ""
Ejemplo n.º 3
0
 def call_auth(self):
     uds_msg = UDSMessage()
     i = 0
     key = []
     for byte_k in self._status2['uds_key']:
         key.append(ord(byte_k) ^ self._seed[i % 4])
         i += 1
     self.frames.extend(
         uds_msg.add_request(
             self._status2['id_uds'],  # ID
             0x27,  # Service
             0x2,  # Sub function
             key))
     self.wait_for_auth = False
Ejemplo n.º 4
0
    def do_start(self, args):
        self.queue_messages = []
        self.last = time.clock()

        data = [0, 0, 0, 0, 0, 0, 0, 0]
        if 'body' in args:
            data = list(bytes.fromhex(args['body']))

        iso_mode = self._get_iso_mode(args)

        padding = args.get('padding', None)
        shift = int(args.get('shift', 0x8))

        if 'range' not in args:
            self.dprint(1, "No range specified")
            self._active = False
            self.set_error_text("ERROR: No range specified")
            return
        start, end = args.get('range', [0, 0])
        for i in range(int(start), int(end)):
            if iso_mode == 1:
                iso_list = ISOTPMessage.generate_can(i, data, padding)
                iso_list.reverse()
                self.queue_messages.extend(iso_list)
            elif iso_mode == 0:
                self.queue_messages.append(
                    CANMessage.init_data(i, len(data), data[:8]))
            elif iso_mode == 2:
                for service in args.get('services', []):
                    uds_m = UDSMessage(shift, padding)
                    for service_id in self._get_range(service['service']):
                        # https://github.com/eik00d/CANToolz/issues/93 by @DePierre
                        subservice_ids = service.get('sub', None)
                        if subservice_ids is None:
                            subservice_ids = [None]
                        else:
                            subservice_ids = self._get_range(subservice_ids)
                        for subservice_id in subservice_ids:
                            iso_list = uds_m.add_request(
                                i, service_id, subservice_id,
                                service.get('data', []))
                            iso_list.reverse()
                            self.queue_messages.extend(iso_list)
        self._full = len(self.queue_messages)
        self._last = 0
Ejemplo n.º 5
0
    def do_effect(self, can_msg, args):
        if self._status2['status'] > 0:
            self.generate_rpm()
        self.generate_vin()
        if args['action'] == 'read' and can_msg.CANData:  # READ
            if self._status2['id_command'] == can_msg.CANFrame.frame_id:
                for cmd, value in self._status2['commands'].items():
                    len_cmd = int(len(str(value)) / 2)

                    if self._status2['status'] != 0 and cmd == "rpm_up":
                        len_cmd2 = len_cmd + 1
                        if can_msg.CANFrame.frame_length == len_cmd2 and can_msg.CANFrame.frame_raw_data[
                                0:len_cmd] == bytes.fromhex(
                                    value
                                )[0:len_cmd] and self._status2['status'] == 1:
                            self.rpm_up += ord(
                                can_msg.CANFrame.
                                frame_raw_data[len_cmd:len_cmd2])
                    elif self._status2['status'] != 0 and cmd == "rpm_down":
                        len_cmd2 = len_cmd + 1
                        if can_msg.CANFrame.frame_length == len_cmd2 and can_msg.CANFrame.frame_raw_data[
                                0:len_cmd] == bytes.fromhex(
                                    value
                                )[0:len_cmd] and self._status2['status'] == 1:
                            self.rpm_down += ord(
                                can_msg.CANFrame.
                                frame_raw_data[len_cmd:len_cmd2])
                    elif self._status2['status'] != 0 and cmd == "stop":
                        if can_msg.CANFrame.frame_length == len_cmd and can_msg.CANFrame.frame_raw_data[
                                0:len_cmd] == bytes.fromhex(value)[0:len_cmd]:
                            self._status2['status'] = 3
                            self.current = 0
                    elif cmd == "init":
                        if not self.init_sess:
                            self.init_sess = ISOTPMessage(
                                can_msg.CANFrame.frame_id)

                        ret = self.init_sess.add_can(can_msg.CANFrame)
                        if ret < 0:
                            self.init_sess = None
                        elif ret == 1:
                            if self.init_sess.message_length != 17:
                                self.init_sess = None
                            else:
                                key = self.init_sess.message_data
                                i = 0
                                vin_x = ""
                                for byte_k in key:
                                    vin_x += chr(byte_k ^ ord(self._auth[i])
                                                 )  # XOR with KEY (to get VIN)
                                    i += 1
                                if vin_x != self._vin:  # auth failed, send error back
                                    self.frames.append(
                                        CANMessage(
                                            self._status2.get(
                                                'id_report',
                                                0xffff), 2, b'\xff\xff', False,
                                            CANMessage.DataFrame))
                                else:  # Auth complite
                                    self._status2['status'] = 1
                                    self.frames.append(
                                        CANMessage(
                                            self._status2.get(
                                                'id_report',
                                                0xffff), 2, b'\xff\x01', False,
                                            CANMessage.DataFrame))
                                    if self.current < 150:
                                        self.current = self.default
                                self.init_sess = None

            elif self._status2['id_uds'] == can_msg.CANFrame.frame_id:
                if not self.init_sess2:
                    self.init_sess2 = ISOTPMessage(can_msg.CANFrame.frame_id)

                ret = self.init_sess2.add_can(can_msg.CANFrame)

                if ret < 0:
                    self.init_sess2 = None
                elif ret == 1:
                    uds_msg = UDSMessage(self._status2.get('uds_shift', 8))
                    uds_msg.add_raw_request(self.init_sess2)
                    if can_msg.CANFrame.frame_id in uds_msg.sessions:
                        if 0x27 in uds_msg.sessions[
                                can_msg.CANFrame.frame_id]:  # Check service
                            if 1 in uds_msg.sessions[
                                    can_msg.CANFrame.
                                    frame_id][0x27]:  # Check sub: SEED request
                                self._seed = [
                                    random.randrange(1, 100),
                                    random.randrange(1, 100),
                                    random.randrange(1, 100),
                                    random.randrange(1, 100)
                                ]
                                # Generate UDS reponse
                                self.frames.extend(
                                    uds_msg.add_request(
                                        self._status2['id_uds'] +
                                        self._status2['uds_shift'],  # ID
                                        0x27 + 0x40,  # Service
                                        0x01,  # Sub function
                                        self._seed))  # data

                            elif 2 in uds_msg.sessions[
                                    can_msg.CANFrame.
                                    frame_id][0x27]:  # Check sub: KEY enter
                                if self._seed is not None:
                                    key_x = ""
                                    key = uds_msg.sessions[
                                        can_msg.CANFrame.frame_id][0x27][2][
                                            'data']  # Read key
                                    i = 0
                                    for byte_k in key:
                                        key_x += chr(byte_k
                                                     ^ self._seed[i % 4])
                                        i += 1
                                    if key_x == self._uds_auth:
                                        self._uds_auth_done = True
                                        self.frames.extend(
                                            uds_msg.add_request(
                                                self._status2['id_uds'] + self.
                                                _status2['uds_shift'],  # ID
                                                0x27 + 0x40,  # Service
                                                0x02,  # Sub function
                                                [0xFF]))  # data
                                    else:
                                        self._uds_auth_done = False
                                        self.frames.extend(
                                            uds_msg.add_request(
                                                self._status2['id_uds'] + self.
                                                _status2['uds_shift'],  # ID
                                                0x27 + 0x40,  # Service
                                                0x02,  # Sub function
                                                [0x00]))  # data
                                self._seed = None
                        elif 0x2e in uds_msg.sessions[
                                can_msg.CANFrame.
                                frame_id] and 0x55 in uds_msg.sessions[
                                    can_msg.CANFrame.
                                    frame_id][0x2e] and uds_msg.sessions[
                                        can_msg.CANFrame.frame_id][0x2e][0x55][
                                            'data'][0] == 0x55:
                            if self._uds_auth_done:
                                new_key = ''.join(
                                    chr(x) for x in uds_msg.sessions[
                                        can_msg.CANFrame.frame_id][0x2e][0x55]
                                    ['data'][1:])
                                if len(new_key) == 17:
                                    self._uds_auth_done = False
                                    self._auth = new_key
                                    self.frames.extend(
                                        uds_msg.add_request(
                                            self._status2['id_uds'] +
                                            self._status2['uds_shift'],  # ID
                                            0x2e + 0x40,  # Service
                                            0x55,  # Sub function
                                            [0x55]))  # data
                                else:
                                    self._uds_auth_done = False
                                    self.frames.extend(
                                        uds_msg.add_request(
                                            self._status2['id_uds'] +
                                            self._status2['uds_shift'],  # ID
                                            0x2e + 0x40,  # Service
                                            0x00,  # Sub function
                                            [0x00]))
                            else:
                                self._uds_auth_done = False
                                self.frames.extend(
                                    uds_msg.add_request(
                                        self._status2['id_uds'] +
                                        self._status2['uds_shift'],  # ID
                                        0x2e + 0x40,  # Service
                                        0x00,  # Sub function
                                        [0x00]))  # data
                    self.init_sess2 = None

        elif args['action'] == 'write' and not can_msg.CANData:
            if len(self.frames) > 0:
                can_msg.CANFrame = self.frames.pop(0)
                can_msg.CANData = True
                can_msg.bus = self._bus
        return can_msg
Ejemplo n.º 6
0
    def do_effect(self, can_msg, args):
        if args['action'] == 'read' and can_msg.CANData and can_msg.CANFrame.frame_id == self._status2[
                'id_uds'] + self._status2['uds_shift']:  # READ
            if not self.init_sess2:
                self.init_sess2 = ISOTPMessage(can_msg.CANFrame.frame_id)

                ret = self.init_sess2.add_can(can_msg.CANFrame)

                if ret < 0:
                    self.init_sess2 = None
                elif ret == 1:
                    uds_msg = UDSMessage()
                    uds_msg.add_raw_request(self.init_sess2)
                    if can_msg.CANFrame.frame_id in uds_msg.sessions:
                        if 0x27 + 0x40 in uds_msg.sessions[
                                can_msg.CANFrame.frame_id]:  # Check service
                            if 1 in uds_msg.sessions[
                                    can_msg.CANFrame.frame_id][
                                        0x27 +
                                        0x40]:  # Check sub: SEED request
                                self._seed = uds_msg.sessions[
                                    can_msg.CANFrame.frame_id][0x27 +
                                                               0x40][1]['data']
                                if self.wait_for_auth:
                                    self.call_auth()
                            elif 2 in uds_msg.sessions[
                                    can_msg.CANFrame.
                                    frame_id][0x27 + 0x40] and len(
                                        uds_msg.sessions[
                                            can_msg.CANFrame.frame_id][0x27 +
                                                                       0x40][2]
                                        ['data']) == 1:  # Check sub: KEY enter
                                self.auth_resp_byte = uds_msg.sessions[
                                    can_msg.CANFrame.frame_id][
                                        0x27 + 0x40][2]['data'][0]
                        elif 0x2e + 0x40 in uds_msg.sessions[
                                can_msg.CANFrame.
                                frame_id] and 0x55 in uds_msg.sessions[
                                    can_msg.CANFrame.frame_id][
                                        0x2e + 0x40] and uds_msg.sessions[
                                            can_msg.CANFrame.frame_id][
                                                0x2e +
                                                0x40][0x55]['data'][0] == 0x55:
                            self.record_status = True
                        elif 0x2e + 0x40 in uds_msg.sessions[
                                can_msg.CANFrame.
                                frame_id] and 0x0 in uds_msg.sessions[
                                    can_msg.CANFrame.frame_id][
                                        0x2e + 0x40] and uds_msg.sessions[
                                            can_msg.CANFrame.frame_id][
                                                0x2e +
                                                0x40][0x0]['data'][0:1] == [
                                                    0x0
                                                ]:  # data
                            self.record_status = False
                    self.init_sess2 = None

        if args['action'] == 'write' and not can_msg.CANData:
            if len(self.frames) > 0:
                can_msg.CANFrame = self.frames.pop(0)
                can_msg.CANData = True
                can_msg.bus = self._bus
        return can_msg