-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzero.scalpel.fuse.pl
342 lines (317 loc) · 10.5 KB
/
zero.scalpel.fuse.pl
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
#!/usr/bin/perl -w
#
# MIT License
#
# Copyright (c) 2018 Anthony Kava
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# zero.scalpel.fuse.pl FUSE driver in Perl for reading scalpel's
# audit.txt file and showing a file system
# that maps files it would have carved to a
# disk image -- this allows for zero-storage
# carving as you can browse and view files
# with only a 'scalpel -p' (preview, scalpel
# writes no files)
#
# Useful for me anyway, maybe not for you,
# but if so then enjoy.
#
# NO WARRANTY (MIT) -- PROOF OF CONCEPT
#
# Version: 0.00.2018.06.10.1301.poc
# @anthonykava aka Karver
#
# TODO: split lots of entries into sub-dirs
# TODO: do better
#
# Uses Fuse Perl module found thuswhere:
#
# http://search.cpan.org/dist/Fuse/
# https://github.com/dpavlin/perl-fuse.git
# (This script has roots in 'examples/example.pl' from the repo)
#
# Debian/Ubuntu can usually do: sudo apt install libfuse-perl
#
# Pre-reqs:
#
# Perl modules: Cwd, Fuse, POSIX
#
# Other: scalpel 1.60
# Debian/Ubuntu: sudo apt install scalpel
# See also: https://github.com/sleuthkit/scalpel
#
# Example use with an E01 and xmount (no root/sudo required):
# (xmount not required with a raw/dd disk image, of course)
#
# $ mkdir xm; xmount --in ewf IMAGE.E01 xm/
# $ scalpel -p xm/IMAGE.dd
# $ ./zero.scalpel.fuse.pl xm/IMAGE.dd
# $ ls mnt/
# $ #...profit... (use whatever tools you like)
# $ fusermount -u mnt/; rmdir mnt # when done
# $ fusermount -u xm/; rmdir xm # when done
use strict; # of course
use warnings; # helpful
use Cwd qw/cwd abs_path/; # for resolving relative paths
use Fuse qw/fuse_get_context/; # kinda the point
use POSIX qw/ENOENT EISDIR EINVAL/; # values for FUSE
my $usage = "\tUsage: $0 imgPath [auditPath] [mntPoint] [debug] [stayAttached]\n";
my $imgPath = shift()||''; # Disk image path
my $auditPath = shift()||'scalpel-output/audit.txt'; # Path to scalpel audit.txt
my $mntPoint = shift()||'mnt'; # Mount point
my $debug = shift()||0; # 0=none, 1=some
my $stayAttached = shift()||0; # 0=be daemon-like, 1=stay
# Init hashes to track things (not ideal, so let's call this beta)
my %fileStarts = ();
my %fileSizes = ();
my %fileExtens = ();
# Make $mntPoint if it doesn't exist, resolve to absolute path (FUSE wants)
mkdir($mntPoint) if !-e $mntPoint;
$mntPoint=abs_path($mntPoint) if -e $mntPoint;
# Check our parameters before proceeding
if(!-e $imgPath)
{
die("\nProblem with imgPath=$imgPath\n\n$usage\n");
}
if(!-e $auditPath)
{
die("\nProblem with auditPath=$auditPath\n\n$usage\n");
}
if(!-e $mntPoint || !-d $mntPoint)
{
die("\nProblem with mntPoint=$mntPoint\n\n$usage\n");
}
# Open image file -- we'll be seeking around inside
my $imgFh;
if(open($imgFh,$imgPath))
{
&debug("Launch: imgPath=$imgPath auditPath=$auditPath mntPoint=$mntPoint debug=$debug");
}
else
{
die("\nProblem OPENING imgPath=$imgPath\n\n$usage\n");
}
# Parse audit.txt and build our FS
if(open(my $fh,$auditPath))
{
my $goTime=0;
foreach(<$fh>)
{
chomp();
$goTime=0 if /Completed/;
if($goTime)
{
my($file,$start,$chop,$len,$image)=split(/\s+/,$_,5);
#File Start Chop Length Extracted From
#00000017.3GP5 487936 YES 2500000 image.reformatted.dd
#00000016.PNG 430592 YES 2500000 image.reformatted.dd
#00000015.JPG 1428805 YES 2500000 image.reformatted.dd
if($file && $start && $len)
{
$fileStarts{$file}=$start;
$fileSizes{$file}=$len;
my $exten='unknown';
$exten=$1 if $file=~/\.([^\.]+)$/;
$fileExtens{$exten}++;
&debug("audit.txt => file=$file start=$start chop=$chop len=$len image=$image exten=$exten");
}
}
elsif(/File\s+Start\s+Chop\s+Length\s+Extracted From/) # scalpel 1.60 specific probably
{
$goTime=1;
}
}
close($fh);
}
else
{
die("\nProblem OPENING auditPath=$auditPath\n");
}
# Build our file system layout as arrays of files in %fs (keys are dirs)
my %fs=('/' => []);
foreach my $ext (sort(keys(%fileExtens)))
{
push(@{ $fs{'/'} },$ext);
$fs{'/'.$ext}=[];
foreach my $file (sort(keys(%fileSizes)))
{
my $fileExt='unknown';
$fileExt=$1 if $file=~/^.+\.([^\.]+)$/;
push(@{ $fs{'/'.$ext} },$file) if $ext eq $fileExt;
}
}
#/*********************************************************************/
#/* Main Procedure */
#/*********************************************************************/
&daemonise() if !$stayAttached; # fork-off
Fuse::main( # FUSE tofu & potatoes
mountpoint => $mntPoint, # <-- mount point here
mountopts => 'ro,allow_other',
getattr => "main::e_getattr",
getdir => "main::e_getdir",
open => "main::e_open",
statfs => "main::e_statfs",
read => "main::e_read",
threaded => 0
);
close($imgFh); # theoretically close image file
#/*********************************************************************/
#/* Subroutines */
#/*********************************************************************/
# e_getattr($file) -- handler for returning FS attributes
sub e_getattr
{
my $path=shift()||'.';
my @ret=(-ENOENT()); # default no entity
&debug("e_getattr($path)");
my $blockSize=2**10 * 64; # our preferred block size
my $size=$blockSize; # default $size to 1 block
my $modes=(0100<<9) + 0444; # default mode to regular file 0444
my($dev,$ino,$rdev,$blocks,$gid,$uid,$nlink,$blksize)=(0,0,0,1,0,0,1,$blockSize); # init stat details
my($atime,$ctime,$mtime)=(time(),time(),time()); # MAC times are now
# If $path is meant to be a directory (e.g., /, ., /jpg)
my $dirTest='/'.$path;
if($path eq '/' || $path eq '.' || $fs{$path} || $fs{$dirTest}) # directory
{
$modes=(0040<<9)+0555; # 0555 mode dir
@ret=($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks);
}
else # regular file
{
$modes=(0100<<9)+0444; # 0444 mode file
my $file='';
$file=$1 if $path=~/^.+\/([^\/]+)$/;
$size=$fileSizes{$file} ? $fileSizes{$file} : 0;
@ret=($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks);
}
return(@ret);
}
# e_getdit($dir) -- handler for returning dir entries
sub e_getdir
{
my $dir=shift()||'/'; # / default, why not
my @ret=();
&debug("e_getdir($dir)");
if($dir eq '/' || $dir eq '.') # root
{
@ret=@{ $fs{'/'} };
}
elsif($dir=~/^\/?([^\/]+)$/) # first level (file extensions)
{
my $ext=$1;
$dir='/'.$dir if $dir!~/^\//;
debug("should show fs dir=$dir now, fs{$dir}=@{ $fs{$dir} }");
@ret=@{ $fs{$dir} };
}
return(@ret,0); # no, I don't know what the last element '0' means
}
# e_open($file) -- handler for opening files, returns file handle
# "VFS sanity check; it keeps all the necessary state, not much to do here."
sub e_open
{
my $file=shift()||'.';
my($flags,$fileinfo)=@_;
my @ret=(-ENOENT());
&debug("open called $file, $flags, $fileinfo");
# If $path is meant to be a directory (e.g., /, ., /jpg)
my $dirTest='/'.$file;
if($file eq '/' || $file eq '.' || $fs{$file} || $fs{$dirTest}) # directory
{
@ret=(-EISDIR()); # error: this is a dir, mate
}
elsif($file=~/^\/?[^\/]+\/([^\/]+)$/) # if $file is meant to be a file
{
my $file=$1;
@ret=(0,rand()) if $fileStarts{$file}; # random file handle for appearances
}
&debug("open ok for file=$file (handle $ret[1])") if $ret[1];
return(@ret);
}
# e_read($file) -- handler for reading files
# "return an error numeric, or binary/text string. (note: 0 means EOF, "0" will"
# "give a byte (ascii \"0\") to the reading program)"
sub e_read
{
my $path=shift()||'.';
my($buf,$off,$fh)=@_;
my $ret=-ENOENT();
&debug("read from $path, $buf \@ $off");
if($path=~/^\/?[^\/]+\/([^\/]+)$/) # if filename seems plausible
{
my $file=$1;
my $start=$fileStarts{$file};
my $size=$fileSizes{$file};
if(!$start || !$size)
{
$ret=-ENOENT(); # give FS error if this failed
}
elsif($off>$size)
{
$ret=-EINVAL(); # invalid FS error if reading beyond end
}
elsif($off==$size)
{
$ret=0; # return 0 if we're done reading
}
else
{
open($imgFh,$imgPath) if !$imgFh;
if($imgFh)
{
seek($imgFh,$start+$off,0);
read($imgFh,$ret,$buf);
&debug("\tread will return ".length($ret)." byte(s) as \$ret");
}
else
{
$ret=-ENOENT(); # give FS error if opening image failed
}
}
}
return($ret);
}
# e_statfs() -- gives FS stats to OS, but I don't understand it yet
sub e_statfs { return 255, 1, 1, 1, 1, 2 }
# daemonise([$logfile]) -- forks-off so we can play like a daemon
# stolen from 'examples/loopback.pl' from the GitHub repo
# "Required for some edge cases where a simple fork() won't do."
# "from http://perldoc.perl.org/perlipc.html#Complete-Dissociation-of-Child-from-Parent"
sub daemonise
{
my $logfile=shift()||cwd().'/log.zero.scalpel.fuse'; # log file in pwd unless passed
chdir("/")||die("can't chdir to /: $!");
open(STDIN,'<','/dev/null')||die("can't read /dev/null: $!"); # redir STDIN (/dev/null)
open(STDOUT,'>>',$logfile)||die("can't open logfile: $!"); # redir STDOUT to log file
defined(my $pid=fork())||die("can't fork: $!"); # when you come to a fork()...
exit(0) if $pid; # "non-zero now means I am the parent"
# (setsid() != -1) || die "Can't start a new session: $!"; # (didn't use this)
open(STDERR,'>&',\*STDOUT)||die("can't dup stdout: $!"); # STDERR to STDOUT
}
# debug($msg[,$lvl=1]) -- print timestamp and $msg if $debug>=$lvl
sub debug
{
my $msg=shift()||'';
my $lvl=shift()||1;
if($debug>=$lvl)
{
chomp($msg);
print scalar(localtime())."\t".$msg."\n" if $msg;
}
}