-
Notifications
You must be signed in to change notification settings - Fork 31
/
impdump.py
executable file
·262 lines (195 loc) · 8.3 KB
/
impdump.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
257
258
259
260
261
262
#!/usr/bin/python
#
# ImpDump uses the creddump library (included) and was heavily adapted
# from creddump's code.
#
# All credit to the original author(s): Brendan Dolan-Gavitt and others
# See the README for full credits.
#
# creddump 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.
#
# creddump 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 creddump. If not, see <http://www.gnu.org/licenses/>.
#
import sys, unicodedata
from binascii import *
from framework.win32.dshashdump import get_syskey,ds_decrypt_pek,ds_decrypt_single_hash,ds_decrypt_with_pek
# manually decrypt a single hash
def decUserHash(bootkey,rawPekKey,user,rawRID,rawLMhash,rawNTLMhash):
# "rawRID" may already be the RID, or the full SID - if latter is true, truncate
rid = int(rawRID,16) if len(rawRID)==8 else int(rawRID[48:],16)
encPEK = unhexlify(rawPekKey[16:])
pek = ds_decrypt_pek(bootkey, encPEK)
if (not rawLMhash):
rawLMhash = ""
if rawLMhash.startswith("1100000000000000"):
rawLMhash = rawLMhash[16:]
if rawNTLMhash.startswith("1100000000000000"):
rawNTLMhash = rawNTLMhash[16:]
pekENClm = unhexlify(rawLMhash)
pekENCntlm = unhexlify(rawNTLMhash)
encLM = ds_decrypt_with_pek(pek, rawLMhash)
encNTLM = ds_decrypt_with_pek(pek, pekENCntlm)
lm = ds_decrypt_single_hash(rid, encLM)
ntlm = ds_decrypt_single_hash(rid, encNTLM)
out = user+":"+str(rid)
if ((not encLM) or (encLM == "")):
out+= ":aad3b435b51404eeaad3b435b51404ee:"
else:
out += ":"+lm.encode('hex')+":"
if (ntlm == ""):
out += "NO PASSWORD*********************:::"
else:
out += ntlm.encode('hex')+":::"
return out
# decrypt user hash histories
def decUserHashHistory(bootkey, rawPekKey, user, rawRID, rawLMhashHistory, rawNTLMhashHistory):
# "rawRID" may already be the RID, or the full SID - if latter is true, truncate
rid = int(rawRID,16) if len(rawRID)==8 else int(rawRID[48:],16)
encPEK = unhexlify(rawPekKey[16:])
pek = ds_decrypt_pek(bootkey, encPEK)
if rawLMhashHistory.startswith("1100000000000000"):
rawLMhashHistory = rawLMhashHistory[16:]
if rawNTLMhashHistory.startswith("1100000000000000"):
rawNTLMhashHistory = rawNTLMhashHistory[16:]
pekENClmHistory = unhexlify(rawLMhashHistory)
pekENCntlmHistory = unhexlify(rawNTLMhashHistory)
encLM = ds_decrypt_with_pek(pek, pekENClmHistory)
encNTLM = ds_decrypt_with_pek(pek, pekENCntlmHistory)
histories = []
for hindex in range(0,len(encNTLM)/16):
ntlm = ds_decrypt_single_hash(rid, encNTLM[hindex*16:(hindex+1)*16])
lm = ds_decrypt_single_hash(rid, encLM[hindex*16:(hindex+1)*16])
if hindex == 0:
out = user+":"+str(rid)
else:
out = user+"_history"+str(hindex-1)+":"+str(rid)
if (lm == ""):
out+= ":aad3b435b51404eeaad3b435b51404ee:"
else:
out += ":"+lm.encode('hex')+":"
if (ntlm == ""):
out += "NO PASSWORD*********************:::"
else:
out += ntlm.encode('hex')+":::"
histories.append(out)
return histories
# decrypt/return just the krbtgt NTLM hash
def decKrbtgt(hiveFile,hashFile):
#get the bootkey from the system hive
bootkey = get_syskey(hiveFile)
f = open(hashFile)
raw = f.read()
f.close()
krbtgt_acct = None
rawPekKey = None
rawRID = None
rawNTLMhash = None
# split along account boundaries
accts = raw.split("ATTm3")
for acct in accts:
if "ATTk590689" in acct:
for part in acct.split('\n'):
if part.startswith("ATTk590689"):
rawPekKey = part.split(":")[1].strip().strip('\'')
elif "u\'krbtgt\'" in acct and "ATTk589914" in acct:
for part in acct.split('\n'):
if part.startswith("ATTr589970"):
rawRID = part.split(":")[1].strip().strip('\'')
elif part.startswith("ATTk589914"):
rawNTLMhash = part.split(":")[1].strip().strip('\'')
print decUserHash(bootkey, rawPekKey, "krbtgt", rawRID, "", rawNTLMhash)
# decrypt/return all user hashes
def decUserHashes(hiveFile,hashFile):
#get the bootkey from the system hive
bootkey = get_syskey(hiveFile)
f = open(hashFile)
raw = f.read()
f.close()
krbtgt_acct = None
rawPekKey = None
rawRID = None
rawNTLMhash = None
rawLMhash = None
# split along account boundaries
accts = raw.split("ATTm3")
for acct in accts:
if "ATTk590689" in acct:
for part in acct.split('\n'):
if part.startswith("ATTk590689"):
rawPekKey = part.split(":")[1].strip().strip('\'')
# only examine accts with a valid NTLM hash
elif "ATTk589914" in acct:
parts = acct.split('\n')
for part in parts:
if part.startswith("ATTm590045"):
name = part.split(":")[1].strip()[1:].strip('\'')
elif part.startswith("ATTr589970"):
rawRID = part.split(":")[1].strip().strip('\'')
elif part.startswith("ATTk589879"):
rawLMhash = part.split(":")[1].strip().strip('\'')
elif part.startswith("ATTk589914"):
rawNTLMhash = part.split(":")[1].strip().strip('\'')
print decUserHash(bootkey, rawPekKey, name, rawRID, "", rawNTLMhash)
# decrypt/return all user hash histories
def decUserHashHistories(hiveFile,hashFile):
#get the bootkey from the system hive
bootkey = get_syskey(hiveFile)
f = open(hashFile)
raw = f.read()
f.close()
krbtgt_acct = None
rawPekKey = None
rawRID = None
rawNTLMhash = None
rawLMhash = None
rawNTLMhashHistory = None
rawLMhashHistory = None
# split along account boundaries
accts = raw.split("ATTm3")
for acct in accts:
if "ATTk590689" in acct:
for part in acct.split('\n'):
if part.startswith("ATTk590689"):
rawPekKey = part.split(":")[1].strip().strip('\'')
# only examine accts with a valid NTLM hash history
elif "ATTk589918" in acct:
parts = acct.split('\n')
for part in parts:
if part.startswith("ATTm590045"):
name = part.split(":")[1].strip()[1:].strip('\'')
elif part.startswith("ATTr589970"):
rawRID = part.split(":")[1].strip().strip('\'')
elif part.startswith("ATTk589984"):
rawLMhashHistory = part.split(":")[1].strip().strip('\'')
elif part.startswith("ATTk589918"):
rawNTLMhashHistory = part.split(":")[1].strip().strip('\'')
histories = decUserHashHistory(bootkey, rawPekKey, name, rawRID, rawLMhashHistory, rawNTLMhashHistory)
for history in histories:
print history
if len(sys.argv) == 3:
decUserHashes(sys.argv[1], sys.argv[2])
elif len(sys.argv) == 4:
if (sys.argv[3].lower() == "-history"):
decUserHashHistories(sys.argv[1], sys.argv[2])
else:
decKrbtgt(sys.argv[1], sys.argv[2])
elif len(sys.argv) == 5:
bootkey = get_syskey(sys.argv[1])
decUserHash(bootKey, sys.argv[2], "user", sys.argv[3], sys.argv[4])
else:
print "\nFirst dump the datatable using esentutl.py:"
print "\tesentutl.py /path/to/ntds.dit export -table datatable | grep -E \"ATTk590689|ATTm3|ATTm590045|ATTm590045|ATTr589970|ATTk589914|ATTk589879|ATTk589984|ATTk589918\" > outfile.txt\n"
print "Then : %s <system hive> <hash file>" % sys.argv[0]
print "\tor : %s <system hive> <hash file> -krbtgt" % sys.argv[0]
print "\tor : %s <system hive> <hash file> -history" % sys.argv[0]
print "\tor : %s <system hive> <rawPekKey> <rawRid> <rawNTLMhash>\n" % sys.argv[0]
sys.exit(1)