-
Notifications
You must be signed in to change notification settings - Fork 38
/
ntfsrecover.py
308 lines (265 loc) · 9.51 KB
/
ntfsrecover.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
from __future__ import unicode_literals, print_function
import struct
import collections
import glob
import fnmatch
import os
import sys
import codecs
def doseek(f, n):
if sys.platform == 'win32':
# Windows raw disks can only be seeked to a multiple of the block size
BLOCKSIZE = 512
na, nb = divmod(n, BLOCKSIZE)
f.seek(na * BLOCKSIZE)
if nb:
f.read(nb)
else:
f.seek(n)
def readat(f, n, s):
pos = f.tell()
doseek(f, n)
res = f.read(s)
doseek(f, pos)
return res
def parseFilename(s):
ref, = struct.unpack('<Q', s[:8])
flen = ord(s[64:65])
fn = s[66:66 + flen*2].decode('UTF-16-LE')
return ref, fn
def parseRaw(s):
return s
ATTR_INFO = {
0x10: ('standard_info', 'STANDARD_INFORMATION ', None),
0x20: ('attr_list', 'ATTRIBUTE_LIST ', None),
0x30: ('filename', 'FILE_NAME ', parseFilename),
0x40: ('vol_ver', 'VOLUME_VERSION', None),
0x40: ('obj_id', 'OBJECT_ID ', None),
0x50: ('security', 'SECURITY_DESCRIPTOR ', None),
0x60: ('vol_name', 'VOLUME_NAME ', None),
0x70: ('vol_info', 'VOLUME_INFORMATION ', None),
0x80: ('data', 'DATA ', None),
0x90: ('index_root', 'INDEX_ROOT ', None),
0xA0: ('index_alloc', 'INDEX_ALLOCATION ', None),
0xB0: ('bitmap', 'BITMAP ', None),
0xC0: ('sym_link', 'SYMBOLIC_LINK', None),
0xC0: ('reparse', 'REPARSE_POINT ', None),
0xD0: ('ea_info', 'EA_INFORMATION ', None),
0xE0: ('ea', 'EA ', None),
0xF0: ('prop_set', 'PROPERTY_SET', None),
0x100: ('log_util', 'LOGGED_UTILITY_STREAM', None),
}
def parse_varint(v):
if not v:
return 0
return int(codecs.encode(v[::-1], 'hex'), 16)
def read_runlist(f, bpc, runlist):
out = bytearray()
for rlen, roff in runlist:
out += readat(f, roff * bpc, rlen * bpc)
return bytes(out)
def parse_attr(f, bpc, chunk):
type, size, nonres, namelen, nameoff = struct.unpack('<iiBBH', chunk[:12])
if namelen:
name = chunk[nameoff:nameoff+namelen*2].decode('UTF-16-LE')
else:
name = None
stype, sname, sparser = ATTR_INFO.get(type, ('unk_%d' % type, str(type), parseRaw))
if sparser is None:
sparser = parseRaw
sname = sname.strip()
if nonres:
rloff = struct.unpack('<H', chunk[32:34])[0]
size_actual = struct.unpack('<Q', chunk[48:56])[0]
rlpos = rloff
runlist = []
curoff = 0
while rlpos < len(chunk):
header = ord(chunk[rlpos:rlpos+1])
if not header:
break
rlpos += 1
lenlen = header & 0xf
offlen = header >> 4
if rlpos + lenlen + offlen > len(chunk):
print("Warning: invalid runlist header %02x (runlist %s)" % (header, codecs.encode(chunk[rloff:], 'hex')), file=sys.stderr)
break
thislen = parse_varint(chunk[rlpos:rlpos+lenlen])
rlpos += lenlen
thisoff = parse_varint(chunk[rlpos:rlpos+offlen])
if thisoff and (thisoff & (1 << (8 * offlen - 1))):
thisoff -= 1 << (8 * offlen)
rlpos += offlen
curoff += thisoff
runlist.append((thislen, curoff))
attrdata = lambda: sparser(read_runlist(f, bpc, runlist)[:size_actual])
else:
attrlen, attroff = struct.unpack('<IH', chunk[16:22])
data = chunk[attroff:attroff+attrlen]
attrdata = lambda: sparser(data)
return sname, name, attrdata
def usa_fixup(chunk, chunkoff, usa_ofs, usa_count):
chunk = bytearray(chunk)
if usa_ofs == 0 or usa_count == 0:
return chunk
upos = usa_ofs
usa_num = chunk[upos:upos+2]
upos += 2
for i in range(len(chunk) // 512):
cpos = i*512+510
if chunk[cpos:cpos+2] != usa_num:
print("Warning: bad USA data at MBR offset %d - disk corrupt?" % (chunkoff + cpos), file=sys.stderr)
else:
chunk[cpos:cpos+2] = chunk[upos:upos+2]
upos += 2
return chunk
def parse_file(f, chunkoff, bpc, chunk):
magic, usa_ofs, usa_count, lsn, seq, link, attr_offset = struct.unpack(
'<IHHQHHH', chunk[:22])
attrs = collections.defaultdict(dict)
try:
chunk = usa_fixup(chunk, chunkoff, usa_ofs, usa_count)
except Exception as e:
print("File at offset %d: failed to perform USA fixup: %s" % (chunkoff, e), file=sys.stderr)
pos = attr_offset
while 1:
if pos > len(chunk) - 12:
# Uhoh, corruption?
break
type, size, nonres, namelen, nameoff = struct.unpack('<iIBBH', chunk[pos:pos+12])
if type == -1:
break
try:
sname, name, data = parse_attr(f, bpc, chunk[pos:pos+size])
attrs[sname][name] = data
except Exception as e:
print("File at offset %d: failed to parse attr type=%d pos=%d: %s" % (chunkoff, type, pos, e), file=sys.stderr)
pos += size
return attrs
def parse_mft(f, bpc, mft):
out = []
for i in range(len(mft) // 1024):
if i % 791 == 0:
sys.stderr.write("\rParsing MFT: %d/%d" % (i, len(mft) // 1024))
sys.stderr.flush()
chunk = mft[i*1024:(i+1)*1024]
if chunk[:4] == b'FILE':
out.append(parse_file(f, i * 1024, bpc, chunk))
else:
out.append(None)
sys.stderr.write("\rParsing MFT: Done! \n")
sys.stderr.flush()
return out
def read_mft(f, bpc, mft_cluster, clusters_per_mft):
print("Loading MBR from cluster %d" % mft_cluster, file=sys.stderr)
mft = readat(f, mft_cluster * bpc, clusters_per_mft * bpc)
try:
mftattr = parse_file(f, 0, bpc, mft[:1024])
newmft = mftattr['DATA'][None]()
if len(newmft) < len(mft):
raise Exception("$MFT truncated")
mft = newmft
except Exception as e:
print("WARNING: Failed to load $MFT (%s), proceeding with partial MFT." % e, file=sys.stderr)
return mft
def get_filepath(mft, i):
bits = []
while 1:
parent, name = mft[i]['FILE_NAME'][None]()
if name == '.':
break
bits.append(name)
i = parent & 0xffffffffffff
return bits[::-1]
def open_output_file(destfn):
if not os.path.isfile(destfn):
return open(destfn, 'wb')
t = 0
while True:
fn = destfn + '_%04d' % t
if not os.path.isfile(fn):
return open(fn, 'wb')
t += 1
raise OSError("File exists.")
def save_file(mfti, destfn):
if '/' in destfn:
try:
os.makedirs(destfn.rsplit('/', 1)[0])
except OSError:
pass
with open_output_file(destfn) as outf:
outf.write(mfti['DATA'][None]())
for ads in mfti['DATA']:
if ads is None:
continue
with open_output_file(destfn + '~' + ads) as outf:
outf.write(mfti['DATA'][ads]())
def parse_args(argv):
import argparse
parser = argparse.ArgumentParser(description="Recover files from an NTFS volume")
parser.add_argument('--sector-size', type=int,
help='Sector size in bytes (default: trust filesystem)')
parser.add_argument('--cluster-size', type=int,
help='Cluster size in sectors (default: trust filesystem)')
parser.add_argument('--mft', type=argparse.FileType('rb'),
help='Use given file as MFT')
parser.add_argument('--save-mft', type=argparse.FileType('wb'),
help='Write extracted MFT to given file')
parser.add_argument('-p', '--pattern', action='append',
help='Recover files matching pattern (glob()); can be specified multiple times')
parser.add_argument('-o', '--outdir',
help='Output directory (default .)')
parser.add_argument('disk', help='NTFS partition (e.g. /dev/disk*, \\\\.\\Harddisk*Partition*)')
return parser.parse_args(argv)
def main(argv):
args = parse_args(argv)
f = open(args.disk, 'rb')
if args.outdir:
try:
os.makedirs(args.outdir)
except OSError:
pass
os.chdir(args.outdir)
# parse essential details of the MBR
if readat(f, 3, 8) != b'NTFS ':
raise ValueError("Not an NTFS disk???")
bps, spc = struct.unpack('<HB', readat(f, 0xb, 3))
if args.sector_size:
bps = args.sector_size
if args.cluster_size:
spc = args.cluster_size
bpc = bps * spc
mft_clust, mftmirr_clust, clust_per_mft = struct.unpack('<QQB', readat(f, 0x30, 17))
print("Reading MFT", file=sys.stderr)
if args.mft:
mftbytes = args.mft.read()
else:
mftbytes = read_mft(f, bpc, mft_clust, clust_per_mft)
if args.save_mft:
args.save_mft.write(mftbytes)
mft = parse_mft(f, bpc, mftbytes)
for i, file in enumerate(mft):
try:
fn = file['FILE_NAME'][None]()[1]
except Exception as e:
continue
try:
fullpath = '/'.join(get_filepath(mft, i))
except Exception as e:
fullpath = '__ORPHANED__/' + fn
if not args.pattern:
print(fullpath)
continue
for pat in args.pattern:
pat = pat.lower().encode('utf8')
if fnmatch.fnmatch(fn.lower().encode('utf8'), pat) or fnmatch.fnmatch(fullpath.lower().encode('utf8'), pat):
print("Recovering", fullpath, end=' ')
try:
save_file(file, fullpath)
except Exception as e:
print("failed:", e)
else:
print("Success!")
if __name__ == '__main__':
import sys
exit(main(sys.argv[1:]))