Exemple #1
0
    def test_process_discovery_versions(self):
        """
        'DiscoveryVersion':
         - request:
           --  header with protocol version and credential authentication;
           --  batch item 'Operation DiscoverVersions' without parameters
         - expected:
           -- response header with procotol-version, time stamp, batch count
           -- batch item with two versions supported by server
        """
        request = hex2bin(
            '42007801000000b04200770100000088420069010000002042006a02000000040'
            '00000010000000042006b0200000004000000010000000042000c010000004842'
            '00230100000040420024050000000400000001000000004200250100000028420'
            '099070000000a4b6d6970436c69656e740000000000004200a10700000006436f'
            '75636f75000042000d0200000004000000010000000042000f010000001842005'
            'c05000000040000001e000000004200790100000000')
        response = hex2bin(
            '42007b01000000d042007a0100000048420069010000002042006a02000000040'
            '00000010000000042006b02000000040000000100000000420092090000000800'
            '00000056bda8eb42000d0200000004000000010000000042000f0100000078420'
            '05c05000000040000001e0000000042007f050000000400000000000000004200'
            '7c0100000050420069010000002042006a0200000004000000010000000042006'
            'b02000000040000000100000000420069010000002042006a0200000004000000'
            '010000000042006b02000000040000000000000000')

        self._integration_test_process(request, response)
Exemple #2
0
    def test_process_discovery_versions(self):
        """
        'DiscoveryVersion':
         - request:
           --  header with protocol version and credential authentication;
           --  batch item 'Operation DiscoverVersions' without parameters
         - expected:
           -- response header with procotol-version, time stamp, batch count
           -- batch item with two versions supported by server
        """
        request = hex2bin(
            '42007801000000b04200770100000088420069010000002042006a02000000040'
            '00000010000000042006b0200000004000000010000000042000c010000004842'
            '00230100000040420024050000000400000001000000004200250100000028420'
            '099070000000a4b6d6970436c69656e740000000000004200a10700000006436f'
            '75636f75000042000d0200000004000000010000000042000f010000001842005'
            'c05000000040000001e000000004200790100000000')
        response = hex2bin(
            '42007b01000000d042007a0100000048420069010000002042006a02000000040'
            '00000010000000042006b02000000040000000100000000420092090000000800'
            '00000056bda8eb42000d0200000004000000010000000042000f0100000078420'
            '05c05000000040000001e0000000042007f050000000400000000000000004200'
            '7c0100000050420069010000002042006a0200000004000000010000000042006'
            'b02000000040000000100000000420069010000002042006a0200000004000000'
            '010000000042006b02000000040000000000000000')

        self._integration_test_process(request, response)
Exemple #3
0
	def decrypt(self, file_json_data, password):
		salt = hex2bin(file_json_data[0])
		iv = hex2bin(file_json_data[1])
		encdata = hex2bin(file_json_data[2])
		key = self.makeKey(password, salt)
		cipher = AES.new(key, AES.MODE_CFB, iv)
		decdata = cipher.decrypt(encdata).decode('utf8')
		return tuple(json.loads(decdata))
Exemple #4
0
 def decrypt(self, file_json_data, password):
     salt = hex2bin(file_json_data[0])
     iv = hex2bin(file_json_data[1])
     encdata = hex2bin(file_json_data[2])
     key = self.makeKey(password, salt)
     cipher = AES.new(key, AES.MODE_CFB, iv)
     decdata = cipher.decrypt(encdata).decode('utf8')
     return tuple(json.loads(decdata))
