| 1 | package MD5Check; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Digest::MD5; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | our @EXPORT = qw(init 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.09 | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | our $VERSION = '0.09'; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | use MD5Check; | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | ## 初始化目录md5值,参数为要监控的目录 | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | my $mydir=shift; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | print init($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 'init("/web")' >file | ||||||||||||||||||||||||||||||||||||||||||
| 48 | $ perl -MMD5Check -e 'print md5check(file)'.. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | my $DEBUG = 0; | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | sub md5_sum { | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my ( $file_name, $mode ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
md5_sum: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my ( $FD, $ctx, $md5 ); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | eval {open( $FD, $file_name ) or warn "Can't open $file_name !"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | unless($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $ctx = Digest::MD5->new; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | binmode($FD) if $mode; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $ctx->addfile($FD) or warn "$!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $md5 = $ctx->hexdigest; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | close $FD if $FD; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | return $md5; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | sub md5check { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | my $file = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
md5check: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | open( my $fd, '<', $file ) or warn "$file: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my $res .= $file . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | while (<$fd>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | next if /$file/; # ignore the recode file itself | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my ( $name, $sum ) = split /\s+/; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $name =~ s/^\*//; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if ( $sum eq md5_sum( $name, 1 ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $res .= "$name OK\n"; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | $res .= "$name FAILED\n"; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | close $fd; | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | return $res; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # 遍历目录计算md5值 | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | sub init { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # 仅仅用来打印init信息 | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | my ($fd) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my $res; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if ( -f $fd ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ( -T $fd ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | #print "按照文本模式进行计算MD5!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $md5value = md5_sum( $fd, 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | print "$fd\t$md5value\n" ; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | elsif ( -B $fd ) { | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | #print "二进制文件用binmod计算MD5!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my $md5value = md5_sum( $fd, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | print "$fd\t$md5value\n"; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | #print "其他文件,按照bimmod计算!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my $md5value = md5_sum( $fd, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | print "$fd\t$md5value\n"; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | elsif ( -d $fd ) { | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $file_md5; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | opendir( my $DH, $fd ) or warn "Can't open dir $fd: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | for ( readdir $DH ) { | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my $file = $fd . '/' . $_; | |||||||||||||||||||||||||||||||||||||||||
| 122 | # 上级目录..,本目录. 以及连接文件跳过 | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | next if ( $file =~ m{/.$} || $file =~ m{/..$} || -l $file ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$file =~ m[/.$] or $file =~ m[/..$] or -l $file
$file =~ m[/.$] or $file =~ m[/..$]
MC/DC0%0/3
$file =~ m[/.$] or $file =~ m[/..$] or -l $file
$file =~ m[/.$]
$file =~ m[/..$]
-l $file
Truth table0%0/4
A or B or C
A= $file =~ m[/.$]B= $file =~ m[/..$]C= -l $file
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | eval { init($file);}; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | closedir $DH; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | sub md5init { | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my ($fd,$outFD) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
md5init: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my $res; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ( -f $fd ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if ( -T $fd ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | #print "按照文本模式进行计算MD5!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $md5value = md5_sum( $fd, 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | print $outFD "$fd\t$md5value\n" ; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | elsif ( -B $fd ) { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | #print "二进制文件用binmod计算MD5!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my $md5value = md5_sum( $fd, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | print $outFD "$fd\t$md5value\n"; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 151 | #print "其他文件,按照bimmod计算!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my $md5value = md5_sum( $fd, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | print $outFD "$fd\t$md5value\n"; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | elsif ( -d $fd ) { | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | my $file_md5; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | print "init for all files $fd:\n"; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | opendir( my $DH, $fd ) or warn "Can't open dir $fd: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | for ( readdir $DH ) { | |||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | my $file = $fd . '/' . $_; | |||||||||||||||||||||||||||||||||||||||||
| 162 | # 上级目录..,本目录. 以及连接文件跳过 | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | next if ( $file =~ m{/.$} || $file =~ m{/..$} || -l $file ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$file =~ m[/.$] or $file =~ m[/..$] or -l $file
$file =~ m[/.$] or $file =~ m[/..$]
MC/DC0%0/3
$file =~ m[/.$] or $file =~ m[/..$] or -l $file
$file =~ m[/.$]
$file =~ m[/..$]
-l $file
Truth table0%0/4
A or B or C
A= $file =~ m[/.$]B= $file =~ m[/..$]C= -l $file
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | eval { md5init($file,$outFD);}; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | print "some wron for init $file\n "; | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | print "Debug::", $res if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | closedir $DH; | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | return $res; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 176 | __END__ | ||||||||||||||||||||||||||||||||||||||||||