เคยเจอไหม มีบางประเทศชอบสแกนเว็บไซต์เรา เพื่อหาช่องโหว่การโจมตี วันนี้ผมมีเทคนิคที่กรองการโจมตีเพื่อเข้าถึงเว็บไซต์มาฝาก ใช้ฟรี
เช่น IP ของเราคือ 49.237.15.129 ลองเรียกผ่าน api ตาม URL นี้
https://m1n.app/api/ipCheck.php?ip=49.237.15.129
จะได้ข้อมูล json เช่น
{"ipAddress":"49.237.15.129","countryCode":"TH"}
มาเริ่มกันเลย จะยกตัวอย่างการกรอง ip เพื่อให้ bot google เข้ามาได้ปกติ และกรองผู้ใช้งานบางประเทศให้เข้าถึงเว็บไซต์ของเราได้ ลองดู code php ตัวอย่าง จะได้ประมาณนี้
<?php header("X-XSS-Protection: 1; mode=block"); header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0'); header('Cache-Control: post-check=0, pre-check=0', FALSE); header('Pragma: no-cache'); if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $user_ip = trim($ips[0]); // Get the first IP } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $user_ip = $_SERVER['HTTP_CLIENT_IP']; } else { $user_ip = $_SERVER['REMOTE_ADDR']; } echo $user_ip; // Define the IP ranges for Google bots $ranges = [ ["lower" => ip2long("66.249.0.0"), "upper" => ip2long("66.249.255.255")], ["lower" => ip2long("91.207.173.0"), "upper" => ip2long("91.207.173.255")] ]; $user_ip_long = ip2long($user_ip); $isException = false; foreach($ranges as $range) { // Check if the user's IP is within any of the Google bot ranges if ($user_ip_long >= $range["lower"] && $user_ip_long <= $range["upper"]) { $isException = true; break; } } // If IP is not a Google bot, check the country code if (!$isException) { $response = file_get_contents("https://m1n.app/api/ipCheck.php?ip={$user_ip}"); $geo = json_decode($response); if (isset($geo->countryCode)) { $country = $geo->countryCode; // Allow access only if the country is TH or the IP is a Google bot if ($country != "TH") { header("Location: https://www.google.com"); exit(); } } } ?>
ลองไปปรับใช้กันดูนะครับ...