Advertisement
Guest User

Untitled

a guest
May 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. function usage(){
  4. printf "Make APK debuggable.\n"
  5. printf "Usage: $0 [options]\n"
  6. printf "\t-f,--file <path> : Direct path to an APK to make debuggable\n"
  7. printf "\t-i,--id <package_name> : Identifier of an APK to pull from device\n"
  8. }
  9.  
  10. if [ $# -eq 0 ]; then
  11. usage
  12. fi
  13.  
  14.  
  15. # Declare alias for sed to make it compatible in both Mac and Unix OS
  16. darwin=false;
  17. case "`uname`" in
  18. Darwin*) darwin=true ;;
  19. esac
  20. if "$darwin"; then
  21. alias sed_regexp="sed -E"
  22. else
  23. alias sed_regexp="sed -r -e"
  24. fi
  25.  
  26.  
  27. # Retrieve zipalign binary from Android SDK
  28. if [ -z "$ANDROID_HOME" ]; then
  29. echo "You must set a ANDROID_HOME environment variable referencing Android SDK folder"
  30. exit 1;
  31. fi
  32. ANDROID_BUILD_TOOLS_VERSION=`ls $ANDROID_HOME/build-tools | tail -1`
  33. ZIPALING_PATH="$ANDROID_HOME/build-tools/$ANDROID_BUILD_TOOLS_VERSION/zipalign"
  34.  
  35.  
  36. function pullFromDevice() {
  37. APK_ID="$1"
  38.  
  39. echo ">> Search an APK on device matching id $APK_ID"
  40.  
  41. APK_MATCHES=`adb shell pm list packages -f | grep "$APK_ID"`
  42. if [ `echo "$APK_MATCHES" | wc -l` -gt 1 ]; then
  43. echo "Too many results:"
  44. echo "$APK_MATCHES"
  45. exit 1;
  46. fi
  47.  
  48. APK_DEVICE_PATH=`echo "$APK_MATCHES" | sed_regexp 's/^package:(.+\.apk).*/\1/'`
  49. APK_DEVICE_ID=`echo "$APK_MATCHES" | sed_regexp 's/.*=([^=]+)$/\1/'`
  50. echo ">>> Found APK $APK_DEVICE_ID at $APK_DEVICE_PATH"
  51.  
  52. APK_LOCAL_PATH="$APK_DEVICE_ID.apk"
  53. echo ">> Pulling APK to $APK_LOCAL_PATH"
  54. adb pull "$APK_DEVICE_PATH" "$APK_LOCAL_PATH"
  55.  
  56. if [ $? -ne 0 ]; then
  57. echo "ERROR while pulling APK"
  58. exit 1
  59. fi
  60. }
  61.  
  62. function makeDebuggable() {
  63. APK_INPUT_PATH="$1"
  64. APK_RECOMPILED_PATH=`echo "$APK_INPUT_PATH" | sed 's/\.apk/.debuggable.apk/'`
  65. APK_ALIGNED_PATH=`echo "$APK_RECOMPILED_PATH" | sed 's/\.apk/.aligned.apk/'`
  66. APK_DECOMPILE_PATH="$APK_INPUT_PATH.out"
  67.  
  68. echo ">> Decompiling APK from $APK_INPUT_PATH"
  69. apktool d -f -o "$APK_DECOMPILE_PATH" "$APK_INPUT_PATH"
  70. if [ $? -ne 0 ]; then
  71. echo "ERROR while decompiling APK"
  72. exit 1
  73. fi
  74.  
  75.  
  76. allowUserCertificates "$APK_DECOMPILE_PATH"
  77.  
  78.  
  79. echo ">> Recompiling APK to $APK_RECOMPILED_PATH"
  80. apktool b --debug --no-crunch -o "$APK_RECOMPILED_PATH" "$APK_DECOMPILE_PATH"
  81. if [ $? -ne 0 ]; then
  82. echo "ERROR while recompiling APK"
  83. echo "Try again after running : apktool empty-framework-dir --force"
  84. exit 1
  85. fi
  86.  
  87.  
  88. echo ">> Signing APK"
  89. CERT_PATH="$APK_DECOMPILE_PATH/resign.keystore"
  90. CERT_ALIAS="alias_name"
  91. CERT_PASS="recompiled"
  92.  
  93. if [ ! -f "$CERT_PATH" ]; then
  94. echo ">>> Generating certificate"
  95. keytool -genkey -v -keystore "$CERT_PATH" -alias "$CERT_ALIAS" -keyalg RSA -keysize 2048 -validity 10000 -dname "CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown" -storepass "$CERT_PASS" -keypass "$CERT_PASS"
  96. if [ $? -ne 0 ]; then
  97. echo "ERROR while generating certificate"
  98. exit 1
  99. fi
  100. fi
  101.  
  102. echo ">>> Signing APK with certificate"
  103. jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore "$CERT_PATH" -storepass "$CERT_PASS" -keypass "$CERT_PASS" "$APK_RECOMPILED_PATH" "$CERT_ALIAS"
  104.  
  105.  
  106. echo ">>> Aligning APK"
  107. $ZIPALING_PATH -f -v 4 "$APK_RECOMPILED_PATH" "$APK_ALIGNED_PATH"
  108. if [ $? -ne 0 ]; then
  109. echo "ERROR while aligning APK"
  110. exit 1
  111. fi
  112. rm -f "$APK_RECOMPILED_PATH"
  113.  
  114. echo ">> Done. Install apk with : adb install -f $APK_ALIGNED_PATH"
  115. }
  116.  
  117. function allowUserCertificates() {
  118. APK_DECOMPILED_PATH="$1"
  119. MANIFEST_FILE="$APK_DECOMPILED_PATH/AndroidManifest.xml"
  120.  
  121. echo ">> Allowing User's SSL certificates"
  122.  
  123. mkdir -p "$APK_DECOMPILED_PATH/res/xml"
  124.  
  125. EXISTING_NETWORK_SECURITY_CONFIG=`grep -E 'android:networkSecurityConfig="@xml/([a-zA-Z0-9_]+)"' -o "$MANIFEST_FILE" | sed -E 's#.*@xml/([a-zA-Z0-9_]+).*#\1#'`
  126. if [ ! -z "$EXISTING_NETWORK_SECURITY_CONFIG" ]; then
  127. NETWORK_SECURITY_CONFIG_FILE="$APK_DECOMPILED_PATH/res/xml/$EXISTING_NETWORK_SECURITY_CONFIG.xml"
  128. echo ">>> Found existing and used network security file : $NETWORK_SECURITY_CONFIG_FILE"
  129.  
  130. EXISTING_DEBUG_NODE=`grep "<debug-overrides>" "$NETWORK_SECURITY_CONFIG_FILE"`
  131. if [ ! -z "$EXISTING_DEBUG_NODE" ]; then
  132. echo ">>> Found debug-overrides XML tag"
  133. # Because we're making a replacement on multiline, we can't use 'sed' command
  134. mv "$NETWORK_SECURITY_CONFIG_FILE" "$NETWORK_SECURITY_CONFIG_FILE.bck"
  135. cat "$NETWORK_SECURITY_CONFIG_FILE.bck" | tr -d '\n' | sed_regexp -e $'s#(</trust-anchors>[\s\t]*</debug-overrides>)#<certificates src="user" />\\1#' > "$NETWORK_SECURITY_CONFIG_FILE"
  136. rm "$NETWORK_SECURITY_CONFIG_FILE.bck"
  137. else
  138. sed_regexp -i '' -e 's#(</network-security-config>)#<debug-overrides><trust-anchors><certificates src=\"user\" /></trust-anchors></debug-overrides>\1#' "$NETWORK_SECURITY_CONFIG_FILE"
  139. fi
  140. else
  141. NETWORK_SECURITY_CONFIG_FILE="$APK_DECOMPILED_PATH/res/xml/network_security_config.xml"
  142. echo ">>> Creating network security file : $NETWORK_SECURITY_CONFIG_FILE"
  143.  
  144. echo "<network-security-config>
  145. <debug-overrides><trust-anchors><certificates src=\"user\" /></trust-anchors></debug-overrides>
  146. </network-security-config>" > "$NETWORK_SECURITY_CONFIG_FILE"
  147.  
  148. sed_regexp -i '' -e 's#(<application)#\1 android:networkSecurityConfig="@xml/network_security_config"#' "$MANIFEST_FILE"
  149. fi
  150. }
  151.  
  152.  
  153. while [[ "$#" -gt 0 ]]; do case $1 in
  154. -f|--file)
  155. makeDebuggable $2
  156. shift;;
  157. -i|--id)
  158. pullFromDevice $2
  159. makeDebuggable $APK_LOCAL_PATH
  160. shift;;
  161. *) "Echo unknown parameter $1"; exit 1;;
  162. esac; shift; done
  163.  
  164. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement