Note finishing 5-- use python achieve firefox browser users to obtain records

A main ideas:
(1) the output of the browser cookie
connecting the respective database, query input, a cursor comes to obtain a cookie,
traverse the cursor, for each row [X] is a single row of data, so that row [end] to give a complete
record.

(2) History records the output browser
connection above, the query a little change, joint use of the two tables query

(3) The output google query records
by regular expressions to extract history, get off to google Search record

(4) The main function
first determines whether the presence of the corresponding database, then perform the corresponding user information acquisition function

Problems encountered 3.
(1) wherein when the input url, encountered spaces in the path, the program acquiring parameter error.
Solution: using double quotes wrapping path at the input
. (2) used in the alternative vim editor, the corresponding statement
ESC:% s / 1/2 / g // 1 to replace paper 2 / g is added to the end of the overall replacement, without replacement g per line only the first
(3) prior to entering the appropriate directory acquired in advance. detect whether the database exists, the results encountered insufficient privileges
inaccessible here for a long time and shift
solution: query Netcom had encountered problems in Microsoft's official direct keywords to help get the problem
mainly to change the file properties -> security - > advanced -> change the file owner to current users
get the current user can open the control panel -> user accounts query
(4) .sqlite database
query tables, show tables can not be used, use the select name from sqlite_master

4. Summary harvest
(1). When the search engine technology is difficult to directly question the answer, see the official documents directly,
use google to search the official website of choice google, Baidu pit once again,
of course, when using Bing Experience not as good as Baidu
(2). regular expressions basis
the appropriate documentation
http://www.runoob.com/regexp/regexp-syntax.html

II. Code

#!/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()

Guess you like

Origin www.cnblogs.com/qianxinggz/p/11402613.html