記憶體鑑識與資料函式庫分析是數位鑑識的重要環節。本文將探討 Volatility 記憶體鑑識框架的應用,包含提取行程資訊、數位憑證等關鍵證據。同時,文章也將示範如何使用 Python 連線與分析 SQLite 資料函式庫,包含列出資料表、取得結構描述等操作。此外,還會介紹 PcapXray 工具在網路鑑識分析中的應用,以及如何利用 Python 從 Windows 登入檔中提取軟體安裝資訊,提供讀者全面的數位鑑識分析技術。
使用Volatility進行記憶體鑑識分析
Volatility是一款開源的記憶體鑑識分析框架,用於從記憶體映像中提取數位證據。在本章中,我們將探討如何使用Volatility及其外掛來分析記憶體映像,並提取有價值的資訊。
Volatility的基本使用
首先,我們需要確定記憶體映像的作業系統版本。為此,我們可以使用windows.info外掛來取得相關資訊。
$ python3 vol.py -f stuxnet.vmem windows.info
內容解密:
python3 vol.py:執行Volatility 3的主程式。-f stuxnet.vmem:指定要分析的記憶體映像檔案。windows.info:外掛名稱,用於取得Windows系統的相關資訊。
執行後,我們可以獲得諸如核心基址、DTB(Directory Table Base)、符號檔案路徑等資訊。
提取行程資訊
Volatility提供了多個外掛來提取行程資訊。例如,windows.pslist.PsList外掛可用於列出正在執行的行程。
$ python3 vol.py -f stuxnet.vmem windows.pslist.PsList
內容解密:
windows.pslist.PsList:外掛名稱,用於列出行程。- 該命令會顯示行程的PID、PPID(父行程ID)、行程名稱等資訊。
另一個有用的外掛是windows.pstree.PsTree,它以樹狀結構顯示行程及其父子關係。
$ python3 vol.py -f stuxnet.vmem windows.pstree.PsTree
內容解密:
windows.pstree.PsTree:外掛名稱,用於以樹狀結構顯示行程。- 該命令會顯示行程的PID、PPID、行程名稱等資訊,並以樹狀結構呈現父子關係。
提取數位憑證
我們還可以使用Volatility來提取數位憑證。
$ sudo python3 vol.py -f stuxnet.vmem windows.registry.certificates.Certificates
內容解密:
windows.registry.certificates.Certificates:外掛名稱,用於提取數位憑證。- 由於需要存取系統資源,因此需要使用
sudo提升許可權。
連線與分析SQLite資料函式庫
除了記憶體鑑識分析外,本章還將介紹如何連線與分析SQLite資料函式庫。SQLite是一種輕量級的資料函式庫,廣泛用於各種應用程式中。
SQLite資料函式庫結構
SQLite資料函式庫的結構可以透過SQLite Browser(http://sqlitebrowser.org)等工具進行檢視。下圖展示了Northwind資料函式庫的結構:
此圖示展示了Northwind資料函式庫中各表之間的關係。
使用sqlite3模組連線SQLite資料函式庫
Python的sqlite3模組提供了一個簡單的介面,用於與SQLite資料函式庫互動。
import sqlite3
def read_from_db(cursor):
cursor.execute('SELECT * FROM Customer')
data = cursor.fetchall()
for row in data:
print(row)
try:
connection = sqlite3.connect("database.sqlite")
cursor = connection.cursor()
read_from_db(cursor)
except sqlite3.DatabaseError as exception:
print("DatabaseError:", exception)
finally:
connection.close()
內容解密:
sqlite3.connect():建立與SQLite資料函式庫的連線。cursor.execute():執行SQL查詢。cursor.fetchall():取得查詢結果的所有列。- 該指令碼演示瞭如何連線SQLite資料函式庫、執行查詢並處理結果。
連線與分析 SQLite 資料函式庫
在前面的章節中,我們介紹瞭如何使用 Python 連線 SQLite 資料函式庫並執行基本的操作。在這裡,我們將繼續探討如何列出資料函式庫中的表格、取得表格的結構描述,以及如何使用 Python 工具進行數位鑑識分析。
列出 SQLite 資料函式庫中的表格
要取得 SQLite 資料函式庫中的表格清單,我們需要對 sqlite_master 表格執行 SELECT 查詢,並使用 fetchall() 方法來取得查詢結果。以下是一個範例指令碼(可在 get_tables_database.py 檔案中找到):
#!/usr/bin/env python3
import sqlite3
connection = sqlite3.connect('database.sqlite')
def tables_in_sqlite_database(connection):
cursor = connection.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = [v[0] for v in cursor.fetchall() if v[0] != "sqlite_sequence"]
cursor.close()
return tables
tables = tables_in_sqlite_database(connection)
tables.remove('Order')
cursor = connection.cursor()
for table in tables:
sql = "select * from {}".format(table)
cursor.execute(sql)
records = cursor.fetchall()
print(sql + " " + str(len(records)) + " elements")
connection.close()
內容解密:
- 連線 SQLite 資料函式庫:使用
sqlite3.connect()方法連線到指定的資料函式庫檔案。 - 定義函式
tables_in_sqlite_database(connection):該函式執行 SELECT 查詢於sqlite_master表格,以取得資料函式庫中的所有表格名稱。 - 篩選表格名稱:使用列表推導式過濾掉
sqlite_sequence表格。 - 關閉遊標:使用
cursor.close()方法關閉遊標。 - 遍歷表格:對於每個表格,執行 SELECT * 查詢,並列印查詢結果的記錄數。
執行上述指令碼後,我們可以得到類別似以下的輸出:
select * from Employee 9 elements
select * from Category 8 elements
select * from Customer 91 elements
select * from Shipper 3 elements
select * from Supplier 29 elements
select * from Product 77 elements
select * from OrderDetail 2155 elements
select * from CustomerCustomerDemo 0 elements
select * from CustomerDemographic 0 elements
select * from Region 4 elements
select * from Territory 53 elements
select * from EmployeeTerritory 49 elements
取得 SQLite 表格的結構描述
要取得 SQLite 表格的結構描述,我們可以定義一個函式 sqlite_table_schema(connection, table_name),該函式對 sqlite_master 表格執行 SELECT 查詢,以取得指定表格的建立陳述式。以下是一個範例指令碼(可在 get_schema_table.py 檔案中找到):
#!/usr/bin/env python3
import sqlite3
def sqlite_table_schema(connection, table_name):
cursor = connection.execute("SELECT sql FROM sqlite_master WHERE name=?;", [table_name])
sql = cursor.fetchone()[0]
cursor.close()
return sql
connection = sqlite3.connect('database.sqlite')
table_name = input("Enter the table name:")
print(sqlite_table_schema(connection, table_name))
connection.close()
內容解密:
- 定義函式
sqlite_table_schema(connection, table_name):該函式執行 SELECT 查詢於sqlite_master表格,以取得指定表格的建立陳述式。 - 使用引數化查詢:傳遞
table_name作為引數,以避免 SQL 注入攻擊。 - 取得查詢結果:使用
fetchone()方法取得第一筆查詢結果。 - 列印表格結構描述:列印指定表格的建立陳述式。
執行上述指令碼後,我們可以得到類別似以下的輸出:
CREATE TABLE "Customer"
(
"Id" VARCHAR(8000) PRIMARY KEY,
"CompanyName" VARCHAR(8000) NULL,
"ContactName" VARCHAR(8000) NULL,
"ContactTitle" VARCHAR(8000) NULL,
"Address" VARCHAR(8000) NULL,
"City" VARCHAR(8000) NULL,
"Region" VARCHAR(8000) NULL,
"PostalCode" VARCHAR(8000) NULL,
"Country" VARCHAR(8000) NULL,
"Phone" VARCHAR(8000) NULL,
"Fax" VARCHAR(8000) NULL
)
網路鑑識與 PcapXray
在這一部分,我們將介紹如何使用 PcapXray 工具進行網路鑑識分析。PcapXray 是一款能夠視覺化網路封包捕捉檔案(pcap)的工具,可以幫助我們分析網路流量。
PcapXray 的安裝與使用
要使用 PcapXray,首先需要安裝相關的依賴套件,包括 tkinter、graphviz、pil 和 imagetk。可以使用以下命令進行安裝:
$ sudo apt install python3-tk && sudo apt install graphviz
$ sudo apt install python3-pil python3-pil.imagetk
接下來,可以從 GitHub 倉函式庫下載 PcapXray 的原始碼,並安裝相關的 Python 模組:
$ sudo pip3 install -r requirements.txt
最後,可以使用以下命令執行 PcapXray:
$ python3 PcapXtray/Source/main.py
此時,將會出現 PcapXray 的圖形介面,可以用來載入 pcap 檔案並顯示網路圖表。
此圖示說明
@startuml
skinparam backgroundColor #FEFEFE
skinparam componentStyle rectangle
title Volatility記憶體鑑識與SQLite資料函式庫分析
package "Docker 架構" {
actor "開發者" as dev
package "Docker Engine" {
component [Docker Daemon] as daemon
component [Docker CLI] as cli
component [REST API] as api
}
package "容器運行時" {
component [containerd] as containerd
component [runc] as runc
}
package "儲存" {
database [Images] as images
database [Volumes] as volumes
database [Networks] as networks
}
cloud "Registry" as registry
}
dev --> cli : 命令操作
cli --> api : API 呼叫
api --> daemon : 處理請求
daemon --> containerd : 容器管理
containerd --> runc : 執行容器
daemon --> images : 映像檔管理
daemon --> registry : 拉取/推送
daemon --> volumes : 資料持久化
daemon --> networks : 網路配置
@enduml圖表內容解密:
- 載入 pcap 檔案:使用 PcapXray 載入 pcap 檔案。
- 顯示網路PcapXray 將會顯示網路圖表,呈現網路流量的相關資訊。
- 分析網路流量:可以使用 PcapXray 分析網路流量,識別可疑的活動。
- 識別惡意流量:PcapXray 可以幫助識別惡意的網路流量。
透過使用 PcapXray,我們可以更有效地進行網路鑑識分析,識別潛在的安全威脅。
使用Python進行數位鑑識分析
在數位鑑識分析中,Python提供了許多強大的工具和模組來幫助分析人員從各種來源提取和分析資料。在本章中,我們將探討如何使用Python工具來分析和視覺化網路封包捕捉檔案,以及如何從Windows登入檔中提取資訊。
使用PcapXray視覺化網路封包捕捉
PcapXray是一個根據Python的工具,用於視覺化網路封包捕捉檔案。您可以從其GitHub儲存函式庫中找到一些pcap示例檔案:
https://github.com/Srinivas11789/PcapXray/tree/master/Source/Module/examples
載入pcap檔案並視覺化網路連線
在下面的範例中,我們載入了torExample.pcap檔案,該檔案顯示了識別出的主機以及連線的來源和目標地址。載入檔案後,我們可以縮放圖形並過濾感興趣的流量。
Figure 12.4 – PcapXray connections in the pcap file
此工具使我們能夠以網路圖的形式視覺化網路封包捕捉,包括裝置識別、通訊的重要部分和檔案提取。
從Windows登入檔中提取資訊
Windows作業系統將所有的系統組態資訊儲存在一個稱為Windows登入檔的內部資料函式庫中。登入檔以鍵值格式儲存資訊,並以階層方式組織。
Windows登入檔的頂級條目
Windows登入檔的頂級條目包括:
HKEY_LOCAL_MACHINE\SYSTEM:system32\config\systemHKEY_LOCAL_MACHINE\SAM:system32\config\samHKEY_LOCAL_MACHINE\SECURITY:system32\config\securityHKEY_LOCAL_MACHINE\SOFTWARE:system32\config\softwareHKEY_USERS\UserProfile:winnt\profiles\usernameHKEY_USERS.DEFAULT:system32\config\default
使用python-registry模組
python-registry是一個允許您存取Windows登入檔的模組。在進行數位鑑識分析時,您可以使用此模組與登入檔互動以尋找證據。
首先,您需要從GitHub儲存函式庫下載並安裝python-registry模組:
$ git clone https://github.com/williballenthin/python-registry.git
$ cd python-registry
$ python3 setup.py install
python-registry模組的主要類別和方法
Registry類別提供了open()和root()方法,分別用於開啟特定位置的記錄和開啟登入檔的根目錄。RegistryKey類別提供了以下方法:timestamp(): 傳回記錄的時間戳記name(): 傳回記錄的名稱path(): 傳回登入檔的路徑parent(): 傳回記錄的父項subkeys(): 傳回特定記錄的所有子記錄列表values(): 傳回特定記錄的所有值列表
RegisterValue類別提供了以下方法:name(): 傳回登入檔值的名稱value_type_str(): 傳回值型別的ASCII名稱value_type(): 傳回值型別的十六進位制編號value(): 傳回分配給該登入檔值的資料
使用python-registry提取軟體資訊
在下面的指令碼中,我們將從Windows登入檔中提取軟體資訊。您可以找到以下程式碼在get_registry_information.py檔案中:
#!/usr/bin/python3
import sys
from Registry import Registry
reg = Registry.Registry(sys.argv[1])
print("Analyzing SOFTWARE in Windows registry...")
try:
key = reg.open("Microsoft\\Windows\\CurrentVersion\\Run")
print("Last modified: %s [UTC]" % key.timestamp())
for value in key.values():
print("Name: " + value.name() + ", Value path: " + value.value())
except Registry.RegistryKeyNotFoundException as exception:
print("Exception", exception)
執行指令碼並提取軟體資訊
執行上述指令碼需要傳遞一個引數,即包含軟體登入檔的路徑:
$ python3 get_registry_information.py <registry_software_path>
輸出結果將顯示Run鍵在Windows登入檔中的值,以及與該鍵相關聯的所有值:
$ python3 get_registry_information.py samples/Vista/SOFTWARE
Analyzing SOFTWARE in Windows registry...
Last modified: 2009-07-03 02:42:25.848957 [UTC]
Name: Windows Defender, Value path: %ProgramFiles%\Windows Defender\MSASCui.exe -hide
內容解密:
- 首先,我們使用
Registry.Registry(sys.argv[1])開啟註冊表檔案。 - 然後,我們使用
reg.open()方法開啟Microsoft\Windows\CurrentVersion\Run鍵。 - 接著,我們使用
key.timestamp()方法取得最後修改時間,並使用key.values()方法取得所有值的列表。 - 最後,我們遍歷所有值,並使用
value.name()和value.value()方法取得名稱和值路徑。
此指令碼使我們能夠提取在Windows啟動期間執行的程式資訊。