免责声明和更多的免责声明
在文章中我已经去掉了敏感的信息,以保护无辜的人的利益。而且这项研究的结果也不足以让我能够黑掉别人的银行账户。即使一个拥有root权限的第三方恶意应用,在没有足够的账户信息的前提下,都不能模拟动态口令的生成。而且,这次研究并没有发现任何代码层面的漏洞,这家银行的生成算法甚至比google的认证算法还要安全,甚至可以说这篇文章是对该银行APP安全性的一次褒奖。他的动态生成算法,完全符合TOTP规范。把数据安全做到了极致。 |
下面就是免责声明 balabala一大堆,请原谅小编就不翻译了,想看的可以去原文地址观看
在阅读源码之前,我还是喜欢先把应用下载下来看看他长啥样,下面是三个阶段的截图。
第一张是安装时的截图,看一下他所需要的权限,这可不是开发人员为何好玩才加上去的。他们其中有些甚至有可能影响到动态口令的生成。第二张是激活界面,需要填四个数字,这四个数字只能通过给银行打电话被告知。第三幅图就是,成功激活后生成的动态口令。
提供adb这个强大的命令行工具,提取apk文件,和获取手机信息全靠它。
这个工具可以吧dex转换成jar包的形式
JD, JD-GUI
$ ./adb shell pm list packages | grep mybank
package:com.mybank
确定路径
$ ./adb shell pm path com.mybank
package:/data/app/com.mybank-1.apk
$ ./adb pull /data/app/com.mybank-1.apk
2950 KB/s (15613144 bytes in 5.168s)
$ unzip com.mybank-1.apk
(file list omitted for brevity)
$ mv classes.dex com.mybank-1.dex
$ ./d2j-dex2jar.sh com.mybank-1.dex
dex2jar com.mybank-1.dex -> com.mybank-1-dex2jar.jar
很轻易的就可以发现几个比较特殊的包,br.com.mybank.integrador.token, br.com.othercompany.token , com.mybank.varejo.token毫无疑问核心代码就在里面,只不过代码应该被混淆了。
public void trocaPINcomLogin(int paramInt, boolean paramBoolean, Perfil paramPerfil)
{
if (paramPerfil == null)
throw new IllegalArgumentException(a.a("1p5/eEf/sl3kbeUcP509qg=="));
if (!this.jdField_a_of_type_U.jdField_a_of_type_JavaUtilHashtable.contains(paramPerfil))
throw new RuntimeException(a.a("86jcmKgr/ZshQu9aGVbuGscy2nHW4UEWqudRoUXhImQ=") + a.a("7u8KqqwqUD3a7FM339fp6pRrxUtQrHDMyqvZ6A2MurQ="));
if ((this.jdField_a_of_type_BrComOtherCompanyTokenParamsGerenciador.isPinObrigatorio()) && (!paramBoolean))
throw new RuntimeException(a.a("aMsL/5kjkXKD4K1SvpTuuJZUS0U0fL19UT2GxjJ/QzQ="));
Configuracao localConfiguracao = paramPerfil.getConfiguracao();
if ((localConfiguracao.a().a()) && (paramPerfil != this.jdField_a_of_type_BrComOtherCompanyTokenPerfil))
throw new RuntimeException(a.a("ASszutKFJW3iqDb7X/+vqAcYxTLXN2SJOIs0ne596Pu3ZoRxjiiscwhV6fT70efX"));
localConfiguracao.a().a(paramInt);
localConfiguracao.a().a(paramBoolean);
this.jdField_a_of_type_U.a(paramPerfil);
if (!paramPerfil.equals(this.jdField_a_of_type_BrComOtherCompanyTokenPerfil))
a(paramPerfil);
}
不过幸运的是,在抛出异常的语句中,我们可以找到一些蛛丝马迹,我们通过观察可以发现,混淆字符串的函数是a.a。根据这些信息的提示,我们可以猜测a.a是一个解密有关的类。顺理成章,我们直接去a函数中分析解密所使用的代码。
def decodeExceptionString(str): aesKey = xorKey = blockSize = 16 aes = AES(aesKey) stringBytes = Base64.decode(str) outputString = "" for blockStart in xrange(0, len(stringBytes), blockSize): encryptedBlock = stringBytes[blockStart:blockStart+blockSize] plaintextBlock = aes.decrypt(encryptedBlock) outputString += plaintextBlock ^ xorKey xorKey = encryptedBlock return outputString
$ ./decode "ASszutKFJW3iqDb7X/+vqAcYxTLXN2SJOIs0ne596Pu3ZoRxjiiscwhV6fT70efX"
N?o é possível alterar PIN sem estar logado.
public String calculate() throws TokenException {
int i = (int)Math.max(Math.min((this.a.getConfiguracao().getAjusteTemporal() + Calendar.getInstance().getTime().getTime() - 1175385600000L) / 36000L, 2147483647L), -2147483648L);
a();
if (i < 0)
throw new TokenException("Janela negativa"), i);
int j = (0x3 & this.a.getConfiguracao().getAlgoritmos().a) >> 0;
switch (j)
{
default:
throw new TokenException("Algoritmo inválido:" + j, i);
case 0:
return a(i);
case 1:
}
return o.a(this.a.getConfiguracao().getChave().a(20), i);
}
很容易读懂,变量i是一个时间戳,从2007年4月11日到现在的秒数除以36,36就是每个动态口令的存活时间。
public class PersistenciaUtils {
public static byte[] getChave(Context paramContext, byte[] paramArrayOfByte) {
try {
byte[] arrayOfByte = MessageDigest.getInstance("SHA-1").digest(getId(paramContext).getBytes());
return arrayOfByte;
} catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
}
return new byte[20];
}
public static String getId(Context paramContext) {
String str = Settings.System.getString(paramContext.getContentResolver(), "android_id");
if (str == null)
str = "";
return str;
}
}
从代码可以看出,先取得android_id的SHA-1摘要,如果获取失败就是用一个默认的16进制hash字符串。
$ ./adb shell
shell@hammerhead:/ $ content query --uri content://settings/secure --projection name:value --where "name='android_id'"
Row: 0 name=android_id, value=0123456789abcdef
shell@hammerhead:/ $ exit
a.aa把blob分成很多段,96位的头,16位的随机数,16位的标签和为加密数据预留的空间。
def decodeBlob(datablob, android_id):
header = datablob[:96]
nonce = datablob[96:112]
tag = datablob[112:128]
cryptotext = datablob[128:]
key1 = SHA1(android_id)[:16]
aes = AES(key1)
cmac = CMAC(aes)
cmac.update(header)
key2 = cmac.getTag()
eax = EAX(key2, aes)
(validTag, plaintext) = eax.checkAndDecrypt(cryptotext, tag)
if validTag:
return plaintext
如果EAX验证成功,aa.a返回解密的内容给PersistenciaDB使用。
这就是密钥所需要的三个数据,只要三个都正确,程序就可以正常运行。
def generateToken(key, timestamp):
message = [0] * 8
for i in xrange(7, 0, -1):
message[i] = timestamp & 0xFF
timestamp >>= 8
hmacSha1 = HMAC_SHA1(key)
hmacSha1.update(message)
hash = hmacSha1.getHash()
k = 0xF & hash[-1]
m = ((0x7F & hash[k]) << 24 | (0xFF & hash[(k + 1)]) << 16 | (0xFF & hash[(k + 2)]) << 8 | 0xFF & hash[(k + 3)]) % 1000000;
return "%06d" % m
基本时间戳是一个占8字节的长整形,手动把它转换成大端的byte数组,接着使用HMAC-SHA1,取得hash最后四位作为整数读取的索引。使用这个整形,mod 1000000,就是我们的随机密码了,简单的超乎我的想象~~
public String generateResponseCode(byte[] challenge)
throws GeneralSecurityException {
byte[] hash = signer.sign(challenge);
// Dynamically truncate the hash
// OffsetBits are the low order bits of the last byte of the hash
int offset = hash[hash.length - 1] & 0xF;
// Grab a positive integer value starting at the given offset.
int truncatedHash = hashToInt(hash, offset) & 0x7FFFFFFF;
int pinValue = truncatedHash % (int) Math.pow(10, codeLength);
return padOutput(pinValue);
}
其实他们使用了基本相同的算法。
Cryptosuite
Arduino的加密算法库 (包括 SHA and HMAC-SHA)
RTClib
JeeNodes and Arduinos所使用的轻量级时间日期库.
2x16LCD_library
A library for 2x16 LCD (like JDH162A or HD44780) written for Energia and Stellaris Launchpad (LM4F).
RTC有一部分需要改进。由于Stellaris LaunchPad没有板载实时时钟,内部时钟需要在每次启动时设置,而且需要一台电脑来辅助,这是很麻烦的事情。
#include
#include
#include
RTC_Millis RTC;
void setup() {
RTC.begin(DateTime(__DATE__, __TIME__));
LCD.init(PE_3, PE_2, PE_1, PD_3, PD_2, PD_1);
LCD.print("Token");
LCD.print("valverde.me", 2, 1);
delay(1000);
LCD.clear();
}
char token[6];
uint8_t message[8];
long timestamp = 0;
long i = 0;
uint8_t key[] = {};
void showToken() {
long now = RTC.now().get() - 228700800 + 7200;
i = now / 36;
int timeLeft = now % 36;
for(int j = 7; j >= 0; j--) {
message[j] = ((byte)(i & 0xFF));
i >>= 8;
}
Sha1.initHmac(key, 20);
Sha1.writebytes(message, 8);
uint8_t * hash = Sha1.resultHmac();
int k = 0xF & hash[19];
int m = ((0x7F & hash[k]) << 24 | (0xFF & hash[(k + 1)]) << 16 | (0xFF & hash[(k + 2)]) << 8 | 0xFF & hash[(k + 3)]) % 1000000;
LCD.print(m, 2, 1);
LCD.print(36 - timeLeft, 2, 15);
}
void loop() {
LCD.clear();
LCD.print("Current token:");
showToken();
delay(1000);
}
最后作者还分享了一个,解决Arduino时间问题的小技巧~这里省略啦~感兴趣的同学可以去原文看。
链接: http://pan.baidu.com/s/1ntJZX1J 密码: 9mne
原文链接:http://blog.valverde.me/2014/01/03/reverse-engineering-my-bank%27s-security-token/#.Uwa0CGI723D
翻译By FreeBuf小编wyl:http://www.freebuf.com/articles/terminal/26631.html