| 39 | rodolico | 1 | #! /usr/bin/env perl
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | # wp core verify-checksums --allow-root 
 | 
        
           |  |  | 4 | # wp-cli.org
 | 
        
           |  |  | 5 |   | 
        
           |  |  | 6 | use warnings;
 | 
        
           |  |  | 7 | use strict;
 | 
        
           |  |  | 8 | use File::Basename;
 | 
        
           |  |  | 9 | use Cwd;
 | 
        
           |  |  | 10 |   | 
        
           | 41 | rodolico | 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 |   | 
        
           | 39 | rodolico | 39 | sub checkSite {
 | 
        
           |  |  | 40 |    my $sitename = shift;
 | 
        
           |  |  | 41 |    # strip off any file name
 | 
        
           |  |  | 42 |    $sitename = dirname( $sitename );
 | 
        
           |  |  | 43 |    # remember our current directory
 | 
        
           |  |  | 44 |    my $oldDir = getcwd();
 | 
        
           |  |  | 45 |    # move to the WordPress install directory
 | 
        
           |  |  | 46 |    chdir $sitename;
 | 
        
           |  |  | 47 |    # run the wp command line tool
 | 
        
           |  |  | 48 |    my $output = `wp core verify-checksums --allow-root 2>&1`;
 | 
        
           |  |  | 49 |    # go back to our old directory
 | 
        
           |  |  | 50 |    chdir $oldDir;
 | 
        
           |  |  | 51 |    # if we have success, return null, else return the output
 | 
        
           | 41 | rodolico | 52 |    return $output =~ m/^success/i ? '' : &getSiteName( $sitename ) . "\n$output";
 | 
        
           | 39 | rodolico | 53 | }
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 | # find all wites which have wp-config in them
 | 
        
           | 41 | rodolico | 56 | my @sites = `$findCommand`;
 | 
        
           | 39 | rodolico | 57 | chomp @sites;
 | 
        
           |  |  | 58 | # check all of those sites
 | 
        
           |  |  | 59 | foreach my $site ( @sites ) {
 | 
        
           |  |  | 60 |    # only check if they are in the web directory
 | 
        
           |  |  | 61 |    next unless $site =~ m@client(\d+)/web(\d+)/web@;
 | 
        
           |  |  | 62 |    # Now, check the site
 | 
        
           |  |  | 63 |    if ( my $result = &checkSite( $site ) ) { # we have an error
 | 
        
           | 41 | rodolico | 64 |       print "Possible Infected WordPress Site $site\n$result\n";
 | 
        
           | 39 | rodolico | 65 |    }
 | 
        
           |  |  | 66 | }   
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 | 1;
 |