chiark / gitweb /
Initial import of $HOME/bin.
[bin.git] / find-zlib
1 #!/usr/bin/perl
2
3 # find-zlib - scan for zlib tables in compiled code
4 # Copyright (C) 2002 RUS-CERT, University of Stuttgart.
5 # Written by Florian Weimer <Weimer@CERT.Uni-Stuttgart.DE>.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 if (@ARGV == 0) {
13     print "usage: find-zlib filename...\n";
14     exit 1;
15 }
16
17 use strict;
18
19 $/ = undef;
20
21 my @inflate_table = (3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
22                      35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227,
23                      258, 0, 0);
24
25 sub table_to_re (@) {
26     my $be = "";
27     my $le = "";
28     my $e;
29     foreach $e (@_) {
30         $be .= pack "N", $e;
31         $le .= pack "V", $e;
32     }
33     return (quotemeta($be), quotemeta($le));
34 }
35
36 sub table_to_re_config (@) {
37     my $be = "";
38     my $le = "";
39     my $e;
40     foreach $e (@_) {
41         $be .= pack "n", $e;
42         $le .= pack "v", $e;
43     }
44     return (quotemeta($be), quotemeta($le));
45 }
46
47 my ($inflate_table_be, $inflate_table_le) = table_to_re (@inflate_table);
48
49 my $line;
50 my (@config_table_le, @config_table_be) = ();
51 foreach $line ([8,   32, 128, 256],
52                [32, 128, 258, 1024],
53                [32, 258, 258, 4096]) {
54     my ($be, $le) = table_to_re_config(@$line);
55     push @config_table_be, $be;
56     push @config_table_le, $le;
57 }
58 my ($config_table_be_32,
59     $config_table_be_64,
60     $config_table_le_32,
61     $config_table_le_64)
62     = (join("....", @config_table_be),
63        join("........", @config_table_be),
64        join("....", @config_table_le),
65        join("........", @config_table_le));
66
67 my $file;
68 my $found = 1;
69 for $file (@ARGV) { 
70     open (FILE, "<$file");
71     my $data = <FILE>;
72     close FILE;
73
74     if ($data =~ /$config_table_le_32/o) {
75         print "$file: zlib configuration table, little endian, 32 bit\n";
76         $found = 0;
77     }
78
79     if ($data =~ /$config_table_be_32/o) {
80         print "$file: zlib configuration table, big endian, 32 bit\n";
81         $found = 0;
82     }
83     if ($data =~ /$config_table_le_64/o) {
84         print "$file: zlib configuration table, little endian, 64 bit\n";
85         $found = 0;
86     }
87
88     if ($data =~ /$config_table_be_64/o) {
89         print "$file: zlib configuration table, big endian, 64bit\n";
90     }
91     if ($data =~ /$inflate_table_le/o) {
92         print "$file: zlib inflate table, little endian\n";
93         $found = 0;
94     }
95     if ($data =~ /$inflate_table_be/o) {
96         print "$file: zlib inflate table, big endian\n";
97         $found = 0;
98     }   
99 }
100 exit $found;