PHP code example of coercive / botkicker

1. Go to this page and download the library: Download coercive/botkicker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

coercive / botkicker example snippets


use Coercive\Security\BotKicker\UserAgentKicker;

# Get Instance
$kicker = new UserAgentKicker; // or other kicker

# Load a default list
$kicker->loadCoerciveLists();
# or load custom list...

# Basic bot detection
if(!$kicker->detect()->getStatus()) {
	echo 'a bot is detected';
}
else {
    # True if in whitelist or not in blacklist
}

if($kicker->isRobotsTxtRequested()) { /* do something */ }

# You can (dis)allow empty current detection
$kicker->allowEmpty( true | false );

# Example of custom datas
$datas = ['bot1', 'bot2', 'bot3'];

# Override auto detection
$kicker->setInputList($datas);

# Or add to auto detection
$kicker->addToInputList($datas);

# Show detection result
$status = $kicker->detect();

var_dump( $status->getStatus() ); # bool if ok or not
var_dump( $status->getMatches() ); # array list of match elements
var_dump( $status->getInput() ); # string the input that match
var_dump( $status->getInputs() ); # array current datas

$kicker->setBlackList([
	'bad',
	'bad too'
]);
$kicker->setWhiteList([
	'good',
	'good too'
]);

$kicker->setBlackListFromFiles([
	'/path/file1',
	'/path/file2'
]);
$kicker->setWhiteListFromFiles([
	'/path/file1',
	'/path/file2'
]);

$kicker->addToBlackList([
	'bad',
	'bad too'
]);
$kicker->addToWhiteList([
	'good',
	'good too'
]);

use Coercive\Security\BotKicker\InputKicker;
use Coercive\Security\BotKicker\UrlKicker;

$url = new UrlKicker; # or new UrlKicker(false) without automatic detection
$url->setUrl('/some/url?query=1'); # cleaned to /some/url
$url->getUrl();
$url->getRawUrl();
$url->isMalformed(); # url with ellipsis "..." or ending with "/null"
$url->find('some');

$input = new InputKicker(true, false); # $_GET only
$input->isValidUtf8();
$input->containsUrl(); # the first url found or empty string

use Coercive\Security\BotKicker\IpKicker;

$ipk = new IpKicker;

# Local files
$ipk->getBingList();
$ipk->getBrevoList();
$ipk->getCloudflareList();
$ipk->getLetsEncryptList();
$ipk->getMailjetList();
$ipk->getStripeList();

# Static
$ipk->getMoneticoList();
$ipk->getPayboxList();
$ipk->getPayPlugList();
$ipk->getProlexisList();

# Dynamic (download) : IPv4 by default, IPv6 with true
$ipk->getBingList(true); # throws RuntimeException on failure
$ipk->getGoogleBotList();
$ipk->getGoogleCloudList();
$ipk->getGoogleUserList();
$ipk->getGoogleSpecialCrawlersList();
$ipk->getGoogleUserTriggeredFetchersList();
$ipk->getGoogleUserTriggeredFetchersGoogleList();

# Dynamic (whois command)
$ipk->getFacebookList();
$ipk->getLinkedInList();
$ipk->getFromASN('AS32934'); # IPv4 and IPv6, or false (IPv4) / true (IPv6)

use Coercive\Security\BotKicker\HostLookUp;
use Coercive\Security\BotKicker\NsLookUp;

$look = new HostLookUp;
# OR
$look = new NsLookUp;

$array = $look->getDomains('111.22.33.4');

use Coercive\Security\BotKicker\HostLookUp;
use Coercive\Security\BotKicker\NsLookUp;

$look = new HostLookUp;
# OR
$look = new NsLookUp;

$array = $look->getIps('www.example.com.');

use Coercive\Security\BotKicker\HostLookUp;
use Coercive\Security\BotKicker\NsLookUp;

$look = new HostLookUp;
# OR
$look = new NsLookUp;

# Match ip > domain (the domain itself or one of its subdomains)
$bool = $look->match('66.249.66.1', 'googlebot.com');

# OR match with reverse (match ip > domain first and domain > ip after)
$bool = $look->match('66.249.66.1', 'googlebot.com', true);

# OR with PHP native functions (IPv4 only, no shell command)
$bool = $look->matchNative('66.249.66.1', 'googlebot.com', true);

use Coercive\Security\BotKicker\ModSec\ModSecLogParser;

$parser = new ModSecLogParser('/var/log/apache2/modsec_audit.log');

foreach ($parser->filterUnallowedMethodRequests() as $entry) {
	echo $entry->getIp(), ' ', $entry->getMethod(), ' ', $entry->getHost(), $entry->getUri(), PHP_EOL;
}

# Also : getEntries(), search(callable), filterAccessByIp(), filterGetRequests(), filterPostRequests(), filterHeadRequests(), filterOptionsRequests()

use Coercive\Security\BotKicker\Syslog\SyslogUfw;

$ufw = new SyslogUfw('/var/log/ufw.log');

foreach ($ufw->getMatches() as $ip => $entity) {
	if($entity->isBlocked()) {
		echo $ip, ' ', implode(',', $entity->getPorts()), PHP_EOL;
	}
}

# Unparsable lines
$ufw->getErrors();