File: Hash.pm

package info (click to toggle)
autopsy 2.08-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,460 kB
  • ctags: 268
  • sloc: perl: 11,817; sh: 644; makefile: 61
file content (950 lines) | stat: -rw-r--r-- 28,920 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
#
# $Date: 2006/05/08 18:46:57 $
#
# Hash database and calculation functions
#
# Brian Carrier [carrier@sleuthkit.org]
# Copyright (c) 2001-2005 by Brian Carrier.  All rights reserved
#
# This file is part of the Autopsy Forensic Browser (Autopsy)
#
# Autopsy is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Autopsy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Autopsy; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR ANY PARTICULAR PURPOSE.
# IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA, OR PROFITS OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package Hash;

$Hash::DB_MANAGER  = 1;
$Hash::DB_LOOKUP   = 2;
$Hash::DB_INDEX    = 3;
$Hash::IMG_VERIFY  = 4;
$Hash::IMG_CALC    = 5;
$Hash::IMG_LIST_FR = 6;
$Hash::IMG_LIST    = 7;
$Hash::BLANK       = 8;

sub main {

    return if ($::LIVE == 1);

    Args::check_view();
    my $view = Args::get_view();

    if ($view == $Hash::BLANK) {
        blank();
        return 0;
    }
    elsif ($view == $Hash::DB_MANAGER) {
        return db_manager();
    }
    elsif ($view == $Hash::DB_LOOKUP) {
        return db_lookup();
    }
    elsif ($view == $Hash::DB_INDEX) {
        return db_index();
    }
    elsif ($view == $Hash::IMG_LIST_FR) {
        return img_list_fr();
    }
    elsif ($view == $Hash::IMG_LIST) {
        return img_list();
    }

    Args::check_vol('vol');
    if ($view == $Hash::IMG_CALC) {
        return img_calc();
    }
    elsif ($view == $Hash::IMG_VERIFY) {
        return img_verify();
    }
    else {
        Print::print_check_err("Invalid Hash View");
    }

}

sub index_md5sum {
    my $db = shift;
    local *OUT;
    Exec::exec_pipe(*OUT, "'$::TSKDIR/hfind' -i md5sum '$db'");
    while ($_ = Exec::read_pipe_line(*OUT)) {
        print "$_<br>\n";
    }
    close(OUT);
}

sub index_nsrl {
    local *OUT;
    Exec::exec_pipe(*OUT, "'$::TSKDIR/hfind' -i nsrl-md5 '$::NSRLDB'");
    while ($_ = Exec::read_pipe_line(*OUT)) {
        print "$_<br>\n";
    }
    close(OUT);
}

