mirror of
https://github.com/kristuff/abuseipdb
synced 2025-08-20 21:43:13 -07:00
Auto cleaning report message
This commit is contained in:
parent
eb4e1d6d69
commit
bca2095bc6
1 changed files with 56 additions and 19 deletions
|
@ -58,7 +58,7 @@ class ApiManager extends ApiDefintion
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $apiKey The AbuseIPDB api key
|
* @param string $apiKey The AbuseIPDB api key
|
||||||
* @param string $userId The AbuseIPDB user's id
|
* @param string $userId The AbuseIPDB user's id
|
||||||
* @param array $myIps The Ips you dont want to report
|
* @param array $myIps The Ips/domain name you dont want to display in report messages
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function __construct(string $apiKey, string $userId, array $myIps = [])
|
public function __construct(string $apiKey, string $userId, array $myIps = [])
|
||||||
|
@ -79,9 +79,9 @@ class ApiManager extends ApiDefintion
|
||||||
return array(
|
return array(
|
||||||
'userId' => $this->aipdbUserId,
|
'userId' => $this->aipdbUserId,
|
||||||
'apiKey' => $this->aipdbApiKey,
|
'apiKey' => $this->aipdbApiKey,
|
||||||
|
'selfIps' => $this->selfIps,
|
||||||
// TODO 'selfIps' => $this->selfIps,
|
|
||||||
// TODO default report cat
|
// TODO default report cat
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,17 +104,24 @@ class ApiManager extends ApiDefintion
|
||||||
throw new \InvalidArgumentException('The file [' . $configPath . '] does not exist.');
|
throw new \InvalidArgumentException('The file [' . $configPath . '] does not exist.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// check file is redable
|
// check file is readable
|
||||||
if (!is_readable($configPath)){
|
if (!is_readable($configPath)){
|
||||||
throw new InvalidPermissionException('The file [' . $configPath . '] is not readable.');
|
throw new InvalidPermissionException('The file [' . $configPath . '] is not readable.');
|
||||||
}
|
}
|
||||||
|
|
||||||
//todo check file exist
|
// todo check file exist
|
||||||
$config = self::loadJsonFile($configPath);
|
$keyConfig = self::loadJsonFile($configPath);
|
||||||
|
$selfIps = [];
|
||||||
|
|
||||||
|
// Look for other optional config files in the same directory
|
||||||
|
$selfIpsConfigPath = pathinfo($configPath, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR . 'self_ips.json';
|
||||||
|
if (file_exists($selfIpsConfigPath)){
|
||||||
|
$selfIps = self::loadJsonFile($selfIpsConfigPath)->self_ips;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO $config->self_ips
|
$app = new ApiManager($keyConfig->api_key, $keyConfig->user_id, $selfIps);
|
||||||
// TODO other options
|
|
||||||
return new ApiManager($config->api_key, $config->user_id);
|
return $app;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -143,7 +150,7 @@ class ApiManager extends ApiDefintion
|
||||||
* @param string $ip The ip to report
|
* @param string $ip The ip to report
|
||||||
* @param string $categories The report categories
|
* @param string $categories The report categories
|
||||||
* @param string $message The report message
|
* @param string $message The report message
|
||||||
* @param bool [$returnArray] True to return an indexed array instead of an object. Default is false.
|
* @param bool $returnArray True to return an indexed array instead of an object. Default is false.
|
||||||
*
|
*
|
||||||
* @return object|array
|
* @return object|array
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
|
@ -165,14 +172,15 @@ class ApiManager extends ApiDefintion
|
||||||
throw new \InvalidArgumentException('report message was empty');
|
throw new \InvalidArgumentException('report message was empty');
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO clean message ? selfips list
|
// validates categories, clean message
|
||||||
$cats = $this->validateCategories($categories);
|
$cats = $this->validateCategories($categories);
|
||||||
|
$msg = $this->cleanMessage($message);
|
||||||
|
|
||||||
// report AbuseIPDB request
|
// report AbuseIPDB request
|
||||||
return $this->apiRequest('report', [
|
return $this->apiRequest('report', [
|
||||||
'ip' => $ip,
|
'ip' => $ip,
|
||||||
'categories' => $cats,
|
'categories' => $cats,
|
||||||
'comment' => $message
|
'comment' => $msg
|
||||||
],
|
],
|
||||||
'POST', $returnArray
|
'POST', $returnArray
|
||||||
);
|
);
|
||||||
|
@ -244,8 +252,8 @@ class ApiManager extends ApiDefintion
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $ip The ip to check
|
* @param string $ip The ip to check
|
||||||
* @param string $maxAge Max age in days
|
* @param string $maxAge Max age in days
|
||||||
* @param bool [$verbose] True to get the full response. Default is false
|
* @param bool $verbose True to get the full response. Default is false
|
||||||
* @param bool [$returnArray] True to return an indexed array instead of an object. Default is false.
|
* @param bool $returnArray True to return an indexed array instead of an object. Default is false.
|
||||||
*
|
*
|
||||||
* @return object|array
|
* @return object|array
|
||||||
* @throws \InvalidArgumentException When maxAge is not a numeric value, when maxAge is less than 1 or
|
* @throws \InvalidArgumentException When maxAge is not a numeric value, when maxAge is less than 1 or
|
||||||
|
@ -290,13 +298,15 @@ class ApiManager extends ApiDefintion
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param string $path The api end path
|
* @param string $path The api end path
|
||||||
* @param array $data The request data
|
* @param array $data The request data
|
||||||
* @param string [$method] The request method. Default is 'GET'
|
* @param string $method The request method. Default is 'GET'
|
||||||
* @param bool [$returnArray] True to return an indexed array instead of an object. Default is false.
|
* @param bool $returnArray True to return an indexed array instead of an object. Default is false.
|
||||||
*
|
*
|
||||||
* @return object|array
|
* @return object|array
|
||||||
*/
|
*/
|
||||||
protected function apiRequest(string $path, array $data, string $method = 'GET', bool $returnArray = false)
|
protected function apiRequest(string $path, array $data, string $method = 'GET', bool $returnArray = false)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
// set api url
|
// set api url
|
||||||
$url = $this->aipdbApiEndpoint . $path;
|
$url = $this->aipdbApiEndpoint . $path;
|
||||||
|
|
||||||
|
@ -331,15 +341,42 @@ class ApiManager extends ApiDefintion
|
||||||
return json_decode($result, $returnArray);
|
return json_decode($result, $returnArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean message in case it comes from fail2ban <matches>
|
||||||
|
* https://wiki.shaunc.com/wikka.php?wakka=ReportingToAbuseIPDBWithFail2Ban
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $message The original message
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function cleanMessage(string $message)
|
||||||
|
{
|
||||||
|
// Remove backslashes and sensitive information from the report
|
||||||
|
$message = str_replace('\\', '', $message);
|
||||||
|
|
||||||
|
// Remove self ips
|
||||||
|
foreach ($this->myIps as $ip){
|
||||||
|
$message = str_replace($ip, '[MUNGED]', $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we're reporting spam, further munge any email addresses in the report
|
||||||
|
$emailPattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
|
||||||
|
$emailRemplacement = "[MUNGED]";
|
||||||
|
preg_replace($emailPattern, $emailRemplacement, $message);
|
||||||
|
|
||||||
|
return $message;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load and returns decoded Json from given file
|
* Load and returns decoded Json from given file
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @static
|
* @static
|
||||||
* @param string $filePath The file's full path
|
* @param string $filePath The file's full path
|
||||||
* @param bool [$trowError] Throw error on true or silent process. Default is true
|
* @param bool $trowError Throw error on true or silent process. Default is true
|
||||||
*
|
*
|
||||||
* @return string|null
|
* @return object|null
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
* @throws \LogicException
|
* @throws \LogicException
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue