2015-04-29 7 views
10

मैंने विजुअल स्टूडियो 2013 का उपयोग करके एक एएसपी.Net वेब फॉर्म एप्लिकेशन बनाया है और मैं डॉट नेट फ्रेम वर्क 4.5 का उपयोग कर रहा हूं, और मैं यह सुनिश्चित करना चाहता हूं कि मेरा साइट क्रॉस साइट रिक्वेस्ट फोर्जरी (CSRF) से सुरक्षित है, मैं कई लेख इस सुविधा MVC क्षुधा पर कार्यान्वित किया जाता है, लेकिन एक बहुत कुछ, वेब प्रपत्रों के बारे में बात this StackOverflow question एक टिप्पणी पर के बारे में बात पाया है बताते हुए की जाती है किएएसपीनेट वेब फॉर्मों में क्रॉस-साइट अनुरोध जालसाजी (सीएसआरएफ) हमलों को रोकने से

"यह एक पुराना सवाल है, लेकिन नवीनतम रूपों के लिए नवीनतम विजुअल स्टूडियो 2012 एएसपी.नेट टेम्पलेट में एंटी-सीएसआरएफ कोड मास्टर पृष्ठ में बेक किया गया है। यदि आपके पास टेम्पलेट्स नहीं हैं, तो यह कोड है उत्पन्न करता है: ... "

लेकिन मेरे मास्टर पेज में उनके उत्तर में वर्णित कोड नहीं है, तो क्या कोई मेरी मदद कर सकता है? क्या यह वास्तव में लागू किया गया है? यदि नहीं, तो कृपया सलाह दें कि ऐसा करने का सबसे अच्छा तरीका क्या है?

+0

संभावित डुप्लिकेट [क्रॉस-साइट अनुरोध फोर्जरी रोकें] (http: // stackoverflow।कॉम/प्रश्न/2467577 9/रोक-क्रॉस-साइट-अनुरोध-जालसाजी) – SilverlightFox

+0

@ सिल्वरलाइटफॉक्स, आशा नहीं;) –

उत्तर

10

ViewStateUserKey & डबल कुकी जमा करें

Visual Studio 2012 के साथ शुरू, माइक्रोसॉफ्ट जोड़ा निर्मित नई वेब प्रपत्रों आवेदन परियोजनाओं के लिए CSRF संरक्षण। इस कोड का उपयोग करने के लिए, अपने समाधान में एक नया एएसपी .NET वेब फॉर्म एप्लिकेशन जोड़ें और साइट के पीछे साइट। मस्टर कोड देखें। यह समाधान साइट से प्राप्त सभी सामग्री पृष्ठों पर सीएसआरएफ सुरक्षा लागू करेगा। मस्टर पेज।

सभी वेब डेटा संशोधन करने के रूपों Site.Master पेज का उपयोग करना चाहिए:

निम्न आवश्यकताओं को इस समाधान के लिए काम करने के लिए पूरा किया जाना चाहिए। डेटा संशोधनों को बनाने वाले सभी अनुरोधों को व्यूस्टेट का उपयोग करना चाहिए। वेबसाइट सभी क्रॉस-साइट स्क्रिप्टिंग (एक्सएसएस) भेद्यता से मुक्त होना चाहिए। विवरण के लिए माइक्रोसॉफ्ट .Net वेब प्रोटेक्शन लाइब्रेरी का उपयोग कर क्रॉस-साइट स्क्रिप्टिंग (एक्सएसएस) को कैसे ठीक करें देखें।

public partial class SiteMaster : MasterPage 
{ 
    private const string AntiXsrfTokenKey = "__AntiXsrfToken"; 
    private const string AntiXsrfUserNameKey = "__AntiXsrfUserName"; 
    private string _antiXsrfTokenValue; 

