mirror of
https://github.com/PacktPublishing/Python-Digital-Forensics-Cookbook.git
synced 2026-02-21 11:18:03 +00:00
136 lines
4.5 KiB
Python
136 lines
4.5 KiB
Python
from __future__ import print_function
|
|
import argparse
|
|
import os
|
|
import pytsk3
|
|
import pyewf
|
|
import sys
|
|
from tabulate import tabulate
|
|
|
|
"""
|
|
MIT License
|
|
|
|
Copyright (c) 2017 Chapin Bryce, Preston Miller
|
|
|
|
Please share comments and questions at:
|
|
https://github.com/PythonForensics/PythonForensicsCookbook
|
|
or email pyforcookbook@gmail.com
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
"""
|
|
|
|
__authors__ = ["Chapin Bryce", "Preston Miller"]
|
|
__date__ = 20170815
|
|
__description__ = "Utility to gather metadata from evidence containers"
|
|
|
|
|
|
class EWFImgInfo(pytsk3.Img_Info):
|
|
def __init__(self, ewf_handle):
|
|
self._ewf_handle = ewf_handle
|
|
super(EWFImgInfo, self).__init__(url="",
|
|
type=pytsk3.TSK_IMG_TYPE_EXTERNAL)
|
|
|
|
def close(self):
|
|
self._ewf_handle.close()
|
|
|
|
def read(self, offset, size):
|
|
self._ewf_handle.seek(offset)
|
|
return self._ewf_handle.read(size)
|
|
|
|
def get_size(self):
|
|
return self._ewf_handle.get_media_size()
|
|
|
|
|
|
def main(image, img_type, part_type):
|
|
print("[+] Opening {}".format(image))
|
|
if img_type == "ewf":
|
|
try:
|
|
filenames = pyewf.glob(image)
|
|
except IOError:
|
|
print("[-] Invalid EWF format:\n {}".format(e))
|
|
sys.exit(2)
|
|
|
|
ewf_handle = pyewf.handle()
|
|
ewf_handle.open(filenames)
|
|
e01_metadata(ewf_handle)
|
|
|
|
# Open PYTSK3 handle on EWF Image
|
|
img_info = EWFImgInfo(ewf_handle)
|
|
else:
|
|
img_info = pytsk3.Img_Info(image)
|
|
|
|
try:
|
|
if part_type is not None:
|
|
attr_id = getattr(pytsk3, "TSK_VS_TYPE_" + part_type)
|
|
volume = pytsk3.Volume_Info(img_info, attr_id)
|
|
else:
|
|
volume = pytsk3.Volume_Info(img_info)
|
|
except IOError:
|
|
_, e, _ = sys.exc_info()
|
|
print("[-] Unable to read partition table:\n {}".format(e))
|
|
sys.exit(3)
|
|
part_metadata(volume)
|
|
|
|
|
|
def part_metadata(vol):
|
|
table = [["Index", "Type", "Offset Start (Sectors)",
|
|
"Length (Sectors)"]]
|
|
for part in vol:
|
|
table.append([part.addr, part.desc.decode("utf-8"), part.start,
|
|
part.len])
|
|
print("\n Partition Metadata")
|
|
print("-" * 20)
|
|
print(tabulate(table, headers="firstrow"))
|
|
|
|
|
|
def e01_metadata(e01_image):
|
|
print("\nEWF Acquisition Metadata")
|
|
print("-" * 20)
|
|
headers = e01_image.get_header_values()
|
|
hashes = e01_image.get_hash_values()
|
|
for k in headers:
|
|
print("{}: {}".format(k, headers[k]))
|
|
for h in hashes:
|
|
print("Acquisition {}: {}".format(h, hashes[h]))
|
|
print("Bytes per Sector: {}".format(e01_image.bytes_per_sector))
|
|
print("Number of Sectors: {}".format(
|
|
e01_image.get_number_of_sectors()))
|
|
print("Total Size: {}".format(e01_image.get_media_size()))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(
|
|
description=__description__,
|
|
epilog="Developed by {} on {}".format(
|
|
", ".join(__authors__), __date__)
|
|
)
|
|
parser.add_argument("EVIDENCE_FILE", help="Evidence file path")
|
|
parser.add_argument("TYPE", help="Type of Evidence",
|
|
choices=("raw", "ewf"))
|
|
parser.add_argument("-p", help="Partition Type",
|
|
choices=("DOS", "GPT", "MAC", "SUN"))
|
|
args = parser.parse_args()
|
|
|
|
if os.path.exists(args.EVIDENCE_FILE) and \
|
|
os.path.isfile(args.EVIDENCE_FILE):
|
|
main(args.EVIDENCE_FILE, args.TYPE, args.p)
|
|
else:
|
|
print("[-] Supplied input file {} does not exist or is not a "
|
|
"file".format(args.EVIDENCE_FILE))
|
|
sys.exit(1)
|