forked from RichieWan9/decompile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaad
executable file
·313 lines (255 loc) · 6.3 KB
/
yaad
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env bash
# Copyright (c) 2016 Feilong Wang
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
prog="$0"
while [ -h "${prog}" ]; do
newProg=`/bin/ls -ld "${prog}"`
echo ${newProg}
newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
if expr "x${newProg}" : 'x/' >/dev/null; then
prog="${newProg}"
else
progdir=`dirname "${prog}"`
prog="${progdir}/${newProg}"
fi
done
oldwd=`pwd`
progdir=`dirname "${prog}"`
cd "${progdir}"
progdir=`pwd`
prog="${progdir}"/`basename "${prog}"`
cd "${oldwd}"
APKTOOL="${progdir}/apktool/apktool"
SMALITOOL="${progdir}/smali4dex/smali"
BAKSMALITOOL="${progdir}/smali4dex/baksmali"
DEX2JARTOOL="${progdir}/dex2jar/d2j-dex2jar.sh"
JAR2JAVATOOL="${progdir}/jad/jad"
yaad_usage() {
cat <<EOF
Decompile a android apk, framework jar or a optimized apk/odex.
Usage: yaad [options] <apk|jar|odex>
-s, --system=<directory>
The directory to look for framework odex files when decomile
a odex file. Defaults to the YAADROOT/system directory.
-h, --help Print this help and exit
To decompile a odex file, it also needs a few of framework odex files. What
framework odex files needed depend on the optimized apk you are decompiling.
The most used framework odex files are android.policy.odex, bouncycastle.odex,
core.odex, pm.odex, core-junit.odex, ext.odex, services.odex, which you can
pull from /system/framework directory in the device at which your optimized
apk is compiled armed. YAAD will give you tips when needed framework odexs
not fond.
Output
All files are saved in the directory named <apk|jar|odex> in current directory.
src.jar Source code formed in jar, open it with jd-gui
src/ Source code formed in java
res/ Resoures and so on
AndroidManifest.xml Android manifest file
Examples
yaad framework-res.apk
yaad service.jar
yaad -s .. Launcher.odex
EOF
}
yaad_dex2code() {
echo
echo "PHASE : Dex2code"
echo
local dex=classes.dex
if [ -n "$1" ]; then
dex="$1"
fi
"$DEX2JARTOOL" "$dex" -o src.jar -ts -f
if [ $? != 0 ]; then
echo "Decompile to jar FAILED."
return 1
fi
rm -rf tree
echo "jad $(pwd)/src.jar -> src/"
unzip -q src.jar -d tree
"$JAR2JAVATOOL" -o -r -ff -dead -sjava -dsrc tree/**/*.class 2>/dev/null
local ret=$?
rm -rf tree
return "$ret"
}
yaad_reverse_apk() {
if [ -z "$FILENAME" ]; then
echo "Error: file name is NULL."
return 1;
fi
if [ -e "$FILENAME" ]; then
rm -rf "$FILENAME"
if [ $? != 0 ]; then
echo "Error: delete $FILENAME FAILED."
return 1
fi
fi
echo
echo "PHASE : Decompile resources"
echo
# Decompile resources
"$APKTOOL" d -s "$FILEPATH" "$FILENAME"
if [ $? != 0 ]; then
echo
echo "Decompile resources FAILED."
fi
if [ ! -e "$FILENAME" ]; then
# Decompile resoures failed.
mkdir "$FILENAME"
fi
echo
echo "PHASE : Decompile code"
echo
# local oldPos=`pwd`
cd "$FILENAME"
# Decompile code
unzip -q -o "$FILEPATH" classes.dex
if [ $? != 0 ]; then
# May be error or built with user tag.
# echo "classes.dex not found."
local pOdexFile=
for tf in `ls "${FILEPATH%.*}".*`
do
# FIXME This should be $FILENAME.odex.
if expr "$tf" : '.*\.\([oO][dD][eE][xX]\)$' > /dev/null; then
pOdexFile="$tf"
break;
fi
done
if [ -r "$pOdexFile" ]; then
yaad_reverse_odex "$pOdexFile"
return $?
fi
echo
echo "$FILENAME.odex not found."
echo "Decompile code FAILED."
return 1
else
local pDexFile=`pwd`/classes.dex
yaad_dex2code "$pDexFile"
local ret=$?
rm classes.dex
return "$ret"
fi
# cd ${oldPos}
}
yaad_reverse_odex() {
echo
echo "PHASE : Odex2dex"
echo
if [ ! -r "$1" ]; then
echo "$1 not found."
return 1
fi
rm -rf out
rm classes.dex
"$BAKSMALITOOL" -d "$SYS_ODEX_DIR" -x "$1"
if [ $? != 0 ]; then
echo "Baksmali FAILED."
echo "Decompile code FAILED"
return 1
fi
"$SMALITOOL" out -o classes.dex
if [ $? != 0 ]; then
echo "Smali FAILED."
echo "Decompile code FAILED"
return 1;
fi
local tmpfile=`pwd`/classes.dex
yaad_dex2code "$tmpfile"
local ret=$?
rm -rf out
rm classes.dex
return "$ret";
}
yaad_reverse_jar() {
if [ ! -e "$FILENAME" ]; then
# Whatever create direcotry
mkdir "$FILENAME"
fi
echo
echo "PHASE : Decompile code"
echo
# local oldPos=`pwd`
cd "$FILENAME"
# Decompile code
unzip -q -o "$FILEPATH" classes.dex
if [ $? = 0 ]; then
local pDexFile=`pwd`/classes.dex
yaad_dex2code "$pDexFile"
local ret=$?
rm classes.dex
return "$ret"
fi
return 1
}
SYS_ODEX_DIR="${progdir}/system"
# SCRIPTNAME="${0##*/}"
TEMP=`getopt -o s:h --long help,system: -n "${0##*/}" -- "$@"`
if [ $? != 0 ] ; then
# echo "Error" >&2
exit 1
fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-h|--help) yaad_usage ; exit 0 ;;
-s|--system)
if [ -d "$2" -a -r "$2" ]; then
# Use absolute path
oldwd=`pwd`
cd "$2"
SYS_ODEX_DIR=`pwd`
cd "$oldwd"
else
SYS_ODEX_DIR=
fi
shift 2 ;;
--) shift ; break ;;
*) echo "Error: $1 is invalid argument!" ; exit 1 ;;
esac
done
if [ ! -r "$1" ]; then
echo "Error: no valid file to decompile."
exit 1
fi
# Use absolute path
FULLNAME="${1##*/}"
oldwd=`pwd`
cd `dirname "$1"`
export FILEPATH=`pwd`/"$FULLNAME"
export FILENAME="${FULLNAME%.*}"
export SYS_ODEX_DIR
cd "$oldwd"
# Whether it is formed in xx.apk/odex
if expr "$FULLNAME" : '.*\.\([aA][pP][kK]\)$' > /dev/null; then
yaad_reverse_apk
elif expr "$FULLNAME" : '.*\.\([jJ][aA][rR]\)$' > /dev/null; then
yaad_reverse_jar
elif expr "$FULLNAME" : '.*\.\([oO][dD][eE][xX]\)$' > /dev/null; then
if [ ! -e "$FILENAME" ]; then
mkdir "$FILENAME"
fi
cd "$FILENAME"
yaad_reverse_odex "$FILEPATH"
else
echo "The decompiled file is not supported."
exit 1
fi
res=$?
echo
echo "Finished"
echo
exit "$res"