File: Makefile.PL

package info (click to toggle)
libfile-scan-perl 1.43-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 328 kB
  • ctags: 55
  • sloc: perl: 1,385; makefile: 14
file content (359 lines) | stat: -rw-r--r-- 9,504 bytes parent folder | download | duplicates (3)
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
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
# Last Modification: Wed May  4 12:45:56 WEST 2005

use strict;

my $debug = 0;
my $bufflen = 1024;
my $min_txt_size = 0;
my $signs = "files/signatures.txt";
my $susp = "files/suspicious.txt";

my @html_scripting = ("HTMLVBS", "HTMLJS");
my $script_lang = {
	'in'  => {
		"HTMLVBS" => "< *script[^>]+language *=[\"' ]*vbscript[\"']*[^>]*\>",
		"HTMLJS"  => "< *script[^>]*(language *=[\"' ]*javascript[\"']*)*[^>]*>",
	},
	'out' => {
		"BAT"     => "Batch",
		"JS"      => "JavaScript",
		"VBS"     => "VBScript",
		"TEST"    => "Test",
	},
	'mix' => {
		"MIXVBS"  => "HTMLVBS/VBS",
	},
};

my $app_signatures = {
	'4d5a'             => { sign     => '\x4d\x5a',
				subtypes => [
					{ type => '4d5a000000',
					  sign => '\x4d\x5a\x00\x00\x00', },
					{ type => '4d5a000001',
					  sign => '\x4d\x5a\x00\x00\x01', },
					{ type => '4d5a000002',
					  sign => '\x4d\x5a\x00\x00\x02', },
					{ type => '4d5a420002',
					  sign => '\x4d\x5a\x42\x00\x02', },
					{ type => '4d5a500002',
					  sign => '\x4d\x5a\x50\x00\x02', },
					{ type => '4d5a900003',
					  sign => '\x4d\x5a\x90\x00\x03', },
					{ type => '4d5a930001',
					  sign => '\x4d\x5a\x93\x00\x01', }, ],
				},
	'4d534654'         => { sign     => '\x4d\x53\x46\x54', },
	'49545346'         => { sign     => '\x49\x54\x53\x46', },
	'd0cf11e0a1b11ae1' => { sign     => '\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1', },
	'474554'           => { sign     => '\x47\x45\x54', },
	'e9'               => { sign     => '\xe9', },
	'7f454c46'         => { sign     => '\x7f\x45\x4c\x46', }
};

my %conversion = ();
my $firstbytes = 32;
my $hash = &load_signatures($signs);
my $linesusp = load_suspicious($susp);
my $code = &get_code($hash);
&make_module($code);

my @ppd;
if ($] >= 5.00503) {
	@ppd = (
		'AUTHOR'   => 'Henrique Dias <hdias@aesbuc.pt>',
		'ABSTRACT' => 'Extension for Scanning files for Viruses',
	);
}

WriteMakefile(
	'NAME'         => 'File::Scan',
	'DISTNAME'     => 'File-Scan',
	'VERSION_FROM' => 'Scan.pm', # finds $VERSION
	'PREREQ_PM'    => {}, # e.g., Module::Name => 1.1
	'dist'         => { 'COMPRESS' => 'gzip -9f', 'SUFFIX' => 'gz', },
	@ppd,
);

