Exemple #1
0
 public static function ajax_blockIP_callback()
 {
     $IP = trim($_POST['IP']);
     $perm = isset($_POST['perm']) && $_POST['perm'] == '1' ? true : false;
     $log = new wfLog(wfConfig::get('apiKey'), wfUtils::getWPVersion());
     if (!wfUtils::isValidIP($IP)) {
         return array('err' => 1, 'errorMsg' => "Please enter a valid IP address to block.");
     }
     if ($IP == wfUtils::getIP()) {
         return array('err' => 1, 'errorMsg' => "You can't block your own IP address.");
     }
     if ($log->isWhitelisted($IP)) {
         return array('err' => 1, 'errorMsg' => "The IP address " . wp_kses($IP, array()) . " is whitelisted and can't be blocked or it is in a range of internal IP addresses that Wordfence does not block. You can remove this IP from the whitelist on the Wordfence options page.");
     }
     if (wfConfig::get('neverBlockBG') != 'treatAsOtherCrawlers') {
         //Either neverBlockVerified or neverBlockUA is selected which means the user doesn't want to block google
         if (wfCrawl::isVerifiedGoogleCrawler($IP)) {
             return array('err' => 1, 'errorMsg' => "The IP address you're trying to block belongs to Google. Your options are currently set to not block these crawlers. Change this in Wordfence options if you want to manually block Google.");
         }
     }
     $log->blockIP($IP, $_POST['reason'], false, $perm);
     return array('ok' => 1);
 }
 public function logLeechAndBlock($type)
 {
     //404 or hit
     if (wfConfig::get('firewallEnabled')) {
         //Moved the following block into the "is fw enabled section" for optimization.
         $IP = wfUtils::getIP();
         $IPnum = wfUtils::inet_pton($IP);
         if ($this->isWhitelisted($IP)) {
             return;
         }
         if (wfConfig::get('neverBlockBG') == 'neverBlockUA' && wfCrawl::isGoogleCrawler()) {
             return;
         }
         if (wfConfig::get('neverBlockBG') == 'neverBlockVerified' && wfCrawl::isVerifiedGoogleCrawler()) {
             return;
         }
         if ($type == '404') {
             $allowed404s = wfConfig::get('allowed404s');
             if (is_string($allowed404s)) {
                 $allowed404s = array_filter(explode("\n", $allowed404s));
                 $allowed404sPattern = '';
                 foreach ($allowed404s as $allowed404) {
                     $allowed404sPattern .= preg_replace('/\\\\\\*/', '.*?', preg_quote($allowed404, '/')) . '|';
                 }
                 $uri = $_SERVER['REQUEST_URI'];
                 if (($index = strpos($uri, '?')) !== false) {
                     $uri = substr($uri, 0, $index);
                 }
                 if ($allowed404sPattern && preg_match('/^' . substr($allowed404sPattern, 0, -1) . '$/i', $uri)) {
                     return;
                 }
             }
         }
         if ($type == '404') {
             $table = $this->scanTable;
         } else {
             if ($type == 'hit') {
                 $table = $this->leechTable;
             } else {
                 wordfence::status(1, 'error', "Invalid type to logLeechAndBlock(): {$type}");
                 return;
             }
         }
         $this->getDB()->queryWrite("insert into {$table} (eMin, IP, hits) values (floor(unix_timestamp() / 60), %s, 1) ON DUPLICATE KEY update hits = IF(@wfcurrenthits := hits + 1, hits + 1, hits + 1)", wfUtils::inet_pton($IP));
         $hitsPerMinute = $this->getDB()->querySingle("select @wfcurrenthits");
         //end block moved into "is fw enabled" section
         //Range blocking was here. Moved to wordfenceClass::veryFirstAction
         if (wfConfig::get('blockFakeBots')) {
             if (wfCrawl::isGooglebot() && !wfCrawl::verifyCrawlerPTR($this->googlePattern, $IP)) {
                 $this->blockIP($IP, "Fake Google crawler automatically blocked");
                 wordfence::status(2, 'info', "Blocking fake Googlebot at IP {$IP}");
             }
         }
         if (wfConfig::get('bannedURLs', false)) {
             $URLs = explode(',', wfConfig::get('bannedURLs'));
             foreach ($URLs as $URL) {
                 if ($_SERVER['REQUEST_URI'] == trim($URL)) {
                     $this->blockIP($IP, "Accessed a banned URL.");
                     $this->do503(3600, "Accessed a banned URL.");
                     //exits
                 }
             }
         }
         if (wfConfig::get('maxGlobalRequests') != 'DISABLED' && $hitsPerMinute > wfConfig::get('maxGlobalRequests')) {
             //Applies to 404 or pageview
             $this->takeBlockingAction('maxGlobalRequests', "Exceeded the maximum global requests per minute for crawlers or humans.");
         }
         if ($type == '404') {
             global $wpdb;
             $p = $wpdb->base_prefix;
             if (wfConfig::get('other_WFNet')) {
                 $this->getDB()->queryWrite("insert IGNORE into {$p}" . "wfNet404s (sig, ctime, URI) values (UNHEX(MD5('%s')), unix_timestamp(), '%s')", $_SERVER['REQUEST_URI'], $_SERVER['REQUEST_URI']);
             }
             $pat = wfConfig::get('vulnRegex');
             if ($pat) {
                 $URL = wfUtils::getRequestedURL();
                 if (preg_match($pat, $URL)) {
                     $this->getDB()->queryWrite("insert IGNORE into {$p}" . "wfVulnScanners (IP, ctime, hits) values (%s, unix_timestamp(), 1) ON DUPLICATE KEY UPDATE ctime = unix_timestamp(), hits = hits + 1", wfUtils::inet_pton($IP));
                     if (wfConfig::get('maxScanHits') != 'DISABLED') {
                         if (empty($_SERVER['HTTP_REFERER'])) {
                             $this->getDB()->queryWrite("insert into " . $this->badLeechersTable . " (eMin, IP, hits) values (floor(unix_timestamp() / 60), %s, 1) ON DUPLICATE KEY update hits = IF(@wfblcurrenthits := hits + 1, hits + 1, hits + 1)", $IPnum);
                             $BL_hitsPerMinute = $this->getDB()->querySingle("select @wfblcurrenthits");
                             if ($BL_hitsPerMinute > wfConfig::get('maxScanHits')) {
                                 $this->takeBlockingAction('maxScanHits', "Exceeded the maximum number of 404 requests per minute for a known security vulnerability.");
                             }
                         }
                     }
                 }
             }
         }
         if (wfConfig::get('other_blockBadPOST') == '1' && $_SERVER['REQUEST_METHOD'] == 'POST' && empty($_SERVER['HTTP_USER_AGENT']) && empty($_SERVER['HTTP_REFERER'])) {
             $this->blockIP($IP, "POST received with blank user-agent and referer");
             $this->do503(3600, "POST received with blank user-agent and referer");
             //exits
         }
         if (isset($_SERVER['HTTP_USER_AGENT']) && wfCrawl::isCrawler($_SERVER['HTTP_USER_AGENT'])) {
             if ($type == 'hit' && wfConfig::get('maxRequestsCrawlers') != 'DISABLED' && $hitsPerMinute > wfConfig::get('maxRequestsCrawlers')) {
                 $this->takeBlockingAction('maxRequestsCrawlers', "Exceeded the maximum number of requests per minute for crawlers.");
                 //may not exit
             } else {
                 if ($type == '404' && wfConfig::get('max404Crawlers') != 'DISABLED' && $hitsPerMinute > wfConfig::get('max404Crawlers')) {
                     $this->takeBlockingAction('max404Crawlers', "Exceeded the maximum number of page not found errors per minute for a crawler.");
                 }
             }
         } else {
             if ($type == 'hit' && wfConfig::get('maxRequestsHumans') != 'DISABLED' && $hitsPerMinute > wfConfig::get('maxRequestsHumans')) {
                 $this->takeBlockingAction('maxRequestsHumans', "Exceeded the maximum number of page requests per minute for humans.");
             } else {
                 if ($type == '404' && wfConfig::get('max404Humans') != 'DISABLED' && $hitsPerMinute > wfConfig::get('max404Humans')) {
                     $this->takeBlockingAction('max404Humans', "Exceeded the maximum number of page not found errors per minute for humans.");
                 }
             }
         }
     }
 }