# Manager/status Window from HOST Manager
sub db_manager {
    Print::print_html_header("Hash Database Manager");

    print <<EOF;

Hash databases allow Autopsy to quickly identify known files.  This includes
files that are known to be good and those that are known to be bad.  The
'hfind' tool is used to lookup entries in the databases and it needs an
index file for each database.  This window allows one to re-index the
database after it has been updated.  

<p>
To edit the location of the databases, you must manually edit the 
<tt>host.aut</tt> file in the host directory.  

<hr>
<center>
<img src=\"pict/hashdb_h_alert.jpg\" alt=\"Alert Database\" border=\"0\">
</center>
<p><b>Overview</b><br>
These files are known to be <U>bad</U> and are the ones that you want to
know about if they are in the image you are analyzing.  For example,
this database would include hashes of known attacker tools, rootkits,
or photographs.  

EOF
    print "<p><b>Details</b><br>\n";
    if ($Caseman::alert_db eq "") {
        print "Location: <tt>Not Configured</tt><br>\n";
    }
    elsif (-e "$Caseman::alert_db") {
        print "Location: <tt>$Caseman::alert_db</tt><br>\n";
        if (-e "$Caseman::alert_db" . "-md5.idx") {
            print "Status: MD5 Index File Exists<br>\n";
        }
        else {
            print "Status: Database has not been MD5 indexed<br>\n";
        }

        # Index Button
        print "<p><a href=\"$::PROGNAME?mod=$::MOD_HASH&"
          . "view=$Hash::DB_INDEX&hash_alert=1&$Args::baseargs\">"
          . "<img src=\"pict/but_indexdb.jpg\" alt=\"Index DB\" "
          . "width=116 height=20 border=\"0\">"
          . "</a>\n";

        # Lookup Button
        if (-e "$Caseman::alert_db" . "-md5.idx") {
            print "<p><b>Lookup</b><br>"
              . "<form action=\"$::PROGNAME\" method=\"get\">"
              . "<input type=\"hidden\" name=\"mod\" value=\"$::MOD_HASH\">\n"
              . "<input type=\"hidden\" name=\"view\" value=\"$Hash::DB_LOOKUP\">\n"
              . "<input type=\"hidden\" name=\"hash_alert\" value=\"1\">\n"
              . Args::make_hidden()
              . "<table cellspacing=\"10\" cellpadding=\"2\">\n<tr>\n"
              . "<td align=\"left\">Enter MD5 Value: "
              . "<input type=\"text\" name=\"md5\" size=40 maxlength=32></td>\n"
              . "<td align=\"left\">"
              . "<input type=\"image\" src=\"pict/but_lookup.jpg\" alt=\"Ok\" "
              . "width=116 height=20 border=\"0\">\n"
              . "</td></tr>\n</table>\n"
              . "</form>";
        }
    }
    else {
        print "Location: <tt>$Caseman::alert_db</tt><br>\n"
          . "ERROR: Database not found<br>\n";
    }

    print <<EOF2;
<hr>
<center>
<img src=\"pict/hashdb_h_ig.jpg\" alt=\"Ignore Database\" border=\"0\">
</center>
<p><b>Overview</b><br>
These files are known to be <U>good</U> and are the ones that you
can ignore if they are found in the image you are analyzing.  For
example, this database would include hashes of known system binaries
and other documents that you do not want to waste time on when running
'sorter' or files that you want to confirm were not modified by an 
attacker.  

EOF2

    print "<p><b>Details</b><br>\n";
    if ($Caseman::exclude_db eq "") {
        print "Location: <tt>Not Configured</tt><br>\n";
    }
    elsif (-e "$Caseman::exclude_db") {
        print "Location: <tt>$Caseman::exclude_db</tt><br>\n";
        if (-e "$Caseman::exclude_db" . "-md5.idx") {
            print "Status: MD5 Index File Exists<br>\n";
        }
        else {
            print "Status: Database has not been MD5 indexed<br>\n";
        }

        # Index Button
        print "<p><a href=\"$::PROGNAME?mod=$::MOD_HASH&view=$Hash::DB_INDEX&"
          . "hash_exclude=1&$Args::baseargs\">"
          . "<img src=\"pict/but_indexdb.jpg\" alt=\"Index DB\" "
          . "width=116 height=20 border=\"0\">"
          . "</a>\n";

        # Lookup Button
        if (-e "$Caseman::exclude_db" . "-md5.idx") {
            print "<p><b>Lookup</b><br>"
              . "<form action=\"$::PROGNAME\" method=\"get\">"
              . "<input type=\"hidden\" name=\"mod\" value=\"$::MOD_HASH\">\n"
              . "<input type=\"hidden\" name=\"view\" value=\"$Hash::DB_LOOKUP\">\n"
              . "<input type=\"hidden\" name=\"hash_exclude\" value=\"1\">\n"
              . Args::make_hidden()
              . "<table cellspacing=\"10\" cellpadding=\"2\">\n<tr>\n"
              . "<td align=\"left\">Enter MD5 Value: "
              . "<input type=\"text\" name=\"md5\" size=40 maxlength=32></td>\n"
              . "<td align=\"left\">"
              . "<input type=\"image\" src=\"pict/but_lookup.jpg\" alt=\"Ok\" "
              . "width=116 height=20 border=\"0\">\n"
              . "</td></tr>\n</table>\n"
              . "</form>";
        }
    }
    else {
        print "Location: <tt>$Caseman::exclude_db</tt><br>\n"
          . "ERROR: Database not found<br>\n";
    }

    print <<EOF3;
<hr>
<center>
<img src=\"pict/hashdb_h_nsrl.jpg\" alt=\"NSRL Database\" border=\"0\">
</center>
<p><b>Overview</b><br>
These files are known to be <U>good</U> and <U>bad</U>.  It is currently
difficult to distinguish between known good and known bad and therefore
the NSRL is no longer used much in Autopsy until a better solution can
be found.

EOF3

    print "<p><b>Details</b><br>\n";
    if ($::NSRLDB eq "") {
        print "Location: <tt>Not Configured</tt><br>\n";
    }
    elsif (-e "$::NSRLDB") {
        print "Location: <tt>$::NSRLDB</tt><br>\n";
        if (-e "$::NSRLDB" . "-md5.idx") {
            print "Status: MD5 Index File Exists<br>\n";
        }
        else {
            print "Status: Database has not been MD5 indexed<br>\n";
        }

        # Index Button
        print "<p><a href=\"$::PROGNAME?mod=$::MOD_HASH&view=$Hash::DB_INDEX&"
          . "hash_nsrl=1&$Args::baseargs\">"
          . "<img src=\"pict/but_indexdb.jpg\" alt=\"Index DB\" "
          . "width=116 height=20 border=\"0\">"
          . "</a>\n";

        # Lookup Button
        if (-e "$::NSRLDB" . "-md5.idx") {
            print "<p><b>Lookup</b><br>"
              . "<form action=\"$::PROGNAME\" method=\"get\">"
              . "<input type=\"hidden\" name=\"mod\" value=\"$::MOD_HASH\">\n"
              . "<input type=\"hidden\" name=\"view\" value=\"$Hash::DB_LOOKUP\">\n"
              . "<input type=\"hidden\" name=\"hash_nsrl\" value=\"1\">\n"
              . Args::make_hidden()
              . "<table cellspacing=\"10\" cellpadding=\"2\">\n<tr>\n"
              . "<td align=\"left\">Enter MD5 Value: "
              . "<input type=\"text\" name=\"md5\" size=40 maxlength=32></td>\n"
              . "<td align=\"left\">"
              . "<input type=\"image\" src=\"pict/but_lookup.jpg\" "
              . "alt=\"Lookup\" width=116 height=20 border=0>\n"
              . "</td></tr>\n</table>\n"
              . "</form>";
        }
    }
    else {
        print "Location: <tt>$::NSRLDB</tt><br>\n"
          . "ERROR: Database not found<br>\n";
    }

    print <<EOF4;

<hr><center>
<table width=600 cellspacing=\"0\" cellpadding=\"2\">
<tr>
  <td align=center>
    <a href=\"$::PROGNAME?mod=$::MOD_CASEMAN&view=$Caseman::VOL_OPEN&$Args::baseargs\">
    <img src=\"pict/menu_b_close.jpg\" alt=\"Close\" width=\"167\" height=20 border=\"0\">
    </a>
  </td>
  <td align=center>
    <a href=\"$::HELP_URL\" target=\"_blank\">
    <img src=\"pict/menu_b_help.jpg\" alt=\"Help\" 
    width=\"167\" height=20 border=0>
    </a>
  </td>
</tr>
</table>
EOF4

    Print::print_html_footer();
    return 0;
}

