
الـ SSRF: لما السيرفر بتاعك يجيب URL، المهاجم يوصل لجوه الـ network بتاعتك
- كاتب موثّق
- OWASP Top 10 2025
محتويات المقال
localhost و127.x من أسابيع. والـ URL بتاعها ماكانش فيه ولا واحد منهم.
الفكرة الغلط الشائعة
اللي بيحصل فعلًا
السيرفر بتاعك بيبقى proxy للمهاجم
دي مشكلة access control
نص الـ URL مش هو المكان اللي رايحله
System.Uri بيعمل normalize لشوية طرق كتابة: في التجربة بتاعتنا، 2130706433 بقت 127.0.0.1 والـ check مسكها. بس [::ffff:127.0.0.1]، وهو نفس عنوان الـ مكتوب كـ IPv6، عدّى على طول، وكمان أي hostname الـ DNS record بتاعه بيشاور لجوه. والـ redirects طريقة تانية للالتفاف: OWASP بتقول "Disable the support for the following of the redirection in your web client". والـ بيهزم الـ checks اللي بتعمل resolve للاسم مرة وتتصل بعدين: "Validate the resolved destination IP addresses against the application's permitted networks, then ensure the HTTP client connects only to validated addresses."
الـ cloud metadata هي الهدف الكلاسيكي
من check على الـ string لـ check على الاتصال
var builder = WebApplication.CreateBuilder(args);builder.Services.AddHttpClient();var app = builder.Build();app.MapGet("/import", async (string url, IHttpClientFactory factory) =>{ var uri = new Uri(url); // Looks careful: blocks localhost, 127.x and the cloud metadata address if (uri.Host is "localhost" or "169.254.169.254" || uri.Host.StartsWith("127.")) return Results.BadRequest("Blocked"); var bytes = await factory.CreateClient().GetByteArrayAsync(uri); return Results.File(bytes, "image/jpeg");});app.Run();using System.Net;using System.Net.Sockets;public static class SafeFetch{ static readonly IPNetwork[] Blocked = [.. new[] { "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", "192.168.0.0/16", "224.0.0.0/4", "::/128", "::1/128", "fc00::/7", "fe80::/10", "ff00::/8" }.Select(n => IPNetwork.Parse(n))]; public static HttpClient Create() => new(new SocketsHttpHandler { AllowAutoRedirect = false, // a redirect would skip the check ConnectCallback = async (ctx, ct) => { // Check the addresses we're about to connect to, not the URL text var ips = (await Dns.GetHostAddressesAsync(ctx.DnsEndPoint.Host, ct)) .Select(a => a.IsIPv4MappedToIPv6 ? a.MapToIPv4() : a).ToArray(); if (ips.Length == 0 || ips.Any(a => Blocked.Any(n => n.Contains(a)))) throw new HttpRequestException($"Blocked: {ctx.DnsEndPoint.Host}"); var socket = new Socket(SocketType.Stream, ProtocolType.Tcp); await socket.ConnectAsync(ips, ctx.DnsEndPoint.Port, ct); return new NetworkStream(socket, ownsSocket: true); }, });}ConnectCallback هو، بكلام Microsoft، "a custom callback used to open new connections"، فالـ check بيشتغل على نفس العناوين اللي الـ socket هيستخدمها، مع كل اتصال جديد. شغّلنا الاتنين على .NET 10 قصاد service "داخلي" للتجربة بتاعنا، شغال على الـ loopback على port 5401، وفي الـ response بتاعه key مزيّف. والـ endpoint الآمن كان بيستخدم SafeFetch.Create() وبيرجّع 400 لما يحصل HttpRequestException:
/import?url=http://localhost:5401/internal/config -> 400 "Blocked"/import?url=http://[::ffff:127.0.0.1]:5401/internal/config -> 200 {"service":"billing-admin","apiKey":"FAKE-INTERNAL-KEY"}/import-safe?url=http://[::ffff:127.0.0.1]:5401/internal/config -> 400 "Blocked: [::ffff:127.0.0.1] ([::ffff:127.0.0.1]:5401)"SafeFetch فيها الـ ranges الأساسية اللي في جدول الـ deny-list بتاع OWASP، وزيادة عليها شوية، زي range الـ carrier-grade NAT. وهي الحل الاحتياطي للـ features اللي لازم تقبل أي URL عام. ولما تبقى عارف الأماكن، الـ allow-list أقوى.
تطبقها إزاي في مشروعك
لاقي كل fetch على السيرفر المستخدم يقدر يأثّر فيه الـ imports بالـ URL، والـ webhooks، والـ link previews، والـ renderers بتاعة الـ PDF والـ HTML، والـ XML parsers اللي بتقبل external entities. وكل واحد منهم مكان المستخدم يقدر يختار فيه رايح فين. اعمل allow-list في أي حتة الأماكن فيها معروفة خزّن الـ hosts المسموحة في الـ configuration، وثبّت الـ scheme والـ port والـ path في الكود، وارفض أي حاجة تانية بدل ما تحاول تنضّفها. اتأكد من الاتصال، مش من الـ string، في الـ URLs المفتوحة استخدم handler زي SafeFetch: اعمل resolve، واتأكد من كل عنوان، واتصل بس بالعناوين اللي اتأكدت منها، وخلّي الـ redirects مقفولة.حدّد الـ egress من الـ network ادّي الـ services اللي بتعمل fetch قواعد outbound خاصة بيها، وامنع الـ ranges الداخلية وعنوان الـ metadata، واطلب IMDSv2 على الـ instances بتاعة AWS. حدّد اللي بيرجع حط حد للحجم والوقت، واتأكد من الـ content type، ورجّع نتيجة متعالجة بدل الـ response الخام.
الخلاصة
الـ SSRF بيحوّل السيرفر بتاعك لـ proxy بصلاحيات الـ network بتاعتك؛ و"من جوه الـ network بتاعتنا" هي الخطر نفسه. الـ OWASP Top 10:2025 بيحط الـ SSRF تحت A01 Broken Access Control. الـ checks على نص الـ URL بتفوّت طرق الكتابة التانية والـ DNS records والـ redirects؛ اتأكد من الـ IPs وقت الاتصال. فضّل الـ allow-lists وابني الـ URLs بنفسك؛ وخلّي الـ deny-lists حل احتياطي للـ URLs المفتوحة. ضيف قواعد egress في الـ network وIMDSv2، عشان check واحد فاتك مايكشفش الـ cloud credentials.
📚 المصادر
Server-Side Request Forgery Prevention Cheat Sheet — OWASP Cheat Sheet Series A01:2025 Broken Access Control — OWASP Top 10:2025 Server Side Request Forgery — OWASP Community Use the Instance Metadata Service to access instance metadata — Amazon EC2 SocketsHttpHandler.ConnectCallback — .NET API, Microsoft Learn
التعليقات (0)