שיטות עבודה מומלצות לאבטחה
2.5.x ✅ 4.0.x ✅
מסמך זה מספק שיטות אבטחה מקיפות למפתחי מודול XOOPS. ביצוע הנחיות אלה יסייע להבטיח שהמודולים שלך מאובטחים ואינם מציגים פרצות בהתקנות XOOPS.
עקרונות אבטחה
Section titled “עקרונות אבטחה”כל מפתח XOOPS צריך לפעול לפי עקרונות האבטחה הבסיסיים הבאים:
- הגנה בעומק: הטמעת שכבות מרובות של בקרות אבטחה
- הרשאות הקטנות ביותר: ספק רק את זכויות הגישה המינימליות הנדרשות
- אימות קלט: לעולם אל תסמוך על קלט משתמש
- מאבטח כברירת מחדל: אבטחה צריכה להיות תצורת ברירת המחדל
- Keep It Simple: קשה יותר לאבטח מערכות מורכבות
תיעוד קשור
Section titled “תיעוד קשור”- CSRF-הגנה - מערכת אסימונים ומחלקה XoopsSecurity
- קלט-חיטוי - MyTextSanitizer ואימות
- SQL-הזרקה-מניעת - נוהלי אבטחת מסדי נתונים
רשימת עיון מהירה
Section titled “רשימת עיון מהירה”לפני שחרור המודול שלך, ודא:
- כל הטפסים כוללים אסימונים של XOOPS
- כל קלט המשתמש מאומת ומחוטא
- כל הפלט הוצא כהלכה
- כל שאילתות מסד הנתונים משתמשות בהצהרות עם פרמטרים
- העלאות קבצים מאומתות כהלכה
- יש בדיקות אימות והרשאות
- טיפול בשגיאות אינו חושף מידע רגיש
- תצורה רגישה מוגנת
- ספריות צד שלישי מעודכנות
- בוצעו בדיקות אבטחה
אימות והרשאה
Section titled “אימות והרשאה”בדיקת אימות משתמש
Section titled “בדיקת אימות משתמש”// Check if user is logged inif (!is_object($GLOBALS['xoopsUser'])) { redirect_header(XOOPS_URL, 3, _NOPERM); exit();}בדיקת הרשאות משתמש
Section titled “בדיקת הרשאות משתמש”// Check if user has permission to access this moduleif (!$GLOBALS['xoopsUser']->isAdmin($xoopsModule->mid())) { redirect_header(XOOPS_URL, 3, _NOPERM); exit();}
// Check specific permission$moduleHandler = xoops_getHandler('module');$module = $moduleHandler->getByDirname('mymodule');$moduleperm_handler = xoops_getHandler('groupperm');$groups = $GLOBALS['xoopsUser']->getGroups();
if (!$moduleperm_handler->checkRight('mymodule_view', $item_id, $groups, $module->getVar('mid'))) { redirect_header(XOOPS_URL, 3, _NOPERM); exit();}הגדרת הרשאות מודול
Section titled “הגדרת הרשאות מודול”// Create permission in install/update function$gpermHandler = xoops_getHandler('groupperm');$gpermHandler->deleteByModule($module->getVar('mid'), 'mymodule_view');
// Add permission for all groups$groups = [XOOPS_GROUP_ADMIN, XOOPS_GROUP_USERS, XOOPS_GROUP_ANONYMOUS];foreach ($groups as $group_id) { $gpermHandler->addRight('mymodule_view', 1, $group_id, $module->getVar('mid'));}אבטחת הפעלה
Section titled “אבטחת הפעלה”שיטות מומלצות לטיפול במפגשים
Section titled “שיטות מומלצות לטיפול במפגשים”- אין לאחסן מידע רגיש בפגישה
- צור מחדש מזהי הפעלה לאחר שינויים של login/privilege
- אמת את נתוני הפגישה לפני השימוש בהם
// Regenerate session ID after loginsession_regenerate_id(true);
// Validate session dataif (isset($_SESSION['mymodule_user_id'])) { $user_id = (int)$_SESSION['mymodule_user_id']; // Verify user exists in database}מניעת קיבוע הפעלה
Section titled “מניעת קיבוע הפעלה”// After successful loginsession_regenerate_id(true);$_SESSION['mymodule_user_ip'] = $_SERVER['REMOTE_ADDR'];
// On subsequent requestsif ($_SESSION['mymodule_user_ip'] !== $_SERVER['REMOTE_ADDR']) { // Possible session hijacking attempt session_destroy(); redirect_header('index.php', 3, 'Session error'); exit();}אבטחת העלאת קבצים
Section titled “אבטחת העלאת קבצים”אימות העלאות קבצים
Section titled “אימות העלאות קבצים”// Check if file was uploaded properlyif (!isset($_FILES['userfile']) || $_FILES['userfile']['error'] != UPLOAD_ERR_OK) { redirect_header('index.php', 3, 'File upload error'); exit();}
// Check file sizeif ($_FILES['userfile']['size'] > 1000000) { // 1MB limit redirect_header('index.php', 3, 'File too large'); exit();}
// Check file type$allowed_types = ['image/jpeg', 'image/png', 'image/gif'];if (!in_array($_FILES['userfile']['type'], $allowed_types)) { redirect_header('index.php', 3, 'Invalid file type'); exit();}
// Validate file extension$filename = $_FILES['userfile']['name'];$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));$allowed_extensions = ['jpg', 'jpeg', 'png', 'gif'];if (!in_array($ext, $allowed_extensions)) { redirect_header('index.php', 3, 'Invalid file extension'); exit();}שימוש בהעלאה של XOOPS
Section titled “שימוש בהעלאה של XOOPS”include_once XOOPS_ROOT_PATH . '/class/uploader.php';
$allowed_mimetypes = ['image/gif', 'image/jpeg', 'image/png'];$maxsize = 1000000; // 1MB$maxwidth = 1024;$maxheight = 768;$upload_dir = XOOPS_ROOT_PATH . '/uploads/mymodule';
$uploader = new XoopsMediaUploader( $upload_dir, $allowed_mimetypes, $maxsize, $maxwidth, $maxheight);
if ($uploader->fetchMedia('userfile')) { $uploader->setPrefix('mymodule_');
if ($uploader->upload()) { $filename = $uploader->getSavedFileName(); // Save filename to database } else { echo $uploader->getErrors(); }} else { echo $uploader->getErrors();}אחסון קבצים שהועלו בצורה מאובטחת
Section titled “אחסון קבצים שהועלו בצורה מאובטחת”// Define upload directory outside web root$upload_dir = XOOPS_VAR_PATH . '/uploads/mymodule';
// Create directory if it doesn't existif (!is_dir($upload_dir)) { mkdir($upload_dir, 0755, true);}
// Move uploaded filemove_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir . '/' . $safe_filename);טיפול בשגיאות ורישום
Section titled “טיפול בשגיאות ורישום”טיפול בשגיאות מאובטח
Section titled “טיפול בשגיאות מאובטח”try { $result = someFunction(); if (!$result) { throw new Exception('Operation failed'); }} catch (Exception $e) { // Log the error xoops_error($e->getMessage());
// Display a generic error message to the user redirect_header('index.php', 3, 'An error occurred. Please try again later.'); exit();}רישום אירועי אבטחה
Section titled “רישום אירועי אבטחה”// Log security eventsxoops_loadLanguage('logger', 'mymodule');$GLOBALS['xoopsLogger']->addExtra('Security', 'Failed login attempt for user: ' . $username);אבטחת תצורה
Section titled “אבטחת תצורה”אחסון תצורה רגישה
Section titled “אחסון תצורה רגישה”// Define configuration path outside web root$config_path = XOOPS_VAR_PATH . '/configs/mymodule/config.php';
// Load configurationif (file_exists($config_path)) { include $config_path;} else { // Handle missing configuration}הגנה על קבצי תצורה
Section titled “הגנה על קבצי תצורה”השתמש ב-.htaccess כדי להגן על קבצי תצורה:
# In .htaccess<Files "config.php"> Order Allow,Deny Deny from all</Files>ספריות של צד שלישי
Section titled “ספריות של צד שלישי”בחירת ספריות
Section titled “בחירת ספריות”- בחר ספריות שמתוחזקות באופן פעיל
- בדוק פרצות אבטחה
- ודא שהרישיון של הספרייה תואם ל- XOOPS
עדכון ספריות
Section titled “עדכון ספריות”// Check library versionif (version_compare(LIBRARY_VERSION, '1.2.3', '<')) { xoops_error('Please update the library to version 1.2.3 or higher');}בידוד ספריות
Section titled “בידוד ספריות”// Load library in a controlled wayfunction loadLibrary($file){ $allowed = ['parser.php', 'formatter.php'];
if (!in_array($file, $allowed)) { return false; }
include_once XOOPS_ROOT_PATH . '/modules/mymodule/libraries/' . $file; return true;}בדיקות אבטחה
Section titled “בדיקות אבטחה”רשימת בדיקה ידנית
Section titled “רשימת בדיקה ידנית”- בדוק את כל הטפסים עם קלט לא חוקי
- ניסיון לעקוף את האימות וההרשאה
- בדוק את פונקציונליות העלאת הקבצים עם קבצים זדוניים
- בדוק אם יש פגיעויות של XSS בכל הפלט
- בדוק הזרקת SQL בכל שאילתות מסד הנתונים
בדיקה אוטומטית
Section titled “בדיקה אוטומטית”השתמש בכלים אוטומטיים כדי לסרוק פגיעויות:
- כלי ניתוח קוד סטטי
- סורקי יישומי אינטרנט
- בודק תלות לספריות צד שלישי
פלט בורח
Section titled “פלט בורח”HTML הקשר
Section titled “HTML הקשר”// For regular HTML contentecho htmlspecialchars($variable, ENT_QUOTES, 'UTF-8');
// Using MyTextSanitizer$myts = MyTextSanitizer::getInstance();echo $myts->htmlSpecialChars($variable);JavaScript הקשר
Section titled “JavaScript הקשר”// For data used in JavaScriptecho json_encode($variable);
// For inline JavaScriptecho 'var data = ' . json_encode($variable) . ';';URL הקשר
Section titled “URL הקשר”// For data used in URLsecho htmlspecialchars(urlencode($variable), ENT_QUOTES, 'UTF-8');משתני תבנית
Section titled “משתני תבנית”// Assign variables to Smarty template$GLOBALS['xoopsTpl']->assign('title', htmlspecialchars($title, ENT_QUOTES, 'UTF-8'));
// For HTML content that should be displayed as-is$GLOBALS['xoopsTpl']->assign('content', $myts->displayTarea($content, 1, 1, 1, 1, 1));משאבים
Section titled “משאבים”#אבטחה #שיטות עבודה מומלצות #xoops #פיתוח מודול #אימות #הרשאה