-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbuseIPDBInterface.class.php
246 lines (210 loc) · 9.25 KB
/
AbuseIPDBInterface.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* INTERFACE FOR NEW VERSION OF ABUSEIPDB API.
* Project : abuseIPDBv2_interfcae
* @author Pierre-Alexandre RACINE <patcha.dev at{@} gmail dot[.] com>
* @copyright Pierre-Alexandre RACINE <patcha.dev at{@} gmail dot[.] com>
* @date 23/11/19 22:32
*
* @link https://github.com/racine-p-a/abuseIPDBv2_interfcae
*
* todo make a license
*/
error_reporting(E_ALL);
ini_set("display_errors", 1);
class AbuseIPDBInterface
{
/**
* Pick you user key here : https://www.abuseipdb.com/account/api and generate one if you do not already own one.
* I strongly recommend to create and use one different for each application/website you use.
*
* @var string Your AbuseIPDB user key
*/
private $apiKey = '128b87698b58fb8be1861c9959a6896b2cbfd97494a5baa58f5a7a0d4e46afbff8c824cd48cb3c99'; // Remove and
// put yours
// here
/**
* AbuseIPDBInterface constructor.
*/
public function __construct()
{
}
/**
* Not working method. AbuseIPDB seems to not get the file attached.
* HELP NEEDED.
* @param string $pathToYourFile Access path to the file you wan to send to abuseIPDB.
*/
public function bulkReport($pathToYourFile='') {
if (
// Is the file accessible ?
is_file($pathToYourFile) &&
is_readable($pathToYourFile)
) {
// Works using POST : https://docs.abuseipdb.com/?php#bulk-report-endpoint
$fp = fopen($pathToYourFile, 'r');
$curl = curl_init("https://api.abuseipdb.com/api/v2/bulk-report");
$cfile = new CURLFile($pathToYourFile, 'text/csv', 'csv');
$data = array();
$data['csv'] = $cfile;
curl_setopt_array($curl, array(
CURLOPT_UPLOAD => 1,
CURLOPT_INFILE => $fp,
CURLOPT_NOPROGRESS => false,
CURLOPT_BUFFERSIZE => 128,
CURLOPT_INFILESIZE => filesize($pathToYourFile),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POST => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
'Key: ' . $this->apiKey,
"cache-control: no-cache",
'Accept: application/json',
"content-type: multipart/form-data"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
}
/**
* Check all IP adress inside a network (using a CIDR notation)
* @see https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
* @param string $networkToCheck A network of IP represented using the CIDR notation (127.0.0.1/24)
* @param int $maxAge The maximal age of reports taken in account.
* @return bool|string
*/
public function checkBlock($networkToCheck='', $maxAge=30) {
$blockDetails = '';
$networkDetails = explode('/', $networkToCheck);
// Works using GET : https://docs.abuseipdb.com/?php#check-endpoint
if (
count($networkDetails) == 2 && // Exactly two parts in the network notation.
filter_var($networkDetails[0], FILTER_VALIDATE_IP) // First part is a correct IP address.
&& intval($networkDetails[1] > 0) // Second part is an integer
&& ($this->apiKey != '')
) {
// URI construction
$uri = 'https://api.abuseipdb.com/api/v2/check-block?network=' . urlencode($networkToCheck);
if(intval($maxAge) > 0) {
$uri .= '&maxAgeInDays=' . intval($maxAge);
}
// CURL request
$headers = array(
'Key: ' . $this->apiKey,
'Accept: application/json'
);
$curlRequest = curl_init($uri);
curl_setopt($curlRequest, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlRequest, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curlRequest, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlRequest, CURLOPT_HTTPHEADER, $headers);
if(curl_error($curlRequest)) {
return $blockDetails;
}
$blockDetails=curl_exec($curlRequest);
curl_close($curlRequest);
}
return $blockDetails;
}
/**
* Report here a suspicious IP address
* @param string $IPToBan The IP you want to report to abuseIPDB
* @param array $categories Optionnal array containing categories of reasons why you report this IP (more here : https://www.abuseipdb.com/categories)
* @param string $comment Optionnal comment. STRIP ANY PERSONALLY IDENTIFIABLE INFORMATION (PPI). ABUSEIPDB IS NOT RESPONSIBLE FOR PPI YOU REVEAL... NOR AM I...
* @return bool|string|void
*/
public function reportIP($IPToBan='', array $categories=array(), $comment='') {
if (filter_var($IPToBan, FILTER_VALIDATE_IP) && $this->apiKey!='') {
// Works using POST : https://docs.abuseipdb.com/?php#report-endpoint
$postdata = http_build_query(
array(
'ip' => $IPToBan,
'categories' => implode(",", $categories),
'comment' => $comment
)
);
// CURL request.
$headers = array('Key: ' . $this->apiKey, 'Accept: application/json');
$curlRequest = curl_init("https://api.abuseipdb.com/api/v2/report");
curl_setopt($curlRequest, CURLOPT_RETURNTRANSFER, 1 ); // Set to 0 for testing to display response from AbuseIPDB
curl_setopt($curlRequest, CURLOPT_POST, 1 );
curl_setopt($curlRequest, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curlRequest, CURLOPT_HTTPHEADER, $headers);
if(curl_error($curlRequest)) {
return;
}
$output=curl_exec($curlRequest);
curl_close($curlRequest);
return $output;
}
}
/**
* Grab the freshest blacklist from ABuseIPDB.
* @param int $confidenceMinimum The minimal confidence that abuseIPDB has in the information (100=sure)
* @return bool|string A list of bad IP you should blacklist (in a json object in our case).
*/
public function getBlacklist($confidenceMinimum=90) {
$blackList = '';
// Works using GET : https://docs.abuseipdb.com/?php#blacklist-endpoint
// URI construction
$uri = 'https://api.abuseipdb.com/api/v2/blacklist?confidenceMinimum=' . intval($confidenceMinimum);
// CURL request
$headers = array('Key: ' . $this->apiKey, 'Accept: application/json');
$curlRequest = curl_init($uri);
curl_setopt($curlRequest, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlRequest, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curlRequest, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlRequest, CURLOPT_HTTPHEADER, $headers);
if(curl_error($curlRequest)) {
return $blackList;
}
$blackList=curl_exec($curlRequest);
curl_close($curlRequest);
return $blackList;
}
/**
* Check an API in the AbuseIPDB database and send back informations.
* @param string $IPToCheck The IP we want to check (IPV4 or IPV6 accepted)
* @param int $maxAge The age of the reports taken in account.
* @param int $verbose Verbose answer ?
* @return bool|string Informations about an IP from AbuseIPDB in a json object.
*/
public function checkIP($IPToCheck='', $maxAge=0, $verbose=0) {
$IPdata = '';
// Works using GET : https://docs.abuseipdb.com/?php#check-endpoint
if (filter_var($IPToCheck, FILTER_VALIDATE_IP) && ($this->apiKey != '')) {
// URI construction
$uri = 'https://api.abuseipdb.com/api/v2/check?ipAddress=' . urlencode($IPToCheck);
if(intval($maxAge) > 0) {
$uri .= '&maxAgeInDays=' . intval($maxAge);
}
if(boolval($verbose) == 1) {
$uri .= '&verbose=' . boolval($verbose);
}
// CURL request
$headers = array('Key: ' . $this->apiKey, 'Accept: application/json');
$curlRequest = curl_init($uri);
curl_setopt($curlRequest, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlRequest, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curlRequest, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlRequest, CURLOPT_HTTPHEADER, $headers);
if(curl_error($curlRequest)) {
return $IPdata;
}
$IPdata=curl_exec($curlRequest);
curl_close($curlRequest);
}
return $IPdata;
}
}