Cross-site scripting attacks are the digital version of the mystery trope where people inject IV lines with hazardous material. In the murder mystery genre, these crimes often focus on someone who looks legitimate, sneaking malicious material into someone’s medicine to harm the patient. Similarly, a cross-site scripting attack is when a threat actor sneaks malicious code into someone’s application to harm end users.
By understanding what cross-site scripting attacks are, developers and security teams can implement best practices to mitigate risk.
What is cross-site scripting?
Cross-site scripting (XSS) attacks fall under the injection attack category where threat actors deliver malicious code by leveraging the inability for a user’s browser to verify the script’s legitimacy. Since the browser treats the application as a trusted site, it executes the malicious code. An XSS vulnerability can exist any time an application allows users to supply input and then outputs it to a page without validating, encoding, or escaping it.
XSS attacks can be categorized based on where the attack originates:
- Server XSS: vulnerability in the server-side code with browser executing embedded malicious script
- Client XSS: vulnerability in client-side code with malicious script coming from a request or stored location on the client or server
How does XSS work?
While all XSS attacks execute in the browser, they differ based on where the attackers introduce the malicious code. Understanding an XSS attack’s mechanics, types, and exploitation methodologies help mitigate risk. OWASP covers this at length for more information click here.
Reflected XSS Attacks (Non-Persistent or Type I)
Reflected XSS attacks are the most common cross-site scripting types. A reflected XSS attack typically follows these steps:
- Malicious link contains attack string in the URI or HTTP parameters.
- Application passes unvalidated input to client.
- Victim’s browser executes the malicious code, often malware that installs keyloggers, steals cookies, performs clipboard theft, and changes page content.
Detecting and analyzing input vectors may include the following:
- Testing all user-defined variables and how users can input them
- Reviewing hidden or less obvious inputs like HTTP parameters, POST data, hidden form field value, predefined selection values
- Examining HTML page results for the test input
- Identifying any improperly encoded, replaced, or filtered special characters
Stored XSS Attacks Stored XSS (Persistent or Type II)
Stored XSS attacks occur when users visit a page containing a malicious script, rather than using a malicious link. A stored XSS attack typically follows these steps:
- Attacker inserts malicious code into a vulnerable page.
- User logs into compromised application.
- Users visit vulnerable pages.
- User’s browser executes the malicious code.
Blind XSS is a persistent XSS subcategory. In these attacks, threat actors store the payload on the server so that the backend application reflects it to the victim.
Detecting and analyzing input vectors may include the following:
- Identifying back-end locations that store user information that the application later displays, including User/Profiles pages, shopping carts, file managers, setting/preferences, messaging, comments, or logs
- Determining whether the page stores inputs
- Reviewing input validation and filtering controls
- Reviewing file upload functionalities for vulnerabilities that allow innocent-looking file types to hide payloads
DOM-based XSS
DOM-based XSS attacks exploit the Document Object Model (DOM), the structural format that defines how a browser represents documents. By controlling the code’s flow using the DOM elements, the malicious code often executes without the server knowing what happened, allowing the attack to bypass typical XSS filtering and detection techniques.
Detecting and analyzing potential vectors for DOM-based XSS attacks can be more difficult because malicious attackers target how the user’s browser renders the code. Detecting and analyzing input vectors may include the following:
- Reviewing the order for how the application renders content within the execution context
- Limiting or avoiding user inserted data when using a JavaScript event handler
- Limiting or avoiding untrusted data as code or markup within JavaScript
- Reviewing encoded and delimited untrusted data for quoted strings
- Identifying dangerous attributes and replacing them with safe attributes
- Avoiding populating HTML rendering methods with untrusted data
- Reviewing methods for passing untrusted data for issues that might impact how the browser renders it
Can APIs be vulnerable to XSS?
APIs can have XSS vulnerabilities that attackers target any time they accept user-supplied data and then return the data as any of the following:
- HTML
- XML
- JSON
If attackers can identify the API endpoints, they make changes to the application through the API’s code. Instead of compromising the application by gaining direct access to it, they can use the APIs to store payloads. For example, attackers might use an API to deliver malicious code to the comments section in an online store which would then compromise any end users who load those comments on their devices.
What is the impact of an XSS attack?
A successful XSS attack can lead to:
- Account takeover: stealing session cookies to hijack accounts and hide as legitimate users
- Unauthorized activities: controlling administrative accounts to view, modify, or steal sensitive information
- Spread malware: forwarding users to malicious links that execute malware to steal credentials, create botnets, or spread ransomware
- Credential theft: spoofing login pages to steal user credentials and forward them to a command and control (C2) server
Best Practices for Mitigating XSS Attack Risk
Shifting security left – implementing application security during the development process – and monitoring application activities can mitigate XSS attack risks.
Automate Vulnerability Scanning
Scanning source code can:
- Identify all third-party components
- Vulnerabilities across source code and third-party components
- Data flows across the application to prioritize vulnerability remediation
Engage in Manual Testing
Manual testing best practices should include reviewing for:
- Output encoding: preventing variables from being displayed in plain text
- HTML sanitization: stripping HTML from a variable to return a safe string that allows styling to be rendered
- Safe sinks: places where the application treats variables as text and never executes them
- Cookie attributes: defining how JavaScript and browsers interact with cookies
Log and monitor activity
Developers should ensure that they capture detailed request data like:
- HTTP headers
- Parameters
- User inputs
By incorporating application logs into your overarching security monitoring, you can create alerts that identify anomalous activity indicating a potential XSS attack.
Graylog: Security Monitoring to Mitigate XSS Attack Risk
Graylog’s purpose-built solution provides lightning-fast search capabilities and flexible integrations that allow your team to collaborate more efficiently. Since you can easily share Dashboards and searches with Graylog’s cloud platform, you have the ability to capture, manage, and share knowledge consistently across DevOps, operations, and security.
With Graylog Security analytics and anomaly detection capabilities, you get the cybersecurity platform you need without the complexity that makes your team’s job harder. With our powerful, lightning-fast features and intuitive user interface, you can lower your labor costs while reducing alert fatigue and getting the answers you need – quickly.
Our prebuilt search templates, dashboards, correlated alerts, and dynamic look-up tables enable you to get immediate value from your logs while empowering your security team.