예제 #1
0
def File():
    reader = DummyPCSCDevice()
    reader.addResponse('45', ['00 0F 81'])
    reader.addResponse('AA 00',
                       ['AF B3 51 CB 24 65 D4 F3 3A C6 27 FD 6E 87 A1 68 F2'])
    reader.addResponse(
        'AF 04 4B 6C AC 34 3A 08 65 89 51 49 64 9C A8 DD E1 F5 AC 4E C6 7B D2 08 90 0A F0 2F 04 9E 05 F0 B0',
        ['00 A2 EE 14 4B 10 12 FB EB 7B 2F 11 13 2D 95 A4 54'])
    #Create STdDataFile
    reader.addResponse('CD 05 00 11 00 50 00 00',
                       ['00 A7 53 16 AD 15 96 B9 53'])
    #Get File Id
    reader.addResponse('6F', ['00 05 2D 5F F6 7F FE C9 D2 D3'])
    #Get File Setting
    reader.addResponse('F5 05',
                       ['00 00 00 11 00 50 00 00 2A AC 75 17 02 4E 09 DC'])
    #Write File
    reader.addResponse(
        '3D 05 00 00 00 34 00 00 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33',
        ['00 76 5C 9D AA 50 EC B6 2F'])
    reader.addResponse(
        '3D 05 34 00 00 1C 00 00 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F',
        ['00 3E 0A ED 98 6B 8B 0F 37'])
    #Read File
    reader.addResponse('BD 05 00 00 00 30 00 00', [
        '00 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 4C 65 F1 F8 42 26 2B AC'
    ])
    reader.addResponse('BD 05 30 00 00 20 00 00', [
        '00 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F F0 22 05 CF 91 3C 03 C9'
    ])
    reader.addResponse('DF 05', ['00 1B EF 0D 32 B6 D1 D7 F9'])

    desfire = DESFire(reader)
    key_setting = desfire.getKeySetting()
    key_setting.setKey('10 18 20 28 30 38 40 48 50 58 60 68 70 78 80 88')
    desfire.authenticate(0, key_setting,
                         '40 E7 D2 71 74 CB A6 75 E8 EF BA B9 9C 53 0E 3D')
    filePerm = DESFireFilePermissions()
    filePerm.unpack('11 00')
    desfire.createStdDataFile(5, filePerm, 80)
    desfire.getFileIDs()
    desfire.getFileSettings(5)
    desfire.writeFileData(
        5, 0, 80,
        '00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F'
    )
    desfire.readFileData(5, 0, 80)
    desfire.deleteFile(5)
예제 #2
0
class MyObserver(CardObserver):

    desfire = None

    @catch_gracefully()
    def update(self, observable, actions):

        (addedcards, removedcards) = actions

        for card in addedcards:
            logger.info("+ Inserted: %s", toHexString(card.atr))

            connection = card.createConnection()
            connection.connect()

            # This will log raw card traffic to console
            connection.addObserver(ConsoleCardConnectionObserver())
            # connection object itself is CardConnectionDecorator wrapper
            # and we need to address the underlying connection object
            # directly
            self.desfire = DESFire(PCSCDevice(connection.component))
            while True:
                num = int(
                    input("""
10. Authenticate
----------------------------
20. Get card information
21. Format card
----------------------------
30. Create application
31. Select applicatino
32. List application
----------------------------
40. Change key
41. Get key settings
42. Change key settings
----------------------------
50. Craete file
51. List files 
52. Write file
53. Read file
90. Exit
"""))
                if num == 90:
                    break
                elif num == 10:
                    self.auth()
                elif num == 20:
                    self.getCardInfo()
                elif num == 21:
                    self.formatCard()
                elif num == 30:
                    self.createApplication()
                elif num == 31:
                    self.selectApplication()
                elif num == 32:
                    self.listApplication()
                elif num == 40:
                    self.changeKey()
                elif num == 41:
                    self.getKeySettings()
                elif num == 42:
                    self.changeKeySettings()
                elif num == 50:
                    self.createFile()
                elif num == 51:
                    self.listFiles()
                elif num == 52:
                    self.writeFile()
                elif num == 53:
                    self.readFile()

    def auth(self):
        key = self.desfire.getKeySetting()
        key.setKey(input('Key: '))
        self.desfire.authenticate(int(input('Key pos: ')), key)

    def getCardInfo(self):
        print(self.desfire.getCardVersion())

    def formatCard(self):
        self.desfire.formatCard()

    def createApplication(self):
        aid = input('App id: ')
        size = int(input('Number keys: '))
        i = 1
        l = list()
        print('Set Settings(y/n):')
        for s in DESFireKeySettings:
            if input(s.name + ': ') == 'y':
                l += [s]
            if i == 4:
                break
            i += 1
        k = int(input('Select key for change othor keys: '))
        v = input('Select Enryption(2K3DES,№K3DES,AES): ')
        l += [DESFireKeySettings(k << 4)]
        self.desfire.createApplication(aid, l, size,
                                       DESFireKeyType['DF_KEY_' + v])

    def changeKey(self):
        i = int(input('Key pos: '))
        old_key = self.desfire.getKeySetting()
        new_key = copy.copy(old_key)
        old_key.setKey(input('Old key: '))
        new_key.setKey(input('New key: '))
        self.desfire.changeKey(i, new_key, old_key)

    def selectApplication(self):
        self.desfire.selectApplication(input('Application id: '))

    def listApplication(self):
        for ids in self.desfire.getApplicationIDs():
            print(byte_array_to_human_readable_hex(ids))

    def changeKeySettings(self):
        i = 1
        l = list()
        print('Set Settings(y/n):')
        for s in DESFireKeySettings:
            if input(s.name + ': ') == 'y':
                l += [s]
            if i == 4:
                break
            i += 1
        k = int(input('Select key for change othor keys: '))
        l += [DESFireKeySettings(k << 4)]
        self.desfire.changeKeySettings(l)

    def getKeySettings(self):
        print(self.desfire.getKeySetting())

    def createFile(self):
        filePerm = DESFireFilePermissions()
        filePerm.setPerm(
            int(input('Read key number: ')), int(intput('Write key number')),
            int(input('read/write key number: ')),
            int(input('Change permmision key number: '))
        )  # key 4 read, key3 write, no key read and write, key2 change permissions
        self.desfire.createStdDataFile(
            int(input('File id: ')), filePerm,
            int(input('File lenght: ')))  # file Id 0, length 32 byte

    def writeFile(self):
        self.desfire.writeFileData(int(input('File id: ')),
                                   int(input('Offset')),
                                   int(input('Length: ')), input('Data: '))

    def readFile(self):
        print(
            byte_array_to_human_readable_hex(
                self.desfire.readFileData(int(input('File id: ')),
                                          int(input('Offset')),
                                          int(input('Length: ')))))

    def getFileSettings(self):
        self.desfire.getFileSettings(int(input('File id: ')))

    def listFiles(self):
        print(self.desfire.getFileIDs())