受好友nameless的委托,对一个名叫nokelock的apk进行插桩,希望在日志中打印出蓝牙加密包的密文,密钥与明文,由于本人是第一次对apk进行插桩,于是写了这一篇文章用以学习和记录。
一
二
base.apk
com.nokelock.blelibrary.b.b
中。jadx
反编译看一下相关的java代码。AES
加密,而我们需要在日志中打印的变量也一目了然,分别是bArr
,bArr2
和instance.doFinal(bArr)。
jadx
中,我们可以直接对相关的java代码按一下tab
键,然后就能得到更加原始的smali代码。apk easy tool
,然后把我们需要反编译的apk拖到这个工具里面,直接先点一下反编译,然后在点一下打开反编译目录,就可以看到apk的smali代码了。com.nokelock.blelibrary.b.b
)一级一级的点进去,就得到了可以直接编辑的smali代码。三
b.smali
文件后,通过对相同方法名和形参类型的寻找,我们可以快速定位到如下代码段;.method public static a([B[B)[B
.locals 2:try_start_0
new-instance v0, Ljavax/crypto/spec/SecretKeySpec;const-string v1, "AES"
invoke-direct {v0, p1, v1}, Ljavax/crypto/spec/SecretKeySpec;-><init>([BLjava/lang/String;)V
const-string p1, "AES/ECB/NoPadding"
invoke-static {p1}, Ljavax/crypto/Cipher;->getInstance(Ljava/lang/String;)Ljavax/crypto/Cipher;
move-result-object p1
const/4 v1, 0x1
invoke-virtual {p1, v1, v0}, Ljavax/crypto/Cipher;->init(ILjava/security/Key;)V
invoke-virtual {p1, p0}, Ljavax/crypto/Cipher;->doFinal([B)[B
move-result-object p0
:try_end_0
.catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0return-object p0
:catch_0
const/4 p0, 0x0return-object p0
.end method.method public static b([B[B)[B
.locals 2:try_start_0
new-instance v0, Ljavax/crypto/spec/SecretKeySpec;const-string v1, "AES"
invoke-direct {v0, p1, v1}, Ljavax/crypto/spec/SecretKeySpec;-><init>([BLjava/lang/String;)V
const-string p1, "AES/ECB/NoPadding"
invoke-static {p1}, Ljavax/crypto/Cipher;->getInstance(Ljava/lang/String;)Ljavax/crypto/Cipher;
move-result-object p1
const/4 v1, 0x2
invoke-virtual {p1, v1, v0}, Ljavax/crypto/Cipher;->init(ILjava/security/Key;)V
invoke-virtual {p1, p0}, Ljavax/crypto/Cipher;->doFinal([B)[B
move-result-object p0
:try_end_0
.catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0return-object p0
:catch_0
const/4 p0, 0x0return-object p0
.end method
SewellDinGLog.smali
直接复制到apk easy tool
反编译完成后的那个smail文件夹中如图所示:b.smali
中。invoke-static {}, LSewellDinGLog;->Log()V# 无参数,用来判断函数是否执行
invoke-static {v1}, LSewellDinGLog;->Log(Ljava/lang/Object;)V# 打印字符串
invoke-static {v1}, LSewellDinGLog;->Log([Ljava/lang/Object;)V# 打印数组
SewellDinGLog.java
来看。import java.util.Arrays;
import android.util.Log;public class SewellDinGLog {
public static void Log(String tag, String msg) {//两个参数
Log.d(tag, msg);
}public static void Log() {//无参数
Log("SewellDinG", "DeBug ...");
}public static void Log(Object someObj) {//一个参数,打印字符串
Log("SewellDinG", someObj.toString());
}public static void Log(Object[] someObj) {//一个参数,打印数组
Log("SewellDinG", Arrays.toString(someObj));
}}
apk easy tool
,点一下回编译,在点一下打开回编译目录,就能找到最新编译成功的apk。adb
命令运行该apk,然后使用命令就可以查看打印的日志。adb logcat -s SewellDinG
四
F:\platform-tools>adb logcat -s SewellDinG
--------- beginning of main
03-27 12:38:56.882 14325 14325 D SewellDinG: [[email protected]
03-27 12:38:56.882 14325 14325 D SewellDinG: [[email protected]
03-27 12:38:56.882 14325 14325 D SewellDinG: [[email protected]
03-27 12:39:05.392 14325 14325 D SewellDinG: [[email protected]
03-27 12:39:05.392 14325 14325 D SewellDinG: [[email protected]
03-27 12:39:05.392 14325 14325 D SewellDinG: [[email protected]
[[email protected]
开头的?后来才发现是SewellDinGLog.smali
代码有问题。byte
类型是数组,而打印数组的java代码是长这个样子的。public static void Log(Object[] someObj) {//一个参数,打印数组
Log("SewellDinG", Arrays.toString(someObj));
}
toString
方法,对于字节数组来说返回是字节数组的地址而非字节数组的值!五
SewellDinGLog.java
所需要的包android.util.Log
根本就没有,在网上找了找也找不到这个包,之后通过google了很久,才知道我需要安装一个Android Studio(https://developer.android.com/studio)。Language
一定要选Java,不要选Kotlin
,不然得像我一样下了很久的安装包最后发现代码根本运行不了。Android studio
,得需要等大约几十分钟安装运行所需要的包。File-->Settings-->Plugins
,然后在插件市场搜索java2smali
,安装完之后重启Android studio
,然后在Build-->Compile to smali
就可以直接生成smali代码了。SewellDinGLog.java
代码进行相应的修改,然后使用Build-->Compile to smali
生成smail代码。package com.example.newlog; import android.util.Log;
public class SewellDinGLog {
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static void Log(String tag, String msg) {
Log.d(tag, msg);
}public static void Log() {
Log("SewellDinG", "DeBug ...");
}public static void Log(byte[] someObj) {
String result = bytesToHex(someObj);
Log("SewellDinG", result);
}}
SewellDinGLog.smali。
SewellDinGLog.smali
是放在apk easy tool
反编译后的smali文件夹的根文件夹下,所以我们需要对SewellDinGLog.smali
内的代码进行修改。Lcom/example/newlog/SewellDinGLog
全部替换成LSewellDinGLog。
SewellDinGLog.smali
复制到smali
文件夹内,然后插桩,回编译就可以了。jadx
反编译看看。SewellDinGLog.Log
函数用以打印变量的值。F:\platform-tools>adb logcat -s SewellDinG
--------- beginning of main
03-27 17:18:08.895 31117 31117 D SewellDinG: 05010630303030303082E3C89616017D
03-27 17:18:08.895 31117 31117 D SewellDinG: 241F632E5907042061014C1A3A45193B
03-27 17:18:08.895 31117 31117 D SewellDinG: 6629B62C88A7E50525E92C328AF258E6
03-27 17:18:10.114 31117 31117 D SewellDinG: 732F5CB22C06B0C2D0D17AD31D165805
03-27 17:18:10.114 31117 31117 D SewellDinG: 241F632E5907042061014C1A3A45193B
03-27 17:18:10.114 31117 31117 D SewellDinG: 05020100E3C896010202000000000000
03-27 17:18:11.770 31117 31117 D SewellDinG: 530B1FCA1467A408A321E71F3D152127
03-27 17:18:11.771 31117 31117 D SewellDinG: 241F632E5907042061014C1A3A45193B
六
invoke-static {p0, p1}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I
看雪ID:oacia
https://bbs.kanxue.com/user-home-963320.htm
# 往期推荐
3、安卓加固脱壳分享
球分享
球点赞
球在看