Guest User

findmd5elf

a guest
Dec 10th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.06 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use constant { STAT_DEVNO=>0, STAT_INO=>1, STAT_MODE=>2, STAT_LINKS=>3, STAT_UID=>4, STAT_GID=>5, STAT_DEVIDENT=>6, STAT_SIZE=>7, STAT_ATIME=>8, STAT_MTIME=>9, STAT_CTIME=>10, STAT_PREFBLKSZ=>11, STAT_BLOCKS=>12 };
  4. use Digest::MD5;
  5.  
  6. $SIG{'USR1'} = sub { print STDERR "Processing: $FILE\n"; };
  7.  
  8. sub walkdir
  9. {
  10.     my $dir = shift;
  11.     opendir my $dh, $dir;
  12.     while(my $entry = readdir $dh)
  13.     {
  14.         my $file = "$dir/$entry";
  15.         $FILE = $file;
  16.         if(-f $file)
  17.         {
  18.             my ($size) = (stat $file)[STAT_SIZE];
  19.             if($size > 1_000_000)
  20.             {
  21.                 if(open my $fh, '<', $file)
  22.                 {
  23.                     read $fh, my $sign, 4;
  24.                     if($sign eq "\x{7F}ELF")
  25.                     {
  26.                         seek $fh, 0, 0;
  27.                         my $ctx = Digest::MD5->new;
  28.                         $ctx->addfile($fh);
  29.                         my $digest = $ctx->hexdigest;
  30.                         if(grep { $digest eq $_ } @ARGV)
  31.                         {
  32.                             print "$digest $file\n";
  33.                         }
  34.                     }
  35.                     close $fh;
  36.                 }
  37.                 else
  38.                 {
  39.                     warn "Can not open $file: $!\n";
  40.                 }
  41.             }
  42.         }
  43.         elsif(-d $file and ! -l $file)
  44.         {
  45.             if($entry !~ /^\.\.?$/)
  46.             {
  47.                 walkdir($file);
  48.             }
  49.         }
  50.     }
  51.     closedir $dh;
  52. }
  53.  
  54. walkdir(shift @ARGV);
Advertisement
Add Comment
Please, Sign In to add comment