android apk v1 签名验证分析

作者:jafon.tian

转载请注明出处:https://blog.csdn.net/JT_Notes

Android的应用包格式具有防篡改,防重打包的能力,这是如何做到的?秘密就隐藏在META-INF文件夹中,下面通过QQ安装包的分析来进行详细的说明和分析。

1. 准备

1 QQ官网下载Android安装包mobileqq_android.apk
2 将安装包后缀由apk改为zip,使用解压软件释放中其中的文件
3 将其中的META-INF文件夹拷贝出来,用于后续分析

  META-INF/

    --ANDROIDR.RSA

    --ANDROIDR.SF

    --MANIFEST.MF

2. 文件分析

名称 说明
MANIFEST.MF 包含了除META-INFO目录外所有文件的路径和hash结果
ANDROIDR.SF 对MF整个文件和里面每个条目进行了二次hash
ANDROIDR.RSA 包含开发者证书以及使用开发者私钥对SF文件的签名

文件之间的逐层保护关系为 RSA文件–>SF文件–>MF文件–>应用其他文件

2.1 MANIFEST.MF文件

下面截取文件头部的片段进行说明,除了文件头,其他文件内容都遵循相同的规则。

Manifest-Version: 1.0

Name: AndroidManifest.xml
SHA1-Digest: 8tS1gpP+QbotpYp6jN1PCfulDbU=

名称 说明
Manifest-Version: 1.0 Manifest版本信息,应该是用于日后扩展使用
Name: AndroidManifest.xml
SHA1-Digest: 8tS1gpP+QbotpYp6jN1PCfulDbU=
Name字段指明应用文件全路径
SHA1-Digest字段指明使用的hash算法是SHA1以及计算结果的base64编码

SHA1-Digest计算过程:

  1. 对文件AndroidManifest.xml的进行SHA1运算,结果用16进制字符串表示为:

      f2d4b58293fe41ba2da58a7a8cdd4f09fba50db5
  2. 将16进制字符串进行base64编码的结果为:

      8tS1gpP+QbotpYp6jN1PCfulDbU=

2.2 ANDROIDR.SF文件

下面截取文件头部的片段进行说明,与MF文件类似,除了文件头,其他文件内容也是遵循相同的规则。

Signature-Version: 1.0
Created-By: 1.0 (Android)
SHA1-Digest-Manifest: ouJT4yGiaaiMea/Wu6708IP44P4=
X-Android-APK-Signed: 2

Name: AndroidManifest.xml
SHA1-Digest: lu99E667G2n1uJJ2nDaMFNZLEmE=
名称 说明
Signature-Version: 1.0
Created-By: 1.0 (Android)
X-Android-APK-Signed: 2
版本信息
SHA1-Digest-Manifest: ouJT4yGiaaiMea/Wu6708IP44P4= 对MANIFEST.MF整个文件进行hash运算结果的base64编码形式,hash运算采用SHA1算法
Name: AndroidManifest.xml
SHA1-Digest: lu99E667G2n1uJJ2nDaMFNZLEmE=
Name字段指明应用文件全路径
SHA1-Digest字段是对SF中对应字段内容的hash运算结果,详细见下文说明

SHA1-Digest-Manifest计算过程:

  1. 对文件MANIFEST.MF进行SHA1运算,结果的16进制字符串表示为:

      a2e253e321a269a88c79afd6bbaef4f083f8e0fe
  2. 将16进制字符串进行base64编码的结果为:

      ouJT4yGiaaiMea/Wu6708IP44P4=

SHA1-Digest计算过程:

  1. 在MANIFEST.MF文件中找到具有相同Name字段的内容。

在MANIFEST.MF文件中找到Name字段为AndroidManifest.xml的条目



Name: AndroidManifest.xml

SHA1-Digest: 8tS1gpP+QbotpYp6jN1PCfulDbU=



为了准确,这里同时给出处理之前16进制的表示形式

00000000h: 4E 61 6D 65 3A 20 41 6E 64 72 6F 69 64 4D 61 6E ; Name: AndroidMan

00000010h: 69 66 65 73 74 2E 78 6D 6C 0D 0A 53 48 41 31 2D ; ifest.xml…SHA1-