    protected void Page_Init(object sender, EventArgs e) 
    { 
    //First, check for the existence of the Anti-XSS cookie 
    var requestCookie = Request.Cookies[AntiXsrfTokenKey]; 
    Guid requestCookieGuidValue; 

    //If the CSRF cookie is found, parse the token from the cookie. 
    //Then, set the global page variable and view state user 
    //key. The global variable will be used to validate that it matches 
    //in the view state form field in the Page.PreLoad method. 
    if (requestCookie != null 
     && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue)) 
    { 
     //Set the global token variable so the cookie value can be 
     //validated against the value in the view state form field in 
     //the Page.PreLoad method. 
     _antiXsrfTokenValue = requestCookie.Value; 

     //Set the view state user key, which will be validated by the 
     //framework during each request 
     Page.ViewStateUserKey = _antiXsrfTokenValue; 
    } 
    //If the CSRF cookie is not found, then this is a new session. 
    else 
    { 
     //Generate a new Anti-XSRF token 
     _antiXsrfTokenValue = Guid.NewGuid().ToString("N"); 

     //Set the view state user key, which will be validated by the 
     //framework during each request 
     Page.ViewStateUserKey = _antiXsrfTokenValue; 

     //Create the non-persistent CSRF cookie 
     var responseCookie = new HttpCookie(AntiXsrfTokenKey) 
     { 
     //Set the HttpOnly property to prevent the cookie from 
     //being accessed by client side script 
     HttpOnly = true, 

     //Add the Anti-XSRF token to the cookie value 
     Value = _antiXsrfTokenValue 
     }; 

     //If we are using SSL, the cookie should be set to secure to 
     //prevent it from being sent over HTTP connections 
     if (FormsAuthentication.RequireSSL && 
      Request.IsSecureConnection) 
     { 
     responseCookie.Secure = true; 
     } 

     //Add the CSRF cookie to the response 
     Response.Cookies.Set(responseCookie); 
    } 

    Page.PreLoad += master_Page_PreLoad; 
    } 

    protected void master_Page_PreLoad(object sender, EventArgs e) 
    { 
    //During the initial page load, add the Anti-XSRF token and user 
    //name to the ViewState 
    if (!IsPostBack) 
    { 
     //Set Anti-XSRF token 
     ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey; 

     //If a user name is assigned, set the user name 
     ViewState[AntiXsrfUserNameKey] = 
      Context.User.Identity.Name ?? String.Empty; 
    } 
    //During all subsequent post backs to the page, the token value from 
    //the cookie should be validated against the token in the view state 
    //form field. Additionally user name should be compared to the 
    //authenticated users name 
    else 
    { 
     //Validate the Anti-XSRF token 
     if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue 
      || (string)ViewState[AntiXsrfUserNameKey] != 
       (Context.User.Identity.Name ?? String.Empty)) 
     { 
     throw new InvalidOperationException("Validation of " + 
          "Anti-XSRF token failed."); 
     } 
    } 
    } 
} 
+0

पर ध्यान न दें मैं VS2017 का उपयोग कर रहा हूं और मुझे यह कोड Site.Master.cs (ब्रांड नई वेब फॉर्म एप्लिकेशन प्रोजेक्ट) में नहीं दिखाई देता है। – joym8

6

जब आप वीएस 2013 में एक नया 'वेब फॉर्म एप्लिकेशन' प्रोजेक्ट बनाते हैं, तो साइट.मास्टर.cs कक्षा के Page_Init अनुभाग में स्वचालित रूप से एक्सएसआरएफ/सीएसआरएफ कोड शामिल कर देगा। यदि आपको अभी भी जेनरेट कोड नहीं मिलता है, तो आप मैन्युअल रूप से Copy + Paste कोड कर सकते हैं। आप सी # का उपयोग कर रहे हैं, तो नीचे दिए गए का उपयोग करें: -

private const string AntiXsrfTokenKey = "__AntiXsrfToken"; 
private const string AntiXsrfUserNameKey = "__AntiXsrfUserName"; 
private string _antiXsrfTokenValue; 

