forked from csababarta/ntdsxtract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdslink.py
executable file
·122 lines (106 loc) · 4.21 KB
/
dslink.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
# 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.
#
# ntdsxtract 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 ntdsxtract. If not, see <http://www.gnu.org/licenses/>.
'''
@author: Csaba Barta
@license: GNU General Public License 2.0 or later
@contact: [email protected]
'''
import ntds.dsfielddictionary
from ntds.dstime import *
import sys
from .lib.map import *
import pickle
from os import path
dsMapLinks = {}
dsMapBackwardLinks = {}
def dsInitLinks(dsESEFile, workdir):
dl = open(dsESEFile, 'r')
dl.seek(0)
line = dl.readline()
if line == "":
sys.stderr.write("[-] Warning! Error processing the first line!\n")
sys.exit(1)
else:
ntds.dsfielddictionary.dsFieldNameRecord = line.split('\t')
record = line.split('\t')
for cid in range(0, len(record)-1):
#------------------------------------------------------------------------------
# filling indexes for membership attributes
#------------------------------------------------------------------------------
if (record[cid] == "link_DNT"):
ntds.dsfielddictionary.dsTargetRecordIdIndex = cid
if (record[cid] == "backlink_DNT"):
ntds.dsfielddictionary.dsSourceRecordIdIndex = cid
if (record[cid] == "link_deltime"):
ntds.dsfielddictionary.dsLinkDeleteTimeIndex = cid
dl.seek(0)
dsCheckMaps(dl, workdir)
#dsBuildLinkMaps(dl)
def dsCheckMaps(dsDatabase, workdir):
try:
global dsMapLinks
global dsMapBackwardLinks
sys.stderr.write("[+] Loading saved map files (Stage 2)...\n")
dsLoadMap(path.join(workdir, "links.map"), dsMapLinks)
dsLoadMap(path.join(workdir, "backlinks.map"), dsMapBackwardLinks)
except Exception as e:
sys.stderr.write("[!] Warning: Opening saved maps failed: " + str(e) + "\n")
sys.stderr.write("[+] Rebuilding maps...\n")
dsBuildLinkMaps(dsDatabase, workdir)
pass
def dsBuildLinkMaps(dsLinks, workdir):
global dsMapLinks
global dsMapBackwardLinks
sys.stderr.write("[+] Extracting object links...\n")
sys.stderr.flush()
lineid = 0
while True:
line = dsLinks.readline()
if line == "":
break
record = line.split('\t')
if lineid != 0:
source = int(record[ntds.dsfielddictionary.dsSourceRecordIdIndex])
target = int(record[ntds.dsfielddictionary.dsTargetRecordIdIndex])
deltime = -1
if record[ntds.dsfielddictionary.dsLinkDeleteTimeIndex] != "":
deltime = dsVerifyDSTime(record[ntds.dsfielddictionary.dsLinkDeleteTimeIndex])
try:
tmp = dsMapLinks[target]
except KeyError:
dsMapLinks[target] = []
pass
try:
dsMapLinks[target].append((source, deltime))
except KeyError:
dsMapLinks[target] = []
dsMapLinks[target].append((source, deltime))
try:
tmp = dsMapBackwardLinks[source]
except KeyError:
dsMapBackwardLinks[source] = []
pass
try:
dsMapBackwardLinks[source].append((target, deltime))
except KeyError:
dsMapBackwardLinks[source] = []
dsMapBackwardLinks[source].append((target, deltime))
lineid += 1
links = open(path.join(workdir, "links.map"), "wb")
pickle.dump(dsMapLinks, links)
links.close()
backlinks = open(path.join(workdir, "backlinks.map"), "wb")
pickle.dump(dsMapBackwardLinks, backlinks)
backlinks.close()