sub db_index {
    Print::print_html_header("Hash Database Indexing");

    if (   (exists $Args::args{'hash_exclude'})
        && ($Args::args{'hash_exclude'} == 1)
        && ($Caseman::exclude_db ne ""))
    {
        Print::log_host_info("Exclude Database Re-Indexed");
        print "<hr><b>Exclude Database Indexing</b><p>\n";
        index_md5sum($Caseman::exclude_db);
    }

    if (   (exists $Args::args{'hash_alert'})
        && ($Args::args{'hash_alert'} == 1)
        && ($Caseman::alert_db ne ""))
    {
        Print::log_host_info("Alert Database Re-Indexed");
        print "<hr><b>Alert Database Indexing</b><p>\n";
        index_md5sum($Caseman::alert_db);
    }

    if (   (exists $Args::args{'hash_nsrl'})
        && ($Args::args{'hash_nsrl'} == 1)
        && ($::NSRLDB ne ""))
    {
        Print::log_host_info("NSRL Database Re-Indexed");
        print "<hr><b>NSRL Database Indexing</b><p>\n";
        index_nsrl();
    }

    print "<p>Indexing Complete<br>\n"
      . "<hr><p>\n<a href=\"$::PROGNAME?mod=$::MOD_HASH&view=$Hash::DB_MANAGER&"
      . "$Args::baseargs\">\n"
      . "<img src=\"pict/menu_b_hashdb.jpg\" width=\"167\" "
      . "height=20 alt=\"Hash Databases\" border=\"0\"></a>\n";

    Print::print_html_footer();
    return 0;
}

