Subversion Repositories sysadmin_scripts

Rev

Rev 39 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 39 Rev 41
Line 6... Line 6...
6
use warnings;
6
use warnings;
7
use strict;
7
use strict;
8
use File::Basename;
8
use File::Basename;
9
use Cwd;
9
use Cwd;
10
 
10
 
-
 
11
# command used to discover WordPress sites. Should return fully qualified path to a file
-
 
12
my $findCommand = "find /var/www/clients -path '*/web/*' -type f -name wp-config.php | grep -v private";
-
 
13
# regular expression to determine path, client directory and web site directory
-
 
14
my $sitePattern = '(.*)/([a-z0-9-]+)/([a-z0-9-]+)/web';
-
 
15
 
-
 
16
sub getSiteName {
-
 
17
   my $path = shift;
-
 
18
#   print STDERR "==================\n$path\n";
-
 
19
   return '' unless $path =~ m/$sitePattern/;
-
 
20
   my $basePath = $1;
-
 
21
   my $client = $2;
-
 
22
   my $site = $3;
-
 
23
#   print STDERR "\t$basePath\n\t$client\n\t$site\n";
-
 
24
   # do a long ls of the client directory, looking for the line which defines the symbolic link
-
 
25
   my $temp = `ls -ablph $basePath/$client | grep $site | grep /var`;
-
 
26
   chomp $temp;
-
 
27
#   print STDERR "\t$temp\n";
-
 
28
   # only get the symbolic link name
-
 
29
   if ( $temp =~ m/.*\s+([a-z0-9.-]+)\s+->/ ) {
-
 
30
#      print STDERR "\tReturning $temp\n";
-
 
31
      return $1;
-
 
32
   } else {
-
 
33
#      print STDERR "\tReturning $path\n";
-
 
34
      return $path;
-
 
35
   }
-
 
36
}
-
 
37
   
-
 
38
 
11
sub checkSite {
39
sub checkSite {
12
   my $sitename = shift;
40
   my $sitename = shift;
13
   # strip off any file name
41
   # strip off any file name
14
   $sitename = dirname( $sitename );
42
   $sitename = dirname( $sitename );
15
   # remember our current directory
43
   # remember our current directory
Line 19... Line 47...
19
   # run the wp command line tool
47
   # run the wp command line tool
20
   my $output = `wp core verify-checksums --allow-root 2>&1`;
48
   my $output = `wp core verify-checksums --allow-root 2>&1`;
21
   # go back to our old directory
49
   # go back to our old directory
22
   chdir $oldDir;
50
   chdir $oldDir;
23
   # if we have success, return null, else return the output
51
   # if we have success, return null, else return the output
24
   return $output =~ m/^success/i ? '' : $output;
52
   return $output =~ m/^success/i ? '' : &getSiteName( $sitename ) . "\n$output";
25
}
53
}
26
   
54
   
27
# find all wites which have wp-config in them
55
# find all wites which have wp-config in them
28
my @sites = `find /var/www/clients/ -type f -name wp-config.php`;
56
my @sites = `$findCommand`;
29
chomp @sites;
57
chomp @sites;
30
# check all of those sites
58
# check all of those sites
31
foreach my $site ( @sites ) {
59
foreach my $site ( @sites ) {
32
   # only check if they are in the web directory
60
   # only check if they are in the web directory
33
   next unless $site =~ m@client(\d+)/web(\d+)/web@;
61
   next unless $site =~ m@client(\d+)/web(\d+)/web@;
34
   # Now, check the site
62
   # Now, check the site
35
   if ( my $result = &checkSite( $site ) ) { # we have an error
63
   if ( my $result = &checkSite( $site ) ) { # we have an error
36
      print "Possible Infected WordPress Site $site\n";
64
      print "Possible Infected WordPress Site $site\n$result\n";
37
   }
65
   }
38
}   
66
}   
39
 
-
 
40
      
67
      
41
1;
68
1;