返回文章列表

Python網頁瀏覽器數位鑑識與密碼學應用

本文探討如何使用 Python 進行網頁瀏覽器數位鑑識,涵蓋從 Firefox 和 Chrome 提取後設資料(如瀏覽歷史、Cookie 和下載記錄)的技術和工具。文章提供 Python 指令碼範例,示範如何操作 SQLite 資料函式庫以取得瀏覽器中的關鍵資訊,並介紹 Firefed 和 Hindsight

資安 Web 開發

在資安事件調查中,從網頁瀏覽器提取數位證據至關重要。本文將探討如何利用 Python 從常見瀏覽器(如 Firefox 和 Chrome)中提取關鍵後設資料,包含瀏覽歷史、Cookie、下載記錄等。我們將示範如何使用 Python 指令碼直接操作 SQLite 資料函式庫,並介紹 Firefed 和 Hindsight 等工具,以自動化和簡化取證流程。同時,本文也將探討 Python 在密碼學中的應用,涵蓋對稱加密、非對稱加密、雜湊函式以及隱寫術等技術,並示範如何使用 PyCryptodome 和 cryptography 等模組實作這些功能。最後,我們將討論安全金鑰生成的最佳實務,以確保資料的安全性和完整性。

從網頁瀏覽器提取後設資料

在數位鑑識和網路安全領域,分析網頁瀏覽器的資料是一項重要的任務。本章節將探討如何從Firefox和Chrome等瀏覽器中提取後設資料,包括下載記錄、Cookie和瀏覽歷史等。

Firefox瀏覽器分析

Firefox瀏覽器將其資料儲存在SQLite資料函式庫中。以下是一個Python指令碼範例,用於提取Firefox瀏覽器的歷史記錄:

import sqlite3

def getHistory(placesDB):
    try:
        connection = sqlite3.connect(placesDB)
        cursor = connection.cursor()
        cursor.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('\n[*] -- Found History --')
        for row in cursor:
            print('[+] ' + str(row[1]) + ' - Visited: ' + str(row[0]))
    except Exception as exception:
        print('\n[*] Error reading moz_places,moz_historyvisits databases ', exception)

內容解密:

  1. 匯入必要的函式庫:使用sqlite3函式庫來連線和操作SQLite資料函式庫。
  2. 定義函式getHistory:該函式接受一個引數placesDB,代表Firefox瀏覽器歷史資料函式庫的路徑。
  3. 連線資料函式庫:使用sqlite3.connect()方法連線到指定的資料函式庫。
  4. 執行SQL查詢:查詢moz_placesmoz_historyvisits表,提取存取過的URL和存取時間。
  5. 列印結果:遍歷查詢結果,列印每個存取記錄的日期和URL。
  6. 錯誤處理:捕捉並列印任何在讀取資料函式庫過程中發生的錯誤。

使用Firefed工具

Firefed是一個開源工具,可以自動化從Firefox瀏覽器提取後設資料的過程。它提供了多種功能,包括提取儲存的密碼、偏好設定、外掛和歷史記錄等。

$ firefed -P
2 profiles found:
default [default]
/home/linux/.mozilla/Firefox/77ud9zvl.default
default-release
/home/linux/.mozilla/Firefox/n0neelh1.default-release

$ firefed -p default-release
[downloads|cookies|bookmarks|history]

內容解密:

  1. 列出Firefox組態檔案:使用-P選項列出所有本地的Firefox組態檔案。
  2. 分析指定的組態檔案:使用-p選項指定要分析的組態檔名稱或目錄。
  3. 提取後設資料:Firefed可以提取下載記錄、Cookie、書籤和歷史記錄等資訊。

Chrome瀏覽器分析

與Firefox類別似,Chrome也將其瀏覽資料儲存在SQLite資料函式庫中。以下是一個Python指令碼範例,用於提取Chrome瀏覽器的下載記錄:

import sqlite3
import datetime
import optparse

def fixDate(timestamp):
    epoch_start = datetime.datetime(1601,1,1)
    delta = datetime.timedelta(microseconds=int(timestamp))
    return epoch_start + delta

def getDownloads(historyDB):
    try:
        connection = sqlite3.connect(historyDB)
        cursor = connection.cursor()
        cursor.execute("SELECT target_path, referrer, start_time, end_time, received_bytes FROM downloads")
        print('\n[*] -- Found Downloads --')
        for row in cursor:
            print('[+] Target Path: ' + str(row[0]))
            print('[+] Referrer: ' + str(row[1]))
            print('[+] Start Time: ' + str(fixDate(row[2])))
            print('[+] End Time: ' + str(fixDate(row[3])))
            print('[+] Received Bytes: ' + str(row[4]))
    except Exception as exception:
        print('\n[*] Error reading downloads database ', exception)