# Lookup hashes in database
sub db_lookup {
    Print::print_html_header("Hash Database Lookup");

    unless ((exists $Args::args{'md5'})
        && ($Args::args{'md5'} =~ /^$::REG_MD5$/o))
    {
        Print::print_err("Invalid MD5 Argument");
    }

    if (   (exists $Args::args{'hash_nsrl'})
        && ($Args::args{'hash_nsrl'} == 1)
        && ($::NSRLDB ne ""))
    {
        print "<hr><b>NSRL Lookup</b><p>\n";

        if (-e "$::NSRLDB") {
            local *OUT;
            Exec::exec_pipe(*OUT,
                "'$::TSKDIR/hfind' '$::NSRLDB' $Args::args{'md5'}");
            print "$_<br>\n" while ($_ = Exec::read_pipe_line(*OUT));
            close(OUT);
            Print::log_host_inv("NSRL Lookup ($Args::args{'md5'})");
        }
        else {
            print "NSRL Database Missing<br>\n";
            Print::log_host_inv(
                "NSRL Lookup ($Args::args{'md5'}) - Database Missing");
        }
    }

    if (   (exists $Args::args{'hash_exclude'})
        && ($Args::args{'hash_exclude'} == 1)
        && ($Caseman::exclude_db ne ""))
    {
        print "<hr><b>Exclude Database Lookup</b><p>\n";

        if (-e "$Caseman::exclude_db") {
            local *OUT;
            Exec::exec_pipe(*OUT,
                "'$::TSKDIR/hfind' '$Caseman::exclude_db' $Args::args{'md5'}");
            print "$_<br>\n" while ($_ = Exec::read_pipe_line(*OUT));
            close(OUT);
            Print::log_host_inv("Exclude Database Lookup ($Args::args{'md5'})");
        }
        else {
            print "Exclude Database Missing<br>\n";
            Print::log_host_inv(
"Exclude Database Lookup ($Args::args{'md5'}) - Database Missing"
            );
        }
    }

    if (   (exists $Args::args{'hash_alert'})
        && ($Args::args{'hash_alert'} == 1)
        && ($Caseman::alert_db ne ""))
    {
        print "<hr><b>Alert Database Lookup</b><p>\n";

        if (-e "$Caseman::alert_db") {
            local *OUT;
            Exec::exec_pipe(*OUT,
                "'$::TSKDIR/hfind' '$Caseman::alert_db' $Args::args{'md5'}");
            print "$_<br>\n" while ($_ = Exec::read_pipe_line(*OUT));
            close(OUT);
            Print::log_host_inv("Alert Database Lookup ($Args::args{'md5'})");
        }
        else {
            print "Alert Database Missing<br>\n";
            Print::log_host_inv(
                "Alert Database Lookup ($Args::args{'md5'}) - Database Missing"
            );
        }
    }

    print "<hr><p>\n"
      . "If any of the hash databases need to be re-indexed, use the "
      . "<U>Hash Database Manager</U><p>"
      . "<a href=\"$::PROGNAME?mod=$::MOD_HASH&view=$Hash::DB_MANAGER&"
      . "$Args::baseargs\" target=\"_top\">\n"
      . "<img src=\"pict/menu_b_hashdb.jpg\" width=\"167\" "
      . "height=20 alt=\"Hash Databases\" border=\"0\"></a>\n";

    Print::print_html_footer();
    return 0;
}

