应该是可以在 Ubuntu 和 macOS 下都能跑的,需要 Android SDK 的支持。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
#!/bin/bash # shell script for getting information from android application package # version: 0.8 # author: dandycheung # date: 2018-09-03 - 2018-09-04 aapt_bin="$HOME/android-sdk/build-tools/28.0.2/aapt" md5cmd="md5sum" md5opt="-b" md5stdin="md5sum" if [ "${OSTYPE//[0-9.]/}" = "darwin" ]; then md5cmd="md5" md5opt="-r" md5stdin="md5" fi appName=""; pkgName=""; pkgMd5=""; verCode=0; verName=""; certSerial=""; certMd5=""; function dbgMsg() { # echo "[DBG] $1"; return; } function trim() { local var="$1" # remove leading whitespace characters var="${var#"${var%%[![:space:]]*}"}" # remove trailing whitespace characters var="${var%"${var##*[![:space:]]}"}" # echo -n "$var" } function getApkInfo() { apk="$1"; # calc package MD5 val=`"$md5cmd" "$md5opt" "$apk"`; pkgMd5=${val%%\ *}; dbgMsg "$pkgMd5"; # get package name, version code and version name full="$($aapt_bin d badging $apk)"; # dbgMsg "$full"; line=`echo "$full" | grep '^package:'`; # dbgMsg $line; pkgName=`echo "$line" | sed -E "s/.*package: name\s*=\s*'([^']+)'.*/\1/g"`; dbgMsg "$pkgName"; verCode=`echo "$line" | sed -E "s/.*versionCode\s*=\s*'([^']+)'.*/\1/g"`; dbgMsg "$verCode"; verName=`echo "$line" | sed -E "s/.*versionName\s*=\s*'([^']+)'.*/\1/g"`; dbgMsg "$verName"; # get app name line=`echo "$full" | grep '^application:'`; appName=`echo "$line" | cut -d\' -f2`; dbgMsg "$appName"; # get certificate file name(TODO: fix it while more than one certificate) certFile=`jar tf "$apk" | egrep '^META-INF/.*\.[DR]SA$'`; dbgMsg "$certFile"; # get certificate serial number and MD5 full=`unzip -cqq "$apk" "$certFile" | openssl pkcs7 -inform DER -noout -print_certs -text`; dbgMsg "$full" line=`echo "$full" | grep 'Serial Number:'`; # line=trim "$line" dbgMsg "$line"; # if the function trim() above works, the field index should be 3 but 11 certSerial=`echo "$line" | cut -d" " -f11`; dbgMsg "$certSerial"; val=`unzip -cqq "$apk" "$certFile" | "$md5cmd"`; certMd5=${val%%\ *}; dbgMsg "$certMd5"; } function showApkInfo() { echo ""; echo "$1"; echo -e "appName:\t$appName"; echo -e "verName:\t$verName"; echo -e "verCode:\t$verCode"; echo -e "pkgName:\t$pkgName"; echo -e "pkgMd5:\t\t$pkgMd5"; echo -e "certSerial:\t$certSerial"; echo -e "certMd5:\t$certMd5"; } # getApkInfo "$1" if [ "$1" = "" ]; then [ -n "$aapt_bin" -a -x "$aapt_bin" ] && find . -type f -iname '*.apk' -print0 | while IFS="" read -r -d "" j; do IFS="$(printf '\nX')"; IFS="${IFS%X}"; getApkInfo "$j"; showApkInfo "$j"; done else getApkInfo "$1" showApkInfo "$1" fi |
永久链接
文中计算证书 MD5 的方法是有缺陷的,因为 CERT.RSA 文件可能会变化,所以还是使用传统的证书的 MD5 值为佳。
certMd5=
unzip -cqq "$apk" "$certFile" | openssl pkcs7 -inform DER -print_certs | openssl x509 -noout -fingerprint -md5 -inform pem | sed -e "s/.*=//g" -e "s/://g"