3

I am a cybersecurity student trying to understand some basic HTML injections. I have been working on this code for a few days and can't understand what I am doing wrong. The code that I have currently does allow for injection, for example if I put <h1>test</h1> into the textbox, it will display test as a header. But if I try <script>alert(1)</script> it won't actually run the script. I have tried setting the value of the text box to "" or with the thought that I could close out that line by inputting the following into the textbox: "><script>alert(1)</script>

I've also tried to cancel out the remainder of the code by adding a comment to the end like this: <script>alert(1)</script><!--

I've tried a number of combinations of each with no luck. Now I actually need to be able to inject a script since I'm playing around with CSP and how that affects injection of scripts into the webpage. I currently DO NOT have a csp specified that would restrict the JavaScript from running. Some other things I've tried include using different browsers, changing browser security, and ensuring that JavaScript is enabled in the browser. Any help would be greatly appreciated!!

<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>
1

3 Answers 3

3

That's because <script>s run at page load, and, when the label's content change, the scripts have ran already.

However, if you inject <script> tags to a different page (through the backend (XSS means Cross-Site Scripting)), it does work.

Alternatively, to make it work in a scenario, where the content injected after page load (like your case), you can use JS events (like onclick) to run your code:

<div onclick="alert(1)">Click me!</div>

Or, to execute it without user interaction, you could use an <iframe>'s onload event:

<iframe onload="alert(1)" style="display:none"></iframe>
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for the response, that definitely helped. I have another question though, is there anyway to specify the source in which the javascript would be ran from? The current csp that I'm using restricts the src to self and *.google.com
Like script src? Basically no; but you can try an AJAX request, if supports CORS, and then eval it
Alright so going back to the xss portion, if in my code I added a window to popup that maybe set something to the input of that textbox, the script would run since the new page loaded on click of the button?
@xswarms 'if in my code I added a window to popup that maybe set something to the input of that textbox, the script would run since the new page loaded on click of the button?' - Sorry, I don't understand what do you ask...
Essentially, I am wanting it to actually run when I put in <script>alert(1)</script>. In reference to the portion that you said "if you inject <script> tags to a different page (through the backend (XSS means Cross-Site Scripting)), it does work." I am wondering if I set the button to open a new window, and in that window have it print the textbox value if it will actually run a script if inserted.
|
2

to execute javascript from your form, you can try:

<iframe src=javascript:alert(1)>

or

<img src=x onerror=alert(1)>

Also worth noting:

script elements inserted using innerHTML do not execute when they are inserted.

2 Comments

Could you add a source for that last quote?
0

To manually execute JavaScript, you may do the following

without editing your HTML file, add this to the Input field on your Browser.

<iframe onload="alert(1)" style="display:none"></iframe>

More information on why this works here

More on how you can perform actions like this 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.