Exemple #5
0
    def test_process_create_symmetric_key(self):
        """
        'Create':
         - request:
           --  header with protocol version and credential authentication;
           --  batch item 'Operation Create':
               -- object type : symmetric key
               -- attributes:
                  -- Cryptographic Algorithm: AES
                  -- Cryptographic Usage Mask
                  -- Cryptographic Length: 128
                  -- Name
         - expected:
           -- response header with procotol-version, time stamp, batch count
           -- batch item with:
             -- operation: Create;
             -- result status: Success;
             -- response payload:
                -- object type: symmetric key;
                -- UID: '1';
                -- attribute template:
                   -- 'Unique Identifier': '1'
        """
        request = hex2bin(
            '42007801000001b04200770100000088420069010000002042006a02000000040'
            '00000010000000042006b0200000004000000010000000042000c010000004842'
            '00230100000040420024050000000400000001000000004200250100000028420'
            '099070000000a4b6d6970436c69656e740000000000004200a10700000006436f'
            '75636f75000042000d0200000004000000010000000042000f010000011842005'
            'c0500000004000000010000000042007901000001004200570500000004000000'
            '020000000042009101000000e8420008010000003042000a07000000174372797'
            '0746f6772617068696320416c676f726974686d0042000b050000000400000003'
            '00000000420008010000003042000a070000001843727970746f6772617068696'
            '3205573616765204d61736b42000b02000000040000000c000000004200080100'
            '00003042000a070000001443727970746f67726170686963204c656e677468000'
            '0000042000b02000000040000008000000000420008010000003842000a070000'
            '00044e616d650000000042000b0100000020420055070000000854657374204b6'
            '57942005405000000040000000100000000')
        response = hex2bin(
            '42007b01000000e042007a0100000048420069010000002042006a02000000040'
            '00000010000000042006b02000000040000000100000000420092090000000800'
            '00000056c488d742000d0200000004000000010000000042000f0100000088420'
            '05c0500000004000000010000000042007f050000000400000000000000004200'
            '7c010000006042005705000000040000000200000000420094070000000131000'
            '000000000004200910100000038420008010000003042000a0700000011556e69'
            '717565204964656e7469666965720000000000000042000b07000000013100000'
            '000000000')

        self._integration_test_process(request, response)
Exemple #6
0
    def test_process_create_symmetric_key(self):
        """
        'Create':
         - request:
           --  header with protocol version and credential authentication;
           --  batch item 'Operation Create':
               -- object type : symmetric key
               -- attributes:
                  -- Cryptographic Algorithm: AES
                  -- Cryptographic Usage Mask
                  -- Cryptographic Length: 128
                  -- Name
         - expected:
           -- response header with procotol-version, time stamp, batch count
           -- batch item with:
             -- operation: Create;
             -- result status: Success;
             -- response payload:
                -- object type: symmetric key;
                -- UID: '1';
                -- attribute template:
                   -- 'Unique Identifier': '1'
        """
        request = hex2bin(
            '42007801000001b04200770100000088420069010000002042006a02000000040'
            '00000010000000042006b0200000004000000010000000042000c010000004842'
            '00230100000040420024050000000400000001000000004200250100000028420'
            '099070000000a4b6d6970436c69656e740000000000004200a10700000006436f'
            '75636f75000042000d0200000004000000010000000042000f010000011842005'
            'c0500000004000000010000000042007901000001004200570500000004000000'
            '020000000042009101000000e8420008010000003042000a07000000174372797'
            '0746f6772617068696320416c676f726974686d0042000b050000000400000003'
            '00000000420008010000003042000a070000001843727970746f6772617068696'
            '3205573616765204d61736b42000b02000000040000000c000000004200080100'
            '00003042000a070000001443727970746f67726170686963204c656e677468000'
            '0000042000b02000000040000008000000000420008010000003842000a070000'
            '00044e616d650000000042000b0100000020420055070000000854657374204b6'
            '57942005405000000040000000100000000')
        response = hex2bin(
            '42007b01000000e042007a0100000048420069010000002042006a02000000040'
            '00000010000000042006b02000000040000000100000000420092090000000800'
            '00000056c488d742000d0200000004000000010000000042000f0100000088420'
            '05c0500000004000000010000000042007f050000000400000000000000004200'
            '7c010000006042005705000000040000000200000000420094070000000131000'
            '000000000004200910100000038420008010000003042000a0700000011556e69'
            '717565204964656e7469666965720000000000000042000b07000000013100000'
            '000000000')

        self._integration_test_process(request, response)
