forked from levyitay/AddSecurityExceptionAndroid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
addSecurityExceptions.sh
executable file
·54 lines (46 loc) · 1.68 KB
/
addSecurityExceptions.sh
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
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ $# -eq 0 ]
then
echo "No arguments supplied"
echo "Usage: $0 <APK filename>"
exit -1
fi
if [ ! -z "$2" ]
then
debugKeystore=$2
else
if [ ! -f ~/.android/debug.keystore ]; then
if [ ! -d ~/.android ]; then
mkdir ~/.android
fi
echo "No debug keystore was found, creating new one..."
keytool -genkey -v -keystore ~/.android/debug.keystore -storepass android -alias androiddebugkey -keypass android -keyalg RSA -keysize 2048 -validity 10000
fi
debugKeystore=~/.android/debug.keystore
fi
fullfile=$1
filename=$(basename "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"
new="_new.apk"
temp="_temp.apk"
tempFileName=$filename$temp
newFileName=$filename$new
tmpDir=/tmp/$filename
java -jar "$DIR/apktool.jar" d -f -s -o "$tmpDir" "$fullfile"
if [ ! -d "$tmpDir/res/xml" ]; then
mkdir "$tmpDir/res/xml"
fi
cp "$DIR/network_security_config.xml" "$tmpDir/res/xml/."
if ! grep -q "networkSecurityConfig" "$tmpDir/AndroidManifest.xml"; then
sed -E "s/(<application.*)(>)/\1 android\:networkSecurityConfig=\"@xml\/network_security_config\" \2 /" "$tmpDir/AndroidManifest.xml" > "$tmpDir/AndroidManifest.xml.new"
mv "$tmpDir/AndroidManifest.xml.new" "$tmpDir/AndroidManifest.xml"
fi
java -jar "$DIR/apktool.jar" empty-framework-dir --force "$tmpDir"
echo "Building temp APK $tempFileName"
java -jar "$DIR/apktool.jar" b -o "./$tempFileName" "$tmpDir"
jarsigner -verbose -keystore $debugKeystore -storepass android -keypass android "./$tempFileName" androiddebugkey
zipalign -p 4 $tempFileName $newFileName
rm -rf $tempFileName
echo "Resigned APK successfully $newFileName"