forked from Te-k/analyst-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpetimeline.py
executable file
·76 lines (64 loc) · 2.74 KB
/
petimeline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#! /usr/bin/python2
import argparse
import lief
import os
import datetime
import magic
import pefile
import hashlib
def extract_datetime(pe):
"""
Extract PE timestamp using lief
"""
if pe.header.time_date_stamps:
return datetime.datetime.fromtimestamp(pe.header.time_date_stamps)
else:
return None
def extract_sig_startdate(pe):
if pe.has_signature:
issuer_serial = ":".join(map(lambda e : "{:02x}".format(e), pe.signature.signer_info.issuer[1]))
for c in pe.signature.certificates:
serial = ":".join(map(lambda e : "{:02x}".format(e), c.serial_number))
if serial == issuer_serial:
d = c.valid_from
return datetime.datetime(year=d[0], month=d[1], day=d[2], hour=d[3], minute=d[4], second=d[5])
else:
return None
def get_sha256(fpath):
with open(fpath,"rb") as f:
res = hashlib.sha256(f.read()).hexdigest()
return res
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Create a timeline of PE/DLL timestamp')
parser.add_argument('DIRECTORY', help='an integer for the accumulator')
parser.add_argument('--recursive', '-r', action='store_true', help='an integer for the accumulator')
args = parser.parse_args()
allfiles = {}
mime = magic.Magic(mime=True)
if args.recursive:
for root, dirs, files in os.walk(args.DIRECTORY):
for f in files:
fpath = os.path.join(root, f)
if mime.from_file(fpath) == "application/x-dosexec":
pe = lief.parse(fpath)
timestamp = extract_datetime(pe)
if timestamp is not None:
allfiles[timestamp] = ("TIMESTAMP", fpath, get_sha256(fpath))
timestamp = extract_sig_startdate(pe)
if timestamp is not None:
allfiles[timestamp] = ("SIGNATURE", fpath, get_sha256(fpath))
else:
for f in os.listdir(args.DIRECTORY):
fpath = os.path.join(args.DIRECTORY, f)
if os.path.isfile(fpath):
if mime.from_file(fpath) == "application/x-dosexec":
pe = lief.parse(fpath)
timestamp = extract_datetime(pe)
if timestamp is not None:
allfiles[timestamp] = ("TIMESTAMP", fpath, get_sha256(fpath))
timestamp = extract_sig_startdate(pe)
if timestamp is not None:
allfiles[timestamp] = ("SIGNATURE", fpath, get_sha256(fpath))
dates = sorted(allfiles.keys())
for d in dates:
print("{} - {} - {} - {}".format(d.strftime("%Y-%m-%d %H:%M:%S"), allfiles[d][0], allfiles[d][2], allfiles[d][1]))