Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IM TRYING TO FIGURE OUT HOW TO LOG ALL THE FULLPATHS, FILESIZE, MODTIME, ACCTIME, CRETIME, & HASHES OF *** EVERY FILE IN THE ENTERED PATH

I"M TRYING TO FIGURE OUT HOW TO LOG ALL THE FULLPATHS, FILESIZE, MODTIME, ACCTIME, CRETIME, & HASHES OF ***EVERY FILE IN THE ENTERED PATH*** INTO A LOG FILE CALLED, "Log.txt." I WANT MY LOG TO CONTAIN SECTIONSOF EACH FILE'S PATH, FILESIZE, MODIFIED TIME, ACCESSED TIME, CREATION TIME & HASHESJUST LIKE THE FOLLOWING EXAMPLE (**HELP!!**)!!! PLEASE STICK TO THE SCRIPT I CREATED...

6308-INFO-2022-06-25 14:40:25,718 Script Start

6308-INFO-2022-06-25 14:41:01,599 Investigator: Hosmer 6308-INFO-2022-06-25 14:41:02,419 Organization: UA 6308-INFO-2022-06-25 14:41:03,292 Purpose : Demo 6308-INFO-2022-06-25 14:41:09,779 ========================================

6308-INFO-2022-06-25 14:41:48,205 ***** System Information ***** 6308-INFO-2022-06-25 14:42:04,246 platform: Windows 6308-INFO-2022-06-25 14:42:21,077 platform-release: 10 6308-INFO-2022-06-25 14:42:21,600 platform-version: 10.0.19043 6308-INFO-2022-06-25 14:42:22,077 architecture: AMD64 6308-INFO-2022-06-25 14:42:22,641 hostname: PYTHON-Travel 6308-INFO-2022-06-25 14:42:23,153 ip-address: 192.168.1.3 6308-INFO-2022-06-25 14:42:23,858 mac-address: 28:16:ad:6b:7f:6e 6308-INFO-2022-06-25 14:42:24,409 processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel 6308-INFO-2022-06-25 14:42:25,055 ram: 16 GB 6308-INFO-2022-06-25 14:42:26,025 ========================================

6308-INFO-2022-06-25 14:43:07,988 User Specifed Directory: c:/tst

6308-INFO-2022-06-25 14:44:42,547 File Processed: 6308-INFO-2022-06-25 14:44:43,637 Path: c:\tst\Castle.JPG 6308-INFO-2022-06-25 14:44:44,226 File Size: 1,224,201 6308-INFO-2022-06-25 14:44:44,781 Last-Modified: 2013-09-20 01:08:27 6308-INFO-2022-06-25 14:44:45,316 Last-Accessed: 2022-06-25 18:26:05 6308-INFO-2022-06-25 14:44:45,869 Created: 2020-05-13 02:40:09 6308-INFO-2022-06-25 14:44:46,645 SHA-256: b6cd6031f40c9164de8b77686318ca702a9dc6c1853beb2fbbe2efc534097278 6308-INFO-2022-06-25 14:44:47,926 ======================================================================

6308-INFO-2022-06-25 14:45:24,833 File Processed: 6308-INFO-2022-06-25 14:45:24,834 Path: c:\tst\Cat.jpg 6308-INFO-2022-06-25 14:45:24,834 File Size: 446,759 6308-INFO-2022-06-25 14:45:24,834 Last-Modified: 2013-09-20 01:22:52 6308-INFO-2022-06-25 14:45:24,834 Last-Accessed: 2022-06-25 18:26:05 6308-INFO-2022-06-25 14:45:24,835 Created: 2020-05-13 02:40:09 6308-INFO-2022-06-25 14:45:24,835 SHA-256: 52d5722249b00842f2e96cc68808048f0341a6f9b5e65eb11585ef7b6967de1c 6308-INFO-2022-06-25 14:45:24,835 ======================================================================

6308-INFO-2022-06-25 14:45:24,848 File Processed: 6308-INFO-2022-06-25 14:45:24,850 Path: c:\tst\CoastLine.JPG 6308-INFO-2022-06-25 14:45:24,853 File Size: 1,997,636 6308-INFO-2022-06-25 14:45:24,854 Last-Modified: 2019-02-14 15:02:05 6308-INFO-2022-06-25 14:45:24,854 Last-Accessed: 2022-06-25 18:26:05 6308-INFO-2022-06-25 14:45:24,855 Created: 2020-05-13 02:40:09 6308-INFO-2022-06-25 14:45:24,855 SHA-256: 818d1ce0bcb3373aacd34f03165693e9964717851f795aef4b40f08bfdcfe7d3 6308-INFO-2022-06-25 14:45:24,855 ======================================================================

