unicode - 在经典 ASP 中过滤编码的 XSS

标签 unicode vbscript asp-classic xss

好的。我正在处理一个用 VBScript 编写的经典 ASP 应用程序。我正在尝试过滤可能通过编码查询字符串出现的 XSS。

我有一个简单的 XSS 消除功能 [getUserInput]。这会查找特殊字符,例如 < >/' .... 并将其替换为空格。效果非常好。

但是,当我输入通过 Server.URLEncode (VBScript) 或 escape (Javascript) 编码的内容时,显然我的上述过滤器不起作用。

我想知道建议的解决方案,以防止这种 Unicode 转换输入使我的页面容易受到 XSS 攻击。

重现步骤:

<%
Response.Write getUserInput(Request("txt1")) + "<br/>"
Response.Write Server.URLEncode(getUserInput(Request("txt1"))) + "<br/>"
'the below line is where I am trying to decode and echo the input
Response.Write URLDecode2((getUserInput(Request("txt1")))) + "<br/>"
Response.Write "<br/>"
%>

<html>
Try any of the below two encoded strings in the input box and hit the button. </br></br>
alert('hi') </br>
%3Cscript%3Ealert%28%27hi%27%29%3C%2Fscript%3E  <br/>
alert(document.cookie) </br>
%3Cscript%3Ealert%28document.cookie%29%3C%2Fscript%3E <br/> 
</br>
<form method="get" name="form1" id="form1" action="#" onSubmit="CallOnLoad()">
    <input type='text' name='txt1' id='txt1' />
    <button name='btn' type='submit' id='btn'>Hitme</button>
</form>
</html>

<%
function getUserInput(input)

dim newString
newString=input  
newString    = replace(newString,"--","")
newString    = replace(newString,";","")          
  newString    = replace(newString,chr(34),"'") 
  newString    = replace(newString,"'","") 
  newString    = replace(newString,"=","=") 
  newString    = replace(newString,"(","[") 
  newString    = replace(newString,")","]")
  newString = replace(newString,"'","''")
  newString = replace(newString,"<","[")
  newString = replace(newString,">","]")  
  newString = replace(newString,"/*","/") 
  newString = replace(newString,"*/","/")
  getUserInput = newString

end function
%>
<%
'URLDecode2 - source code from http://www.motobit.com/tips/detpg_URLDecode/ 
Function URLDecode2(ByVal What)

  Dim Pos, pPos
  What = Replace(What, "+", " ")
  on error resume Next
  Dim Stream: Set Stream = CreateObject("ADODB.Stream")
  If err = 0 Then 
    on error goto 0
    Stream.Type = 2 'String
    Stream.Open
    Pos = InStr(1, What, "%")
    pPos = 1
    Do While Pos > 0
      Stream.WriteText Mid(What, pPos, Pos - pPos) + _
        Chr(CLng("&H" & Mid(What, Pos + 1, 2)))
      pPos = Pos + 3
      Pos = InStr(pPos, What, "%")
    Loop
    Stream.WriteText Mid(What, pPos)
    Stream.Position = 0
    URLDecode2 = Stream.ReadText
    Stream.Close
  Else 'URL decode using string concentation
    on error goto 0
    Pos = InStr(1, What, "%")
    Do While Pos>0 
      What = Left(What, Pos-1) + _
        Chr(Clng("&H" & Mid(What, Pos+1, 2))) + _
        Mid(What, Pos+3)
      Pos = InStr(Pos+1, What, "%")
    Loop
    URLDecode2 = What
  End If
End Function
%>

我需要在 IIS 7.5/Win 2008 R2 上托管该应用程序。 请提供简单/优雅的建议?

How To: Prevent Cross-Site Scripting in ASP.NET是一篇好文章,但它并不适合我的场景,因为我正在处理经典 ASP。

谢谢。

最佳答案

I am trying to filter possible XSS

这很大程度上是浪费时间。

这种 XSS 是一个输出阶段问题,是由于将数据插入 HTML 而不对其进行 HTML 转义而引起的。

尽管“反XSS”工具和功能被严重误导,但这个问题根本无法通过输入阶段的过滤来解决。在输入时,不知道数据将在哪里结束,哪些数据需要进行 HTML 转义(而不是注入(inject)到 SQL 或 JavaScript 等其他文本上下文中),以及在数据结束之前将对该数据进行哪些处理在页面上 - 在您的示例中,您使用 URL 解码来演示这一点,但它可以是任何内容。

纠正 < 引起的问题和& ,您需要记得调用Server.HTMLEncode在您注入(inject)输出页面的每个字符串上,以便它们将被转换为 &lt;&amp;分别。这是安全的,并且还允许用户随意使用任何角色,而其中一些角色不会消失。 (想象一下尝试将此消息发布到 SO,并删除所有 < !)

这样您就不必担心 < 的编码版本,因为当您将它们放在页面上时,您已经对它们进行了解码,并且可以应用正确的编码形式,即 HTML 转义。

这与 Unicode 没有任何关系。非 ASCII 字符可以毫无问题地包含在页面中;它只是 < , & ,并且在属性值中,引号字符用作分隔符,这在注入(inject) HTML 时会带来任何危险。

过去存在过长 UTF-8 序列的问题,其中 <可能会以无效的顶部位组字节序列走私,该序列将通过 HTML 编码不变。然后 IE(和 Opera)会将其视为真实的 < 。虽然如果您使用具有 native 字节字符串而不是 Unicode 的语言来使用 UTF-8,过滤超长仍然是一个好主意,但它不再像以前那样有必要,因为当前所有主要浏览器都正确地将超长视为非功能性无效序列。

使用正确的 HTML 转义,您的示例可能如下所示(省略了未更改的 URLDecode2 函数):

<p>
    <%= Server.HTMLEncode( Request("txt1") ) %> <br/>
    <%= Server.HTMLEncode( URLDecode2(Request("txt1")) ) %>
</p>
<p>
    Try any of the below two encoded strings in the input box and hit the button.
</p>
<ul>
    <li>&lt;script>alert('hi')&lt;/script></li>
    <li>%3Cscript%3Ealert%28%27hi%27%29%3C%2Fscript%3E</li>
    <li>&lt;script>alert(document.cookie)&lt;/script></li>
    <li>%3Cscript%3Ealert%28document.cookie%29%3C%2Fscript%3E<li>
</ul>
<form method="get" action="this.asp">
    <input type="text" name="txt1" value="<%= Server.HTMLEncode( Request("txt1") ) %>"/>
    <input type="submit" value="Hitme"/>
</form>

(在 ASP.NET 4.0 中,有一个方便的 <%: 快捷方式可以为您执行 <%= Server.HTMLEncode;不幸的是,在经典 ASP 中并非如此。)

关于unicode - 在经典 ASP 中过滤编码的 XSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7598783/

相关文章:

mysql - 为什么 isNULL 在带有参数化查询的 vbScript 中返回空字符串

javascript - 使用 Javascript 和经典 ASP 的 PayPal 基础知识

c# - 为什么'Weiss'='Weiß'或通过Unicode列在SQL Server中搜索

java - 如何验证 CharSequence 的实例是否是 Unicode 标量值序列?

javascript - 使用 Javascript 下载文件

vbscript 以 block 的形式读取文件

vbscript - 如何使用 vb 脚本最大化、恢复或最小化窗口?

ajax post 在 CLASSIC asp 中获取值(value)

search - 在 Sphinx 搜索中,如何将 "hashtag"添加到 charset_table?

php - 为什么从 utf8 更改为 utf8mb4 会降低我的数据库速度?