笔记整理5——利用python实现firefox浏览器用户记录获取

一.主要思路:
(1).输出浏览器中的cookie
连接相应的数据库,输入查询语句,得到自带cookie的一个游标,
遍历该游标,每个row[x]为单行一列的数据,从而row[end]得到一条完整的
记录。

(2).输出浏览器的历史记录
连接同上,查询语句稍微有变化,利用了两张表进行联合查询

(3).输出google的查询记录
通过正则表达式对历史记录进行提取,获取到google过的查询记录

(4).主函数
首先判断相应的数据库是否存在,再执行相应函数进行用户信息获取

3.遭遇问题
(1).其中在输入url时,遇到空格路径,程序获取参数错误
解决方式:在输入时利用双引号包裹路径
(2).在vim编辑器中使用了替换,相对应的语句为
ESC :%s/1/2/g // 把全文的1替换成2 /末尾加g 是全局替换,不加g 只替换每行第一个
(3).在获取之前提前进入相应目录探测该数据库是否存在,结果遇到权限不足
无法访问,在这里倒腾了很久
解决方式:在微软官网通过遭遇问题的直接关键字进行查询,获得了问题帮助
主要是更改文件属性-->安全-->高级-->更改文件所有者为当前用户
获取当前用户可以打开控制面板-->用户账户进行查询
(4).sqlite数据库
查询表时,show tables不能用,使用select name from sqlite_master

4.总结收获
(1).搜索引擎搜索技术问题很难直接得出答案时,直接参见官方文档,
利用google在官方网站进行搜索,首选google,又被百度坑了一次,
当然,使用Bing时体验不如百度
(2).正则表达式基础
相应文档
http://www.runoob.com/regexp/regexp-syntax.html

二.代码

#!/usr/bin/python
#encoding:utf-8


import re
import os
import optparse
import sqlite3


def printCookies(cookieDB):#找到存储cookie的数据库,获取cookie数据
    try:
        conn = sqlite3.connect(cookieDB)
        c = conn.cursor()
        c.execute('SELECT host,name,value FROM moz_cookies')
        print '---Found Cookies---'
        for row in c:
            host = str(row[0])
            name = str(row[1])
            value = str(row[2])
            print 'Host: '+str(host)+' Name: '+str(name)+' Value: '+str(value)
    except Exception,e:
        print e
        if 'encrypted' in e:
            print '[-] You should updata your python-sqlite3 library'
            exit(0)


def printHistory(placesDB):   #通过places数据库,获取用户的历史浏览记录,下载记录等信息
    History = open('History.txt', 'w')
    try:
        conn = sqlite3.connect(placesDB)

        c = conn.cursor()
        c.execute('SELECT url,datetime(visit_date/1000000,\'unixepoch\') \
                FROM moz_places,moz_historyvisits WHERE visit_count > 0 \
                AND moz_places.id == moz_historyvisits.place_id;')

        print >> History,'[*]---Found History---'
        for row in c:   #遍历每一行
            url = str(row[0])     #遍历每一个属性
            date = str(row[1])
            print >> History,'[+] '+str(date)+' - visited: '+str(url)   #输出利用了重定向,使得输出结果放在一个文件中
    except Exception,e:
        print e
        if 'encrypted' in e:
            print '[-] You should updata your python-sqlite3 library'
            exit(0)


def printGoogle(placesDB):   #查找google搜索记录
    try:
        conn = sqlite3.connect(placesDB)
        c = conn.cursor()
        c.execute('SELECT url,datetime(visit_date/1000000,\'unixepoch\') \
                FROM moz_places,moz_historyvisits WHERE visit_count > 0 \
                AND moz_places.id == moz_historyvisits.place_id;')
        print '[*]---Found Google History---'
        for row in c:   #遍历每一行
            url = str(row[0])     #遍历每一个属性
            date = str(row[1])
            if 'google'in url.lower():
                r = re.findall(r'q=.*\&',url)   #利用正则表达式,查找搜索的关键字
                if r:
                    Google = open('google.txt', 'w')
                    search = r[0].split('&')[0]
                    search = search.replace('q=','').replace('+',' ')
                    print >> Google,'[+] ' +date+ ' - Searched For: '+search
    except Exception,e:
        print e
        if 'encrypted' in e:
            print '[-] You should updata your python-sqlite3 library'
            exit(0)


def main():
    parser = optparse.OptionParser('-p <Database PathName>')
    parser.add_option('-p', dest='path', type='string', help='specify the PathName')
    (options,args) = parser.parse_args()
    path = options.path
    if path == None:
        print parser.usage
        exit(0)
    elif os.path.isdir(path) == False:
        print '[-] Your path not exist'
        exit(0)
    else:
        cookieDB = os.path.join(path, 'cookies.sqlite')
        if os.path.isfile(cookieDB):
            printCookies(cookieDB)
        else:
            print '[-] The cookies.sqlite not exist'

        placeDB = os.path.join(path, 'places.sqlite')  #复合目录
        if os.path.isfile(placeDB):
            printHistory(placeDB)
            printGoogle(placeDB)
        else:
            print '[-] The places.sqlite not exist'



if __name__ == '__main__':
    main()


posted @ 2019-08-23 21:00  qianxinggz  阅读(574)  评论(0编辑  收藏  举报