Android通过签名判断是否系统编译的apk

Android平台编译用到的签名公私钥文件存在以下目录:

build/target/product/security

里面包含四对公私钥:

media.pk8、media.pk8、platform.pk8、platform.pk8、shared.pk8、shared.pk8、testkey.pk8、testkey.pk8

系统编译出来的apk,根据策略不同,会选用不同的签名文件。

第三方的apk一般为保证不被篡改和正常升级,都有自己固定的签名文件,不会和Android平台的相同。

所以通过apk的签名可以判断这个应用是否为本系统自带应用。

签名文件分析:

可以通过下面命令来显示公钥的详细信息:

openssl x509 -in build/target/product/security/testkey.x509.pem -text -noout

显示结果如下:

Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 10623618503190643167 (0x936eacbe07f201df)
    Signature Algorithm: sha1WithRSAEncryption
        Issuer: C=US, ST=California, L=Mountain View, O=Android, OU=Android, CN=Android/[email protected]
        Validity
            Not Before: Feb 29 01:33:46 2008 GMT
            Not After : Jul 17 01:33:46 2035 GMT
        Subject: C=US, ST=California, L=Mountain View, O=Android, OU=Android, CN=Android/[email protected]
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
                    00:d6:93:19:04:de:c6:0b:24:b1:ed:c7:62:e0:d9:
                    d8:25:3e:3e:cd:6c:eb:1d:e2:ff:06:8c:a8:e8:bc:
                    a8:cd:6b:d3:78:6e:a7:0a:a7:6c:e6:0e:bb:0f:99:
                    35:59:ff:d9:3e:77:a9:43:e7:e8:3d:4b:64:b8:e4:
                    fe:a2:d3:e6:56:f1:e2:67:a8:1b:bf:b2:30:b5:78:
                    c2:04:43:be:4c:72:18:b8:46:f5:21:15:86:f0:38:
                    a1:4e:89:c2:be:38:7f:8e:be:cf:8f:ca:c3:da:1e:
                    e3:30:c9:ea:93:d0:a7:c3:dc:4a:f3:50:22:0d:50:
                    08:07:32:e0:80:97:17:ee:6a:05:33:59:e6:a6:94:
                    ec:2c:b3:f2:84:a0:a4:66:c8:7a:94:d8:3b:31:09:
                    3a:67:37:2e:2f:64:12:c0:6e:6d:42:f1:58:18:df:
                    fe:03:81:cc:0c:d4:44:da:6c:dd:c3:b8:24:58:19:
                    48:01:b3:25:64:13:4f:bf:de:98:c9:28:77:48:db:
                    f5:67:6a:54:0d:81:54:c8:bb:ca:07:b9:e2:47:55:
                    33:11:c4:6b:9a:f7:6f:de:ec:cc:8e:69:e7:c8:a2:
                    d0:8e:78:26:20:94:3f:99:72:7d:3c:04:fe:72:99:
                    1d:99:df:9b:ae:38:a0:b2:17:7f:a3:1d:5b:6a:fe:
                    e9:1f
                Exponent: 3 (0x3)
        X509v3 extensions:
            X509v3 Subject Key Identifier: 
                48:59:00:56:3D:27:2C:46:AE:11:86:05:A4:74:19:AC:09:CA:8C:11
            X509v3 Authority Key Identifier: 
                keyid:48:59:00:56:3D:27:2C:46:AE:11:86:05:A4:74:19:AC:09:CA:8C:11
                DirName:/C=US/ST=California/L=Mountain View/O=Android/OU=Android/CN=Android/[email protected]
                serial:93:6E:AC:BE:07:F2:01:DF


            X509v3 Basic Constraints: 
                CA:TRUE
    Signature Algorithm: sha1WithRSAEncryption
         7a:af:96:8c:eb:50:c4:41:05:51:18:d0:da:ab:af:01:5b:8a:
         76:5a:27:a7:15:a2:c2:b4:4f:22:14:15:ff:da:ce:03:09:5a:
         bf:a4:2d:f7:07:08:72:6c:20:69:e5:c3:6e:dd:ae:04:00:be:
         29:45:2c:08:4b:c2:7e:b6:a1:7e:ac:9d:be:18:2c:20:4e:b1:
         53:11:f4:55:d8:24:b6:56:db:e4:dc:22:40:91:2d:75:86:fe:
         88:95:1d:01:a8:fe:b5:ae:5a:42:60:53:5d:f8:34:31:05:24:
         22:46:8c:36:e2:2c:2a:5e:f9:94:d6:1d:d7:30:6a:e4:c9:f6:
         95:1b:a3:c1:2f:1d:19:14:dd:c6:1f:1a:62:da:2d:f8:27:f6:
         03:fe:a5:60:3b:2c:54:0d:bd:7c:01:9c:36:ba:b2:9a:42:71:
         c1:17:df:52:3c:db:c5:f3:81:7a:49:e0:ef:a6:0c:bd:7f:74:
         17:7e:7a:4f:19:3d:43:f4:22:07:72:66:6e:4c:4d:83:e1:bd:
         5a:86:08:7c:f3:4f:2d:ec:21:e2:45:ca:6c:2b:b0:16:e6:83:
         63:80:50:d2:c4:30:ee:a7:c2:6a:1c:49:d3:76:0a:58:ab:7f:
         1a:82:cc:93:8b:48:31:38:43:24:bd:04:01:fa:12:16:3a:50:
         57:0e:68:4d


我们可以通过其中的Serial Number: 10623618503190643167 (0x936eacbe07f201df)

代码如下:

//media,platform,shared,testkey
private static String mRomSingnum[] = {"17490167175725919310","12941516320735154170","17485000800801945210","10623618503190643167"};

	public static boolean isRomSign(Context context) {
		try {
			PackageInfo packageInfo = context.getPackageManager()
					.getPackageInfo(context.getPackageName(),
							PackageManager.GET_SIGNATURES);
			Signature[] signs = packageInfo.signatures;
			Signature sign = signs[0];
			String signNumber = parseSignature(sign.toByteArray());
            for(String num:mRomSingnum) {
                if(num.equals(signNumber)) {
                    return true;
                }
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


	private static String parseSignature(byte[] signature) {
		try {
			CertificateFactory certFactory = CertificateFactory
					.getInstance("X.509");
			X509Certificate cert = (X509Certificate) certFactory
					.generateCertificate(new ByteArrayInputStream(signature));
			//String pubKey = cert.getPublicKey().toString();
			String signNumber = cert.getSerialNumber().toString();
			return signNumber;
		} catch (CertificateException e) {
			e.printStackTrace();
		}
		return null;
	}



你可能感兴趣的:(Android通过签名判断是否系统编译的apk)