Skip to content

Instantly share code, notes, and snippets.

@kernoeb
Last active September 20, 2020 00:04
Show Gist options
  • Save kernoeb/e93ba765b1e89bc60475684d85ca48d0 to your computer and use it in GitHub Desktop.
Save kernoeb/e93ba765b1e89bc60475684d85ca48d0 to your computer and use it in GitHub Desktop.
Decompile, edit, rebuild and sign .apk files to accept https analyses (e.g. Charles Proxy)
#!/bin/bash
###########
# kernoeb #
###########
apk_dir=.apk_dir # name of the build apk
keystore=key.keystore # name of the keystore
apktool=apktool.jar # apktool.jar location
alias_name=alias_name # temporary alias name
application=application.apk # temporary file name
function cat_security {
echo -e "\e[32mAdding or replacing network_security_config file\e[39m"
cat > "res/xml/network_security_config.xml" << EOF
<network-security-config>
<debug-overrides>
<trust-anchors>
<!-- Trust user added CAs while debuggable only -->
<certificates src="user" />
</trust-anchors>
</debug-overrides>
</network-security-config>
EOF
}
trap "exit" INT # Force stop on ctrl+c
if [ ! $# -eq 0 ]
then
rm -rf "$apk_dir"
if [ ! -f "$keystore" ]
then
echo -e "\e[32mPlease generate the key!\e[39m"
keytool -genkey -v -keystore "$keystore" -alias "$alias_name" -keyalg RSA -keysize 2048 -validity 10000
else
echo -e "\e[32mKey $keystore found!\e[39m"
fi
echo -e "\e[32mDecompiling application...\e[39m"
java -jar "$apktool" d "$1" -o "$apk_dir"
cd "$apk_dir"
cat_security
if grep 'android:networkSecurityConfig="@xml/network_security_config"' "AndroidManifest.xml"
then
echo -e "\e[32mAlready here : networkSecurityConfig\e[39m"
else
echo -e "\e[32mEditing AndroidManifest.xml with networkSecurityConfig\e[39m"
tmp1="<application "
tmp2="<application android:networkSecurityConfig=\"@xml\/network_security_config\" "
sed -i -e "s/${tmp1}/${tmp2}/g" "AndroidManifest.xml"
fi
cd ..
echo -e "\e[32mRebuilding application\e[39m"
java -jar "$apktool" b -f -d "$apk_dir" -o "$apk_dir"/dist/"$application"
echo -e "\e[32mSign the application\e[39m"
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore "$keystore" "$apk_dir"/dist/"$application" "$alias_name"
echo -e "\e[32mAlign the application\e[39m"
if [ -n "$2" ]; then
if [ -f "$2" ]; then
rm "$2"
fi
zipalign -v 4 "$apk_dir"/dist/"$application" "$2"
else
zipalign -v 4 "$apk_dir"/dist/"$application" "application-aligned.apk"
fi
if [ -n "$3" ] && [ "$3" = "keep" ]; then
echo -e "\e[32mKeeping build directory\e[39m"
else
rm -rf "$apk_dir"
fi
else
echo -e "\e[91mEnter as an argument the path of the apk\e[39m"
echo -e "\e[32m$0 /path/to/file.apk [new_name.apk] [keep]\e[39m"
for i in `seq ${#0}`; do printf " ";done # just print spaces
echo -e " ↳ keep directory"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment