Python数字取证 Windows中的重要工件-III

Python数字取证 Windows中的重要工件-III

本章将解释调查员在对Windows进行取证分析时可以获得的进一步工件。

事件日志

Windows事件日志文件,正如其名称所示,是存储重大事件的特殊文件,如用户登录计算机时,程序遇到错误时,关于系统变化,RDP访问,应用程序特定事件等。网络调查员总是对事件日志信息感兴趣,因为它提供了许多关于系统访问的有用历史信息。在下面的Python脚本中,我们将处理传统的和当前的Windows事件日志格式。

对于Python脚本,我们需要安装第三方模块,即 pytsk3、pyewf、unicodecsv、pyevt和pyevt x。我们可以按照下面的步骤从事件日志中提取信息。

  • 首先,搜索所有符合输入参数的事件日志。

  • 然后,执行文件签名验证。

  • 现在,用适当的库处理找到的每个事件日志。

  • 最后,将输出写入电子表格。

Python代码

让我们看看如何使用Python代码来达到这个目的–

首先,导入以下Python库 –

from __future__ import print_function
import argparse
import unicodecsv as csv
import os
import pytsk3
import pyewf
import pyevt
import pyevtx
import sys
from utility.pytskutil import TSKUtil

现在,为命令行处理程序提供参数。注意,这里它将接受三个参数–第一个是证据文件的路径,第二个是证据文件的类型,第三个是要处理的事件日志的名称。

if __name__ == "__main__":
   parser = argparse.ArgumentParser('Information from Event Logs')
   parser.add_argument("EVIDENCE_FILE", help = "Evidence file path")
   parser.add_argument("TYPE", help = "Type of Evidence",choices = ("raw", "ewf"))
   parser.add_argument(
      "LOG_NAME",help = "Event Log Name (SecEvent.Evt, SysEvent.Evt, ""etc.)")

   parser.add_argument(
      "-d", help = "Event log directory to scan",default = "/WINDOWS/SYSTEM32/WINEVT")

   parser.add_argument(
      "-f", help = "Enable fuzzy search for either evt or"" evtx extension", action = "store_true")
   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.LOG_NAME, args.d, args.f)
   else:
      print("[-] Supplied input file {} does not exist or is not a ""file".format(args.EVIDENCE_FILE))
   sys.exit(1)

现在,通过创建我们的 TSKUtil 对象,与事件日志互动,查询用户提供的路径是否存在。它可以在 main() 方法的帮助下完成,如下所示

def main(evidence, image_type, log, win_event, fuzzy):
   tsk_util = TSKUtil(evidence, image_type)
   event_dir = tsk_util.query_directory(win_event)

   if event_dir is not None:
      if fuzzy is True:
         event_log = tsk_util.recurse_files(log, path=win_event)
   else:
      event_log = tsk_util.recurse_files(log, path=win_event, logic="equal")

   if event_log is not None:
      event_data = []
      for hit in event_log:
         event_file = hit[2]
         temp_evt = write_file(event_file)

现在,我们需要进行签名验证,然后定义一个方法,将整个内容写到当前目录中:

def write_file(event_file):
   with open(event_file.info.name.name, "w") as outfile:
      outfile.write(event_file.read_random(0, event_file.info.meta.size))
   return event_file.info.name.name
      if pyevt.check_file_signature(temp_evt):
         evt_log = pyevt.open(temp_evt)
         print("[+] Identified {} records in {}".format(
            evt_log.number_of_records, temp_evt))

         for i, record in enumerate(evt_log.records):
            strings = ""
            for s in record.strings:
               if s is not None:
                  strings += s + "\n"
            event_data.append([
               i, hit[0], record.computer_name,
               record.user_security_identifier,
               record.creation_time, record.written_time,
               record.event_category, record.source_name,
               record.event_identifier, record.event_type,
               strings, "",
               os.path.join(win_event, hit[1].lstrip("//"))
            ])
      elif pyevtx.check_file_signature(temp_evt):
         evtx_log = pyevtx.open(temp_evt)
         print("[+] Identified {} records in {}".format(
            evtx_log.number_of_records, temp_evt))
         for i, record in enumerate(evtx_log.records):
            strings = ""
            for s in record.strings:
               if s is not None:
               strings += s + "\n"
         event_data.append([
            i, hit[0], record.computer_name,
            record.user_security_identifier, "",
            record.written_time, record.event_level,
            record.source_name, record.event_identifier,
            "", strings, record.xml_string,
            os.path.join(win_event, hit[1].lstrip("//"))
      ])
      else:
         print("[-] {} not a valid event log. Removing temp" file...".format(temp_evt))
         os.remove(temp_evt)
      continue
      write_output(event_data)
   else:
      print("[-] {} Event log not found in {} directory".format(log, win_event))
      sys.exit(3)
else:
   print("[-] Win XP Event Log Directory {} not found".format(win_event))
   sys.exit(2

最后,定义一个将输出写入电子表格的方法,如下:

def write_output(data):
   output_name = "parsed_event_logs.csv"
   print("[+] Writing {} to current working directory: {}".format(
      output_name, os.getcwd()))

   with open(output_name, "wb") as outfile:
      writer = csv.writer(outfile)
      writer.writerow([
         "Index", "File name", "Computer Name", "SID",
         "Event Create Date", "Event Written Date",
         "Event Category/Level", "Event Source", "Event ID",
         "Event Type", "Data", "XML Data", "File Path"
      ])
      writer.writerows(data)

一旦你成功运行上述脚本,我们将在电子表格中获得事件记录的信息。

互联网历史

互联网历史记录对法医分析员非常有用;因为大多数网络犯罪只发生在互联网上。让我们看看如何从IE浏览器中提取互联网历史记录,因为我们讨论的是Windows取证,而IE浏览器在Windows中是默认的。

在IE浏览器中,互联网历史记录被保存在 index.dat 文件中。让我们看看一个Python脚本,它将从 index.dat 文件中提取信息。

我们可以按照下面的步骤来提取 index.d at文件中的信息。

  • 首先,在系统中搜索 index.dat 文件。

  • 然后,通过迭代从该文件中提取信息。

  • 现在,将所有这些信息写到CSV报告中。

Python代码

让我们看看如何使用Python代码来达到这个目的–

首先,导入以下Python库 –

from __future__ import print_function
import argparse

from datetime import datetime, timedelta
import os
import pytsk3
import pyewf
import pymsiecf
import sys
import unicodecsv as csv

from utility.pytskutil import TSKUtil

现在,为命令行处理程序提供参数。注意,这里它将接受两个参数–第一个是证据文件的路径,第二个是证据文件的类型。

if __name__ == "__main__":
parser = argparse.ArgumentParser('getting information from internet history')
   parser.add_argument("EVIDENCE_FILE", help = "Evidence file path")
   parser.add_argument("TYPE", help = "Type of Evidence",choices = ("raw", "ewf"))
   parser.add_argument("-d", help = "Index.dat directory to scan",default = "/USERS")
   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.d)
   else:
      print("[-] Supplied input file {} does not exist or is not a ""file".format(args.EVIDENCE_FILE))
      sys.exit(1)

现在,通过创建一个 TSKUtil 的对象来解释证据文件,并在文件系统中迭代找到index.dat文件。它可以通过定义 main() 函数来完成,如下所示

def main(evidence, image_type, path):
   tsk_util = TSKUtil(evidence, image_type)
   index_dir = tsk_util.query_directory(path)

   if index_dir is not None:
      index_files = tsk_util.recurse_files("index.dat", path = path,logic = "equal")

      if index_files is not None:
         print("[+] Identified {} potential index.dat files".format(len(index_files)))
         index_data = []

         for hit in index_files:
            index_file = hit[2]
            temp_index = write_file(index_file)

现在,定义一个函数,在该函数的帮助下,我们可以将index.dat文件的信息复制到当前工作目录中,以后它们可以由第三方模块处理。

def write_file(index_file):
   with open(index_file.info.name.name, "w") as outfile:
   outfile.write(index_file.read_random(0, index_file.info.meta.size))
return index_file.info.name.name

现在,使用以下代码,在内置函数即 check_file_signature() 的帮助下执行签名验证。

if pymsiecf.check_file_signature(temp_index):
   index_dat = pymsiecf.open(temp_index)
   print("[+] Identified {} records in {}".format(
   index_dat.number_of_items, temp_index))

   for i, record in enumerate(index_dat.items):
   try:
      data = record.data
   if data is not None:
      data = data.rstrip("\x00")
   except AttributeError:

   if isinstance(record, pymsiecf.redirected):
      index_data.append([
         i, temp_index, "", "", "", "", "",record.location, "", "", record.offset,os.path.join(path, hit[1].lstrip("//"))])

   elif isinstance(record, pymsiecf.leak):
      index_data.append([
         i, temp_index, record.filename, "","", "", "", "", "", "", record.offset,os.path.join(path, hit[1].lstrip("//"))])
   continue

   index_data.append([
      i, temp_index, record.filename,
      record.type, record.primary_time,
      record.secondary_time,
      record.last_checked_time, record.location,
      record.number_of_hits, data, record.offset,
      os.path.join(path, hit[1].lstrip("//"))
   ])
   else:
      print("[-] {} not a valid index.dat file. Removing "
      "temp file..".format(temp_index))
      os.remove("index.dat")
      continue
      os.remove("index.dat")
      write_output(index_data)
   else:
      print("[-] Index.dat files not found in {} directory".format(path))
   sys.exit(3)
   else:
      print("[-] Directory {} not found".format(win_event))
   sys.exit(2)

现在,定义一个方法来打印CSV文件的输出,如下图所示

def write_output(data):
   output_name = "Internet_Indexdat_Summary_Report.csv"
   print("[+] Writing {} with {} parsed index.dat files to current "
   "working directory: {}".format(output_name, len(data),os.getcwd()))

   with open(output_name, "wb") as outfile:
      writer = csv.writer(outfile)
      writer.writerow(["Index", "File Name", "Record Name",
      "Record Type", "Primary Date", "Secondary Date",
      "Last Checked Date", "Location", "No. of Hits",
      "Record Data", "Record Offset", "File Path"])
      writer.writerows(data)

运行上述脚本后,我们将在CSV文件中获得index.dat文件的信息。

卷影复制

影子副本是Windows中包含的技术,用于手动或自动获取计算机文件的备份副本或快照。它也被称为卷快照服务或卷影服务(VSS)。

在这些VSS文件的帮助下,法医专家可以获得一些历史信息,了解系统如何随时间变化以及计算机上存在哪些文件。影子复制技术要求文件系统必须是NTFS,以便创建和存储影子副本。

在本节中,我们将看到一个Python脚本,它有助于访问取证图像中存在的任何卷的影子副本。

对于Python脚本,我们需要安装第三方模块,即 pytsk3、pyewf、unicodecsv、pyvshadowvss。 我们可以按照下面的步骤来提取VSS文件的信息

  • 首先,访问原始镜像的卷,确定所有的NTFS分区。

  • 然后,通过遍历这些影子副本,从其中提取信息。

  • 现在,最后我们需要在快照中创建一个数据的文件列表。

Python代码

让我们看看如何使用Python代码来达到这个目的–

首先,导入以下Python库 –

from __future__ import print_function
import argparse
from datetime import datetime, timedelta

import os
import pytsk3
import pyewf
import pyvshadow
import sys
import unicodecsv as csv

from utility import vss
from utility.pytskutil import TSKUtil
from utility import pytskutil

现在,为命令行处理程序提供参数。这里它将接受两个参数–第一个是证据文件的路径,第二个是输出文件。

if __name__ == "__main__":
   parser = argparse.ArgumentParser('Parsing Shadow Copies')
   parser.add_argument("EVIDENCE_FILE", help = "Evidence file path")
   parser.add_argument("OUTPUT_CSV", help = "Output CSV with VSS file listing")
   args = parser.parse_args()

现在,验证输入文件路径的存在,同时将目录与输出文件分开。

directory = os.path.dirname(args.OUTPUT_CSV)
if not os.path.exists(directory) and directory != "":
   os.makedirs(directory)
if os.path.exists(args.EVIDENCE_FILE) and \ os.path.isfile(args.EVIDENCE_FILE):
   main(args.EVIDENCE_FILE, args.OUTPUT_CSV)
else:
   print("[-] Supplied input file {} does not exist or is not a "
   "file".format(args.EVIDENCE_FILE))

   sys.exit(1)

现在,通过创建 TSKUtil 对象与证据文件的卷进行交互。它可以在 main() 方法的帮助下完成,如下所示

def main(evidence, output):
   tsk_util = TSKUtil(evidence, "raw")
   img_vol = tsk_util.return_vol()

if img_vol is not None:
   for part in img_vol:
      if tsk_util.detect_ntfs(img_vol, part):
         print("Exploring NTFS Partition for VSS")
         explore_vss(evidence, part.start * img_vol.info.block_size,output)
      else:
         print("[-] Must be a physical preservation to be compatible ""with this script")
         sys.exit(2)

现在,定义一个探索解析过的卷影文件的方法,如下所示

def explore_vss(evidence, part_offset, output):
   vss_volume = pyvshadow.volume()
   vss_handle = vss.VShadowVolume(evidence, part_offset)
   vss_count = vss.GetVssStoreCount(evidence, part_offset)

   if vss_count > 0:
      vss_volume.open_file_object(vss_handle)
      vss_data = []

      for x in range(vss_count):
         print("Gathering data for VSC {} of {}".format(x, vss_count))
         vss_store = vss_volume.get_store(x)
         image = vss.VShadowImgInfo(vss_store)
         vss_data.append(pytskutil.openVSSFS(image, x))
write_csv(vss_data, output)

最后,定义在电子表格中写入结果的方法,如下所示

def write_csv(data, output):
   if data == []:
      print("[-] No output results to write")
      sys.exit(3)
   print("[+] Writing output to {}".format(output))
   if os.path.exists(output):
      append = True
with open(output, "ab") as csvfile:
      csv_writer = csv.writer(csvfile)
      headers = ["VSS", "File", "File Ext", "File Type", "Create Date",
         "Modify Date", "Change Date", "Size", "File Path"]
      if not append:
         csv_writer.writerow(headers)
      for result_list in data:
         csv_writer.writerows(result_list)

一旦你成功运行这个Python脚本,我们将把驻留在VSS中的信息获取到电子表格中。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程