请引用下面我的代码来模拟XSS攻击。但不幸的是它不起作用。
现在当我输入 <script type="text/javascript">alert("Hacked")</script>
,不应该抛出一个消息框。但它没有显示。尽管我可以在查看源代码中以未编码的形式看到它。这应该是一些小错误,但我无法弄清楚。
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
ViewBag.Message = "Nothing entered";
return View();
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Index(string Id)
{
ViewBag.Message = Id;
return View();
}
}
查看代码是
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<form method="post">
<input type="text" name="Id" />
<input type="submit" name="submit" value="submit"/>
</form>
<h2>The Data you entered is</h2>
<h1>@Html.Raw(ViewBag.Message)</h1>
最佳答案
Now when I enter alert("Hacked"), shouldnt a message box be thrown.
不,当然不是。 alert("Hacked")
是一个 javascript 函数,而 <h1>
内有 Html.Raw标签。
所以你最终得到以下标记:
<h1>alert("Hacked")<h1>
您不能指望从中抛出任何消息框。如果您想扔东西,请确保您正确破解它,即在文本框中输入以下内容:
<script>alert("Gotcha this time")</script>
关于javascript - 模拟XSS攻击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21351451/