API8:2023 - Security Misconfiguration چیست
به واسطه پیکربندی های نادرست و یا عدم مدیریت صحیح تنظیمات مربوط به پیکربندی، امکان بهره برداری مهاجم از تنظیمات پیش فرض و یا نادرست وجود دارد.
مثال:
درخواست GET برای دریافت تنظیمات سیستم:
GET /api/configurations
کد آسیب پذیر(NET.):
using System.Web.Http; namespace MyAPI.Controllers { public class UserController : ApiController { // GET api/user/{id} public IHttpActionResult GetUser(int id) { // Fetch user data from the database without proper access control var user = Database.GetUser(id); return Ok(user); } // Other methods... } }
پیشگیری (NET.):
using System.Web.Http; using Microsoft.AspNetCore.Authorization; namespace MyAPI.Controllers { [Authorize] // Apply authorization to the controller public class UserController : ApiController { // GET api/user/{id} [Authorize(Roles = "Admin")] // Restrict access to authorized users with the "Admin" role public IHttpActionResult GetUser(int id) { // Fetch user data from the database only if the user has the "Admin" role var user = Database.GetUser(id); return Ok(user); } // Other methods... } }
کد آسیب پذیر (جاوا):
@RestController public class UserController { @Autowired private UserRepository userRepository; // GET /user/{id} @RequestMapping(value = "/user/{id}", method = RequestMethod.GET) public User getUser(@PathVariable int id) { // Fetch user data from the database without proper access control User user = userRepository.findById(id); return user; } // Other methods... }
پیشگیری (جاوا):
@RestController public class UserController { @Autowired private UserRepository userRepository; // GET /user/{id} @PreAuthorize("hasRole('ADMIN')") // Restrict access to users with the "ADMIN" role @RequestMapping(value = "/user/{id}", method = RequestMethod.GET) public User getUser(@PathVariable int id) { // Fetch user data from the database only if the user has the "ADMIN" role User user = userRepository.findById(id); return user; } // Other methods... }
پیشنهادات کلی جلوگیری:
قبل از ارسال درخواست به یک URL مشخص، بررسی و اعتبارسنجی دقیق URI و منبع مقصد.
محدود کردن قابلیت دریافت اطلاعات از منابع خارجی و محدود کردن لیست دسترسیهای مجاز به URL های راه دور.
استفاده از Whitelist برای نشان دادن تنها آدرس های معتبر و اجازه دسترسی به آنها.
اعتبارسنجی و فیلتر کردن ورودی کاربران و پارامترهای مرتبط با URL مورد استفاده قبل از استفاده از آنها در درخواست.
استفاده از محدودیتهای شبکه، مانند فایروال، برای محدود کردن دسترسی به منابع خارجی.
آموزش تیم توسعه برای ارزیابی و اعتبارسنجی صحیح URI قبل از استفاده از آن در درخواست ها.