未加星标

Compare generating executables for equivalence

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

I need to compare 2 executables and/or shared objects, compiled using the same compiler/flags and verify that they have not changed. We work in a regulated environment, so it would be really useful for testing purposes to isolate exactly what parts of the executable has changed.

Using MD5Sums/Hashes doesn't work due to the headers containing information about the file.

Does anyone know of a program or way to verify that 2 files are executionally the same even if they were built at a different time?

An interesting question. I have a similar problem on linux. Intrusion detection systems like OSSEC or tripwire may generate false positives if the hashsum of an executable changes all of a sudden. This may be nothing worse than the Linux "prelink" program patching the executable file for faster startups.

In order to compare two binaries (in the ELF format ), one can use the "readelf" executable and then "diff" to compare outputs. I'm sure there are refined solutions, but without further ado, a poor man's comparator in Perl:

#!/usr/bin/perl -w $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"; } # Identify sections in ELF @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; } } # Dump file header @lines = pipeIt("readelf --file-header --wide '$exe'"); print @lines; # Dump all interesting section headers @lines = pipeIt("readelf --all --wide '$exe'"); print @lines; # Dump individual sections as hexdump 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; }

Now you can run for example, on machine 1:

./checkexe.pl /usr/bin/curl > curl_machine1

And on machine 2:

./checkexe.pl /usr/bin/curl > curl_machine2

After having copypasted, SFTP-ed or NSF-ed (you don't use FTP, do you?) the files into the same filetree, compare the files:

diff --side-by-side --width=200 curl_machine1 curl_machine2 | less

In my case, differences exist in section ".gnu.conflict", ".gnu.liblist", ".got.plt" and ".dynbss", which might be ok for a "prelink" intervention, but in the code section, ".text", which would be a Bad Sign.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

tags: exe,lines,my,file,section,curl,executable
分页:12
转载请注明
本文标题:Compare generating executables for equivalence
本站链接:https://www.codesec.net/view/604896.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(7)