XSS(Cross-Site Scripting,跨站腳本攻擊)是一種常見的 Web 安全漏洞,攻擊者透過向網站輸入惡意 JavaScript 程式碼,使其在其他使用者的瀏覽器中執行。這可能導致惡意重導、竊取 Cookie、偽造請求等安全風險。
在 ASP.NET Web Forms 應用程式中,如果沒有適當處理使用者輸入,攻擊者可以透過 <script>
標籤插入惡意指令碼。
假設我們有一個簡單的表單,讓使用者輸入姓名並顯示歡迎訊息。
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" ValidateRequest="false" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<main>
<section class="row" aria-labelledby="aspnetTitle">
<h1 id="aspnetTitle">ASP.NET</h1>
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS, and JavaScript.</p>
<p><a href="http://www.asp.net" class="btn btn-primary btn-md">Learn more »</a></p>
</section>
<div class="row">
<section class="col-md-4" aria-labelledby="gettingStartedTitle">
<h2 id="gettingStartedTitle">Getting started</h2>
<p>
ASP.NET Web Forms lets you build dynamic websites using a familiar drag-and-drop, event-driven model.
A design surface and hundreds of controls and components let you rapidly build sophisticated, powerful UI-driven sites with data access.
</p>
<p>
<a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301948">Learn more »</a>
</p>
</section>
<section class="col-md-4" aria-labelledby="librariesTitle">
<h2 id="librariesTitle">Get more libraries</h2>
<p>
NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.
</p>
<p>
<a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301949">Learn more »</a>
</p>
</section>
<section class="col-md-4" aria-labelledby="hostingTitle">
<h2 id="hostingTitle">Web Hosting</h2>
<p>
You can easily find a web hosting company that offers the right mix of features and price for your applications.
</p>
<p>
<a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301950">Learn more »</a>
</p>
</section>
</div>
<div>
<asp:Label ID="Label1" runat="server" Text="請輸入您的名字:" />
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="送出" OnClick="Button1_Click" CssClass="btn btn-primary"/>
<br /><br />
<asp:Label ID="ResultLabel" runat="server" ForeColor="Blue" />
</div>
</main>
</asp:Content>
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim userName As String = TextBox1.Text.Trim()
If userName <> "" Then
ResultLabel.Text = "Hello, " & userName & "!歡迎來到 ASP.NET Web Forms!"
Else
ResultLabel.Text = "請輸入您的名字!"
End If
End Sub
模擬 XSS 測試
正常頁面如下
如果用戶輸入以下內容:
<script>alert('XSS 測試成功!')</script>
這段腳本將會被執行,導致彈出提示框,這就代表網站存在 XSS 漏洞。
Server.HtmlEncode()
來轉譯輸入如果你的應用程式不需要 HTML 標籤,那麼最簡單的方法就是對所有輸入進行 HTML 編碼,防止 JavaScript 執行。
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim userName As String = TextBox1.Text.Trim()
If userName <> "" Then
ResultLabel.Text = "Hello, " & Server.HtmlEncode(userName) & "!歡迎來到 ASP.NET Web Forms!"
Else
ResultLabel.Text = "請輸入您的名字!"
End If
End Sub
這樣即使輸入 <script>alert('XSS')</script>,也只會顯示成純文字,而不會執行 JavaScript。
XSS(跨網站指令碼攻擊)是一種常見的 Web 安全漏洞,攻擊者可以利用未經過濾的輸入來注入惡意腳本,進而影響其他用戶的瀏覽體驗,甚至竊取敏感資訊,透過防護措施,我們可以有效降低 XSS 攻擊的風險,確保 ASP.NET 應用程式的安全性,提供用戶更可靠的使用體驗。
如果你有興趣進一步學習,請留言告訴我或留言與我分享~~~~