-
Notifications
You must be signed in to change notification settings - Fork 108
/
dstimeline.py
executable file
·256 lines (235 loc) · 10.6 KB
/
dstimeline.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python
# This file is part of ntdsxtract.
#
# ntdsxtract is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ntdsdump is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ntdsdump. If not, see <http://www.gnu.org/licenses/>.
'''
@author: Csaba Barta
@license: GNU General Public License 2.0 or later
@contact: [email protected]
'''
import sys
import time
from operator import *
from ntds.version import *
from ntds.dsdatabase import *
from ntds.dsobjects import *
from ntds.dstime import *
from ntds.dsrecord import *
from ntds.lib.fs import *
from ntds.lib.csvoutput import *
times = []
timeline = []
def usage():
sys.stderr.write("\nDSTimeline v" + str(ntds.version.version))
sys.stderr.write("\nConstructs timeline")
sys.stderr.write("\n\nusage: %s <datatable> <work directory> [option]" % sys.argv[0])
sys.stderr.write("\n\n datatable")
sys.stderr.write("\n The full path to the file called datatable extracted by esedbexport")
sys.stderr.write("\n work directory")
sys.stderr.write("\n The full path to the directory where ntdsxtract should store its")
sys.stderr.write("\n cache files and output files. If the directory does not exist")
sys.stderr.write("\n it will be created.")
sys.stderr.write("\n options:")
sys.stderr.write("\n --b")
sys.stderr.write("\n Output timeline in mactime body format.")
sys.stderr.write("\n --csv")
sys.stderr.write("\n Output timeline in CSV format.")
sys.stderr.write("\n --outfile <name of the output file>")
sys.stderr.write("\n The filename of the output file to which ntdsxtract should write the")
sys.stderr.write("\n output")
sys.stderr.write("\n --debug")
sys.stderr.write("\n Turn on detailed error messages and stack trace")
sys.stderr.write("\n\nFields of the default output")
sys.stderr.write("\n Timestamp|Action|Record ID|Obj. name|Obj. type")
sys.stderr.write("\n")
if len(sys.argv) < 2:
usage()
sys.exit(1)
sys.stderr.write("\n[+] Started at: %s" % time.strftime(
"%a, %d %b %Y %H:%M:%S UTC",
time.gmtime()))
bodyformat = False
csvformat = False
outfile = ""
sys.stderr.write("\n[+] Started with options:")
optid = 0
for opt in sys.argv:
if opt == "--b":
if csvformat == True:
sys.stderr.write("\n[!] Error! CSV and body format cannot be defined at the same time!\n")
sys.exit(1)
bodyformat = True
sys.stderr.write("\n\t[-] Using mactime body format")
if opt == "--csv":
if bodyformat == True:
sys.stderr.write("\n[!] Error! CSV and body format cannot be defined at the same time!\n")
sys.exit(1)
csvformat = True
sys.stderr.write("\n\t[-] Using CSV format")
if opt == "--outfile":
if len(sys.argv) < optid + 2:
usage()
sys.exit(1)
outfile = sys.argv[optid + 1]
sys.stderr.write("\n\t[-] Output filename: " + sys.argv[optid + 1])
optid += 1
# Check the datatable
if not checkfile(sys.argv[1]):
sys.stderr.write("\n[!] Error! datatable cannot be found!\n")
sys.exit(1)
# Check the workdir
wd = ensure_dir(sys.argv[2])
# Check the output file
if outfile != "" and csvformat == True:
init_csv(path.join(wd, outfile))
write_csv(["Timestamp", "Event", "Record ID", "Object name",
"Object type"
])
bof = None
if outfile != "" and bodyformat == True:
bof = open(path.join(wd, outfile), "wb")
# Initialize engine
db = dsInitDatabase(sys.argv[1], wd)
i = 0
l = len(dsMapLineIdByRecordId)
for recordid in dsMapLineIdByRecordId:
sys.stderr.write("\r[+] Building timeline - %d%% -> %d records processed" % (
i*100/l,
i
))
sys.stderr.flush()
try:
tmp = dsObject(db, recordid)
except:
continue
if bodyformat == True:
if tmp.WhenChanged != -1 or tmp.WhenCreated != -1:
times.append((recordid,
0 if tmp.WhenCreated == -1 else tmp.WhenCreated,
0 if tmp.WhenChanged == -1 else tmp.WhenChanged,
tmp.Name + " (" + str(tmp.GUID) + ")",
tmp.Type,
""
))
if tmp.Type == "Person":
user = dsAccount(db, recordid)
if user.LastLogon != -1:
times.append((recordid,
0,
user.LastLogon,
user.Name + " (" + str(user.GUID) + ")",
user.Type,
"Logged in"
))
if user.LastLogonTimeStamp != -1:
times.append((recordid,
0,
user.LastLogonTimeStamp,
user.Name + " (" + str(user.GUID) + ")",
user.Type,
"Login timestamp sync"
))
if user.PasswordLastSet != -1:
times.append((recordid,
0,
user.PasswordLastSet,
user.Name + " (" + str(user.GUID) + ")",
user.Type,
"Password changed"
))
user = None
if csvformat == True:
if dsVerifyDSTimeStamp(tmp.WhenCreated) != -1:
times.append((recordid, tmp.WhenCreated, "Created", tmp.Name + " (" + str(tmp.GUID) + ")", tmp.Type))
if dsVerifyDSTimeStamp(tmp.WhenChanged) != -1:
times.append((recordid, tmp.WhenChanged, "Modified", tmp.Name + " (" + str(tmp.GUID) + ")", tmp.Type))
if tmp.Type == "Person":
user = dsAccount(db, recordid)
if user.LastLogon != -1 and user.LastLogon != 0:
times.append((recordid,
user.LastLogon,
"Logged in",
user.Name + " (" + str(user.GUID) + ")",
user.Type
))
if user.LastLogonTimeStamp != -1 and user.LastLogonTimeStamp != 0:
times.append((recordid,
user.LastLogonTimeStamp,
"Login timestamp sync",
user.Name + " (" + str(user.GUID) + ")",
user.Type
))
if user.PasswordLastSet != -1 and user.PasswordLastSet != 0:
times.append((recordid,
user.PasswordLastSet,
"Password changed",
user.Name + " (" + str(user.GUID) + ")",
user.Type
))
user = None
i += 1
sys.stderr.write("\n")
timeline = sorted(times, key=itemgetter(1))
for item in timeline:
if bodyformat == True:
(id, ctimestamp, mtimestamp, name, type, actiontype) = item
if actiontype != "":
sys.stdout.write("\n0|%s (%s) - (%s)|%d||0|0|0|0|%d|0|%d" % (
name,
type,
actiontype,
id,
dsGetPOSIXTimeStamp(mtimestamp),
dsGetPOSIXTimeStamp(ctimestamp)
))
if outfile != "":
bof.write("0|%s (%s) - (%s)|%d||0|0|0|0|%d|0|%d\n" % (
name,
type,
actiontype,
id,
dsGetPOSIXTimeStamp(mtimestamp),
dsGetPOSIXTimeStamp(ctimestamp)
))
else:
sys.stdout.write("\n0|%s (%s)|%d||0|0|0|0|%d|0|%d" % (
name,
type,
id,
dsGetPOSIXTimeStamp(mtimestamp),
dsGetPOSIXTimeStamp(ctimestamp)
))
if outfile != "":
bof.write("0|%s (%s)|%d||0|0|0|0|%d|0|%d\n" % (
name,
type,
id,
dsGetPOSIXTimeStamp(mtimestamp),
dsGetPOSIXTimeStamp(ctimestamp)
))
if csvformat == True:
(id, timestamp, action, name, type) = item
sys.stdout.write("\n%s|%s|%d|%s (%s)" % (
dsGetDSTimeStampStr(timestamp),
action,
id,
name,
type
))
if outfile != "":
write_csv(["=\"" + dsGetDSTimeStampStr(timestamp) + "\"", action, id, name,
type
])
sys.stdout.write("\n")
sys.stdout.flush()