內容解密:

  1. 匯入必要的函式庫:使用sqlite3函式庫來操作SQLite資料函式庫,使用datetimetimedelta來處理時間戳。
  2. 定義函式fixDate:將Chrome的時間戳(自1601年1月1日以來的微秒數)轉換為可讀的日期時間格式。
  3. 定義函式getDownloads:提取Chrome瀏覽器的下載記錄,包括目標路徑、參照者、開始時間、結束時間和接收到的位元組數。
  4. 連線資料函式庫並執行查詢:連線到指定的歷史資料函式庫,並查詢downloads表。
  5. 列印結果:遍歷查詢結果,列印每個下載記錄的詳細資訊。

從瀏覽器中提取地理位置與後設資料

在數位鑑識領域中,瀏覽器中的資料對於調查至關重要。本章節將著重於如何從Firefox和Chrome瀏覽器中提取後設資料,包括下載歷史、瀏覽記錄和Cookie等。

從Firefox瀏覽器提取後設資料

Firefox使用SQLite資料函式庫儲存使用者資料。我們可以透過查詢這些資料函式庫來提取所需資訊。以下是提取Firefox下載歷史的Python指令碼範例:

import sqlite3

def fix_date(epoch):
    # 將Epoch時間轉換為可讀格式
    from datetime import datetime
    return datetime.fromtimestamp(epoch).strftime('%Y-%m-%d %H:%M:%S')

def get_downloads_history(downloads_file):
    conn = sqlite3.connect(downloads_file)
    cursor = conn.cursor()
    cursor.execute('SELECT url, referrer, startTime, endTime, totalBytes FROM moz_downloads')
    for row in cursor.fetchall():
        print("下載:", row[0])
        print("\t來源:", row[1])
        print("\t開始時間:", fix_date(row[2]/1000000))  # 將微秒轉換為秒
        print("\t結束時間:", fix_date(row[3]/1000000))
        print("\t大小:", row[4])
    conn.close()

# 使用範例
get_downloads_history('/path/to/your/firefox/profile/downloads.sqlite')

內容解密:

  1. 匯入必要的模組:首先,我們匯入了sqlite3模組,以便能夠連線和操作SQLite資料函式庫。
  2. 定義日期轉換函式fix_date函式將Epoch時間轉換為人類可讀的日期格式。由於Firefox儲存的時間是微秒級別,我們需要將其除以1,000,000來轉換為秒。
  3. 連線資料函式庫並查詢get_downloads_history函式連線到指定的SQLite資料函式庫,並執行SQL查詢來取得下載歷史記錄。
  4. 輸出結果:對於每一條下載記錄,我們輸出其URL、參照頁面、開始和結束時間以及總大小。

從Chrome瀏覽器提取後設資料

與Firefox類別似,Chrome也使用SQLite資料函式庫來儲存使用者資料。以下是提取Chrome下載歷史的Python指令碼範例:

import sqlite3
import argparse

def fix_date(epoch):
    # 將Epoch時間轉換為可讀格式
    from datetime import datetime, timedelta
    epoch_start = datetime(1601, 1, 1)
    delta = timedelta(microseconds=epoch)
    return epoch_start + delta

def get_metadata_history_file(location_history_file):
    conn = sqlite3.connect(location_history_file)
    cursor = conn.cursor()
    cursor.execute('SELECT target_path, referrer, start_time, end_time, received_bytes FROM downloads')
    for row in cursor.fetchall():
        print("下載:", row[0].encode('utf-8'))
        print("\t來源:", str(row[1]))
        print("\t開始時間:", str(fix_date(row[2])))
        print("\t結束時間:", str(fix_date(row[3])))
        print("\t大小:", str(row[4]))
    conn.close()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='提取Chrome下載歷史')
    parser.add_argument('--location', required=True, help='Chrome歷史檔案路徑')
    args = parser.parse_args()
    get_metadata_history_file(args.location)

內容解密:

  1. 處理命令列引數:使用argparse模組來處理命令列引數,指定Chrome歷史檔案的路徑。
  2. 日期轉換:Chrome使用不同的Epoch起始時間(1601年1月1日),因此我們需要相應地調整日期轉換函式。
  3. 查詢下載歷史:連線到指定的History檔案,並查詢downloads表格以取得下載記錄。
  4. 輸出結果:與Firefox指令碼類別似,輸出每一條下載記錄的詳細資訊。

使用Hindsight進行Chrome取證

Hindsight是一個開源工具,用於解析Chrome瀏覽器的資料。它支援多種輸出格式,並提供了一個簡單的網頁介面來進行分析。

