Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Session 03 - Python for Forensics

The code here is to support the Session 03 lecture.

database.py

import sys, sqlite3

def getCSV(conn, tgtTbl):
    csv = ""
    csv_cursor = conn.cursor()
    data = csv_cursor.execute("SELECT * FROM " + tgtTbl + ";")
    for row in data:
        csv += ', '.join(map(str, row)) + "\n"
    return csv

def usage():
    print("[-] Incorrect arguments...")
    print("Usage:", sys.argv[0], "<database>")
    sys.exit()

if len(sys.argv) != 2:
    usage()

tgtBase = sys.argv[1]

conn = sqlite3.connect(tgtBase)
c = conn.cursor()
results = c.execute("SELECT tbl_name FROM sqlite_master WHERE type = 'table';")

for table in results:
    tableName = table[0]
    print("[+] Processing Table:", tableName)
    fileName = tableName + ".csv"
    f = open(fileName, 'w')
    f.write(getCSV(conn, tableName))
    f.close()

conn.close()
[ Last updated: Tuesday 13 February 2024 @ 11:22:49 +0000 // branch: master // commit: bd41815 ]