示例#1
0
    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
            logger.info("Opened connection %s", connection.component)
            desfire = DESFire(PCSCDevice(connection.component))
            key_setting=desfire.getKeySetting()
            logger.info('Auth Key %d',0)
            desfire.authenticate(0,key_setting)
            info=desfire.getCardVersion()
            logger.info(info)
            logger.info('Format card')
            desfire.formatCard()
            logger.info('Create application with ID 00AE16')
            desfire.createApplication("00 AE 16",[DESFireKeySettings.KS_ALLOW_CHANGE_MK,DESFireKeySettings.KS_LISTING_WITHOUT_MK,DESFireKeySettings.KS_CONFIGURATION_CHANGEABLE],14,DESFireKeyType.DF_KEY_AES)
            logger.info('Select application with ID 00AE16')
            desfire.selectApplication('00 AE 16')
            default_key=desfire.createKeySetting('00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00',0,DESFireKeyType.DF_KEY_AES,[])            
            app_key=desfire.createKeySetting('00 10 20 31 40 50 60 70 80 90 A0 B0 B0 A0 90 80',0,DESFireKeyType.DF_KEY_AES,[])
            logger.info('Auth Key %d',0)
            desfire.authenticate(0,default_key)
            logger.info('Cange Key %d',0)
            desfire.changeKey(0,app_key,default_key)
            logger.info('Auth Key %d',0)
            desfire.authenticate(0,app_key)
            desfire.changeKeySettings([ DESFireKeySettings.KS_ALLOW_CHANGE_MK, DESFireKeySettings.KS_CONFIGURATION_CHANGEABLE, DESFireKeySettings.KS_CHANGE_KEY_WITH_KEY_1])
            app_key_1=desfire.createKeySetting('11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF 00',0,DESFireKeyType.DF_KEY_AES,[])
            logger.info('Cange Key %d',1)
            desfire.changeKey(1,app_key_1,default_key)
            logger.info('Auth Key %d',1)
            desfire.authenticate(1,app_key_1)
            app_key_2=desfire.createKeySetting('22 33 44 55 66 77 88 99 AA BB CC DD EE FF 00 11',0,DESFireKeyType.DF_KEY_AES,[])
            logger.info('Cange Key %d',2)
            desfire.changeKey(2,app_key_2,default_key)
            app_key_3=desfire.createKeySetting('33 44 55 66 77 88 99 AA BB CC DD EE FF 00 11 22',0,DESFireKeyType.DF_KEY_AES,[])
            logger.info('Cange Key %d',3)
            desfire.changeKey(3,app_key_3,default_key)
            app_key_4=desfire.createKeySetting('44 55 66 77 88 99 AA BB CC DD EE FF 00 11 22 33',0,DESFireKeyType.DF_KEY_AES,[])
            logger.info('Cange Key %d',4)
            desfire.changeKey(4,app_key_4,default_key)
            logger.info('Auth Key %d',0)
            desfire.authenticate(0,app_key)
            filePerm=DESFireFilePermissions()
            filePerm.setPerm(0x04,0x03,0x0F,0x02) # key 4 read, key3 write, no key read and write, key2 change permissions
            logger.info('Creat File with ID %d and %d byte',0,32)
            desfire.createStdDataFile(0,filePerm,32) # file Id 0, length 32 byte
            logger.info('Auth Key %d',3)
            desfire.authenticate(3,app_key_3)
            write='00 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'
            logger.info('Data write %s',write)
            desfire.writeFileData(0,0,32,write)
            logger.info('Auth Key %d',4)
            desfire.authenticate(4,app_key_4)
            read=desfire.readFileData(0,0,32)
            logger.info('Data read %s',byte_array_to_human_readable_hex(read))
