Beispiel #1
0
    def __recv_packet(self):
        if len(self.rbuffer) < 12:
            return

        (signature, checksum, sz) = struct.unpack("!III", self.rbuffer[:12])

        if not signature == 0xDECAFBAD:
            raise BufferError("Invalid signature in packet header")

        if len(self.rbuffer) < 12 + sz:
            return

        content = self.rbuffer[12:(12 + sz)]

        calc_csum = ExternalInterface.adler32(content)

        if not calc_csum == checksum:
            print content.encode('hex')
            raise BufferError(
                "Invalid checksum in packet header {} vs {}".format(
                    calc_csum, checksum))

        cmd = kismet_pb2.Command()
        cmd.ParseFromString(content)

        if cmd.command in self.handlers:
            self.handlers[cmd.command](cmd.seqno, cmd.content)
        else:
            print "Unhandled", cmd.command

        self.rbuffer = self.rbuffer[12 + sz:]
Beispiel #2
0
    def write_ext_packet(self, cmdtype, content):
        cp = kismet_pb2.Command()

        cp.command = cmdtype
        cp.seqno = self.cmdnum
        cp.content = content.SerializeToString()

        self.write_raw_packet(cp)

        self.cmdnum = self.cmdnum + 1
Beispiel #3
0
    def write_ext_packet(self, cmdtype, content):
        """
        Generate a Kismet external interface command, frame, and transmit it.

        :param cmdtype: Command type string
        :param content: Command content, must be a serializable protobuf object

        :return: None
        """
        cp = kismet_pb2.Command()

        cp.command = cmdtype
        cp.seqno = self.cmdnum
        cp.content = content.SerializeToString()

        self.write_raw_packet(cp)

        self.cmdnum = self.cmdnum + 1
def location_updater(c):
    try:
        # c, addr = lsock.accept()
        kserv = (kserv_ip, int(kserv_port))
        ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        connected = False

        try:
            s_print("Connecting to Kismet server.")
            ssock.connect(kserv)
            connected = True
        except ConnectionRefusedError:
            s_print("Kismet Server offline")

        while connected:
            data = bytearray(c.recv(buffer))
            if len(data) == 0:
                break
            location = gpsd.get_current()
            s_print(f"Passing {len(data)} Bytes")
            for byte in range(len(data)):
                command = data[byte:byte + 13].decode('utf-8',
                                                      'backslashreplace')
                checksum_passed = False
                if command == "KDSDATAREPORT" or command == "LBTDATAREPORT":
                    try:
                        # Packet v2 Structure:
                        # Signature 32bits
                        # DE CA FB AD
                        # Checksum Replacement Pt 1. 16b
                        # AB CD
                        # Checksum Replacement Pt 2. AKA Proto Version 16b
                        # 00 02
                        # Length 32b
                        # 00 00 00 A4
                        # Command 32Bytes
                        # byte starts here
                        # 4B 44 53 44 41 54 41 52 45 50 4F 52 54 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
                        # Sequence Number 32b
                        # 00 00 00 09
                        # s_print(command)
                        if data[byte - 8:byte -
                                4] == bytearray(b'\xab\xcd\x00\x02'):
                            # s_print('Got proto v2!')
                            proto2 = True
                            data_size = struct.unpack('!I',
                                                      data[byte - 4:byte])[0]
                            data_range = data[byte + 36:byte + 36 + data_size]
                            checksum_passed = True
                        else:
                            proto2 = False
                            data_size = struct.unpack(
                                '!I', data[byte - 6:byte - 2])[0]
                            data_range = data[byte - 2:byte + data_size - 2]
                            original_checksum = struct.unpack(
                                '!I', data[byte - 10:byte - 6])[0]
                            # s_print("Original Checksum: " + str(original_checksum))
                            # s_print("Calculated Checksum: " + str(kismet_adler32(data_range)))
                            if original_checksum == kismet_adler32(data_range):
                                checksum_passed = True
                                kis_command = kismet.Command()
                                kis_command.ParseFromString(data_range)
                                # s_print(kis_command)
                                # s_print(type(data[byte - 8:byte - 4]))
                                # s_print(data[byte - 8:byte - 4])
                                # s_print("Got Proto V1 or Something else. " +
                                #         "This will work, but please update to a newer version of Kismet")

                        # s_print(data_size)
                        # s_print(f'Data in:\n{data_range.hex()}')

                        if checksum_passed:
                            if command == "KDSDATAREPORT":
                                kis_content = kds.DataReport()
                            elif command == "LBTDATAREPORT":
                                kis_content = lbt.LinuxBluetoothDataReport()

                            if proto2:
                                kis_content.ParseFromString(data_range)
                            else:
                                kis_content.ParseFromString(
                                    kis_command.content)

                            try:
                                kis_content.gps.lat = location.lat
                                kis_content.gps.lon = location.lon
                                kis_content.gps.alt = location.alt
                                if proto2:
                                    data_out = kis_content.SerializeToString()
                                    data[byte + 36:byte + 36 +
                                         data_size] = data_out
                                else:
                                    kis_command.content = kis_content.SerializeToString(
                                    )
                                    data_out = kis_command.SerializeToString()
                                    data[byte - 2:byte + data_size -
                                         2] = data_out
                                    # s_print(f'Data out:\n{data_range.hex()}')
                                    data[byte - 10:byte - 6] = struct.pack(
                                        '!I', kismet_adler32(data_out))
                            except UserWarning:
                                s_print("No GPS Data, not altering location.")
                    except Exception as e:
                        s_print(e)

            ssock.send(data)
            c.send(ssock.recv(buffer))
            del data
    except Exception as e:
        print(e)

    c.close()