Exemple #3
0
 private function googleSafetyCheckOK()
 {
     //returns true if OK to block. Returns false if we must not block.
     $cacheKey = md5((isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '') . ' ' . wfUtils::getIP());
     //Cache so we can call this multiple times in one request
     if (!isset(self::$gbSafeCache[$cacheKey])) {
         $nb = wfConfig::get('neverBlockBG');
         if ($nb == 'treatAsOtherCrawlers') {
             self::$gbSafeCache[$cacheKey] = true;
             //OK to block because we're treating google like everyone else
         } else {
             if ($nb == 'neverBlockUA' || $nb == 'neverBlockVerified') {
                 if (wfCrawl::isGoogleCrawler()) {
                     //Check the UA using regex
                     if ($nb == 'neverBlockVerified') {
                         if (wfCrawl::isVerifiedGoogleCrawler($this->googlePattern, wfUtils::getIP())) {
                             //UA check passed, now verify using PTR if configured to
                             self::$gbSafeCache[$cacheKey] = false;
                             //This is a verified Google crawler, so no we can't block it
                         } else {
                             self::$gbSafeCache[$cacheKey] = true;
                             //This is a crawler claiming to be Google but it did not verify
                         }
                     } else {
                         //neverBlockUA
                         self::$gbSafeCache[$cacheKey] = false;
                         //User configured us to only do a UA check and this claims to be google so don't block
                     }
                 } else {
                     self::$gbSafeCache[$cacheKey] = true;
                     //This isn't a Google UA, so it's OK to block
                 }
             } else {
                 //error_log("Wordfence error: neverBlockBG option is not set.");
                 self::$gbSafeCache[$cacheKey] = false;
                 //Oops the config option is not set. This should never happen because it's set on install. So we return false to indicate it's not OK to block just for safety.
             }
         }
     }
     if (!isset(self::$gbSafeCache[$cacheKey])) {
         //error_log("Wordfence assertion fail in googleSafetyCheckOK: cached value is not set.");
         return false;
         //for safety
     }
     return self::$gbSafeCache[$cacheKey];
     //return cached value
 }
