line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MD5Check; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
16373
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
26
|
|
5
|
1
|
|
|
1
|
|
625
|
use utf8; |
|
1
|
|
|
|
|
14
|
|
|
1
|
|
|
|
|
4
|
|
6
|
1
|
|
|
1
|
|
28
|
use Digest::MD5; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
618
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
our @EXPORT = qw(md5init md5check); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
MD5Check - Use it for init Web files's md5 values of your site(or other dir), and check if it changed! |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
检查web目录(或者其他重要系统目录)md5值,当目录文件变化提醒。用于文件防篡改。 |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 VERSION |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Version 0.06 |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our $VERSION = '0.07'; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use MD5Check; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
## 初始化目录md5值,参数为要监控的目录 |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $mydir=shift; |
33
|
|
|
|
|
|
|
print md5init($mydir); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
## 对目录文件进行检查,只需输入之前保存的md5 文件值。 |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
use MD5Check; |
38
|
|
|
|
|
|
|
my $mydir=shift; |
39
|
|
|
|
|
|
|
print md5check($mydir); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# oneliner,perl单行程序实现功能。 |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
需要安装该模块,简单通过 cpanm MD5Check 安装。 |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
## 相关实例可以直接使用bin/下的init.pl 和 check.pl 单行执行 |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
$ perl -MMD5Check -e 'print md5init("/web")' >file |
48
|
|
|
|
|
|
|
$ perl -MMD5Check -e 'print md5check(file) perl -MMD5Check -e 'md5check(file)'.. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $DEBUG = 0; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub md5_sum { |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
0
|
0
|
|
my ( $file_name, $mode ) = @_; |
58
|
0
|
|
|
|
|
|
my ( $FD, $ctx, $md5 ); |
59
|
0
|
0
|
|
|
|
|
eval {open( $FD, $file_name ) or warn "Can't open $file_name !"; |
|
0
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
#$ctx = Digest::MD5->new; |
61
|
|
|
|
|
|
|
#binmode($FD) if $mode; |
62
|
|
|
|
|
|
|
#$ctx->addfile($FD) or warn "$!\n"; |
63
|
|
|
|
|
|
|
#$md5 = $ctx->hexdigest; |
64
|
|
|
|
|
|
|
}; |
65
|
0
|
0
|
|
|
|
|
unless($@) { |
66
|
0
|
|
|
|
|
|
$ctx = Digest::MD5->new; |
67
|
0
|
0
|
|
|
|
|
binmode($FD) if $mode; |
68
|
0
|
0
|
|
|
|
|
$ctx->addfile($FD) or warn "$!\n"; |
69
|
0
|
|
|
|
|
|
$md5 = $ctx->hexdigest; |
70
|
0
|
0
|
|
|
|
|
close $FD if $FD; |
71
|
0
|
|
|
|
|
|
return $md5; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub md5check { |
76
|
0
|
|
|
0
|
0
|
|
my $file = shift; |
77
|
0
|
0
|
|
|
|
|
open( my $fd, '<', $file ) or warn "$file: $!\n"; |
78
|
0
|
|
|
|
|
|
my $res .= $file . "\n"; |
79
|
0
|
|
|
|
|
|
while (<$fd>) { |
80
|
0
|
|
|
|
|
|
my ( $name, $sum ) = split /\s+/; |
81
|
0
|
|
|
|
|
|
$name =~ s/^\*//; |
82
|
0
|
0
|
|
|
|
|
if ( $sum eq md5_sum( $name, 1 ) ) { |
83
|
0
|
|
|
|
|
|
$res .= "$name OK\n"; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
else { |
86
|
0
|
|
|
|
|
|
$res .= "$name FAILED\n"; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
close $fd; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
return $res; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# 遍历目录计算md5值 |
96
|
|
|
|
|
|
|
sub md5init { |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
0
|
0
|
|
my $fd = shift; |
99
|
0
|
|
|
|
|
|
my $res; |
100
|
0
|
0
|
|
|
|
|
if ( -f $fd ) { |
|
|
0
|
|
|
|
|
|
101
|
0
|
0
|
|
|
|
|
if ( -T $fd ) { |
|
|
0
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
#print "按照文本模式进行计算MD5!\n"; |
104
|
0
|
|
|
|
|
|
my $md5value = md5_sum( $fd, 0 ); |
105
|
0
|
0
|
|
|
|
|
return "$fd\t$md5value\n" if $md5value ; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
elsif ( -B $fd ) { |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
#print "二进制文件用binmod计算MD5!\n"; |
110
|
0
|
|
|
|
|
|
my $md5value = md5_sum( $fd, 1 ); |
111
|
0
|
0
|
|
|
|
|
return "$fd\t$md5value\n" if $md5value; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
else { |
114
|
|
|
|
|
|
|
#print "其他文件,按照bimmod计算!\n"; |
115
|
0
|
|
|
|
|
|
my $md5value = md5_sum( $fd, 1 ); |
116
|
0
|
0
|
|
|
|
|
return "$fd\t$md5value\n" if $md5value; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
elsif ( -d $fd ) { |
120
|
0
|
|
|
|
|
|
my $file_md5; |
121
|
0
|
|
|
|
|
|
print "init for all files $fd:\n"; |
122
|
0
|
0
|
|
|
|
|
opendir( my $DH, $fd ) or warn "Can't open dir $fd: $!"; |
123
|
0
|
|
|
|
|
|
for ( readdir $DH ) { |
124
|
0
|
|
|
|
|
|
my $file = $fd . '/' . $_; |
125
|
|
|
|
|
|
|
# 上级目录..,本目录. 以及连接文件跳过 |
126
|
0
|
0
|
0
|
|
|
|
next if ( $file =~ m{/.$} || $file =~ m{/..$} || -l $file ); |
|
|
|
0
|
|
|
|
|
127
|
0
|
|
|
|
|
|
eval {$res .= md5init($file);}; |
|
0
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
|
if ($@) { |
129
|
0
|
|
|
|
|
|
print "some wron for init $file\n "; |
130
|
|
|
|
|
|
|
} |
131
|
0
|
0
|
|
|
|
|
print "Debug::", $res if $DEBUG; |
132
|
|
|
|
|
|
|
} |
133
|
0
|
|
|
|
|
|
closedir $DH; |
134
|
|
|
|
|
|
|
} |
135
|
0
|
|
|
|
|
|
return $res; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; |
139
|
|
|
|
|
|
|
__END__ |