Google Authenticator采用的算法是T-OTP(Time-Based One-Time Password),需要知道以下三点信息:
* Key: 共享密钥
* Current Time: 当前时间输入
* HMAC-SHA1函数
共享密钥
共享密码用于在手机端上建立账户。密码内容可以是通过手机拍照二维码或者手工输入,并会被进行base32加密。
手工密码的输入格式如下:
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
包含该令牌的二维码的内容是一个URL:
otpauth://totp/Google%3Ayourname@gmail.com?secret=xxxx&issuer=Google
时间输入(当前时间)
输入的时间值来自于手机,一旦注册获得密钥后,就无需与服务器再进行通信,所以要确保手机上的时间准确,因为往后的步骤服务器可能会验证多个收到的OTP token,但时间值服务器只会取当前时间: 服务器会比对所有提交的token以确认是否有正确输入。
Hash函数
验证所用的方法是HMAC-SHA1,以一个密钥和一个消息为输入,生成一个20字节消息摘要作为输出。其算法可以简单表示为:
hmac = SHA1(secret + SHA1(secret + input))
T-OTP与H-OTP的区别是T-OTP以当前时间作为输入,而H-OTP以自增counter(based on a seed)作为输入,该计数器使用时需要两边同步。
算法
首先,要进行密钥的base32加密。虽然谷歌上的密钥格式是带空格的,不过base32拒绝空格输入,并只允许大写。所以要作如下处理:
original_secret = xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
secret = BASE32_DECODE(TO_UPPERCASE(REMOVE_SPACES(original_secret)))
第二步要获取当前时间值,这里使用的是UNIX time函数,或者可以用纪元秒。
input = CURRENT_UNIX_TIME()
在Google Authenticator中,input值拥有一个有效期。因为如果直接根据时间进行计算,结果将时刻发生改变,那么将很难进行复用。Google Authenticator默认使用30秒作为有效期(时间片),最后input的取值为从Unix epoch(1970年1月1日 00:00:00)来经历的30秒的个数。
input = CURRENT_UNIX_TIME() / 30
最后一步是进行HMAC-SHA1运算
1.original_secret = xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
2.secret = BASE32_DECODE(TO_UPPERCASE(REMOVE_SPACES(original_secret)))
3.input = CURRENT_UNIX_TIME() / 30
4.hmac = SHA1(secret + SHA1(secret + input))
HMAC运算后的结果会是20字节即40位16进制数,我们需要的是常规6位数字密码. 首先要对20字节的SHA1进行瘦身。我们把SHA1的最后4个比特数(每个数的取值是0~15)用来做索引号,然后使用从索引号开始的4个字节计算OTP。因此,索引号的操作范围是15+4=19,加上是以零开始,所以能完整表示20字节的信息。4字节的获取方法是:
1.然后将它转化为标准的32bit无符号整数(4 bytes = 32 bit):
2.large_integer = INT(four_bytes)
最后再进行7位数(1百万)取整,就可得到6位数字了:
1.large_integer = INT(four_bytes)
2.small_integer = large_integer % 1,000,000
这也是我们最后要的目标结果,整个过程总结如下:
1.original_secret = xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
2.secret = BASE32_DECODE(TO_UPPERCASE(REMOVE_SPACES(original_secret)))
3.input = CURRENT_UNIX_TIME() / 30
4.hmac = SHA1(secret + SHA1(secret + input))
5.four_bytes = hmac[LAST_BYTE(hmac):LAST_BYTE(hmac) + 4]
6.large_integer = INT(four_bytes)
7.small_integer = large_integer % 1,000,000
一个完整可执行的GO语言程序,可以这里进行查看:
http://garbagecollected.org/2014/09/14/how-google-authenticator-works/
2015/07/29
2015/06/11
Linux 32-bit和64-bit编译
Environment:
Ubuntu 14.04 64-bit desktop, as guest virtual machine in virtual box
Host: MacBook Pro
在64位机器上缺省编译或运行时查找的是64位程序,但如果编译32位程序通常会遇到一些问题,这里做一个说明。
Build 32-bit C/C++ program on 64-bit Linux
sudo apt-get install g++ #to build C++ source files
sudo apt-get install g++-multilib #to build 32-bit executable on 64-bit Linux
g++ -m32 -g -o a.out file1.cpp file2.cpp #specify 32-bit binary
Run 32-bit executable on 64-bit Linux
要运行32位legacy程序,则需要安装相应的32位库文件(ia32-libs)。
首先添加 i386 architecture,然后安装必要的32位库。
sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386
sudo ./file-name
以下步骤可能也需要:
sudo apt-get update
sudo apt-get install lib32z1 lib32ncurses5 lib32bz2-1.0
特别的,比如需要libgc library,则需指定i386类型下载:
sudo apt-get install libgc1c2 (for 32-bit linux)
sudo apt-get install libgc1c2:i386 (for 64-bit linux)
可以用命令 “file file_name”查看执行文件类型:
file-name: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.8, not stripped
Ubuntu 14.04 64-bit desktop, as guest virtual machine in virtual box
Host: MacBook Pro
在64位机器上缺省编译或运行时查找的是64位程序,但如果编译32位程序通常会遇到一些问题,这里做一个说明。
Build 32-bit C/C++ program on 64-bit Linux
sudo apt-get install g++ #to build C++ source files
sudo apt-get install g++-multilib #to build 32-bit executable on 64-bit Linux
g++ -m32 -g -o a.out file1.cpp file2.cpp #specify 32-bit binary
Run 32-bit executable on 64-bit Linux
要运行32位legacy程序,则需要安装相应的32位库文件(ia32-libs)。
首先添加 i386 architecture,然后安装必要的32位库。
sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386
sudo ./file-name
以下步骤可能也需要:
sudo apt-get update
sudo apt-get install lib32z1 lib32ncurses5 lib32bz2-1.0
特别的,比如需要libgc library,则需指定i386类型下载:
sudo apt-get install libgc1c2 (for 32-bit linux)
sudo apt-get install libgc1c2:i386 (for 64-bit linux)
可以用命令 “file file_name”查看执行文件类型:
file-name: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.8, not stripped
2015/06/10
Shrink Virtualbox VM VDI File Size
Virtualbox 的VM image size (.vdi file)有时候会变得很大, 如何缩减文件大小呢?
这里我的host = MacBook Pro, guest = Win7. 虚拟机创建的时候设置的是 “Dynamically Expanding Storage” , 但以后随着增删文件操作而磁盘只会增加不会减少。
- Run defrag in the guest (Windows only)
- Nullify free space:With a Linux Guest run this:
sudo dd if=/dev/zero of=/bigemptyfile bs=4096k sudo rm -rf /bigemptyfileWith a Windows Guest, download SDelete from Sysinternals and run this:sdelete –z c: - Shutdown the guest VM
- Now run VBoxManage's
modifyhdcommand with the--compactoption:With a Linux Host run this:vboxmanage modifyhd /path/to/thedisk.vdi --compactWith a Windows Host run this:VBoxManage.exe modifyhd c:\path\to\thedisk.vdi --compactWith a Mac Host run this:VBoxManage modifyhd /path/to/thedisk.vdi --compact
This reduces the vdi size.
2015/01/13
一些用来加固应用的.so库
libsecexe.so 梆梆加固
libAPKProtect.so APKProtect加固,
libprotectClass.so 360加固,
libNSaferOnly.so 通付盾加固,
libnqshield.so 网秦加固,
libshell.so 腾讯加固,
ijiami.dat 爱加密加固,
libddog.so 娜迦加固,
libmobisec.so 阿里加固,
libbaiduprotect.so 百度加固
2015/01/08
Virtualization on Android OS
Virtualisation on Android OS:
- OKL4 Microvisor from Open Kernel Labs. VMM runs on highest privilege, virtualising: processor(instructions,registers), memory, I/O. The other applications run on light-weight lower level.
- VLX from VirtualLogix.
- Mobile visor platform (MVP) from VMware, offering a hypervisor on smart mobile phones
Linux Container.
major difference between hypervisor and V-OS:
hypervisor lies between OS and hardware, while V-OS lies above OS level. so the security of V-OS actually heavily depends on protection/obfuscation on application code.
major difference btw. security method of V-OS and app-wrapping:
Problems: the purpose of security is not very clear. what does V-OS want to protect?
algorithm/implementation or sensitive data. code obfuscation can only improve the difficulty of reverse engineering (sometimes it is sufficient). if it is sensitive data, no absolute secure solution without hardware isolation support. the best effort may be to provide multiple layer securities.
订阅:
博文 (Atom)