File: recursivefiles2sha256sum.pl

package info (click to toggle)
hashcat 6.2.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 62,716 kB
  • sloc: lisp: 584,043; ansic: 372,246; perl: 24,890; sh: 3,927; python: 868; makefile: 726
file content (92 lines) | stat: -rwxr-xr-x 1,671 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/perl

##
## This script was created to be used in conjunction with Hashcat mode 29700 (Keepass in keyfile only mode).
## This can be useful if you have a partition and forgot which of the files was used as the keyfile.
##
## Example use (if your target drive is mounted to /mnt/sda1 and (optionally) another one to /mnt/sda2):
##
## $ perl recursivefiles2sha256sum /mnt/sda1 /mnt/sda2 > wordlist.dict
## $ ./hashcat kdbxdb.hash wordlist.dict
##
## Note that the redirection operator > also works on Windows cmd.exe.
## To run perl in Windows use strawberry perl

use strict;
use warnings;
use File::Find;
use Digest::SHA;
use XML::Simple;

my $sha = Digest::SHA->new ("sha256");

my @folders = @ARGV;

if (scalar @folders == 0)
{
  die ("use: $0 folder1 folder2...\n");
}

my $conf =
{
  wanted   => \&handlefile,
  no_chdir => 1,
};

find ($conf, @folders);

sub handlefile
{
  my $file = $_;

  return unless -f $file;
  return unless -r $file;

  open (my $fh, $file) or return;

  binmode $fh;

  my $nread = read ($fh, my $data, 5);

  close ($fh);

  my $xmlhash;

  if (($nread == 5) && ($data eq "<?xml"))
  {
    my $xml = eval { XMLin ($file) or die };

    if ($@)
    {
      # not interesting
    }
    else
    {
      if (exists $xml->{"Key"} && exists $xml->{"Key"}->{"Data"})
      {
        chomp $xml->{"Key"}->{"Data"};

        $xmlhash = $xml->{"Key"}->{"Data"};
      }
      else
      {
        # not interesting
      }
    }
  }

  if (defined $xmlhash)
  {
    print "$xmlhash\n";
  }
  else
  {
    my $sha_copy = $sha->clone;

    $sha_copy->addfile  ($file);

    my $digest = $sha_copy->hexdigest;

    print "$digest\n";
  }
}