Exemple #7
0
def getSignature(information):

    keys = information.keys()
    keys.sort()

    total = u''.join([information[key]
                      for key in keys]).replace(u"'", u"'\\''")

    hash = sha.new(total.encode('utf-8')).hexdigest()

    paddedHash = '0001'

    for i in range(0, 105):
        paddedHash += 'ff'

    paddedHash += '00' + hash

    decryptedSig = hex2dec(paddedHash)

    sig = powmod(decryptedSig, hex2dec(privKey), hex2dec(pubKey))
    sig = dec2hex(sig)
    sig = hex2bin(sig)
    sig = b64encode(sig)

    return sig
Exemple #8
0
def _String_To_Bytes(string, add_escapes=False):
    '''
    Converts the given string into bytes.
    Strings should either be hex representations (2 characters at a time)
    or wildcards given as '.' (also in pairs, where .. matches a single
    wildcard byte). Byte values that match special regex control
    chars will be escaped.

    * add_escapes
      - Bool, if True then re.escape will be called on the non-wildcard
        entries.
      - This should be applied if the bytes will be used as a regex pattern.
    '''
    # Replace the spaces.
    # To make striding more convenient, double all + and - so that they take
    #  up 2 chars each.
    string = string.replace('-', '--').replace('+', '++')

    # Make sure the input is even length, since hex conversions
    #  require 2 chars at a time (to make up a full byte).
    assert len(string) % 2 == 0

    new_bytes = b''

    # Loop over the pairs, using even indices.
    for even_index in range(0, len(string), 2):
        char_pair = string[even_index:even_index + 2]

        # Special chars will be handled directly.
        if char_pair == '..':
            # Encode as a single '.' so this matches one byte.
            new_bytes += str.encode('.')
        # Everything else should be strings representing hex values.
        else:
            this_byte = hex2bin(char_pair)
            # Note: for low values, this tends to produce a special
            #  string in the form '\x##', but for values that can map
            #  to normal characters, it uses that character instead.
            # However, that character could also be a special regex
            #  character, and hence direct mapping is not safe.
            # As a workaround, aim to always put an escape character
            #  prior to the encoded byte; however, this requires that
            #  the escape be a byte also (a normal python escape will
            #  escape the / in /x## and blows up). Hopefully regex
            #  will unpack the byte escape and work fine.
            # Use re.escape for this, since trying to do it manually
            #  is way too much effort (get 'bad escape' style errors
            #  easily).
            # Note: re.escape does something weird with \x00, converting
            #  it to \\000, but this appears to be okay in practice.
            if add_escapes:
                this_byte = re.escape(this_byte)
            new_bytes += this_byte

    return new_bytes
Exemple #9
0
    def _setTimeStamp(self, data, ref):
        """
        Take raw TimeStamp data from 'ref' and update with it
        the TimeStamp in 'data'.

        Applied to the data blob sent by processor before comparing it
        with the reference response data.
        """

        time_stamp_pattern = hex2bin('4200920900000008')
        try:
            idx_data = data.index(time_stamp_pattern)
            idx_ref = ref.index(time_stamp_pattern)
        except Exception:
            return data
        data = data[:idx_data] + ref[idx_ref:idx_ref+16] + data[idx_data+16:]
        return data
Exemple #10
0
    def _setTimeStamp(self, data, ref):
        """
        Take raw TimeStamp data from 'ref' and update with it
        the TimeStamp in 'data'.

        Applied to the data blob sent by processor before comparing it
        with the reference response data.
        """

        time_stamp_pattern = hex2bin('4200920900000008')
        try:
            idx_data = data.index(time_stamp_pattern)
            idx_ref = ref.index(time_stamp_pattern)
        except Exception:
            return data
        data = data[:idx_data] + ref[idx_ref:idx_ref + 16] + data[idx_data +
                                                                  16:]
        return data
def getSignature(information):
	
	keys = information.keys()
	keys.sort()
	
	total = u''.join([information[key] for key in keys]).replace(u"'", u"'\\''")
	
	hash = sha.new(total.encode('utf-8')).hexdigest()
	
	paddedHash = '0001'

	for i in range(0, 105):
		paddedHash += 'ff'

	paddedHash += '00' + hash
		
	decryptedSig = hex2dec(paddedHash)
	
	sig = powmod(decryptedSig, hex2dec(privKey), hex2dec(pubKey))
	sig = dec2hex(sig)
	sig = hex2bin(sig)
	sig = b64encode(sig)

	return sig
