Exemple #1
0
class EncryptedKey(univ.Choice):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('encryptedValue', EncryptedValue()),
        namedtype.NamedType(
            'envelopedData',
            rfc2315.EnvelopedData().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatConstructed, 0))))
def Signature(pe):
    try:
        security = pe.OPTIONAL_HEADER.DATA_DIRECTORY[
            pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']]
    except IndexError:
        print(' No signature')
        return

    address = security.VirtualAddress
    size = security.Size

    if address == 0:
        print(' No signature')
        return

    signature = pe.write()[address + 8:address + size]
    if len(signature) != size - 8:
        print(
            ' Unable to extract full signature, file is most likely truncated')
        print(' Extracted: %d bytes' % len(signature))
        print(' Expected: %d bytes' % (size - 8))
        return

    try:
        from pyasn1.codec.der import decoder as der_decoder
    except ImportError:
        print(' Signature present but error importing pyasn1 module')
        return
    try:
        from pyasn1_modules import rfc2315
    except ImportError:
        print(' Signature present but error importing pyasn1_modules module')
        return

    signatureArg = C2SIP2(signature)

    contentInfo, _ = der_decoder.decode(signatureArg,
                                        asn1Spec=rfc2315.ContentInfo())
    contentType = contentInfo.getComponentByName('contentType')
    contentInfoMap = {
        (1, 2, 840, 113549, 1, 7, 1): rfc2315.Data(),
        (1, 2, 840, 113549, 1, 7, 2): rfc2315.SignedData(),
        (1, 2, 840, 113549, 1, 7, 3): rfc2315.EnvelopedData(),
        (1, 2, 840, 113549, 1, 7, 4): rfc2315.SignedAndEnvelopedData(),
        (1, 2, 840, 113549, 1, 7, 5): rfc2315.DigestedData(),
        (1, 2, 840, 113549, 1, 7, 6): rfc2315.EncryptedData()
    }
    content, _ = der_decoder.decode(contentInfo.getComponentByName('content'),
                                    asn1Spec=contentInfoMap[contentType])

    for line in content.prettyPrint().split('\n'):
        print(line)
        oMatch = re.match('( *)value=0x....(.+)', line)
        if oMatch != None:
            if sys.version_info[0] > 2:
                print(oMatch.groups()[0] + '      ' +
                      repr(binascii.a2b_hex(oMatch.groups()[1]).decode()))
            else:
                print(oMatch.groups()[0] + '      ' +
                      repr(binascii.a2b_hex(oMatch.groups()[1])))
    def testDerCodec(self):

        substrate = pem.readBase64fromText(self.pem_text_unordered)

        asn1Object, rest = der_decoder.decode(substrate,
                                              asn1Spec=self.asn1Spec)

        assert not rest
        assert asn1Object.prettyPrint()
        assert der_encoder.encode(asn1Object) == substrate

        contentType = asn1Object['contentType']
        substrate = asn1Object['content']

        contentInfoMap = {
            (1, 2, 840, 113549, 1, 7, 1): rfc2315.Data(),
            (1, 2, 840, 113549, 1, 7, 2): rfc2315.SignedData(),
            (1, 2, 840, 113549, 1, 7, 3): rfc2315.EnvelopedData(),
            (1, 2, 840, 113549, 1, 7, 4): rfc2315.SignedAndEnvelopedData(),
            (1, 2, 840, 113549, 1, 7, 5): rfc2315.DigestedData(),
            (1, 2, 840, 113549, 1, 7, 6): rfc2315.EncryptedData()
        }

        innerAsn1Object, rest = der_decoder.decode(
            substrate, asn1Spec=contentInfoMap[contentType])

        asn1Object['content'] = der_encoder.encode(innerAsn1Object)

        substrate = pem.readBase64fromText(self.pem_text_reordered)

        assert not rest
        assert asn1Object.prettyPrint()
        assert der_encoder.encode(asn1Object) == substrate
Exemple #4
0
def apk_signatures(cert_file_object):
    """
  returns a 3-tuple with the hexstring md5, sha1, sha256 hashes of the
  first certificate of a pkcs7 signature, intended for apk signatures

  cert_file_object is a file-like object in binary mode
  """
    # TODO zipfile objects don't have the b in the mode even though they are
    # binary so we can't check mode
    content_info, _ = der_decoder.decode(cert_file_object.read(),
                                         asn1Spec=rfc2315.ContentInfo())
    content_type = content_info.getComponentByName("contentType")
    content_info_map = {
        (1, 2, 840, 113549, 1, 7, 1): rfc2315.Data(),
        (1, 2, 840, 113549, 1, 7, 2): rfc2315.SignedData(),
        (1, 2, 840, 113549, 1, 7, 3): rfc2315.EnvelopedData(),
        (1, 2, 840, 113549, 1, 7, 4): rfc2315.SignedAndEnvelopedData(),
        (1, 2, 840, 113549, 1, 7, 5): rfc2315.DigestedData(),
        (1, 2, 840, 113549, 1, 7, 6): rfc2315.EncryptedData()
    }
    content, _ = der_decoder.decode(content_info.getComponentByName("content"),
                                    asn1Spec=content_info_map[content_type])
    certs = content.getComponentByName("certificates")
    der = der_encoder.encode(certs[0])
    return file_hashes(io.BytesIO(der))
def Signature(pe):
    try:
        security = pe.OPTIONAL_HEADER.DATA_DIRECTORY[
            pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']]
    except IndexError:
        print(' No signature')
        return

    address = security.VirtualAddress
    size = security.Size

    if address == 0:
        print(' No signature')
        return

    signature = pe.write()[address + 8:address + size]

    try:
        from pyasn1.codec.der import decoder as der_decoder
    except:
        print(' Signature present but error importing pyasn1 module')
        return
    try:
        from pyasn1_modules import rfc2315
    except:
        print(' Signature present but error importing pyasn1_modules module')
        return

    contentInfo, _ = der_decoder.decode(str(signature),
                                        asn1Spec=rfc2315.ContentInfo())
    contentType = contentInfo.getComponentByName('contentType')
    contentInfoMap = {
        (1, 2, 840, 113549, 1, 7, 1): rfc2315.Data(),
        (1, 2, 840, 113549, 1, 7, 2): rfc2315.SignedData(),
        (1, 2, 840, 113549, 1, 7, 3): rfc2315.EnvelopedData(),
        (1, 2, 840, 113549, 1, 7, 4): rfc2315.SignedAndEnvelopedData(),
        (1, 2, 840, 113549, 1, 7, 5): rfc2315.DigestedData(),
        (1, 2, 840, 113549, 1, 7, 6): rfc2315.EncryptedData()
    }
    content, _ = der_decoder.decode(contentInfo.getComponentByName('content'),
                                    asn1Spec=contentInfoMap[contentType])

    for line in content.prettyPrint().split('\n'):
        print(line)
        oMatch = re.match('( *)value=0x....(.+)', line)
        if oMatch != None:
            print(oMatch.groups()[0] + '      ' +
                  repr(binascii.a2b_hex(oMatch.groups()[1])))
Exemple #6
0
def decode(signature):
    contentInfo, _ = der_decoder.decode(signature,
                                        asn1Spec=rfc2315.ContentInfo())
    contentType = contentInfo.getComponentByName('contentType')

    contentInfoMap = {
        (1, 2, 840, 113549, 1, 7, 1): rfc2315.Data(),
        (1, 2, 840, 113549, 1, 7, 2): rfc2315.SignedData(),
        (1, 2, 840, 113549, 1, 7, 3): rfc2315.EnvelopedData(),
        (1, 2, 840, 113549, 1, 7, 4): rfc2315.SignedAndEnvelopedData(),
        (1, 2, 840, 113549, 1, 7, 5): rfc2315.DigestedData(),
        (1, 2, 840, 113549, 1, 7, 6): rfc2315.EncryptedData()
    }

    content, _ = der_decoder.decode(contentInfo.getComponentByName('content'),
                                    asn1Spec=contentInfoMap[contentType])

    return content
def _extract_certs_from_authenticode_blob(buf):

    contentInfo, _ = der_decoder.decode(buf, asn1Spec=rfc2315.ContentInfo())
    contentType = contentInfo.getComponentByName('contentType')
    contentInfoMap = {
        (1, 2, 840, 113549, 1, 7, 1): rfc2315.Data(),
        (1, 2, 840, 113549, 1, 7, 2): rfc2315.SignedData(),
        (1, 2, 840, 113549, 1, 7, 3): rfc2315.EnvelopedData(),
        (1, 2, 840, 113549, 1, 7, 4): rfc2315.SignedAndEnvelopedData(),
        (1, 2, 840, 113549, 1, 7, 5): rfc2315.DigestedData(),
        (1, 2, 840, 113549, 1, 7, 6): rfc2315.EncryptedData()
    }
    content, _ = der_decoder.decode(contentInfo.getComponentByName('content'),
                                    asn1Spec=contentInfoMap[contentType])
    certs = []
    for cert in content['certificates']:
        tbscert = cert['certificate']['tbsCertificate']
        certs.append(_extract_authenticode_tbscerts(tbscert))
    for c in content['signerInfos']:
        tbscert = c['issuerAndSerialNumber']
        certs.append(_extract_authenticode_tbscerts(tbscert))
    return certs
Exemple #8
0
    sys.stdin, ('-----BEGIN PKCS7-----', '-----END PKCS7-----')
    )

assert substrate, 'bad PKCS7 data on input'
        
contentInfo, rest = decoder.decode(substrate, asn1Spec=rfc2315.ContentInfo())

if rest: substrate = substrate[:-len(rest)]
    
print(contentInfo.prettyPrint())

assert encoder.encode(contentInfo) == substrate, 're-encode fails'

contentType = contentInfo.getComponentByName('contentType')

contentInfoMap = {
    (1, 2, 840, 113549, 1, 7, 1): rfc2315.Data(),
    (1, 2, 840, 113549, 1, 7, 2): rfc2315.SignedData(),
    (1, 2, 840, 113549, 1, 7, 3): rfc2315.EnvelopedData(),
    (1, 2, 840, 113549, 1, 7, 4): rfc2315.SignedAndEnvelopedData(),
    (1, 2, 840, 113549, 1, 7, 5): rfc2315.DigestedData(),
    (1, 2, 840, 113549, 1, 7, 6): rfc2315.EncryptedData()
    }

content, _ = decoder.decode(
    contentInfo.getComponentByName('content'),
    asn1Spec=contentInfoMap[contentType]
    )

print(content.prettyPrint())
def print_rsa(file_name):
        file = open(file_name,"r")
        buffer = file.read()
        buffer_base = base64.b64encode(buffer)
        file.close()
        
        file = open(file_name + ".pem","w")
        file.write('-----BEGIN PKCS7-----\n')
        file.write(buffer_base)
        file.write('\n-----END PKCS7-----\n')
        file.close()
        
        file = open(file_name + ".pem","r")
        
        idx, substrate = pem.readPemBlocksFromFile(
            file, ('-----BEGIN PKCS7-----', '-----END PKCS7-----')
            )
        
        file.close()
        assert substrate, 'bad PKCS7 data on input'
                
        contentInfo, rest = decoder.decode(substrate, asn1Spec=rfc2315.ContentInfo())
        
        if rest: substrate = substrate[:-len(rest)]
        
        #/home/retme/Desktop/xx/SIGN.RSA
        
        #print contentInfo
            #ContentInfo
        print(contentInfo.prettyPrint())
        buf =   contentInfo.getComponentByName('content')

        assert encoder.encode(contentInfo, defMode=False) == substrate or \
               encoder.encode(contentInfo, defMode=True) == substrate, \
               're-encode fails'
        
        contentType = contentInfo.getComponentByName('contentType')
        
        #print contentInfo
        #certificates = contentInfo.getComponentByName('certificates')
        
        #certificates.prettyPrint()
        #print certificates
        contentInfoMap = {
            (1, 2, 840, 113549, 1, 7, 1): rfc2315.Data(),
            (1, 2, 840, 113549, 1, 7, 2): rfc2315.SignedData(),
            (1, 2, 840, 113549, 1, 7, 3): rfc2315.EnvelopedData(),
            (1, 2, 840, 113549, 1, 7, 4): rfc2315.SignedAndEnvelopedData(),
            (1, 2, 840, 113549, 1, 7, 5): rfc2315.DigestedData(),
            (1, 2, 840, 113549, 1, 7, 6): rfc2315.EncryptedData()
            }
        
        content, _ = decoder.decode(
            contentInfo.getComponentByName('content'),
            asn1Spec=contentInfoMap[contentType]
            )
        
        
        #content.getComponentByName('certificates').setComponentByPosition(1)
        #print content.getComponentByName('certificates').getComponentByPosition(0).getComponentByName('certificate').getComponentByName('tbsCertificate').getComponentByName('serialNumber')
        
        
        
        #print content
        print(content.prettyPrint())