我是一名网络安全专业的学生,试图了解一些基本的HTML注入。我已经在这段代码上工作了几天,无法理解我做错了什么。我当前拥有的代码允许注入,例如,如果将<h1>test</h1>
放到文本框中,它将显示test作为标题。但是,如果我尝试<script>alert(1)</script>
,则实际上不会运行该脚本。我尝试将文本框的值设置为“”,或者想到可以通过在文本框中输入以下内容来结束该行:"><script>alert(1)</script>
我还试图通过在末尾添加注释来取消代码的其余部分:<script>alert(1)</script><!--
我尝试了每种组合的许多组合,但没有运气。现在我实际上需要能够注入脚本,因为我正在使用CSP以及它如何影响将脚本注入到网页中。我目前没有指定csp来限制JavaScript的运行。我尝试过的其他操作包括使用不同的浏览器,更改浏览器安全性以及确保在浏览器中启用了JavaScript。任何帮助将不胜感激!
<html>
<script language='JavaScript'>
function getwords(){
textbox = document.getElementById('words');
label = document.getElementById('label');
label.innerHTML = textbox.value;
}
</script>
<body>
<input type="text" id="words">
<input type="button" onclick="getwords()" id="Button" value="Enter" />
<label id="label">
</label>
</body>
</html>
这是因为<script>
在页面加载时运行,并且当label
的内容更改时,脚本已经运行。
但是,如果将<script>
标记插入另一个页面(通过后端(XSS表示Cross-Site脚本)),它确实可以工作。
或者,使其在注入内容[[页面加载后(如您的情况)的情况下起作用,可以使用JS事件(如onclick
)来运行代码:
<div onclick="alert(1)">Click me!</div>
或者,要在没有用户交互的情况下执行它,您可以使用<iframe>
的onload
事件:
<iframe onload="alert(1)" style="display:none"></iframe>
<iframe src=javascript:alert(1)>
或
<img src=x onerror=alert(1)>
也值得注意:[script
元素,使用innerHTML
插入]不会在执行时执行插入。
无需编辑HTML文件,将其添加到浏览器的输入字段中。
<iframe onload="alert(1)" style="display:none"></iframe>
有关why this works here的更多信息有关如何执行此处操作的更多信息:developer.mozilla.org
<html> <script language='JavaScript'> function getwords(){ textbox = document.getElementById('words'); label = document.getElementById('label'); label.innerHTML = textbox.value; } </script> <body> <input type="text" id="words"> <input type="button" onclick="getwords()" id="Button" value="Enter" /> <label id="label"> </label> </body> </html>