在本教程中,我們將學習使用 Python 的取證科學、基本的 Python 取證應用、哈希函數、破解加密、可視化、命名約定、Dshell 和 Scapy、網路取證及其詳細說明。
介紹
收集和保存證據對於計算機設備上的網路取證調查和分析至關重要。它在法庭上對罪犯起著重要的作用。如今,技術使我們只需在瀏覽器上輸入查詢就能獲得信息。但這也招致了網路騙子。網路騙子是那些利用他們的系統和互聯網進行惡意活動的人。他們可以從坐在別的地方得到你所有的信息。
由於其廣泛的應用,Python 還提供了與數字取證合作的工具。通過使用它,我們可以收集數據,提取證據,還可以加密密碼。它將支持我們恢復證據的可靠性。
在進一步深入之前,您必須熟悉 Python 及其高級概念。
計算取證導論
計算取證是用來解決各種取證學科中的問題的研究的一部分。它使用基於計算機的建模、分析、計算機模擬和識別。Python 取證是由切特·霍姆斯特發明的。還有圖案證據,如指紋、鞋印、工具痕迹和任何文件。它利用程序、對象的範圍和物質。還有生理和行為模式,如數字證據、 DNA 和犯罪現場。
我們也可以使用各種演算法來處理信號和圖像處理。通過使用演算法,我們還可以處理、數據挖掘、計算機圖形學、機器學習、計算機視覺數據可視化和統計模式識別。
總之,計算取證是用來研究數字證據的,計算取證處理的是各種類型的證據。
Python 取證應用的命名約定
我們必須熟悉命名約定和模式,以遵循 Python 取證準則。請考慮下表。
| | 命名約定 | 例子 |
| 局部變數 | 帶可選下劃線的 camelCase | 學生姓名 |
| 常數 | 大寫,由下劃線分隔的單詞 | 學生姓名 |
| 全局變數 | 帶可選下劃線的 camelCase 前綴 | my_studentName |
| 功能 | 帶可選下劃線的 PascalCase 主動態 | 我的學生姓名 |
| 組件 | 以駝色為前綴 | _studentname |
| 班級 | 用 Pascalcase 給類加前綴;保持分類 | classMyStudentName |
| 目標 | 前綴 ob 帶 camelcase | ob_studentName |
哈希演算法是將二進位數據流作為輸入的最佳方式之一。在現實生活中,我們可以加密我們的密碼、文件,甚至任何類型的數字文件或數據。該演算法接受輸入並生成加密消息。讓我們看看給定的例子。
例子
import sys,string,md5
print("Enter the name")
line=sys.stdin.readline()
line=line.rstrip()
md5_object=md5.new()
md5_object.update(line)
print(md5_object.hexdigest())
Python 哈希函數
Python 哈希函數用於將大量數據映射到一個固定值。輸入返回相同的輸出。它是一個哈希和,存儲具有精確信息的特徵。一旦我們將數據映射到一個固定值,就無法恢復。這就是為什麼我們也稱它為單向密碼演算法。
讓我們理解下面的例子-
示例-
import hashlib
import uuid
def hash_pass(password):
s = uuid.uuid4().hex
return hashlib.sha256(s.encode() + password.encode()).hexdigest() + ':' + s
def verify_password(hashed_password, user_password):
password, s = hashed_password.split(':')
return password == hashlib.sha256(s.encode() + user_password.encode()).hexdigest()
new_password = input('Enter your password :')
hashed_password = hash_pass(new_password)
print('The hash string to store in the db is: ' + hashed_password)
輸出:
Enter your password: sharma
The hash string to store in the db is: 947782bdb0c7a5ad642f1f26179b6aef2d9857427b45a09af4fce3b8f1346e91:8a8371941513482487e5ab8af2ae6466
現在,我們將重新輸入密碼。
old_password = input('Enter new password ')
if verify_password(hashed_password, old_password):
print(' Entered password is correct')
else:
print('Passwords do not match')
輸出:
Enter your password devansh
The hash string to store in the db is: 4762866edd3b49c7736163ef3d981e42629a09a9ca7e081f56d116e137d77b9c:ebbf5b16bd9f4b989505a495bf7ae9b9
Enter new password sharma
Passwords do not match
哈希函數具有以下屬性。
- 我們可以簡單地將任何哈希值轉換為任何輸入值。
- 它不能產生與給定哈希值相同的輸出。
- 不移動哈希值就轉換輸入是不現實的。
破解 Python 中的一個加密
我們必須知道如何加密我們在分析和取證過程中獲取的文本數據。首先,了解基本的密碼。
一般來說,秘密信息是由軍方人員發送的,以傳達他們的計劃,而不會被他們的敵人看到。這些消息不是人類可讀的格式。純文本使用加密演算法加密,這些文本稱為密文。
假設一個總指揮官給高級指揮官發了一條信息,從他們的敵人那裡保存文本。在這裡,我們把字母表中的純文本字母移動四位。現在,A 是 E,每個 B 是 F,所以沒有
讓我們理解下面這個破解向量數據的例子。
示例-
import sys
def decryption(text,cipher):
simple_text=''
for each in cipher:
x = (ord(each)-text) % 126
if x < 32:
x+=95
simple_text += chr(x)
print(simple_text)
cipher_text = input('Enter the message: ')
for i in range(1,95,1):
decryption(i,cipher_text)
輸出:
Enter message: Yes
~
}
|
{
z
y
x
w
v
u
t
s
r
r~
q
q}
p
p|
o
o{
n
nz
m
my
l
lx
k
kw
j
jv
i
iu
h
ht
g
gs
f
fr
e
eq
d
dp
dp~
c
co
co}
b
bn
bn|
a
am
am{
`
`l
`lz
_
_k
_ky
j
jx
i
iw
h
hv
g
gu
f
ft
虛擬化
虛擬化是模擬諸如工作站、網路和存儲等信息技術系統行為。我們製作這樣一個資源的虛擬實例。這可以在虛擬機管理程序的幫助下完成。
硬體虛擬化在計算機取證中起著非常重要的作用。通過使用虛擬化,我們可以獲得以下優勢。
- 我們可以在驗證狀態下使用工作站進行每次調查。
- 我們可以通過在虛擬機上包含驅動器的 dd 映像來恢復刪除的數據。
- 虛擬機可以變成有助於收集證據的恢復設備。
我們定義了以下使用 Python 創建虛擬機的步驟
步驟- 1: 假設我們將本地機器視為「假人」。每個虛擬機至少有 512 兆內存。
virmach_memory = 512 * 1024 * 1024
步驟- 2: 現在,我們將該虛擬機連接到默認集群。
virmach_cluster = api.clusters.get(name = "Default")
步驟- 3: 接下來,從虛擬硬碟引導虛擬機。
vm_os = params.OperatingSystem(boot = [params.Boot(dev = "hd")])
現在,我們將把上述步驟組合成一個虛擬機參數對象。讓我們理解下面的例子。
示例-
from ovirtsdk.xml import params
from ovirtsdk.api import API
try:
# We need to provide Api credentials for virtual machine
api = API(url="https://HOST",
username="Example",
password="example123",
ca_file="ca.crt")
virmach_name = "dummy"
virmach_memory = 512 * 1024 * 1024 # calculating the memory in bytes
virmach_cluster = api.clusters.get(name="Default")
virmach_template = api.templates.get(name="Blank")
# here we are assigning the parameters to operating system
virmach_os = params.OperatingSystem(boot=[params.Boot(dev="hd")])
virmach_params = params.VM(name=virmach_name,
memory=virmach_memory,
cluster=virmach_cluster,
template=virmach_template,
os = virmach_os)
try:
api.vms.add(vm=virmach_params)
print("Virtual machine '%s' added successfully." % virmach_name)
except Exception as ex:
print("Adding virtual machine '%s' failed: %s" % (virmach_name, ex))
api.disconnect()
except Exception as ex:
輸出:
Virtual Machine dummy added successfully.
Python 中的網路取證
Python 還提供了與網路取證合作的工具。在現代,Python 網路取證環境投資會遇到很多困難。這些問題可能是對違規報告的響應、執行與敏感性相關的評估或驗證合規性。讓我們了解一下網路取證的基本術語。
客戶端- 客戶端運行個人電腦和工作站。
伺服器- 伺服器執行客戶端的請求。
協議- 協議是數據傳輸時必須遵循的一組規則。
網路套接字- 網路套接字是一種提供全雙工通信並通過 TCP 連接運行的協議。我們可以使用 websockets 發送雙向消息。
在這些協議的幫助下,我們可以驗證第三方用戶發送或接收的信息。但是,加密對於保護通道是必要的。
讓我們理解下面的網路示例
示例-
import socket
# creating a socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# getting local machine name
host = socket.gethostname()
port = 8080
# connection to hostname on the port.
sock.connect((host, port))
# Receive no more than 1024 bytes
temp = sock.recv(1024)
print("The client waits for connection")
sock.close()
輸出:
The client waits for connection
Python Scapy 和 Dshell
讓我們了解一下 Python Scapy 和 Dshell 的簡介。
python scapy(python scapy)
一個基於 Python 的工具,用於分析和處理網路流量。藉助 scapy,我們可以分析包操作。我們還可以捕獲和解碼大量協議的數據包。使用 scapy 的好處是向調查人員提供有關網路流量的詳細報告。OS 指紋 app 等第三方工具也可以在 Scapy 中使用。讓我們理解下面的例子。
示例-
#Imports scapy and GeoIP toolkit
import scapy, GeoIP
from scapy import *
geoIp = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
def locatePackage(pkg):
# extracts the source IP address
source = pkg.getlayer(IP).src
# extracts the destination IP address
destination = pkg.getlayer(IP).dst
# gets Country details of source
srcCountry = geoIp.country_code_by_addr(source)
dstCountry = geoIp.country_code_by_addr(destination)
# gets country details of destination
print src+"("+sourceCountry+") >> "+destination+"("+destinationcountry+")\n"
輸出:
source INDIA >> destination USA
python shell
Dshell 是一個基於 Python 的網路取證分析工具包。它由美國陸軍研究實驗室開發,並於 2014 年開源發布。這使得取證調查變得非常容易。Dshell 提供以下解碼器。
- reservedips – 用於確定域名系統問題的解決方案。
- rip-http – 它從 http 流量中提取文件。
- 大流量- 是表示列表凈流量的解碼器。
- 協議- 標識非標準協議。
- dns – 它提取 dns 相關的查詢。
Python 搜索
搜查是取證調查中最重要的部分。如今,好的搜索是在負責證據的調查員身上。從消息中搜索關鍵詞是調查的支柱。我們可以藉助一個關鍵詞找到有力的證據。
從刪除的消息中獲取信息需要經驗和知識。
Python 提供了各種內置模塊來支持搜索操作。研究者可以使用諸如「誰」、「什麼」、「哪裡」、「何時」、「哪個」等關鍵詞來找到結果。讓我們理解下面的例子。
示例-
# Searching a particular word from a message
str1 = "This is an example for Computational forensics of gathering evidence!"
str2 = "string"
print(str1.find(str2))
print(str1.find(str2, 10))
print(str1.find(str2, 40))
輸出:
11
11
-1
Python 索引
索引是調查員可以用來從文件中收集潛在證據的功能。證據可以限制在內存快照、磁碟映像、文件或網路跟蹤中。對於像關鍵詞搜索這樣耗時的任務來說,減少時間是非常有幫助的。索引還用於在互動式搜索階段定位關鍵詞。在下面的例子中,我們已經用 Python 解釋了索引。
示例-
list1 = [123, 'example', 'creative', 'indexing']
print("Index example : ", list1.index('example'))
print("Index for indexing : ", list1.index('indexing'))
str1 = "This is a message for forensic investigation indexing"
str2 = "message"
print("Index of the character keyword found is ")
print(str1.index(str2))
輸出:
Index example : 1
Index for indexing : 3
Index of the character keyword found is
10
Python 圖像庫
取證調查的真正意義是從現有資源中提取有價值的信息。從資源中獲取所有相關信息對於報告至關重要。它幫助我們得出適當的結果。
資源數據可以是簡單的數據結構,如資料庫,也可以是複雜的數據結構,如 JPEG 圖像。
調查人員可以很容易地從簡單的數據結構中獲取信息,但從複雜的數據結構中提取信息是一項繁瑣的任務。
Python 提供了名為 PIL 的圖像庫。它用於為 Python 解釋器添加圖像處理功能。它還支持文件格式、圖形功能,並提供強大的圖像處理。讓我們理解下面的圖像,從圖像中提取數據。
我們定義編程示例來解釋它實際上是如何工作的。
步驟- 1: 假設我們有一個下面的圖像,我們需要提取細節。
步驟 2: 圖像由各種像素值組成。PIL 圖書館使用提取圖像細節來收集證據。讓我們理解下面的例子。
示例-
from PIL import Image
im = Image.open('penguin.jpeg', 'r')
pix_val = list(im.getdata())
pix_val_flat = [x for sets in pix_val for x in sets]
print(pix_val_flat)
輸出:
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
輸出以列表的形式返回。RGB 組合的像素值可以更好地描述需要哪些數據。
Python 多進程支持
取證專家發現將數字解決方案應用於常見犯罪的大型數字證據存在困難。大多數數字證據都是單線程的,這意味著我們一次只能執行一個命令。讓我們看看多進程的簡要介紹。
多進程
多進程是系統支持多個進程的一種能力。它使幾個程序能夠同時運行。多進程有兩種類型- 對稱處理和非對稱處理。
讓我們理解以下多進程的例子。
示例-
import random
import multiprocessing
def list_append(count, id, out_list):
# count number of process at a time
for i in range(count):
out_list.append(random.random())
if __name__ == "__main__":
size = 810
procs = 2
jobs = []
for i in range(0, procs):
out_list = list() # list of processes
process1 = multiprocessing.Process(
target=list_append, args=(size, i, out_list))
# appends the list of processes
jobs.append(process1)
# Calculate the random number of processes
for j in jobs:
j.start() # initiate the process
# After the processes have finished execution
for j in jobs:
j.join()
print("List processing complete.")
輸出:
List processing complete
Python 中的移動取證
取證投資不僅僅局限於硬碟、CPU 等標準計算機硬體。藉助分析非標準硬體或瞬時證據的技術來跟蹤硬體。
如今,智能手機被廣泛用於數字調查,但它們仍然意味著不標準。通過對智能手機的適當研究,我們可以提取照片、智能手機和信息。
安卓智能手機使用個人識別碼或字母數字密碼。密碼可以是 4 到 16 位數字/字元。
在下面的例子中,我們將通過一個鎖屏來提取數據。智能手機密碼一般存儲在/數據/系統中的文件 password.key 中。
安卓存儲了這個密碼的加鹽 SHA1 哈希和 MD5 哈希。讓我們看看下面的例子。
示例-
public byte[] passwordToHash(String password) {
if (password == null) {
return null;
}
String algo = null;
byte[] hashed = null;
try {
byte[] saltedPassword = (password + getSalt()).getBytes();
byte[] sha1 = MessageDigest.getInstance(algo = "SHA-1").digest(saltedPassword);
byte[] md5 = MessageDigest.getInstance(algo = "MD5").digest(saltedPassword);
hashed = (toHex(sha1) + toHex(md5)).getBytes();
} catch (NoSuchAlgorithmException e) {
Log.w(TAG, "Failed to encode string because of missing algorithm: " + algo);
}
return hashed;
}
以上代碼是破解智能手機密碼的示例代碼。由於哈希密碼存儲在鹽文件中,因此破解密碼不會影響字典攻擊。salt 文件是 64 位隨機整數的十六進位表示字元串。根智能手機或 JTAG 適配器可以訪問鹽文件。
紮根智能手機
文件的轉儲/數據/系統/密碼. key 存儲在鎖屏幕下的 SQLite 資料庫中。密碼存儲在設置資料庫下。
JTAG 適配器
JTAG 代表聯合試驗行動小組,可用於獲取鹽。類似地,一個重複框或一個跳汰機適配器可以用來訪問銷售文件。我們可以利用從 Riff-box 獲得的信息找到加密數據的位置。規則如下。
- 找到關聯字元串「 password_salt 」。
- salt 文件的寬度以位元組表示。這是它的長度。
- 這是實際搜索以獲得存儲的智能手機密碼/pin 碼的長度。
內存和取證
Python 取證主要關注易失性內存,並藉助於基於 Python 的框架 Volatility。
易失存儲器
易失性存儲器是一種當系統電源關閉或中斷時被擦除的存儲器。簡而言之,如果我們正在處理一個沒有保存到硬碟上的文檔,突然斷電,我們將丟失數據。
易失性存儲器遵循與其他取證調查相同的模式。
- 首先,需要選擇投資的對象。
- 獲取取證數據。
- 法醫分析
內存轉儲是用於分析從內存收集的數據的工具。
YARA 規則
YARA 是一個用來檢查可疑文件/目錄和匹配字元串的工具。它基於模式匹配實現。它在取證分析中起著重要的作用。
示例-
import operator
import os
import sys
sys.path.insert(0, os.getcwd())
import plyara.interp as interp
# Plyara is a script that lexes and parses a file consisting of one more Yara
if __name__ == '__main__':
file_to_analyze = sys.argv[1]
Dictrules = interp.parseString(open(file_to_analyze).read())
authors = {}
imps = {}
meta_keys = {}
max_strings = []
max_string_len = 0
tags = {}
rule_count = 0
for rule in Dictrules:
rule_count += 1
# Imports
if 'imports' in rule:
for imp in rule['imports']:
imp = imp.replace('"', '')
if imp in imps:
imps[imp] += 1
else:
imps[imp] = 1
# Tags
if 'tags' in rule:
for tag in rule['tags']:
if tag in tags:
tags[tag] += 1
else:
tags[tag] = 1
# Metadata
if 'metadata' in rule:
for key in rule['metadata']:
if key in meta_keys:
meta_keys[key] += 1
else:
meta_keys[key] = 1
if key in ['Author', 'author']:
if rule['metadata'][key] in authors:
authors[rule['metadata'][key]] += 1
else:
authors[rule['metadata'][key]] = 1
# Strings
if 'strings' in rule:
for strr in rule['strings']:
if len(strr['value']) > max_string_len:
max_string_len = len(strr['value'])
max_strings = [(rule['rule_name'], strr['name'], strr['value'])]
elif len(strr['value']) == max_string_len:
max_strings.append((rule['rule_name'], strr['key'], strr['value']))
print("\nThe number of rules implemented" + str(rule_count))
ordered_meta_keys = sorted(meta_keys.items(), key=operator.itemgetter(1),
reverse = True)
ordered_authors = sorted(authors.items(), key=operator.itemgetter(1),
reverse = True)
ordered_imps = sorted(imps.items(), key=operator.itemgetter(1), reverse=True)
ordered_tags = sorted(tags.items(), key=operator.itemgetter(1), reverse=True)
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/130698.html