使用Hindsight的步驟:

  1. 安裝依賴:首先,需要安裝requirements.txt中列出的依賴包。
    pip install -r requirements.txt
    
  2. 執行Hindsight:可以使用命令列或網頁介面來執行Hindsight。命令列模式需要指定Chrome組態檔案的路徑。
    python hindsight.py --input /path/to/your/chrome/profile
    
    網頁介面模式則透過執行hindsight_gui.py並存取http://localhost:8080來使用。

密碼學與隱寫術:Python 實作

Python 不僅是電腦安全領域中最常用的語言之一,也以其在密碼學應用中的解決方案而聞名。本章將探討 Python 中的密碼學函式和實作,詳細介紹一些加密和解密演算法以及雜湊函式。

密碼學簡介

密碼學可以被定義為隱藏資訊的實踐,包括訊息完整性檢查、傳送者/接收者身份驗證和數位簽名的技術。

常見的密碼學演算法型別

  1. 雜湊函式:也稱為單向加密,雜湊函式為明文輸入輸出固定長度的雜湊值,理論上無法還原明文的長度或內容。單向密碼學函式用於網站以一種無法檢索的方式儲存密碼。由於被設計為單向函式,取得輸入資料的唯一方法是透過暴力搜尋可能的輸入或使用匹配雜湊表。

  2. 金鑰雜湊函式:用於構建訊息驗證碼(MAC),旨在防止暴力攻擊。

使用 PyCryptodome 進行加密和解密

本文將回顧密碼學演算法和 PyCryptodome 模組,用於加密和解密資料。

PyCryptodome 模組介紹

PyCryptodome 是 Python 中一個自包含的密碼學函式庫,支援多種加密演算法,包括對稱加密、非對稱加密和雜湊函式。

from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes

#### 內容解密:
# 這段程式碼首先匯入了必要的模組,包括 AES 加密演算法和取得隨機位元組的函式。
# AES 是一種對稱加密演算法,使用相同的金鑰進行加密和解密。

def encrypt_data(data, key):
    cipher = AES.new(key, AES.MODE_EAX)
    ciphertext, tag = cipher.encrypt_and_digest(data)
    return cipher.nonce + tag + ciphertext

#### 內容解密:
# 此函式使用 AES 加密演算法對資料進行加密。
# 首先,使用提供的金鑰建立一個新的 AES 加密物件。
# 然後,使用 `encrypt_and_digest` 方法對資料進行加密並生成驗證標籤。
# 最後,傳回 nonce、tag 和 ciphertext 的串接結果。

def decrypt_data(encrypted_data, key):
    nonce = encrypted_data[:16]
    tag = encrypted_data[16:32]
    ciphertext = encrypted_data[32:]
    cipher = AES.new(key, AES.MODE_EAX, nonce=nonce)
    data = cipher.decrypt_and_verify(ciphertext, tag)
    return data

#### 內容解密:
# 此函式使用 AES 解密演算法對加密資料進行解密。
# 首先,從加密資料中提取 nonce、tag 和 ciphertext。
# 然後,使用提供的金鑰和 nonce 建立一個新的 AES 解密物件。
# 最後,使用 `decrypt_and_verify` 方法對 ciphertext 進行解密並驗證 tag。

key = get_random_bytes(16)
data = b"This is a secret message."
encrypted_data = encrypt_data(data, key)
print("Encrypted Data:", encrypted_data)

decrypted_data = decrypt_data(encrypted_data, key)
print("Decrypted Data:", decrypted_data)

#### 內容解密:
# 這段程式碼首先生成一個隨機的 16 位元組金鑰。
# 然後,定義一個秘密訊息並使用 `encrypt_data` 函式進行加密。
# 最後,使用 `decrypt_data` 函式對加密資料進行解密並列印結果。

使用 Cryptography 模組進行加密和解密

除了 PyCryptodome,Python 的 cryptography 模組也提供了一系列豐富的密碼學功能。

隱寫術技術

隱寫術是一種將秘密資訊隱藏在非秘密資訊中的技術。本章還將介紹如何使用 stepic 模組在圖片中隱藏資訊。

安全金鑰生成

最後,本章將介紹如何使用 secrets 和 hashlib 模組安全地生成金鑰。

問題

  1. 在 PyCryptodome 中,如何使用 AES 演算法進行對稱加密?
  2. cryptography 模組提供了哪些密碼學功能?
  3. 如何使用 stepic 模組在圖片中隱藏資訊?
  4. secrets 和 hashlib 模組在金鑰生成中扮演什麼角色?

進一步閱讀