Exemple #12
0
def Apply_Binary_Patch_Group(patch_list):
    '''
    Applies a group of patches as a single unit.
    If any patch runs into an error, no patch in the group will be applied.
    Returns True on success, False on failure.
    '''
    # Start with a search for matches.
    # These calls may raise an exception on error, or could return None
    #  in dev mode.
    matches_list = []
    for patch in patch_list:
        matches_list.append(_Get_Matches(patch))

    # Return early if a None was returned.
    if None in matches_list:

        # Get the indices of the patches that had errors or no errors.
        correct_patches = []
        failed_patches = []
        for index, matches in enumerate(matches_list):
            if matches == None:
                failed_patches.append(index)
            else:
                correct_patches.append(index)

        # Print them.
        if Settings.developer:
            Print('Correct patches : {}.'.format(correct_patches))
            Print('Failed patches  : {}.'.format(failed_patches))
        return False

    # It should now be safe to apply all patches in the group.
    file_contents = File_Manager.Load_File(patch.file)

    for patch, matches in zip(patch_list, matches_list):
        # With at least one match, set as modified.
        # (If the patch defined 0 matches as okay, this may be skipped.)
        file_contents.Set_Modified()

        # Loop over the matches to apply each of them.
        for match in matches:

            # Grab the offset of the match.
            offset = match.start()

            # Verify there are a matched number of insertions and
            #  deletions in the new_code.
            if patch.new_code.count('+') != patch.new_code.count('-'):
                raise Exception('Error: Binary patch changes code size.')

            #-Removed; old style before insert/delete characters were
            #  added in. Was this unsafe on the wildcards anyway, which
            #  could take the same value as normal bytes? May have just
            #  gotten lucky that this case didn't come up.
            ## Get the wildcard char, as an int (since the loop below unpacks
            ##  the byte string into ints automatically, and also pulls ints
            ##  from the original binary).
            #wildcard = str.encode('.')[0]
            ## Apply the patch, leaving wildcard entries unchanged.
            ## This will edit in place on the bytearray.
            #new_bytes = _String_To_Bytes(patch.new_code)
            #for index, new_byte in enumerate(new_bytes):
            #    if new_byte == wildcard:
            #        continue
            #    file_contents.binary[offset + index] = new_byte

            # Stride through the new code.
            # For convenience, this will work on char pairs (for byte
            #  conversion when needed), and so a pre-pass will duplicate
            #  all control characters (+-) accordingly. '.' is not
            #  duplicated since it is already doubled in the original
            #  string.
            new_code = patch.new_code
            for control_char in ['+', '-']:
                new_code = new_code.replace(control_char, control_char * 2)

            # Note the code length before starting, for error check later.
            start_length = len(file_contents.binary)

            # Loop over the pairs, using even indices.
            for even_index in range(0, len(new_code), 2):
                char_pair = new_code[even_index:even_index + 2]

                # If this is a wildcard, advance the offset with no change.
                if char_pair == '..':
                    offset += 1

                # If this is a deletion, remove the byte from the
                #  file_contents and do not advance the offset (which will
                #  then be pointing at the post-deletion byte automatically).
                elif char_pair == '--':
                    file_contents.binary.pop(offset)

                # If this is an addition, insert a 0.
                elif char_pair == '++':
                    file_contents.binary.insert(offset, 0)

                else:
                    # This is a replacement byte.
                    # Convert it, insert, and inc the offset.
                    # Note: bytearray requires an int version of this value,
                    #  and hex2bin returns a byte string version.
                    #  Indexing into a bytearray of byte strings
                    #  returns an int, not a string.
                    new_byte = hex2bin(char_pair)[0]
                    file_contents.binary[offset] = new_byte
                    offset += 1

            # Error check.
            assert len(file_contents.binary) == start_length
    return True