Advertisement
mayankjoin3

security perplexity pro

May 4th, 2025
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. <?php
  2. // Prevent direct access
  3. if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
  4.     http_response_code(403);
  5.     exit("Forbidden");
  6. }
  7.  
  8. // Security Headers (improved)
  9. header('X-Frame-Options: SAMEORIGIN');
  10. header('X-XSS-Protection: 1; mode=block');
  11. header('X-Content-Type-Options: nosniff');
  12. header("Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; frame-ancestors 'self'; base-uri 'self';");
  13. header('Referrer-Policy: strict-origin-when-cross-origin');
  14. header('Strict-Transport-Security: max-age=31536000; includeSubDomains; preload'); // For HTTPS only
  15. header_remove("X-Powered-By");
  16.  
  17. // Error handling
  18. error_reporting(0); // Set to E_ALL for development
  19. ini_set('display_errors', 0);
  20. ini_set('log_errors', 1);
  21. if (!is_dir(__DIR__ . '/logs')) {
  22.     mkdir(__DIR__ . '/logs', 0700, true);
  23. }
  24. ini_set('error_log', __DIR__ . '/logs/security.log');
  25.  
  26. // Secure sessions
  27. ini_set('session.cookie_httponly', 1);
  28. ini_set('session.cookie_secure', (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'));
  29. ini_set('session.use_only_cookies', 1);
  30. ini_set('session.cookie_samesite', 'Strict'); // Newer PHP versions
  31. session_start();
  32.  
  33. // Session fixation & binding
  34. if (!isset($_SESSION['initiated'])) {
  35.     session_regenerate_id(true);
  36.     $_SESSION['initiated'] = true;
  37.     $_SESSION['user_agent'] = $_SERVER['HTTP_USER_AGENT'] ?? '';
  38.     $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'] ?? '';
  39. } else {
  40.     if (
  41.         ($_SESSION['user_agent'] !== ($_SERVER['HTTP_USER_AGENT'] ?? '')) ||
  42.         ($_SESSION['ip'] !== ($_SERVER['REMOTE_ADDR'] ?? ''))
  43.     ) {
  44.         session_unset();
  45.         session_destroy();
  46.         error_log("Session hijack attempt blocked.");
  47.         exit("Session Error");
  48.     }
  49. }
  50.  
  51. // Input sanitization (do not overwrite superglobals, sanitize on output/use)
  52. function clean_input($data) {
  53.     return htmlspecialchars(trim($data), ENT_QUOTES, 'UTF-8');
  54. }
  55.  
  56. // Safe include
  57. function safe_include($file) {
  58.     $realpath = realpath($file);
  59.     if ($realpath && strpos($realpath, __DIR__) === 0) {
  60.         include $realpath;
  61.     } else {
  62.         error_log("Blocked unsafe include attempt: $file");
  63.         http_response_code(403);
  64.         exit("Invalid include");
  65.     }
  66. }
  67.  
  68. // CSRF token functions
  69. function generate_csrf_token() {
  70.     if (empty($_SESSION['csrf_token'])) {
  71.         $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
  72.     }
  73.     return $_SESSION['csrf_token'];
  74. }
  75. function verify_csrf_token($token) {
  76.     return isset($_SESSION['csrf_token']) && hash_equals($_SESSION['csrf_token'], $token);
  77. }
  78.  
  79. // Disable dangerous PHP functions (if possible)
  80. if (function_exists('ini_set')) {
  81.     ini_set('disable_functions', 'exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source');
  82. }
  83.  
  84. // Prevent clickjacking via JavaScript (defense-in-depth)
  85. echo "<script>if(top!==self)proxyLocation(top).href=self.location;</script>";
  86.  
  87. // Ensure PHP version is up-to-date (manual check, not code)
  88. ?>
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement