表格

2015-11-04 22 views
2

所有防止XSS攻擊:表格

我有一個項目,我使用C#MVC4

在項目工作的問題,我接受URL等參數從用戶,然後進行一些處理並將處理結果發送到由用戶提供的URL

使用以下代碼被髮送其結果是:

var context = HttpContext.Current; 

context.Response.Write("<html><head>"); 
context.Response.Write("</head><body>"); 

context.Response.Write(string.Format("<form name=\"myform\" method=\"post\" action=\"{0}\" >", postUrl)); 

context.Response.Write("</form>"); 
context.Response.Write("<script type=\"text/javascript\">document.myform.submit();</script></body></html>"); 

context.Response.Write("</body>"); 
context.Response.Flush(); 
context.Response.Clear(); 
context.ApplicationInstance.CompleteRequest(); 

每當用戶試圖進行XSS等傳遞的一個URL值的javascript%3aalert( 'XSS')%2F%2F,JavaScript的運行並彈出顯示。

我已經嘗試Antixss.HtmlEncode()在將URL傳遞到string.Format之前對其進行編碼,但仍然無效。我也試過Antixss.UrlEncode(),但是這樣做會導致錯誤,因爲表單沒有提交到URL。

請幫我一下,有什麼我失蹤?我還可以做些什麼?

在此先感謝。

+0

追加元素對象到DOM以及使用該用戶提供的值的那些對象的設置的屬性,其迴避所有插值攻擊......將用戶給定值作爲JSON發送。 – dandavis

+0

對不起,我不好。請提供示例代碼。 – Hardeshorlar

+0

我沒有經驗的C#但這似乎是一個很好的解決方案:http://weblogs.asp.net/jongalloway/preventing-javascript-encoding-xss-attacks-in-asp-net-mvc – kazandzhiro

回答

1

您需要三方面的方法來解決這個問題。

防止XSS注射:

注意,如果用戶注入的URL值

" /> <script>alert('xss')</script> 

,這也將讓你容易受到傷害:

<form name="myform" method="post" action="" /> <script>alert('xss')</script>" > 

因此,你應該使用HttpUtility.HtmlAttributeEncode功能解決這個問題。

但是,不要停在那裏。如上所述,您應該針對javascript:樣式網址進行投放。爲此,我會確保網址以http://https://開頭。如果沒有,請拋出一個SecurityException,您應該記錄並處理服務器端,並向用戶顯示一個自定義錯誤頁面。

最後,你想防止Open Redirect Vulnerabilities。這是通過將用戶重定向到其他域來阻止釣魚攻擊。再次,使用白名單方法並確保將域重定向到您自己的域名。請注意這裏的解析,因爲很容易弄錯 - 一個http://example.org?http://example.com的URL將通過許多寫得很差的驗證例程的驗證過濾器example.com。我建議在.NET中使用Uri對象,並通過該對象檢索域,而不是滾動您自己的字符串函數。

您也可以檢查該URL是否爲相對URL,如果可以接受則允許該URL。使用類似this function的東西,它使用內置的.NET庫來確保它是相對與否。

0

只是一個想法 - 嘗試把這個腳本中,而不是僅僅document.myform.submit(和清除表格的action屬性):

if("{0}".indexOf('http') !== 0) { 
    //this is some sort of injection, or it's pointing at something on your server. Should cover http and https. 
    //Specifically, it makes sure that the url starts with 'http' - so a 'javascript' url isn't going to work. 
} else { 
    document.myform.action="{0}" 
    document.myform.submit(); 
} 

還有更多你可以做,但是這應該幫助。

0

既然你要添加的postUrl作爲表單標籤的屬性「行動」,你可以嘗試使用HtmlAttributeEncode方法在HttpUtility

[ValidateInput(false)] 
public ActionResult Test(string url) 
{ 
    var context = System.Web.HttpContext.Current; 

    context.Response.Write("<html><head>"); 
    context.Response.Write("</head><body>"); 

    context.Response.Write(string.Format("<form name=\"myform\" method=\"post\" action=\"{0}\" >", HttpUtility.HtmlAttributeEncode(url))); 

    context.Response.Write("</form>"); 
    context.Response.Write("<script type=\"text/javascript\">document.myform.submit();</script></body></html>"); 

    context.Response.Write("</body>"); 
    context.Response.Flush(); 
    context.Response.Clear(); 
    context.ApplicationInstance.CompleteRequest(); 
    return null; 
} 

http://localhost:39200/home/test?url=https%3A%2F%2Fwww.google.com - 曾爲 http://localhost:39200/home/test?url=%3Cscript%3Ealert(%27test%27)%3C%2Fscript%3E - 工作(沒有顯示警告)

根據輸入的白名單驗證用戶輸入始終是一種良好的做法,以防止XSS漏洞利用。