sub load_suspicious {
	my $file = shift;

	my @all = ();
	my $pattern = '(?<![\\{\\\\])([\\da-f]{2})(?!\\})';
	open(FILE, "<$file") or die("Can't open $file: $!");
	while(<FILE>) {
		next if(/^#/);
		chomp();
		my ($txt, $hex) = split(/::/);
		$hex =~ s/$pattern/\\x$1/og;
		push(@all, "\/$hex\/s");
	}
	close(FILE);
	return(join(" ||\n\t\t\t\t\t\t", @all));
}

sub load_signatures {
	my $file = shift;

	my $pattern = '(?<![\\{\\\\])([\\da-f]{2})(?!\\})';
	my %script = ();
	@script{keys(%{$script_lang->{'in'}})} = ();
	@script{keys(%{$script_lang->{'out'}})} = ();

	my $hash = {};
	open(FILE, "<$file") or die("Can't open $file: $!");
	while(<FILE>) {
		next if(/^#/);
		chomp;
		my @elem = split(/::/);
		scalar(@elem) == 5 or die("Wrong signature: $_");
		$elem[2] =~ s/\@/\\\@/g;
		$elem[3] =~ s/ +//g;
		$elem[3] =~ s/eq/\=\=/ig;
		$elem[3] =~ s/ne/\!\=/ig;
		$elem[3] =~ s/lt/\</ig;
		$elem[3] =~ s/le/\<\=/ig;
		$elem[3] =~ s/gt/\>/ig;
		$elem[3] =~ s/ge/\>\=/ig;
		$elem[3] =~ s/([\=\!\<\>][\=]?\d+)/\$total$1/g;
		$elem[3] =~ s/or/ \|\| /ig;
		$elem[3] =~ s/and/ \&\& /ig;
		if(exists($script{$elem[1]})) {
			my (@tmp) = ($elem[4] =~ /$pattern/og);
			my $len = int(length(join("", @tmp))/2);
			$min_txt_size = $len if($len < $min_txt_size || !$min_txt_size);
		}
		$elem[4] =~ s/$pattern/\\x$1/og;
		$hash->{$elem[1]}->{$elem[3]}->{$elem[2]} = $elem[4];
	}
	close(FILE);
	return($hash);
}

sub make_module {
	my $code = shift;

	open(BASEFILE, "<files/Scan.base") or die("Can't open files/Scan.base: $!");
	open(PMFILE, ">Scan.pm") or die("Can't open Scan.pm: $!");
	while(<BASEFILE>) {
		s/\$min_txt_size/$min_txt_size/;
		print PMFILE $_;
		if(/^__DATA__/) {
			print PMFILE $code;
		}
	}
	close(PMFILE);
	close(BASEFILE);
}

sub get_code {
	my $patterns = shift;

	my $today = &string_date();
	my $code = <<ENDOFCODE1;
# generated in: $today

sub get_app_sign {
	\$_ = pop;
ENDOFCODE1
	my $c = 0;
	for my $key (keys(%{$app_signatures})) {
		$c++;
		$conversion{$key} = $c;
		my $n = length($key)/2;
		$firstbytes = $n if($n > $firstbytes);
		my $sign = $app_signatures->{$key}->{sign};
		if(exists($app_signatures->{$key}->{subtypes})) {
			$code .= "\t/\^$sign/o and \$_[0] = $c;\n";
			my $sc = 0;
			for my $a (@{$app_signatures->{$key}->{subtypes}}) {
				$sc++;
				my $t = $a->{type};
				my $s = $a->{sign};
				$conversion{$t} = $sc;
				$code .= "\t/\^$s/o and return(\$_[1] = $sc);\n";
				my $n = length($t)/2;
				$firstbytes = $n if($n > $firstbytes);
			}
		} else {
			$code .= "\t/\^$sign/o and return(\$_[0] = $c);\n";
		}
	}
	$code .= <<ENDOFCODE2;
	return(0);
}

sub exception {
	\$_ = shift;
	return(/^%PDF-/o ? 1 : 0);
}

sub scan_text {
	my \$self = shift;
	my \$file = shift;

	my (\$buff, \$save, \$virus, \$script) = ("", "", "", "");
	my \$skip = 0;
	my \$size = $bufflen;
ENDOFCODE2

	$code .= ($] < 5.006) ? "\tsysopen(FILE, \$file, 0)" : "\topen(FILE, \"<\", \$file)";
	$code .= " or return(&_set_error(\"Can't open \$file: \$!\"));\n";

	$code .= <<ENDOFCODE3;
	LINE: while(read(FILE, \$buff, \$size)) {
		unless(\$save) {
			last LINE if(\$skip = &exception(\$buff));
			if(exists(\$self->{'callback'})) {
				if(my \$ret = \$self->{'callback'}->(\$file, \$buff) || "") {
					&ret_callback(\$ret);
					\$ret and last LINE;
				}
			}
		}
		study;
		\$_ = (\$save .= \$buff);
		unless(\$script) {
			TEST: {
				local \$_ = lc(\$save);
ENDOFCODE3
	for my $sl (@html_scripting) {
		$code .= "\t\t\t\t/" . $script_lang->{'in'}->{$sl} . "/os and \$script = \"$sl\", last TEST;\n";
	}
	$code .= "\t\t\t}\n\t\t}\n\t\tif(\$script) {\n";
	for my $sl (keys(%{$script_lang->{'in'}})) {
		if(scalar(keys(%{$patterns->{$sl}->{'0'}}))) {
			$code .= "\t\t\tif(\$script eq \"$sl\") {\n";
			while(my($key, $value) = each(%{$patterns->{$sl}->{'0'}})) {
				$code .= "\t\t\t\t/$value/s and \$virus = \"$key\", last LINE;\n";
			}
			$code .= "\t\t\t}\n";
		}
	}
	$code .= <<ENDOFCODE4;
			local \$_ = lc(\$save);
			/<\\/script[^>]*>/s and \$script = "";
		} else {
ENDOFCODE4
	for my $sl (keys(%{$script_lang->{'out'}})) {
		while(my($key, $value) = each(%{$patterns->{$sl}->{'0'}})) {
			$code .= "\t\t\t/$value/s and \$virus = \"$key\", last LINE;\n";
		}
	}
	$code .= "\t\t}\n";
	if(scalar(keys(%{$script_lang->{'mix'}}))) {
		$code .= "\t\tunless(\$script eq \"HTMLJS\") {\n";
		for my $sl (keys(%{$script_lang->{'mix'}})) {
			while(my($key, $value) = each(%{$patterns->{$sl}->{'0'}})) {
				$code .= "\t\t\t/$value/s and \$virus = \"$key\", last LINE;\n";
			}
		}
		$code .= "\t\t}\n";
	}
	$code .= <<ENDOFCODE5;
		\$save = substr(\$buff, (length(\$buff)/2));
	}
	close(FILE);
	&_set_skip(\$skip) if(\$skip);
	return(\$virus);
}

sub scan_binary {
	my \$self = shift;
	my \$file = shift;

	my (\$skip, \$suspicious, \$type, \$subtype, \$total) = (0, 0, 0, 0, 0);
	my (\$virus, \$buff, \$save) = ("", "", "");
	my \$size = $bufflen;
ENDOFCODE5

	$code .= ($] < 5.006) ? "\tsysopen(FILE, \$file, 0)" : "\topen(FILE, \"<\", \$file)";
	$code .= " or return(&_set_error(\"Can't open \$file: \$!\"));\n";

	$code .= <<ENDOFCODE6;
	binmode(FILE);
	LINE: while(read(FILE, \$buff, \$size)) {
		\$total += length(\$buff);
ENDOFCODE6
	$code .= "\t\tprint STDERR \"\$total\\n\";\n" if($debug);

	$code .= <<ENDOFCODE7;
		unless(\$save) {
			my \$begin = substr(\$buff, 0, $firstbytes, "");
			unless(length(\$begin) >= $firstbytes) { \$skip = 3; last LINE; }
			if(exists(\$self->{'callback'})) {
				if(my \$ret = \$self->{'callback'}->(\$file, \$begin) || "") {
					&ret_callback(\$ret);
					\$ret and last LINE;
				}
			}
			&get_app_sign(\$type, \$subtype, \$begin);
			unless(\$type) { \$skip = 1; last LINE; }
		}
		study;
		\$_ = (\$save .= \$buff);
		unless(\$suspicious) {
			local \$_ = lc(\$save);
			\$suspicious = 1 if($linesusp);
		}
ENDOFCODE7
	my $lcode = "";
	for my $key (keys(%{$app_signatures})) {
		my $c = $conversion{$key};
		$lcode .= ($lcode) ? "\t\t} els" : "\t\t";
		$lcode .= "if(\$type == $c) {\n";
		if(exists($app_signatures->{$key}->{subtypes})) {
			my $stcode = "";
			for my $a (@{$app_signatures->{$key}->{subtypes}}) {
				my $st = $a->{type};
				my $c = $conversion{$st};
				$stcode .= ($stcode) ? "\t\t\t} els" : "\t\t\t";
				$stcode .= "if(\$subtype == $c) {\n";
				$stcode .= &subgene($patterns->{$st}, "\t\t\t\t");
			}
			$lcode .= "$stcode\t\t\t\}\n" if($stcode);
		}
		$lcode .= &subgene($patterns->{$key}, "\t\t\t");
	}
	$code .= $lcode;
	$code .= <<ENDOFCODE8;
		}
		\$save = substr(\$buff, (length(\$buff)/2));
	}
	close(FILE);
	&_set_skip(\$skip) if(\$skip);
	\$suspicious = 0 if(\$virus);
	&_set_suspicious(\$suspicious) if(\$suspicious);
	return(\$virus);
}
ENDOFCODE8
	return($code);
}

sub subgene {
	my $pat = shift;
	my $tab = shift;

	my $code = "";
	for my $limit (keys(%{$pat})) {
		my $tabs = $tab;
		if($limit) {
			$code .= $tabs . "if($limit) \{\n";
			$tabs .= "\t";
		}
		while(my($key, $value) = each(%{$pat->{$limit}})) {
			$code .= $tabs . "/$value/s and \$virus = \"$key\", last LINE;\n";
		}
		$code .= "$tab\}\n" if($limit);
	}
	return($code);
}

sub string_date {
	my ($sec,$min,$hour,$mday,$mon,$year) = localtime();
	return sprintf("%04d/%02d/%02d %02d:%02d:%02d",
		$year + 1900, $mon + 1, $mday, $hour, $min, $sec);
}