Exemple #4
0
 /**
  * @return array|null|object
  */
 public function execute()
 {
     global $wpdb;
     $sql = $this->buildQuery();
     $results = $wpdb->get_results($sql, ARRAY_A);
     $this->getWFLog()->processGetHitsResults('', $results);
     $verifyCrawlers = false;
     if ($this->filters !== null && count($this->filters->getFilters()) > 0) {
         $filters = $this->filters->getFilters();
         foreach ($filters as $f) {
             if (strtolower($f->getParam()) == "isgoogle") {
                 $verifyCrawlers = true;
                 break;
             }
         }
     }
     foreach ($results as $key => &$row) {
         if ($row['isGoogle'] && $verifyCrawlers) {
             if (!wfCrawl::isVerifiedGoogleCrawler($row['IP'], $row['UA'])) {
                 unset($results[$key]);
                 //foreach copies $results and iterates on the copy, so it is safe to mutate $results within the loop
                 continue;
             }
         }
         $row['actionData'] = (array) json_decode($row['actionData'], true);
     }
     return array_values($results);
 }
Exemple #5
0
 public static function veryFirstAction()
 {
     $wfFunc = isset($_GET['_wfsf']) ? @$_GET['_wfsf'] : false;
     if ($wfFunc == 'unlockEmail') {
         if (!wp_verify_nonce(@$_POST['nonce'], 'wf-form')) {
             die("Sorry but your browser sent an invalid security token when trying to use this form.");
         }
         $numTries = get_transient('wordfenceUnlockTries');
         if ($numTries > 10) {
             echo "<html><body><h1>Please wait 3 minutes and try again</h1><p>You have used this form too much. Please wait 3 minutes and try again.</p></body></html>";
             exit;
         }
         if (!$numTries) {
             $numTries = 1;
         } else {
             $numTries = $numTries + 1;
         }
         set_transient('wordfenceUnlockTries', $numTries, 180);
         $email = trim($_POST['email']);
         global $wpdb;
         $ws = $wpdb->get_results($wpdb->prepare("SELECT ID, user_login FROM {$wpdb->users} WHERE user_email = %s", $email));
         foreach ($ws as $user) {
             $userDat = get_userdata($user->ID);
             if (wfUtils::isAdmin($userDat)) {
                 if ($email == $userDat->user_email) {
                     $found = true;
                     break;
                 }
             }
         }
         if (!$found) {
             foreach (wfConfig::getAlertEmails() as $alertEmail) {
                 if ($alertEmail == $email) {
                     $found = true;
                     break;
                 }
             }
         }
         if ($found) {
             $key = wfUtils::bigRandomHex();
             $IP = wfUtils::getIP();
             set_transient('wfunlock_' . $key, $IP, 1800);
             $content = wfUtils::tmpl('email_unlockRequest.php', array('siteName' => get_bloginfo('name', 'raw'), 'siteURL' => wfUtils::getSiteBaseURL(), 'unlockHref' => wfUtils::getSiteBaseURL() . '?_wfsf=unlockAccess&key=' . $key, 'key' => $key, 'IP' => $IP));
             wp_mail($email, "Unlock email requested", $content, "Content-Type: text/html");
         }
         echo "<html><body><h1>Your request was received</h1><p>We received a request to email \"" . wp_kses($email, array()) . "\" instructions to unlock their access. If that is the email address of a site administrator or someone on the Wordfence alert list, then they have been emailed instructions on how to regain access to this sytem. The instructions we sent will expire 30 minutes from now.</body></html>";
         exit;
     } else {
         if ($wfFunc == 'unlockAccess') {
             if (!preg_match('/^\\d+\\.\\d+\\.\\d+\\.\\d+$/', get_transient('wfunlock_' . $_GET['key']))) {
                 echo "Invalid key provided for authentication.";
                 exit;
             }
             /* You can enable this for paranoid security leve.
             			if(get_transient('wfunlock_' . $_GET['key']) != wfUtils::getIP()){
             				echo "You can only use this link from the IP address you used to generate the unlock email.";
             				exit();
             			}
             			*/
             $wfLog = new wfLog(wfConfig::get('apiKey'), wfUtils::getWPVersion());
             if ($_GET['func'] == 'unlockMyIP') {
                 $wfLog->unblockIP(wfUtils::getIP());
                 $wfLog->unlockOutIP(wfUtils::getIP());
                 delete_transient('wflginfl_' . bin2hex(wfUtils::inet_pton(wfUtils::getIP())));
                 //Reset login failure counter
                 header('Location: ' . wp_login_url());
                 exit;
             } else {
                 if ($_GET['func'] == 'unlockAllIPs') {
                     wordfence::status(1, 'info', "Request received via unlock email link to unblock all IP's.");
                     $wfLog->unblockAllIPs();
                     $wfLog->unlockAllIPs();
                     delete_transient('wflginfl_' . bin2hex(wfUtils::inet_pton(wfUtils::getIP())));
                     //Reset login failure counter
                     header('Location: ' . wp_login_url());
                     exit;
                 } else {
                     if ($_GET['func'] == 'disableRules') {
                         wfConfig::set('firewallEnabled', 0);
                         wfConfig::set('loginSecurityEnabled', 0);
                         wordfence::status(1, 'info', "Request received via unlock email link to unblock all IP's via disabling firewall rules.");
                         $wfLog->unblockAllIPs();
                         $wfLog->unlockAllIPs();
                         delete_transient('wflginfl_' . bin2hex(wfUtils::inet_pton(wfUtils::getIP())));
                         //Reset login failure counter
                         wfConfig::set('cbl_countries', '');
                         //unblock all countries
                         header('Location: ' . wp_login_url());
                         exit;
                     } else {
                         echo "Invalid function specified. Please check the link we emailed you and make sure it was not cut-off by your email reader.";
                         exit;
                     }
                 }
             }
         }
     }
     if (wfConfig::get('firewallEnabled')) {
         $wfLog = self::getLog();
         $wfLog->firewallBadIPs();
         $IP = wfUtils::getIP();
         if ($wfLog->isWhitelisted($IP)) {
             return;
         }
         if (wfConfig::get('neverBlockBG') == 'neverBlockUA' && wfCrawl::isGoogleCrawler()) {
             return;
         }
         if (wfConfig::get('neverBlockBG') == 'neverBlockVerified' && wfCrawl::isVerifiedGoogleCrawler()) {
             return;
         }
         if (wfConfig::get('blockFakeBots')) {
             if (wfCrawl::isGooglebot() && !wfCrawl::verifyCrawlerPTR($wfLog->getGooglePattern(), $IP)) {
                 $wfLog->blockIP($IP, "Fake Google crawler automatically blocked");
                 wordfence::status(2, 'info', "Blocking fake Googlebot at IP {$IP}");
                 $wfLog->do503(3600, "Fake Google crawler automatically blocked.");
             }
         }
         if (wfConfig::get('bannedURLs', false)) {
             $URLs = explode(',', wfConfig::get('bannedURLs'));
             foreach ($URLs as $URL) {
                 if ($_SERVER['REQUEST_URI'] == trim($URL)) {
                     $wfLog->blockIP($IP, "Accessed a banned URL.");
                     $wfLog->do503(3600, "Accessed a banned URL.");
                     //exits
                 }
             }
         }
         if (wfConfig::get('other_blockBadPOST') == '1' && $_SERVER['REQUEST_METHOD'] == 'POST' && empty($_SERVER['HTTP_USER_AGENT']) && empty($_SERVER['HTTP_REFERER'])) {
             $wfLog->blockIP($IP, "POST received with blank user-agent and referer");
             $wfLog->do503(3600, "POST received with blank user-agent and referer");
             //exits
         }
     }
 }