一个有趣的问题。我在Linux上有类似的问题。入侵检测系统(如OSSEC或tripwire)可能会产生误报,如果可执行文件的哈希值突然改变。这可能只是Linux的“prelink”程序为了更快的启动而修补可执行文件。
为了比较两个二进制文件(在
ELF格式中),可以使用“readelf”可执行文件,然后使用“diff”来比较输出。我相信有更精细的解决方案,但不多说了,以下是一个穷人版的Perl比较器:
$exe = $ARGV[0];
if (!$exe) {
die "Please give name of executable\n"
}
if (! -f $exe) {
die "Executable $exe not found or not a file\n";
}
if (! (`file '$exe'` =~ /\bELF\b.*?\bexecutable\b/)) {
die "file command says '$exe' is not an ELF executable\n";
}
@lines = pipeIt("readelf --wide --section-headers '$exe'");
@sections = ();
for my $line (@lines) {
if ($line =~ /^\s*\[\s*(\d+)\s*\]\s+(\S+)/) {
my $secnum = $1;
my $secnam = $2;
print "Found section $1 named $2\n";
push @sections, $secnam;
}
}
@lines = pipeIt("readelf --file-header --wide '$exe'");
print @lines;
@lines = pipeIt("readelf --all --wide '$exe'");
print @lines;
for my $section (@sections) {
@lines = pipeIt("readelf --hex-dump='$section' --wide '$exe'");
print @lines;
}
sub pipeIt {
my($cmd) = @_;
my $fh;
open ($fh,"$cmd |") or die "Could not open pipe from command '$cmd': $!\n";
my @lines = <$fh>;
close $fh or die "Could not close pipe to command '$cmd': $!\n";
return @lines;
}
现在你可以在机器1上运行,例如:
./checkexe.pl /usr/bin/curl > curl_machine1
并在第二台机器上:
./checkexe.pl /usr/bin/curl > curl_machine2
在将文件复制粘贴、通过SFTP或NSF传输(您不使用FTP,对吧?)到相同的文件树中后,请比较这些文件:
diff --side-by-side --width=200 curl_machine1 curl_machine2 | less
在我的情况下,".gnu.conflict"、".gnu.liblist"、".got.plt"和".dynbss"这些部分存在差异,这可能对于"prelink"干预来说是可以的,但在代码部分".text"中,这将是一个不好的迹象。