删除apk/dex里的support类的shell脚本

以前全局搜字符串时候经常搜到support-v4/7/13的字符串,逆向时候烦的不行,写了一个轮子,专门删除dex里面的这部分代码。

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
if [ "$#" -ne 1 -a "$#" -ne 2 ];then
echo "Delete support-v4/v7/v13 classes in dex or apk.
Usage:
./del-support.sh input-dex/apk [output-dex/apk]
Sample:
./del-support.sh classes.dex out.dex
./del-support.sh input.apk output.apk
Author: LeadroyaL
Time: 2018/3/5"
exit
fi

echo "GOGOGO!"

if [[ $1 == *.dex ]];then
echo "dex file!"
t=dex
filename=${1%%.dex}
fi
if [[ $1 == *.apk ]];then
echo "apk file!"
t=apk
filename=${1%%.apk}
fi
echo type is $t and filename is $filename
if [[ $t == apk ]];then
unzip $1 classes.dex
fi

java -jar baksmali.jar d $1
rm -rf out/android/support/
java -jar smali.jar a out/

if [ "$#" -eq 1 ];then
if [[ $t == apk ]];then
o="output.apk"
else
o="out.dex"
fi
else
o=$2
fi

if [[ $t == apk ]];then
if [[ $o != *.apk ]];then
o=$o.apk
else
o=$o
fi
mv out.dex classes.dex
cp $1 $o
zip $o -u classes.dex
else
mv out.dex $o
fi

echo Success and $o generated.

第一次写shell脚本,坑的要死,记一下几个坑:

  • [][[]] 是不一样的,正则匹配字符串时候千万不能写[ $1 == *.dex ] ,要写[[ $1 == *.dex ]] ,前者*.dex的意义是补全当前目录下的所有.dex文件
  • ${xxxx}可以进行字符串的拼接删除操作

效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  /tmp ./del-support.sh app-debug.apk
GOGOGO!
apk file!
type is apk and filename is app-debug
Archive: app-debug.apk
replace classes.dex? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: classes.dex
updating: classes.dex (deflated 69%)
Success and output.apk generated.
➜ /tmp ./del-support.sh classes.dex
GOGOGO!
dex file!
type is dex and filename is classes
Success and out.dex generated.