|
| 1 | +#!/usr/bin/perl |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | + |
| 5 | +my $usersdir = "/home/diablo/var/users"; |
| 6 | +my $ignore = "admin|public-gate|public-mule"; |
| 7 | +my %data; |
| 8 | + |
| 9 | +sub error { |
| 10 | + my $msg = shift; |
| 11 | + print STDERR "Error: $msg\n"; |
| 12 | +} |
| 13 | + |
| 14 | +# Read the accountfile and save the passhash1, lastlogin_ip and lastlogin_owner |
| 15 | +# in a hash of hashes: $data{username}->{$key} = $value |
| 16 | +foreach my $accountfile (<$usersdir/*>) { |
| 17 | + |
| 18 | + my $username = $accountfile; |
| 19 | + $username =~ s|^.*/||g; |
| 20 | + |
| 21 | + next if($username =~ /($ignore)/); |
| 22 | + |
| 23 | + unless(-f $accountfile) { |
| 24 | + error("Skipping $accountfile. Not a regular file."); |
| 25 | + next; |
| 26 | + } |
| 27 | + |
| 28 | + unless(-r $accountfile) { |
| 29 | + error("Skipping $accountfile. Not readable."); |
| 30 | + next; |
| 31 | + } |
| 32 | + |
| 33 | + open(FILE, "$accountfile") or error($!); |
| 34 | + my @filecontent = grep {/BNET\\\\acct\\\\/} <FILE>; |
| 35 | + close(FILE) or error($!); |
| 36 | + |
| 37 | + foreach(@filecontent) { |
| 38 | + chomp(); |
| 39 | + if(/^\"BNET\\\\acct\\\\(.*)\"=\"(.*)\"$/) { |
| 40 | + my $hashkey = $1; |
| 41 | + my $hashval = $2; |
| 42 | + next unless($hashkey =~ m/passhash1|lastlogin_ip|lastlogin_owner/); |
| 43 | + $data{$username}->{$hashkey} = $hashval; |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +sub warning { |
| 49 | + my $type = shift; |
| 50 | + my $acc = shift; |
| 51 | + my $compareacc = shift; |
| 52 | + my $value = shift; |
| 53 | + |
| 54 | + if($type eq "PASS") { |
| 55 | + print "$type: $acc and $compareacc have the same password ($value)\n"; |
| 56 | + } elsif($type eq "IP") { |
| 57 | + print "$type: $acc and $compareacc have the same lastlogin IP address ($value)\n"; |
| 58 | + } elsif($type eq "USER") { |
| 59 | + print "$type: $acc and $compareacc have the same lastlogin windows user ($value)\n"; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +# Yes, this looks a bit weird. Why not use foreach(keys(...)). This is faster because it |
| 64 | +# compares every pair only once. foreach(...) { foreach(..) } would compare a lot more |
| 65 | +# already compared pairs. |
| 66 | +my @keys = sort keys %data; |
| 67 | +for(my $i=0; $i<=$#keys; $i++) { |
| 68 | + |
| 69 | + for(my $j=$i+1; $j<=$#keys; $j++) { |
| 70 | + |
| 71 | + if($data{$keys[$i]}->{'passhash1'} eq $data{$keys[$j]}->{'passhash1'}) { |
| 72 | + warning("PASS", $keys[$i], $keys[$j], $data{$keys[$i]}->{'passhash1'}); |
| 73 | + } |
| 74 | + |
| 75 | + if($data{$keys[$i]}->{'lastlogin_ip'} eq $data{$keys[$j]}->{'lastlogin_ip'}) { |
| 76 | + warning("IP", $keys[$i], $keys[$j], $data{$keys[$i]}->{'lastlogin_ip'}); |
| 77 | + } |
| 78 | + |
| 79 | + if($data{$keys[$i]}->{'lastlogin_owner'} eq $data{$keys[$j]}->{'lastlogin_owner'}) { |
| 80 | + warning("USER", $keys[$i], $keys[$j], $data{$keys[$i]}->{'lastlogin_owner'}); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | +} |
| 85 | + |
0 commit comments