6308-INFO-2022-06-25 14:45:24,858 File Processed: 6308-INFO-2022-06-25 14:45:24,858 Path: c:\tst\Deutchland.JPG 6308-INFO-2022-06-25 14:45:24,859 File Size: 600,630 6308-INFO-2022-06-25 14:45:24,859 Last-Modified: 2013-09-20 02:04:44 6308-INFO-2022-06-25 14:45:24,860 Last-Accessed: 2022-06-25 18:26:05 6308-INFO-2022-06-25 14:45:24,860 Created: 2020-05-13 02:40:09 6308-INFO-2022-06-25 14:45:24,860 SHA-256: 08447ed9d5639d36963ce554ee2ae61e63734d3eb445353cd83657512abe8643 6308-INFO-2022-06-25 14:45:24,860 ======================================================================

6308-INFO-2022-06-25 14:45:24,864 File Processed: 6308-INFO-2022-06-25 14:45:24,864 Path: c:\tst\Disney.jpg 6308-INFO-2022-06-25 14:45:24,864 File Size: 304,930 6308-INFO-2022-06-25 14:45:24,864 Last-Modified: 2013-09-20 01:26:30 6308-INFO-2022-06-25 14:45:24,865 Last-Accessed: 2022-06-25 18:26:05 6308-INFO-2022-06-25 14:45:24,866 Created: 2020-05-13 02:40:09 6308-INFO-2022-06-25 14:45:24,866 SHA-256: d27f8cc8192f3f9f1361b7fc8739e6d4361d123650d717264a7242d8984d881f 6308-INFO-2022-06-25 14:45:24,866 ======================================================================

*****************HERE IS MY SCRIPT:**********************

''' IMPORT STANDARD LIBRARIES '''

import os

import sys

import time

import hashlib

import re

import logging

import platform

import socket

import uuid

import json

import psutil # pip install psutil

''' IMPORT 3RD PARTY LIBRARIES '''

from prettytable import PrettyTable # This Library Allows for the Data, etc., # to be Printed in a Table-Format in the Terminal. '

'' DEFINE PSEUDO CONSTANTS '''

investigator = input("Investigator Name: ") # Enter Your Name at this Prompt.

organization = input("Organization: ") # Enter the Organization at this Prompt (i.e. ASU).

className = input("Class Code: ") # Enter the Class at this Prompt (i.e. PYTHON).

''' LOCAL FUNCTIONS ''' # This Define Function Can Explain Itself....

A Series of Functions to Gather MY System's Information, # Which I Will Then Use to Append to "Log.txt"

def getSystemInfo():

try: info={}

info['platform']=platform.system()

info['platform-release']=platform.release()

info['platform-version']=platform.version()

info['architecture']=platform.machine()

info['hostname']=socket.gethostname()

info['ip-address']=socket.gethostbyname(socket.gethostname())

info['mac-address']=':'.join(re.findall('..', "%012x" % uuid.getnode()))

info['processor']=platform.processor()

info['ram']=str(round(psutil.virtual_memory().total / (1024.0 **3))) + " GB"

return info

except SystemExit as exception:

logging.exception(exception)

return False

def hashValues(file_path): # Iterates through the User's Entered Path. # I put the Data from "HashingExample.py" into a function, so to make it look nice. # I noticed other forms used online, but this format helped me to retain the concept.

with open(file_path, 'rb') as target:

fileContents = target.read()

sha256_data = hashlib.sha256()

sha256_data.update(fileContents)

hexDigest = sha256_data.hexdigest()

return hexDigest

def GetFileMetaData(fileName):

try:

metaData = os.stat(fileName) # Uses the "stat" Method via "os" in order to obtain Meta Data.

fileSize = metaData.st_size #

"fileSize" Variable Extracts File Size data, and the others Extract MAC Times.

timeLastAccess = metaData.st_atime

timeLastModified = metaData.st_mtime

timeCreated = metaData.st_ctime

macTimeList = [timeLastModified, timeCreated, timeLastAccess] # "macTimeList" Variable Groups the MAC Times into a List

return True, None, fileSize, macTimeList

except Exception as err:

return False, str(err), None, None

''' MAIN ENTRY POINT '''

def main(): ######### BELOW IS A SERIES OF FUNCTIONS THAT LOG DATA TO "Log.txt." ######## # "__SYS_INFO__" Contains ALL of the System Information via the "getSystemInfo()" Call Function.

__SYS_INFO__ = getSystemInfo()

if __SYS_INFO__: # Removed Old Logging Script; Replaced it with MY NAME if os.path.isfile('Log.txt.'): os.remove("Log.txt.") # "logging.basicConfig()" Is How I Want the "Log.txt" File Format. # Meaning, I used this Function to Configure the Logging System # to Write the Logs to "Log.txt.".

logging.basicConfig(filename='Log.txt', filemode='w', level=logging.INFO, format='%(process)d-%(levelname)s-%(asctime)s %(message)s')

logging.info("Investigator: " + investigator)

logging.info("Organization: " + organization)

logging.info("Class Name: " + className) # Where the First Line in "Log.txt" Begins.

logging.info("Script Start ")

# Beginning Line for the System Information/Data Logged.

logging.info("***********\tSYSTEM INFORMATION/DATA\t ***********")

for (sys_keys, sys_values) in __SYS_INFO__.items():

print(f"{sys_keys}: {sys_values}") # Modified logging.info(), via a "for" loop, to Call and Append ALL of the "__SYS_INFO__" # into the File, "Log.txt."

logging.info(f"{sys_keys}: {sys_values}") # This "logging.info()" Function Prints "=" 100Xs for Line-Separation. logging.info("="*100 + " ")

# ResultTable variable contains the Class Arguments, which are what print during execution.

resultTable = PrettyTable(['Full_Path', 'Size', 'Last_Modified', 'Last_Accessed', 'Created', 'Hash_Values'])

try:

while True:

targetDIR = input(" Please Enter in a Path to Examine: ")

if os.path.isdir(targetDIR):

break

print(" Unrecognizable Path. Please Try Again")

for root, dir, files in os.walk(targetDIR): # This goes according to the absolute path, so to find the files (i.e., root > dir(s) > file(s)).

for eachFile in files: # Narrows the "files" down to "eachFile" Accordingly.

full_path = os.path.join(root, eachFile)

abs_path = os.path.abspath(full_path)

fileSize = os.path.getsize(abs_path)

success, errInfo, fileSize, mac_list = GetFileMetaData(abs_path)

# If "success" = True, and "errInfo" = None, then MetaData and HashValues continue into the "if success" statement.

if success:

modTime = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(mac_list[0])) # The "0" Position calls the "Last_Modified" Data in "macTimeList" Variable.

accTime = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(mac_list[1])) # The "1" Position calls the "Last_Accessed" Data in "macTimeList" Variable.

creTime = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(mac_list[2])) # The "2" Position calls the "Last_Created" Data in "macTimeList" Variable.

hashes = hashValues(abs_path)

# resultTable adds a row to the "PrettyTable()" Class Arguments according to what's in each Variable.

resultTable.add_row( [ abs_path, fileSize, modTime, accTime, creTime, hashes ] )

else:

print("Fail: ", abs_path, "Exception = ", errInfo)

resultTable.align = "l"

print(resultTable.get_string(sortby="Size", reversesort=True))

# If ANY of the Data is Incorrect/Invalid, an "Exception" will occur explaining why.

except Exception as err:

print(" Script Aborted ", "Exception = ", err)

logging.basicConfig(filename='Log.txt', filemode='w', level=logging.INFO, format='%(process)d-%(levelname)s-%(asctime)s %(message)s')

logging.info("Full Path: " + abs_path)

logging.info("File Size: " + str(fileSize))

logging.info("Modified Time: " + modTime)

logging.info("Last Accessed: " + accTime)

logging.info("Creation: " + creTime)

logging.info("SHA-256 Hash: " + hashes)

if __name__ == '__main__':

main() # Calls the "MAIN" Script.

print(" Script-End ") # End of the Program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 18th International Conference Dasfaa 2013 Wuhan China April 22 25 2013 Proceedings Part 2 Lncs 7826

Authors: Weiyi Meng ,Ling Feng ,Stephane Bressan ,Werner Winiwarter ,Wei Song

2013th Edition

3642374492, 978-3642374494

More Books

Students also viewed these Databases questions