############ INTEGRITY CHECKS ##################

# Special view for printing integrity check menu
# We show any file that we have a reference for

# pass the md5 hash (from md5.txt) and then the sorted array
sub int_menu_print {
    my %md5s = %{$_[0]};
    my @sort = @{$_[1]};

    for (my $i = 0; $i <= $#sort; $i++) {

        print
"<tr><td align=\"right\"><tt><b>$Caseman::vol2sname{$sort[$i]}</b></tt></td>\n";

        # It already exists, so make verify button
        if (exists $md5s{$sort[$i]}) {
            print "<td><tt>$md5s{$sort[$i]}</tt></td><td>"
              . "<form action=\"$::PROGNAME\" method=\"get\" target=\"cont\">\n"
              . "<input type=\"hidden\" name=\"vol\" value=\"$sort[$i]\">\n"
              . Args::make_hidden()
              . "<input type=\"hidden\" name=\"mod\" value=\"$::MOD_HASH\">\n"
              . "<input type=\"hidden\" name=\"view\" value=\"$Hash::IMG_VERIFY\">\n"
              . "<input type=\"image\" src=\"pict/int_b_valid.jpg\" "
              . "alt=\"Validate\" border=\"0\">\n"
              . "</form></td></tr>\n";
        }

        # we currenly only support integrity for raw and split image formats
        elsif (($Caseman::vol2itype{$sort[$i]} ne "raw")
            && ($Caseman::vol2itype{$sort[$i]} ne "split"))
        {
            print
"<td colspan=2>Integrity checks for image type $Caseman::vol2itype{$sort[$i]} not yet supported</td></tr>\n";
        }

        # Generate New button
        else {
            print "<td>&nbsp;</td><td>"
              . "<form action=\"$::PROGNAME\" method=\"get\" target=\"cont\">\n"
              . "<input type=\"hidden\" name=\"vol\" value=\"$sort[$i]\">\n"
              . Args::make_hidden()
              . "<input type=\"hidden\" name=\"mod\" value=\"$::MOD_HASH\">\n"
              . "<input type=\"hidden\" name=\"view\" value=\"$Hash::IMG_CALC\">\n"
              . "<input type=\"image\" src=\"pict/int_b_calc.jpg\" "
              . "alt=\"Calculate\" border=\"0\">\n"
              . "</form></td></tr>\n";
        }
    }

    return;
}

# Create a frame with two rows, one with the list of images to check
# and then the bottom actually does it.
sub img_list_fr {
    Print::print_html_header_frameset(
        "$Args::args{'case'}:$Args::args{'host'} Integrity Check");

    print "<frameset rows=\"80%,20%\">\n";

    # Block List
    print "<frame src=\"$::PROGNAME?mod=$::MOD_HASH&view=$Hash::IMG_LIST&"
      . "$Args::baseargs\">\n"
      . "<frame src=\"$::PROGNAME?mod=$::MOD_HASH&view=$Hash::BLANK&"
      . "$Args::baseargs\" name=\"cont\">\n"
      . "</frameset>\n";

    Print::print_html_footer_frameset();
    return 0;
}