00000020h: 44 69 67 65 73 74 3A 20 38 74 53 31 67 70 50 2B ; Digest: 8tS1gpP+

00000030h: 51 62 6F 74 70 59 70 36 6A 4E 31 50 43 66 75 6C ; QbotpYp6jN1PCful

00000040h: 44 62 55 3D                                  ; DbU=

  1. 对内容进行格式化处理,每一对Key-Value占据一行,最后一行添加换行符(\r\n)



Name: AndroidManifest.xml

SHA1-Digest: 8tS1gpP+QbotpYp6jN1PCfulDbU=





为了准确,这里也同时给出处理之前16进制的表示形式

00000000h: 4E 61 6D 65 3A 20 41 6E 64 72 6F 69 64 4D 61 6E ; Name: AndroidMan

00000010h: 69 66 65 73 74 2E 78 6D 6C 0D 0A 53 48 41 31 2D ; ifest.xml…SHA1-

00000020h: 44 69 67 65 73 74 3A 20 38 74 53 31 67 70 50 2B ; Digest: 8tS1gpP+

00000030h: 51 62 6F 74 70 59 70 36 6A 4E 31 50 43 66 75 6C ; QbotpYp6jN1PCful

00000040h: 44 62 55 3D 0D 0A 0D 0A ; DbU=…

  1. 对格式化之后的数据进行SHA1运算。
    结果的16进制字符串为:

    96ef7d13aebb1b69f5b892769c368c14d64b1261
  2. 将16进制字符串进行base64编码的结果为:

    lu99E667G2n1uJJ2nDaMFNZLEmE=

2.3 ANDROIDR.RSA文件

ANDROIDR.RSA是个DER格式的文件,可通过openssl对其进行解析(完整解析见文章最后的附录部分)。
此文件包含开发者公钥、开发者私钥对自身公钥证书的签名和对ANDROIDR.SF文件的签名。从RSA文件中提取这三部分信息。

名称 说明
开发者公钥 n:
a15e9756216f694c5915e0b529095254367c4e64faeff07ae13488d946615a58
ddc31a415f717d019edc6d30b9603d3e2a7b3de0ab7e0cf52dfee39373bc472f
a997027d798d59f81d525a69ecf156e885fd1e2790924386b2230cc90e3b7adc
95603ddcf4c40bdc72f22db0f216a99c371d3bf89cba6578c60699e8a0d53695
e:
010001
对自身公钥证书的签名 94a9b80e80691645dd42d6611775a855f71bcd4d77cb60a8e29404035a5e00b2
1bcc5d4a562482126bd91b6b0e50709377ceb9ef8c2efd12cc8b16afd9a159f3
50bb270b14204ff065d843832720702e28b41491fbc3a205f5f2f42526d67f17
614d8a974de6487b2c866efede3b4e49a0f916baa3c1336fd2ee1b1629652049
对SF文件的签名 2f5dabb56f826f75600a42cde32ba1886267b2ea7f2f5c05547d64861b5ad8f7
c910e089ec7cdb29c4bf6282899694579770440d2a9cee054833290d15b2d696
9d8e07dd18150948323d8a19f8f3ef6b021a5d58eb36b5fdcde8371e4649e396
8ad1e49ba4c4938488e61a1f994897cc57f401b994534c75b9bae8199ec8298e

SF文件验证

  1. 使用开发者公钥对SF文件签名进行解密运算,得到SF文件SHA1的运算结果

    52C4960A8D0C5FEFC62039A339C3B4AE7D3BD2D9
  2. 对ANDROIDR.SF文件进行hash运算,得到SHA1运算结果

    52c4960a8d0c5fefc62039a339c3b4ae7d3bd2d9
  3. 比对步骤1和步骤2的运算结果相同即可。

附录:ANDROIDR.RSA完整解析