示例#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())
示例#3
0
def Test_DES():
    """
        *** GetCardVersion()
        TX CMAC:  50 20 EC 82 60 86 DF 12
        Sending:  00 00 FF 04 FC <D4 40 01 60> 8B 00
        Response: 00 00 FF 0B F5 <D5 41 00 AF 04 01 01 01 00 1A 05> 15 00 AA AA AA AA AA AA AA AA
        Sending:  00 00 FF 04 FC <D4 40 01 AF> 3C 00
        Response: 00 00 FF 0B F5 <D5 41 00 AF 04 01 01 01 04 1A 05> 11 00 AA AA AA AA AA AA AA AA
        Sending:  00 00 FF 04 FC <D4 40 01 AF> 3C 00
        Response: 00 00 FF 1A E6 <D5 41 00 00 04 06 3F 72 63 34 80 BA 45 19 E3 20 49 13 CD C8 10 BA FA 40 17 59> 98 00
        RX CMAC:  CD C8 10 BA FA 40 17 59
        """

    print('Test Des')
    reader = DummyPCSCDevice()

    reader = DummyPCSCDevice()
    #Auth
    reader.addResponse('45', [
        '00 0F 01', '00 0F 02 25 DD 8D 77 31 B1 CF D5',
        '00 0D 02 61 3F B2 D3 F4 53 D2 E4'
    ])
    reader.addResponse(
        '1A 00', ['AF 5D 99 4C E0 85 F2 40 89', 'AF 84 76 D1 CF 30 24 B7 C7'])
    reader.addResponse('AF 21 D0 AD 5F 2F D9 74 54 A7 46 CC 80 56 7F 1B 1C',
                       ['00 91 3C 6D ED 84 22 1C 41'])
    #Get Card information
    reader.addResponse('60', ['AF 04 01 01 01 00 1A 05'])
    reader.addResponse('AF', [
        'AF 04 01 01 01 04 1A 05',
        '00 04 06 3F 72 63 34 80 BA 45 19 E3 20 49 13 CD C8 10 BA FA 40 17 59'
    ])
    #Format Card
    reader.addResponse('FC', ['00 9C 2C 81 3A 06 5C 45 F7'])
    #Create Application 2 Key 2K3DES
    reader.addResponse('CA 16 DE 00 0F 02', ['00 0A 13 79 B0 1D 85 AD 47'])
    #Create Application 1 Key 2K3DES
    reader.addResponse('CA CC BB AA 0F 01', ['00 F1 1A C0 73 8E F8 38 78'])
    #Create Application AES
    reader.addResponse('CA 16 AE 00 0F 82', ['00 3B 68 D7 2A 3B E0 D2 0C'])
    #Create Application 3K3DES
    reader.addResponse('CA 24 DE 00 0F 42', ['00 5D 73 AE 52 87 A1 BB E4'])
    #Get Application IDS
    reader.addResponse('6A', [
        '00 16 DE 00 24 DE 00 16 AE 00 CC BB AA 27 39 15 4E 26 30 D6 50',
        '00 16 DE 00 24 DE 00 16 AE 00 52 0E 51 E0 0A F0 6D 5E'
    ])
    #Delete Application
    reader.addResponse('DA CC BB AA', ['00 A9 AF 19 05 22 92 F6 62'])
    #Select Application
    reader.addResponse('5A 16 DE 00', ['00'])
    #Authenticate 2
    reader.addResponse('AF DA C6 7A B7 43 76 3D C9 FA F8 A0 AE 50 4E 80 C5',
                       ['00 13 E9 E4 FA 43 88 BF 16'])
    #Cange Key Setting
    reader.addResponse('54 27 88 28 05 FC 3F D4 9D',
                       ['00 A6 28 37 83 74 27 0A CD'])

    desfire = DESFire(reader)
    key_setting = desfire.getKeySetting()
    desfire.authenticate(0, key_setting, '84 9B 36 C5 F8 BF 4A 09')
    desfire.getCardVersion()
    desfire.formatCard()
    desfire.createApplication("00 DE 16", [
        DESFireKeySettings.KS_ALLOW_CHANGE_MK,
        DESFireKeySettings.KS_LISTING_WITHOUT_MK,
        DESFireKeySettings.KS_CREATE_DELETE_WITHOUT_MK,
        DESFireKeySettings.KS_CONFIGURATION_CHANGEABLE
    ], 2, DESFireKeyType.DF_KEY_2K3DES)
    desfire.createApplication("00 DE 24", [
        DESFireKeySettings.KS_ALLOW_CHANGE_MK,
        DESFireKeySettings.KS_LISTING_WITHOUT_MK,
        DESFireKeySettings.KS_CREATE_DELETE_WITHOUT_MK,
        DESFireKeySettings.KS_CONFIGURATION_CHANGEABLE
    ], 2, DESFireKeyType.DF_KEY_3K3DES)
    desfire.createApplication("00 AE 16", [
        DESFireKeySettings.KS_ALLOW_CHANGE_MK,
        DESFireKeySettings.KS_LISTING_WITHOUT_MK,
        DESFireKeySettings.KS_CREATE_DELETE_WITHOUT_MK,
        DESFireKeySettings.KS_CONFIGURATION_CHANGEABLE
    ], 2, DESFireKeyType.DF_KEY_AES)
    desfire.createApplication("AA BB CC", [
        DESFireKeySettings.KS_ALLOW_CHANGE_MK,
        DESFireKeySettings.KS_LISTING_WITHOUT_MK,
        DESFireKeySettings.KS_CREATE_DELETE_WITHOUT_MK,
        DESFireKeySettings.KS_CONFIGURATION_CHANGEABLE
    ], 1, DESFireKeyType.DF_KEY_2K3DES)
    desfire.getApplicationIDs()
    desfire.deleteApplication("AA BB CC")
    desfire.getApplicationIDs()
    desfire.selectApplication('00 DE 16')
    key_setting.setKey('00 00 00 00 00 00 00 00')
    desfire.authenticate(0, key_setting, '49 EC 63 DE CD E0 07 72')
    desfire.getKeySetting()
    desfire.changeKeySettings([
        DESFireKeySettings.KS_ALLOW_CHANGE_MK,
        DESFireKeySettings.KS_CREATE_DELETE_WITHOUT_MK,
        DESFireKeySettings.KS_CONFIGURATION_CHANGEABLE
    ])
    desfire.getKeySetting()

    print('[+] Test_DES Succsess')