# Reads the MD5 file to fill in the MENU list for the integrity
# check mode
sub img_list {
    Print::print_html_header("Image Integrity Menu");

    my %md5s;
    my @dls;
    my @str;
    my @img;
    my @body;
    my @tl;

    # Read the known values if the file exists
    if (open(FILE, "$::host_dir" . "/md5.txt")) {

        # Read the md5 values into a hash
        while (<FILE>) {
            s/^\s+//;
            s/\s+$//;

            if (/($::REG_MD5)\s+(.*)/o) {
                $md5s{"$2"} = $1;
                $md5s{"$2"} =~ tr/[a-f]/[A-F]/;
            }
            else {
                print "Error reading line $. of md5.txt: $_<br>\n";
                return 1;
            }
        }
        close(FILE);
    }

    # sort the images into the different types
    foreach my $k (keys %Caseman::vol2cat) {
        if ($Caseman::vol2cat{$k} eq "image") {
            push @img, $k;
        }
        elsif ($Caseman::vol2ftype{$k} eq "dls") {
            push @dls, $k;
        }
        elsif ($Caseman::vol2ftype{$k} eq "strings") {
            push @str, $k;
        }
        elsif ($Caseman::vol2ftype{$k} eq "body") {
            push @body, $k;
        }
        elsif ($Caseman::vol2ftype{$k} eq "timeline") {
            push @tl, $k;
        }
    }

    print "<center><table cellspacing=\"10\" cellpadding=\"2\">";

    #  image files
    if (scalar @img > 0) {
        print "<tr><th colspan=3>"
          . "<img src=\"pict/int_h_img.jpg\" alt=\"Image Files\">"
          . "</th></tr>\n";
        my @sort = sort { $a cmp $b } @img;
        int_menu_print(\%md5s, \@sort);
    }

    # Unallocated (dls) images
    if (scalar @dls > 0) {
        print "<tr><th colspan=3>&nbsp;</th></tr>\n"
          . "<tr><th colspan=3>"
          . "<img src=\"pict/int_h_unalloc.jpg\" alt=\"Unallocated Data Files\">"
          . "</th></tr>\n";
        my @sort = sort { $a cmp $b } @dls;
        int_menu_print(\%md5s, \@sort);
    }

    # Strings files (of dls or fs images)
    if (scalar @str > 0) {
        print "<tr><th colspan=3>&nbsp;</th></tr>\n"
          . "<tr><th colspan=3>"
          . "<img src=\"pict/int_h_str.jpg\" alt=\"Strings of Images\">"
          . "</th></tr>\n";
        my @sort = sort { $a cmp $b } @str;
        int_menu_print(\%md5s, \@sort);

    }

    # timeline body files
    if (scalar @body > 0) {
        print "<tr><th colspan=3>&nbsp;</th></tr>\n"
          . "<tr><th colspan=3>"
          . "<img src=\"pict/int_h_data.jpg\" alt=\"Timeline Data Files\">"
          . "</th></tr>\n";
        my @sort = sort { $a cmp $b } @body;
        int_menu_print(\%md5s, \@sort);
    }

    # timeline files
    if (scalar @tl > 0) {
        print "<tr><th colspan=3>&nbsp;</th></tr>\n"
          . "<tr><th colspan=3>"
          . "<img src=\"pict/int_h_tl.jpg\" alt=\"Timelines\">"
          . "</th></tr>\n";
        my @sort = sort { $a cmp $b } @tl;
        int_menu_print(\%md5s, \@sort);
    }

    print <<EOF;
</table>
<p>
<table cellspacing=20 width=600 cellpadding=2>
<tr>
  <td><a href=\"$::PROGNAME?$Args::baseargs&mod=$::MOD_CASEMAN&view=$Caseman::VOL_OPEN\" target=\"_top\">
    <img src=\"pict/menu_b_close.jpg\" alt=\"close\" 
    width=\"167\" height=20 border=\"0\">
  </a>
  </td>
  <td><a href=\"$::PROGNAME?$Args::baseargs&mod=$::MOD_HASH&view=$Hash::IMG_LIST_FR\" target=\"_top\">
    <img src=\"pict/menu_b_ref.jpg\" alt=\"Refresh\" 
    width=\"167\" height=20 border=\"0\">
  </a>
  </td>
  <td align=center>
    <a href=\"$::HELP_URL\" target=\"_blank\">
    <img src=\"pict/menu_b_help.jpg\" alt=\"Help\" 
    width=\"167\" height=20 border=0>
  </a>
  </td>
</tr>
</table>

EOF
    Print::print_html_footer();
    return 0;
}