openssl asn1parse -inform DER -dump -in ANDROIDR.RSA
    0:d=0  hl=4 l= 938 cons: SEQUENCE          
    4:d=1  hl=2 l=   9 prim: OBJECT            :pkcs7-signedData
   15:d=1  hl=4 l= 923 cons: cont [ 0 ]        
   19:d=2  hl=4 l= 919 cons: SEQUENCE          
   23:d=3  hl=2 l=   1 prim: INTEGER           :01
   26:d=3  hl=2 l=  11 cons: SET               
   28:d=4  hl=2 l=   9 cons: SEQUENCE          
   30:d=5  hl=2 l=   5 prim: OBJECT            :sha1
   37:d=5  hl=2 l=   0 prim: NULL              
   39:d=3  hl=2 l=  11 cons: SEQUENCE          
   41:d=4  hl=2 l=   9 prim: OBJECT            :pkcs7-data
   52:d=3  hl=4 l= 599 cons: cont [ 0 ]        
   56:d=4  hl=4 l= 595 cons: SEQUENCE          
   60:d=5  hl=4 l= 444 cons: SEQUENCE          
   64:d=6  hl=2 l=   3 cons: cont [ 0 ]        
   66:d=7  hl=2 l=   1 prim: INTEGER           :02
   69:d=6  hl=2 l=   4 prim: INTEGER           :4BBB0361
   75:d=6  hl=2 l=  13 cons: SEQUENCE          
   77:d=7  hl=2 l=   9 prim: OBJECT            :sha1WithRSAEncryption
   88:d=7  hl=2 l=   0 prim: NULL              
   90:d=6  hl=2 l= 109 cons: SEQUENCE          
   92:d=7  hl=2 l=  14 cons: SET               
   94:d=8  hl=2 l=  12 cons: SEQUENCE          
   96:d=9  hl=2 l=   3 prim: OBJECT            :countryName
  101:d=9  hl=2 l=   5 prim: PRINTABLESTRING   :China
  108:d=7  hl=2 l=  15 cons: SET               
  110:d=8  hl=2 l=  13 cons: SEQUENCE          
  112:d=9  hl=2 l=   3 prim: OBJECT            :stateOrProvinceName
  117:d=9  hl=2 l=   6 prim: UTF8STRING        :北京
  125:d=7  hl=2 l=  15 cons: SET               
  127:d=8  hl=2 l=  13 cons: SEQUENCE          
  129:d=9  hl=2 l=   3 prim: OBJECT            :localityName
  134:d=9  hl=2 l=   6 prim: UTF8STRING        :北京
  142:d=7  hl=2 l=  15 cons: SET               
  144:d=8  hl=2 l=  13 cons: SEQUENCE          
  146:d=9  hl=2 l=   3 prim: OBJECT            :organizationName
  151:d=9  hl=2 l=   6 prim: UTF8STRING        :腾讯
  159:d=7  hl=2 l=  27 cons: SET               
  161:d=8  hl=2 l=  25 cons: SEQUENCE          
  163:d=9  hl=2 l=   3 prim: OBJECT            :organizationalUnitName
  168:d=9  hl=2 l=  18 prim: UTF8STRING        :无线业务系统
  188:d=7  hl=2 l=  11 cons: SET               
  190:d=8  hl=2 l=   9 cons: SEQUENCE          
  192:d=9  hl=2 l=   3 prim: OBJECT            :commonName
  197:d=9  hl=2 l=   2 prim: PRINTABLESTRING   :QQ
  201:d=6  hl=2 l=  32 cons: SEQUENCE          
  203:d=7  hl=2 l=  13 prim: UTCTIME           :100406094817Z
  218:d=7  hl=2 l=  15 prim: GENERALIZEDTIME   :22840120094817Z
  235:d=6  hl=2 l= 109 cons: SEQUENCE          
  237:d=7  hl=2 l=  14 cons: SET               
  239:d=8  hl=2 l=  12 cons: SEQUENCE          
  241:d=9  hl=2 l=   3 prim: OBJECT            :countryName
  246:d=9  hl=2 l=   5 prim: PRINTABLESTRING   :China
  253:d=7  hl=2 l=  15 cons: SET               
  255:d=8  hl=2 l=  13 cons: SEQUENCE          
  257:d=9  hl=2 l=   3 prim: OBJECT            :stateOrProvinceName
  262:d=9  hl=2 l=   6 prim: UTF8STRING        :北京
  270:d=7  hl=2 l=  15 cons: SET               
  272:d=8  hl=2 l=  13 cons: SEQUENCE          
  274:d=9  hl=2 l=   3 prim: OBJECT            :localityName
  279:d=9  hl=2 l=   6 prim: UTF8STRING        :北京
  287:d=7  hl=2 l=  15 cons: SET               
  289:d=8  hl=2 l=  13 cons: SEQUENCE          
  291:d=9  hl=2 l=   3 prim: OBJECT            :organizationName
  296:d=9  hl=2 l=   6 prim: UTF8STRING        :腾讯
  304:d=7  hl=2 l=  27 cons: SET               
  306:d=8  hl=2 l=  25 cons: SEQUENCE          
  308:d=9  hl=2 l=   3 prim: OBJECT            :organizationalUnitName
  313:d=9  hl=2 l=  18 prim: UTF8STRING        :无线业务系统
  333:d=7  hl=2 l=  11 cons: SET               
  335:d=8  hl=2 l=   9 cons: SEQUENCE          
  337:d=9  hl=2 l=   3 prim: OBJECT            :commonName
  342:d=9  hl=2 l=   2 prim: PRINTABLESTRING   :QQ
  346:d=6  hl=3 l= 159 cons: SEQUENCE          
  349:d=7  hl=2 l=  13 cons: SEQUENCE          
  351:d=8  hl=2 l=   9 prim: OBJECT            :rsaEncryption
  362:d=8  hl=2 l=   0 prim: NULL              
  364:d=7  hl=3 l= 141 prim: BIT STRING        
      0000 - 00 30 81 89 02 81 81 00-a1 5e 97 56 21 6f 69 4c   .0.......^.V!oiL
      0010 - 59 15 e0 b5 29 09 52 54-36 7c 4e 64 fa ef f0 7a   Y...).RT6|Nd...z
      0020 - e1 34 88 d9 46 61 5a 58-dd c3 1a 41 5f 71 7d 01   .4..FaZX...A_q}.
      0030 - 9e dc 6d 30 b9 60 3d 3e-2a 7b 3d e0 ab 7e 0c f5   ..m0.`=>*{=..~..
      0040 - 2d fe e3 93 73 bc 47 2f-a9 97 02 7d 79 8d 59 f8   -...s.G/...}y.Y.
      0050 - 1d 52 5a 69 ec f1 56 e8-85 fd 1e 27 90 92 43 86   .RZi..V....'..C.
      0060 - b2 23 0c c9 0e 3b 7a dc-95 60 3d dc f4 c4 0b dc   .#...;z..`=.....
      0070 - 72 f2 2d b0 f2 16 a9 9c-37 1d 3b f8 9c ba 65 78   r.-.....7.;...ex
      0080 - c6 06 99 e8 a0 d5 36 95-02 03 01 00 01            ......6......
  508:d=5  hl=2 l=  13 cons: SEQUENCE          
  510:d=6  hl=2 l=   9 prim: OBJECT            :sha1WithRSAEncryption
  521:d=6  hl=2 l=   0 prim: NULL              
  523:d=5  hl=3 l= 129 prim: BIT STRING        
      0000 - 00 94 a9 b8 0e 80 69 16-45 dd 42 d6 61 17 75 a8   ......i.E.B.a.u.
      0010 - 55 f7 1b cd 4d 77 cb 60-a8 e2 94 04 03 5a 5e 00   U...Mw.`.....Z^.
      0020 - b2 1b cc 5d 4a 56 24 82-12 6b d9 1b 6b 0e 50 70   ...]JV$..k..k.Pp
      0030 - 93 77 ce b9 ef 8c 2e fd-12 cc 8b 16 af d9 a1 59   .w.............Y
      0040 - f3 50 bb 27 0b 14 20 4f-f0 65 d8 43 83 27 20 70   .P.'.. O.e.C.' p
      0050 - 2e 28 b4 14 91 fb c3 a2-05 f5 f2 f4 25 26 d6 7f   .(..........%&..
      0060 - 17 61 4d 8a 97 4d e6 48-7b 2c 86 6e fe de 3b 4e   .aM..M.H{,.n..;N
      0070 - 49 a0 f9 16 ba a3 c1 33-6f d2 ee 1b 16 29 65 20   I......3o....)e 
      0080 - 49                                                I
  655:d=3  hl=4 l= 283 cons: SET               
  659:d=4  hl=4 l= 279 cons: SEQUENCE          
  663:d=5  hl=2 l=   1 prim: INTEGER           :01
  666:d=5  hl=2 l= 117 cons: SEQUENCE          
  668:d=6  hl=2 l= 109 cons: SEQUENCE          
  670:d=7  hl=2 l=  14 cons: SET               
  672:d=8  hl=2 l=  12 cons: SEQUENCE          
  674:d=9  hl=2 l=   3 prim: OBJECT            :countryName
  679:d=9  hl=2 l=   5 prim: PRINTABLESTRING   :China
  686:d=7  hl=2 l=  15 cons: SET               
  688:d=8  hl=2 l=  13 cons: SEQUENCE          
  690:d=9  hl=2 l=   3 prim: OBJECT            :stateOrProvinceName
  695:d=9  hl=2 l=   6 prim: UTF8STRING        :北京
  703:d=7  hl=2 l=  15 cons: SET               
  705:d=8  hl=2 l=  13 cons: SEQUENCE          
  707:d=9  hl=2 l=   3 prim: OBJECT            :localityName
  712:d=9  hl=2 l=   6 prim: UTF8STRING        :北京
  720:d=7  hl=2 l=  15 cons: SET               
  722:d=8  hl=2 l=  13 cons: SEQUENCE          
  724:d=9  hl=2 l=   3 prim: OBJECT            :organizationName
  729:d=9  hl=2 l=   6 prim: UTF8STRING        :腾讯
  737:d=7  hl=2 l=  27 cons: SET               
  739:d=8  hl=2 l=  25 cons: SEQUENCE          
  741:d=9  hl=2 l=   3 prim: OBJECT            :organizationalUnitName
  746:d=9  hl=2 l=  18 prim: UTF8STRING        :无线业务系统
  766:d=7  hl=2 l=  11 cons: SET               
  768:d=8  hl=2 l=   9 cons: SEQUENCE          
  770:d=9  hl=2 l=   3 prim: OBJECT            :commonName
  775:d=9  hl=2 l=   2 prim: PRINTABLESTRING   :QQ
  779:d=6  hl=2 l=   4 prim: INTEGER           :4BBB0361
  785:d=5  hl=2 l=   9 cons: SEQUENCE          
  787:d=6  hl=2 l=   5 prim: OBJECT            :sha1
  794:d=6  hl=2 l=   0 prim: NULL              
  796:d=5  hl=2 l=  13 cons: SEQUENCE          
  798:d=6  hl=2 l=   9 prim: OBJECT            :rsaEncryption
  809:d=6  hl=2 l=   0 prim: NULL              
  811:d=5  hl=3 l= 128 prim: OCTET STRING      
      0000 - 2f 5d ab b5 6f 82 6f 75-60 0a 42 cd e3 2b a1 88   /]..o.ou`.B..+..
      0010 - 62 67 b2 ea 7f 2f 5c 05-54 7d 64 86 1b 5a d8 f7   bg.../\.T}d..Z..
      0020 - c9 10 e0 89 ec 7c db 29-c4 bf 62 82 89 96 94 57   .....|.)..b....W
      0030 - 97 70 44 0d 2a 9c ee 05-48 33 29 0d 15 b2 d6 96   .pD.*...H3).....
      0040 - 9d 8e 07 dd 18 15 09 48-32 3d 8a 19 f8 f3 ef 6b   .......H2=.....k
      0050 - 02 1a 5d 58 eb 36 b5 fd-cd e8 37 1e 46 49 e3 96   ..]X.6....7.FI..
      0060 - 8a d1 e4 9b a4 c4 93 84-88 e6 1a 1f 99 48 97 cc   .............H..
      0070 - 57 f4 01 b9 94 53 4c 75-b9 ba e8 19 9e c8 29 8e   W....SLu......).

猜你喜欢

转载自blog.csdn.net/JT_Notes/article/details/82852488