protected void Page_Init(object sender, EventArgs e) 
    { 
     // The code below helps to protect against XSRF attacks 
     var requestCookie = Request.Cookies[AntiXsrfTokenKey]; 
     Guid requestCookieGuidValue; 
     if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue)) 
     { 
      // Use the Anti-XSRF token from the cookie 
      _antiXsrfTokenValue = requestCookie.Value; 
      Page.ViewStateUserKey = _antiXsrfTokenValue; 
     } 
     else 
     { 
      // Generate a new Anti-XSRF token and save to the cookie 
      _antiXsrfTokenValue = Guid.NewGuid().ToString("N"); 
      Page.ViewStateUserKey = _antiXsrfTokenValue; 

      var responseCookie = new HttpCookie(AntiXsrfTokenKey) 
      { 
       HttpOnly = true, 
       Value = _antiXsrfTokenValue 
      }; 
      if (FormsAuthentication.RequireSSL && Request.IsSecureConnection) 
      { 
       responseCookie.Secure = true; 
      } 
      Response.Cookies.Set(responseCookie); 
     } 

     Page.PreLoad += master_Page_PreLoad; 
    } 

    protected void master_Page_PreLoad(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      // Set Anti-XSRF token 
      ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey; 
      ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty; 
     } 
     else 
     { 
      // Validate the Anti-XSRF token 
      if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue 
       || (string)ViewState[AntiXsrfUserNameKey] != (Context.User.Identity.Name ?? String.Empty)) 
      { 
       throw new InvalidOperationException("Validation of Anti-XSRF token failed."); 
      } 
     } 
    } 
+0

हाँ मैं सी # का उपयोग कर रहा हूं, और मैंने इस फ़ंक्शन को कॉपी और पिछले करने की कोशिश की, लेकिन ऐसा लगता है कि इसमें कई त्रुटियां हैं। क्या आप वाकई ऑटो जनरेट कोड हैं? –

+0

यह पहचान नहीं रहा है: Request.Cookies में AntiXsrfTokenKey [AntiXsrfTokenKey]; और इन चरों को पहचानने के साथ-साथ _antiXsrfTokenValue, master_Page_PreLoad –

+0

@NadaNaeem में मुझे अनुपस्थित स्थिरांक शामिल नहीं हैं। –

15

आप निम्न का प्रयास कर सकते हैं। वेब-फॉर्म में जोड़ें:

<%= System.Web.Helpers.AntiForgery.GetHtml() %> 

यह एक छिपे हुए फ़ील्ड और कुकी को जोड़ देगा। तो अगर आप कुछ फार्म डेटा को भरें और इसे वापस सर्वर आप एक सरल जांच की जरूरत पर पोस्ट करें: यदि विरोधी XSFR जांच में विफल रहता है

protected void Page_Load(object sender, EventArgs e) 
{ 
if (IsPostBack) 
AntiForgery.Validate(); 
} 

AntiForgery.Validate(); एक अपवाद फेंकता है।

+0

एंटीफ़ोर्गेरी जोड़ने के लिए नेमस्पेस क्या है। मैंने system.web.helper को शामिल करने की कोशिश की लेकिन इसमें एंटीफॉर्गेरी नहीं है !!!! – sarathkumar

+2

नामस्थान सिस्टम.Web.Helpers Microsoft.AspNet.WebPages Nuget-Package में है। – Saftpresse99

-4

आप इसे जहां

if ((context.Request.UrlReferrer == null || context.Request.Url.Host != context.Request.UrlReferrer.Host)) 
    { 
     context.Response.Redirect("~/error.aspx", false); 
    } 

से आ रही है कोड का टुकड़ा नीचे इस्तेमाल कर सकते हैं, जो अनुरोध की जाँच करेगा यह मेरे लिए अच्छा काम करता है !!!

+1

यह गलत और खतरनाक है। यह केवल उसी मेजबान में सीएसआरएफ को सीमित करता है, और अगर रेफरर हेडर छीन लिया जाता है तो संभावित रूप से वास्तविक अनुरोध तोड़ सकता है, जो अपेक्षाकृत आम है। – Steve

संबंधित मुद्दे