# Pass the relative path (images/xyz) of the file.  The MD5 is
# returned (or NULL) (in all caps)
sub lookup_md5 {
    my $vol = shift;
    my $md5 = "";

    my $md5_file = "$::host_dir/md5.txt";

    if (-e "$md5_file") {
        unless (open(FILE, $md5_file)) {
            print "Error opening $md5_file<br>\n";
            return "";
        }

        while (<FILE>) {
            s/^\s+//;
            s/\s+$//;

            if (/($::REG_MD5)\s+(.*)/o) {
                my $m = $1;
                if ($2 =~ /$vol$/) {
                    $md5 = $m;
                    $md5 =~ tr/[a-f]/[A-F]/;
                    last;
                }
            }
            else {
                print "Error reading line $. of $md5_file: $_<br>\n";
                return "";
            }
        }
        close(FILE);
    }

    return $md5;
}

sub img_verify {

    Print::print_html_header("Image Integrity Check");

    my $vol = Args::get_vol('vol');

    my $md5 = lookup_md5($vol);

    if ($md5 eq "") {
        print
"The MD5 value of <tt>$Caseman::vol2sname{$vol}</tt> was not found<br>"
          . "It can be calculated by pressing the button below."
          . "<br><br>\n<form action=\"$::PROGNAME\" method=\"get\">\n"
          . "<input type=\"hidden\" name=\"mod\" value=\"$::MOD_HASH\">\n"
          . "<input type=\"hidden\" name=\"view\" value=\"$Hash::IMG_CALC\">\n"
          . "<input type=\"hidden\" name=\"vol\" value=\"$vol\">\n"
          . Args::make_hidden()
          . "<input type=\"image\" src=\"pict/int_b_calc.jpg\" "
          . "alt=\"Calculate\" border=\"0\">\n</form>";
        return 1;
    }

    Print::log_host_inv("$Caseman::vol2sname{$vol}: Checking image integrity");

    print "Original MD5: <tt>$md5</tt><br>\n";

    # We have the original value, now get the new one
    my $img = $Caseman::vol2path{$vol};

    my $cur;
    if ($Caseman::vol2itype{$vol} eq "split") {
        $cur = calc_md5_split($img);
    }
    else {
        $cur = calc_md5($img);
    }

    if ($cur =~ /^$::REG_MD5$/o) {
        print "Current MD5: <tt>$cur</tt><br><br>\n";

        if ($cur eq $md5) {
            print "Pass<br>\n";
            Print::log_host_inv(
                "$Caseman::vol2sname{$vol}: Image integrity check PASSED");
        }
        else {
            print "<font color=\"$::DEL_COLOR[0]\">Fail: Restore from backup"
              . "<br>\n";

            Print::log_host_inv(
                "$Caseman::vol2sname{$vol}: Image integrity check FAILED");
        }
        Print::print_html_footer();
        return 0;
    }
    else {
        print "$cur<br>\n";
        Print::print_html_footer();
        return 1;
    }
}

# Calculate the MD5 value of a file (given the full path)
# return the value in upper case
# This one supports only single files - not split volumes
sub calc_md5 {
    my $img = shift;

    my $hit_cnt = 0;
    $SIG{ALRM} = sub {
        if (($hit_cnt++ % 5) == 0) {
            print "+";
        }
        else {
            print "-";
        }
        alarm(5);
    };

    alarm(5);
    local *OUT;
    Exec::exec_pipe(*OUT, "'$::TSKDIR/md5' $img");

    alarm(0);
    $SIG{ALRM} = 'DEFAULT';
    print "<br>\n"
      if ($hit_cnt > 0);

    my $out = Exec::read_pipe_line(*OUT);
    close(OUT);

    $out = "Error calculating MD5"
      if ((!defined $out) || ($out eq ""));

    if ($out =~ /^($::REG_MD5)\s+/) {
        my $m = $1;
        $m =~ tr/[a-f]/[A-F]/;
        return $m;
    }
    else {
        return $out;
    }
}

