<?php
date_default_timezone_set('America/New_York');
$ip=$_SERVER['REMOTE_ADDR'];
//$ip="127.0.0.2"; // for testing
$origRequest = $_GET['req'];
putenv("TZ=US/Eastern");
$timeNow = time();
$fresh = time() - 900; // AbuseIPDB rejects IPs reported by a user within 15 minutes
$domain = "[example.tld]"; // domain of reporting site or server
$currentDateTime = (date("M d, Y h:i:s a"));
$ports="80,443";
$categories="15,21"; // for AbuseIPDB Report
$con = mysqli_connect("[database-host-domain]","[database_user]","[database_password]","[database_name]");
if (!$con) { die('Could not connect: ' . mysqli_error($con)); }
$result = mysqli_query($con, "SELECT * FROM reports WHERE (ip4 LIKE '$ip' AND time >= '$fresh')");
$row = mysqli_fetch_array($result);
$reportDate = $row['datetime'];
if (empty($reportDate)) {
// sanitize
$timeNow = mysqli_real_escape_string($con, $timeNow);
$ip = mysqli_real_escape_string($con, $ip);
$domain = mysqli_real_escape_string($con, $domain);
$currentDateTime = mysqli_real_escape_string($con, $currentDateTime);
$origRequest = mysqli_real_escape_string($con, $origRequest);
$comment="Hit on " . $origRequest; // for AbuseIPDB Report and database entry
/* If origRequest is stripped out by mysqli_real_escape_string, it probably means it contained malicious SQL code. Therefore:*/
if (empty($origRequest)) {
$comment = "Web-based SQL injection attempt";
$categories = "15,16,21";
}
// insert to db
mysqli_select_db($con, "[database_name]");
$sql = "INSERT INTO reports (datetime, time, ip4, ports, categories, domain, origRequest, comment) VALUES ('$currentDateTime','$timeNow','$ip','$ports','$categories','$domain','$origRequest','$comment')";
if (!mysqli_query($con,$sql)) {
echo("Error description: " . mysqli_error($con));
}
// make report
$data = (array(
"ip" => $ip,
"categories" => $categories,
"comment" => $comment
));
$headers = array('Key: [key goes here]', 'Accept: application/json');
$ch = curl_init("https://api.abuseipdb.com/api/v2/report");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); // Set to 0 for testing to display response from AbuseIPDB
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output=curl_exec($ch);
curl_close($ch);
}
include("401.php");
?>