File: tree.pl

package info (click to toggle)
tct 1.07-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,828 kB
  • ctags: 1,128
  • sloc: perl: 9,604; ansic: 4,861; makefile: 516; sh: 77
file content (236 lines) | stat: -rw-r--r-- 5,853 bytes parent folder | download | duplicates (4)
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
#
#  Grind through a directory; get all entries in a dir, then
# process all.  If run into dir, call itself again, ad infinitum.
# The first time this is called use a flag set to 1 to process
# the initial dir as well (instead of only subdirs).  Normally use 
# with flag==0.
#

require "crunch.pl";

sub process_dir {
local($dir, $flag) = @_;
local(@dirs);

print "going into process dir with \"$dir\"...\n" if $debug;

# others hate /proc...
next if ($proc_fs && ($dir =~ /^\/proc/));

print "processing dir $dir (in process_dir)\n" if $verbose;

if (defined($already_seen{$dir})) {
	print "We've already processed this directory ($dir), skipping...\n"
		if $debug;
	return;
	}

$already_seen{$dir} = 1;

if (!opendir(DIR, $dir)) {
	warn "Can't open $dir via opendir (in process_dir())\n" if $debug;
	return;
	}

#
# Suck in all the dir entries
@next = readdir(DIR);

#
#  If we're stupid enough to call this with a trailing slash in the
# filename, or if we call it with just "/", strip it off.
#
$dir =~ s@/$@@;

# 
# Go over each of the dir entries
while (($_ = pop(@next))) {
	print "next dir entry: $_\n" if $debug;

	# skip ".", "..", and null dir entries
	next if ($_ eq "\." || $_ eq "\.\." || ! $_);

	print "DIR: $dir\n" if $debug;

	#
	# Add the path to the filename
	$file = $_;	# (we'll need this in a bit)
	$_ = "$dir/$_";

	#
	#  What is the entry?  Gather it for processing based on type.
	# if (-f || (-d && $recursion))    { 
	if (-f || -d) {
		print "$_ Is -f\n" if $debug;
		# get rid of extra slashes...
		$_ =~ s@/+@/@g;
		push(@files, $_);
		#
		# Config file?
		if ($_ =~ /$conf_pattern/ && $do_file_collect) {
			print "$_ Is conf file\n" if $debug;
			# $conf_files{$_} = $file;
			$conf_files{$_} = $_;
			}
		}
	if    (-f) { # dir is used above, need to use if statement
		     # to make sense of it all, else files (-f) will
		     # get confused.  Poor programming... ;-(
		   }
	elsif (-d) { print "$_ Is -d\n" if $debug; push(@dirs, $_); }
	elsif (-l) { print "$_ Is -l\n" if $debug; push(@symlinks, $_);}
	elsif (-S) { print "$_ Is -S\n" if $debug; push(@sockets, $_);}
	elsif (-p) { print "$_ Is -p\n" if $debug; push(@pipes, $_);}
	elsif (-b) { print "$_ Is -b\n" if $debug; push(@blocks, $_);}
	elsif (-c) { print "$_ Is -c\n" if $debug; push(@characters, $_);}
	else       { print "$_ Is... what the heck?\n" if $debug; }
	}

closedir(DIR);

#
#  Process subdirs in the dir (if -R flag used in mactime); crunch, and 
# search it for more dirs...
#
if ($recursion) {
	for (@dirs) {
		# some systems hate symlinks...
		next if (!$follow_dir_sym_links && -l $_);

		# others hate /proc...
		next if ($proc_fs && /^\/proc/);

		print "Processing subdir $_\n" if $debug;
		&crunch($_);
		&process_dir($_, 0);
		}
	}

#
# Crunch all the little stuff.  Still need to fix symlinks
#
while (($_=pop(@files))) { print "file $_\n" if $debug; &crunch($_); }
while (($_=pop(@symlinks)))  { print "symlink $_\n"    if $debug; &crunch($_); }
while (($_=pop(@sockets)))   { print "socket $_\n"     if $debug; &crunch($_); }
while (($_=pop(@blocks)))    { print "block file $_\n" if $debug; &crunch($_); }
while (($_=pop(@characters))){ print "character $_\n"  if $debug; &crunch($_); }
while (($_=pop(@pipes)))     { print "pipe $_\n"       if $debug; &crunch($_); }

#
# If it's a config file, grab it and put it into the vault
#
# $debug = 1;
for $file (keys %conf_files) {

	print "$file Is conf file\n" if $debug;

	next unless $file && -f $file;

	next if (!$follow_file_sym_links && -l $file);

	&vault_file_cp($file);

	($st_dev,$st_ino,$st_mode,$st_nlink,$st_uid,$st_gid,$st_rdev,$st_size,
	$st_atime,$st_mtime,$st_ctime,$st_blksize,$st_blocks) = lstat($file);

	$ls = &faux_ls($file, $st_mode);
	}

# $debug = 0;
undef(%conf_files);

}

#
#  Do some preprocessing; get forensic info on tools you're using,
# the $PATH variable, dirs you want to examine while the rest of the 
# kit is grinding away, etc.  Tell the user when you're done processing this.
#
sub do_first_looks {

print "\nStarting preprocessing paths and filenames on $hostname...\n";

die "Can't open $CONFIG/paths.pl (in do_first_looks())!\n" unless open (PATHS, "$CONFIG/paths.pl");

print "\tpreprocessing $CONFIG/paths.pl\n" if $verbose;

while (<PATHS>) {
	next if (/^\s*#/ || /^\s*$/);
	# looks like...
	#	$LAST = "/bin/last";
	# or:
	#
	print "COMM: $_" if $debug;

	chop();

	($command = $_) =~ s@^.*\"(/.*)\".*$@$1@;
	print "\t==> $command\n" if $debug;

	if (!-f $command) {
		print "Can't find $command, moving on...\n" if $debug;
		next;
		}
	&crunch($command);
	}

print "finished the conf/paths.pl file...\n" if $debug;


#
#  Next process the $PATH var
#

print "Processing \$PATH elements...\n";
for (split(/:/, $ENV{'PATH'})) {
	print "$_\n";
	&process_dir($_, 0);
	}

#
#  Always start with current dir.  Nuke copying stuff temporarily
# by changing the pattern ;-)
#

$old_conf_pattern = $conf_pattern;
$conf_pattern = "XXXXXXXXXX__";

# chop($cwd = `pwd`);
chop($cwd = &command_to_string("pwd"));

print "\tProcessing dir $cwd\n";
&process_dir($cwd, 0);

# change pattern back!
$conf_pattern = $old_conf_pattern;

die "Can't open the first-looks file $toolkit (in do_first_looks())\n" 
	unless open(TOOLKIT, $toolkit);

print "\tpreprocessing $toolkit\n" if $verbose;

while (<TOOLKIT>) {
	next if (/^\s*#/ || /^\s*$/);

	chop;

	# prepend the $CORPSE value for dead stuff... normally this does 
	# nothing... testing still...
	# $_ = "$CORPSE/$_" if $CORPSE;

	# absolute path?
	if (!/^\// || !-d) { 
		print "Hey - the line \"$_\" isn't a directory or an absolute path!\n" if $debug;
		next;
		}
	print "Processing dir $_\n" if $debug;
	print "\tProcessing dir $_\n";
	&process_dir($_, 0);
	}

print "\nFinished preprocessing your toolkit... you may now use programs or\n";
print "examine files in the above directories\n\n";

}

1;