API7:2023 - Server Side Request Forgery چیست
مهاجم به واسطه این آسیب پذیری امکان جعل درخواست در سمت سرور و ارسال درخواست های تقلبی را به مقصد مجاز را دارد.
مثال:
درخواست GET برای دریافت تصویر از یک URL مشخص:
GET /api/image?url=http://malicious-website.com/malware.jpg
کد آسیب پذیر(NET.):
[Route("api/images")] public class ImageController : ApiController { [HttpGet] public IHttpActionResult GetImage(string url) { // Fetch the image from the specified URL without proper validation using (WebClient client = new WebClient()) { byte[] imageData = client.DownloadData(url); return File(imageData, "image/jpeg"); } } // Other methods... }
پیشگیری (NET.):
[Route("api/images")] public class ImageController : ApiController { [HttpGet] public IHttpActionResult GetImage(string url) { // Validate and sanitize the URL before fetching the image if (!IsValidUrl(url)) { return BadRequest("Invalid URL"); } using (WebClient client = new WebClient()) { byte[] imageData = client.DownloadData(url); return File(imageData, "image/jpeg"); } } private bool IsValidUrl(string url) { // Implement URL validation logic here (e.g., whitelist trusted domains) // Return true if the URL is valid, otherwise false // Example validation logic: return url.StartsWith("http://trusted-domain.com"); } // Other methods... }
کد آسیب پذیر (جاوا):
@RestController @RequestMapping("/api/images") public class ImageController { @GetMapping public ResponseEntity<byte[]> getImage(@RequestParam("url") String url) throws IOException { // Fetch the image from the specified URL without proper validation URL imageUrl = new URL(url); byte[] imageData = IOUtils.toByteArray(imageUrl); return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(imageData); } // Other methods... }
پیشگیری (جاوا):
@RestController @RequestMapping("/api/images") public class ImageController { @GetMapping public ResponseEntity<byte[]> getImage(@RequestParam("url") String url) throws IOException { // Validate and sanitize the URL before fetching the image if (!isValidUrl(url)) { return ResponseEntity.badRequest().build(); } URL imageUrl = new URL(url); byte[] imageData = IOUtils.toByteArray(imageUrl); return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(imageData); } private boolean isValidUrl(String url) { // Implement URL validation logic here (e.g., whitelist trusted domains) // Return true if the URL is valid, otherwise false // Example validation logic: return url.startsWith("http://trusted-domain.com"); } // Other methods... }
پیشنهادات کلی جلوگیری:
قبل از ارسال درخواست به یک URL مشخص، بررسی و اعتبارسنجی دقیق URI و منبع مقصد.
محدود کردن قابلیت دریافت اطلاعات از منابع خارجی و محدود کردن لیست دسترسیهای مجاز به URL های راه دور.
استفاده از Whitelist برای نشان دادن تنها آدرس های معتبر و اجازه دسترسی به آنها.
اعتبارسنجی و فیلتر کردن ورودی کاربران و پارامترهای مرتبط با URL مورد استفاده قبل از استفاده از آنها در درخواست.
استفاده از محدودیتهای شبکه، مانند فایروال، برای محدود کردن دسترسی به منابع خارجی.
آموزش تیم توسعه برای ارزیابی و اعتبارسنجی صحیح URI قبل از استفاده از آن در درخواست ها.