-
Notifications
You must be signed in to change notification settings - Fork 74
/
main.py
executable file
·380 lines (346 loc) · 13.6 KB
/
main.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python3
"""Main RecuperaBit process."""
# RecuperaBit
# Copyright 2014-2021 Andrea Lazzarotto
#
# This file is part of RecuperaBit.
#
# RecuperaBit 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.
#
# RecuperaBit 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 RecuperaBit. If not, see <http://www.gnu.org/licenses/>.
import argparse
import codecs
import itertools
import locale
import logging
import os.path
import pickle
import sys
from recuperabit import logic, utils
# scanners
from recuperabit.fs.ntfs import NTFSScanner
__author__ = "Andrea Lazzarotto"
__copyright__ = "(c) 2014-2021, Andrea Lazzarotto"
__license__ = "GPLv3"
__version__ = "1.1.6"
__maintainer__ = "Andrea Lazzarotto"
__email__ = "andrea.lazzarotto@gmail.com"
# classes of available scanners
plugins = (
NTFSScanner,
)
commands = (
('help', 'Print this help message'),
('recoverable', 'List recoverable partitions'),
('recoverable_size <size>', 'List recoverable partitions based on the minimum <size>'),
('other', 'List unrecoverable partitions'),
('allparts', 'List all partitions'),
('tree <part#>', 'Show contents of partition (tree)'),
('csv <part#> <path>', 'Save a CSV representation in a file'),
('bodyfile <part#> <path>', 'Save a body file representation in a file'),
('tikzplot <part#> [<path>]', 'Produce LaTeX code to draw a Tikz figure'),
('restore <part#> <file>', 'Recursively restore files from <file>'),
('locate <part#> <text>', 'Print all file paths that match a string'),
('traceback <part#> <file>', 'Print ids and paths for all ancestors of <file>'),
('merge <part#> <part#>', 'Merge the two partitions into the first one'),
('quit', 'Close the program')
)
rebuilt = set()
def list_parts(parts, shorthands, test):
"""List partitions corresponding to test."""
for i, part in shorthands:
if test(parts[part]):
print('Partition #' + str(i), '->', parts[part])
def check_valid_part(num, parts, shorthands, rebuild=True):
"""Check if the required partition is valid."""
try:
i = int(num)
except ValueError:
print('Value is not valid!')
return None
if i in range(len(shorthands)):
i, par = shorthands[i]
part = parts[par]
if rebuild and par not in rebuilt:
print('Rebuilding partition...')
part.rebuild()
rebuilt.add(par)
print('Done')
return part
print('No partition with given ID!')
return None
def interpret(cmd, arguments, parts, shorthands, outdir):
"""Perform command required by user."""
if cmd == 'help':
print('Available commands:')
for name, desc in commands:
print(' %s%s' % (name.ljust(28), desc))
elif cmd == 'tree':
if len(arguments) != 1:
print('Wrong number of parameters!')
else:
part = check_valid_part(arguments[0], parts, shorthands)
if part is not None:
print('-'*10)
print(utils.tree_folder(part.root))
print(utils.tree_folder(part.lost))
print('-'*10)
elif cmd == 'bodyfile':
if len(arguments) != 2:
print('Wrong number of parameters!')
else:
part = check_valid_part(arguments[0], parts, shorthands)
if part is not None:
contents = [
'# ---' + repr(part) + '---',
'# Full paths'
] + utils.bodyfile_folder(part.root) + [
'# \n# Orphaned files'
] + utils.bodyfile_folder(part.lost)
fname = os.path.join(outdir, arguments[1])
try:
with codecs.open(fname, 'w', encoding='utf8') as outfile:
outfile.write('\n'.join(contents))
print('Saved body file to %s' % fname)
except IOError:
print('Cannot open file %s for output!' % fname)
elif cmd == 'csv':
if len(arguments) != 2:
print('Wrong number of parameters!')
else:
part = check_valid_part(arguments[0], parts, shorthands)
if part is not None:
contents = utils.csv_part(part)
fname = os.path.join(outdir, arguments[1])
try:
with codecs.open(fname, 'w', encoding='utf8') as outfile:
outfile.write(
'\n'.join(contents)
)
print('Saved CSV file to %s' % fname)
except IOError:
print('Cannot open file %s for output!' % fname)
elif cmd == 'tikzplot':
if len(arguments) not in (1, 2):
print('Wrong number of parameters!')
else:
part = check_valid_part(arguments[0], parts, shorthands)
if part is not None:
if len(arguments) > 1:
fname = os.path.join(outdir, arguments[1])
try:
with codecs.open(fname, 'w') as outfile:
outfile.write(utils.tikz_part(part) + '\n')
print('Saved Tikz code to %s' % fname)
except IOError:
print('Cannot open file %s for output!' % fname)
else:
print(utils.tikz_part(part))
elif cmd == 'restore':
if len(arguments) != 2:
print('Wrong number of parameters!')
else:
partid = arguments[0]
part = check_valid_part(partid, parts, shorthands)
if part is not None:
index = arguments[1]
partition_dir = os.path.join(outdir, 'Partition' + str(partid))
myfile = None
try:
indexi = int(index)
except ValueError:
indexi = index
for i in [index, indexi]:
myfile = part.get(i, myfile)
if myfile is None:
print('The index is not valid')
else:
logic.recursive_restore(myfile, part, partition_dir)
elif cmd == 'locate':
if len(arguments) != 2:
print('Wrong number of parameters!')
else:
part = check_valid_part(arguments[0], parts, shorthands)
if part is not None:
text = arguments[1]
results = utils.locate(part, text)
for node, path in results:
desc = (
' [GHOST]' if node.is_ghost else
' [DELETED]' if node.is_deleted else ''
)
print('[%s]: %s%s' % (node.index, path, desc))
elif cmd == 'traceback':
if len(arguments) != 2:
print('Wrong number of parameters!')
else:
partid = arguments[0]
part = check_valid_part(partid, parts, shorthands)
if part is not None:
index = arguments[1]
myfile = None
try:
indexi = int(index)
except ValueError:
indexi = index
for i in [index, indexi]:
myfile = part.get(i, myfile)
if myfile is None:
print('The index is not valid')
else:
while myfile is not None:
print('[{}] {}'.format(myfile.index, myfile.full_path(part)))
myfile = part.get(myfile.parent)
elif cmd == 'merge':
if len(arguments) != 2:
print('Wrong number of parameters!')
else:
part1 = check_valid_part(arguments[0], parts, shorthands, rebuild=False)
part2 = check_valid_part(arguments[1], parts, shorthands, rebuild=False)
if None in (part1, part2):
return
if part1.fs_type != part2.fs_type:
print('Cannot merge partitions with types (%s, %s)' % (part1.fs_type, part2.fs_type))
return
print('Merging partitions...')
utils.merge(part1, part2)
source_position = int(arguments[1])
destination_position = int(arguments[0])
_, par_source = shorthands[source_position]
_, par_destination = shorthands[destination_position]
del shorthands[source_position]
del parts[par_source]
for par in (par_source, par_destination):
try:
rebuilt.remove(par)
except:
pass
print('There are now %d partitions.' % (len(parts), ))
elif cmd == 'recoverable':
list_parts(parts, shorthands, lambda x: x.recoverable)
elif cmd == 'recoverable_size':
if len(arguments) != 1:
print('Wrong number of parameters!')
else:
list_parts(parts, shorthands, lambda x: x.size is not None and x.size > arguments[0])
elif cmd == 'other':
list_parts(parts, shorthands, lambda x: not x.recoverable)
elif cmd == 'allparts':
list_parts(parts, shorthands, lambda x: True)
elif cmd == 'quit':
exit(0)
else:
print('Unknown command.')
def main():
"""Wrap the program logic inside a function."""
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
print(" ___ ___ _ _ ")
print(" | _ \___ __ _ _ _ __ ___ _ _ __ _| _ |_) |_ ")
print(" | / -_) _| || | '_ \/ -_) '_/ _` | _ \ | _|")
print(" |_|_\___\__|\_,_| .__/\___|_| \__,_|___/_|\__|")
print(" |_| v{}".format(__version__))
print(' ', __copyright__, '<%s>' % __email__)
print(' Released under the', __license__)
print('')
parser = argparse.ArgumentParser(
description='Reconstruct the directory structure of possibly damaged '
'filesystems.'
)
parser.add_argument('path', type=str, help='path to the disk image')
parser.add_argument(
'-s', '--savefile', type=str, help='path of the scan save file'
)
parser.add_argument(
'-w', '--overwrite', action='store_true',
help='force overwrite of the save file'
)
parser.add_argument(
'-o', '--outputdir', type=str, help='directory for restored contents'
' and output files'
)
args = parser.parse_args()
try:
image = open(args.path, 'rb')
except IOError:
logging.error('Unable to open image file!')
exit(1)
read_results = False
write_results = False
# Set output directory
if args.outputdir is None:
logging.info('No output directory specified, defaulting to '
'recuperabit_output')
args.outputdir = 'recuperabit_output'
# Try to reload information from the savefile
if args.savefile is not None:
if args.overwrite:
logging.info('Results will be saved to %s', args.savefile)
write_results = True
else:
logging.info('Checking if results already exist.')
try:
savefile = open(args.savefile, 'rb')
logging.info('Results will be read from %s', args.savefile)
read_results = True
except IOError:
logging.info('Unable to open save file.')
logging.info('Results will be saved to %s', args.savefile)
write_results = True
if read_results:
logging.info('The save file exists. Trying to read it...')
try:
indexes = pickle.load(savefile)
savefile.close()
except IndexError:
logging.error('Malformed save file!')
exit(1)
else:
indexes = itertools.count()
# Ask for confirmation before beginning the process
try:
confirm = input('Type [Enter] to start the analysis or '
'"exit" / "quit" / "q" to quit: ')
except EOFError:
print('')
exit(0)
if confirm in ('exit', 'quit', 'q'):
exit(0)
# Create the output directory
if not logic.makedirs(args.outputdir):
logging.error('Cannot create output directory!')
exit(1)
scanners = [pl(image) for pl in plugins]
logging.info('Analysis started! This is going to take time...')
interesting = utils.feed_all(image, scanners, indexes)
logging.info('First scan completed')
if write_results:
logging.info('Saving results to %s', args.savefile)
with open(args.savefile, 'wb') as savefile:
pickle.dump(interesting, savefile)
# Ask for partitions
parts = {}
for scanner in scanners:
parts.update(scanner.get_partitions())
shorthands = list(enumerate(parts))
logging.info('%i partitions found.', len(parts))
while True:
print('\nWrite command ("help" for details):')
try:
command = input('> ').split(' ')
except (EOFError, KeyboardInterrupt):
print('')
exit(0)
cmd = command[0]
arguments = command[1:]
interpret(cmd, arguments, parts, shorthands, args.outputdir)
if __name__ == '__main__':
main()