# Same as the version above, but this one can do split images
# it fails though if the file is not a multiple of 512
sub calc_md5_split {
    my $img = shift;

    my $hit_cnt = 0;
    $SIG{ALRM} = sub {
        if (($hit_cnt++ % 5) == 0) {
            print "+";
        }
        else {
            print "-";
        }
        alarm(5);
    };

    alarm(5);
    local *OUT;

    # We use the dls method so that we can handle split images
    Exec::exec_pipe(*OUT, "'$::TSKDIR/dls' -f raw -e $img | '$::TSKDIR/md5'");

    alarm(0);
    $SIG{ALRM} = 'DEFAULT';
    print "<br>\n"
      if ($hit_cnt > 0);

    my $out = Exec::read_pipe_line(*OUT);
    close(OUT);

    $out = "Error calculating MD5"
      if ((!defined $out) || ($out eq ""));

    if ($out =~ /^($::REG_MD5)\s+/) {
        my $m = $1;
        $m =~ tr/[a-f]/[A-F]/;
        return $m;
    }
    else {
        return $out;
    }
}

# Pass it the full path and the short name
# and it adds it to md5.txt and returns the MD5
sub int_create_wrap {
    my $vol = shift;
    my $img = $Caseman::vol2path{$vol};

    my $m;
    if (   (exists $Caseman::vol2itype{$vol})
        && ($Caseman::vol2itype{$vol} eq "split"))
    {
        $m = calc_md5_split($img);
    }
    else {
        $m = calc_md5($img);
    }
    Caseman::update_md5($vol, $m) if ($m =~ /^$::REG_MD5$/o);
    return $m;
}

sub img_calc {
    Print::print_html_header("Image Integrity Creation");
    my $vol = Args::get_vol('vol');
    print "Calculating MD5 value for <tt>$Caseman::vol2sname{$vol}</tt><br>\n";
    Print::log_host_inv("$Caseman::vol2sname{$vol}: Calculating MD5 value");

    my $m = int_create_wrap($vol);

    print "MD5: <tt>$m</tt><br>\n";
    print "<br>Value saved to host file<br><br>\n";

    Print::print_html_footer();
    return 0;
}

# Conver the 'image' format to the 'volume' format
# Make one central file
sub convert {
    my %img2vol = %{shift()};

    Print::log_host_info("Converting format of MD5 hash files");

    # Get out of here if there are no hash files
    return 0
      unless ((-e "$::host_dir" . "$::IMGDIR" . "/md5.txt")
        || (-e "$::host_dir" . "$::DATADIR" . "/md5.txt"));

    # We are going ot make a single file
    my $md5_file_new = "$::host_dir" . "/md5.txt";
    open MD5_NEW, ">$md5_file_new"
      or die "Can't open writing file: $md5_file_new";

    # Read the md5s for the image directory
    my $md5_file = "$::host_dir" . "$::IMGDIR" . "/md5.txt";
    if (open(FILE, $md5_file)) {

        # Read the md5 values into a hash
        while (<FILE>) {
            s/^\s+//;
            s/\s+$//;

            if (/($::REG_MD5)\s+(.*)/o) {
                my $md5 = $1;
                my $img = $2;

                unless (exists $img2vol{$img}) {
                    print STDERR
"Error finding image during hash file conversion: $img.  Skipping\n";
                    next;
                }
                my $vol = $img2vol{$img};

                print MD5_NEW "$md5 $vol\n";
            }
            else {
                print MD5_NEW "$_";
            }
        }
        close(FILE);
        rename $md5_file, $md5_file . ".bak";
    }

    # Now do the data directory
    $md5_file = "$::host_dir" . "$::DATADIR" . "/md5.txt";
    if (open(FILE, $md5_file)) {

        # Read the md5 values into a hash
        while (<FILE>) {
            s/^\s+//;
            s/\s+$//;

            if (/($::REG_MD5)\s+(.*)/o) {
                my $md5 = $1;
                my $img = $2;

                unless (exists $img2vol{$img}) {
                    print STDERR
"Error finding image during hash file conversion: $img.  Skipping\n";
                    next;
                }
                my $vol = $img2vol{$img};

                print MD5_NEW "$md5 $vol\n";
            }
            else {
                print MD5_NEW "$_";
            }
        }
        close(FILE);
        rename $md5_file, $md5_file . ".bak";
    }

    close(MD5_NEW);
    return 0;
}

# Blank Page
sub blank {
    Print::print_html_header("");
    print "<!-- This Page Intentionally Left Blank -->\n";
    Print::print_html_footer();
    return 0;
}