A simple img src attribute can be a vulnerable entry point for attackers.
This is because the img src attribute can be manipulated by an attacker to inject malicious JavaScript code.
Imagine an attacker adding a JavaScript alert() function to the img src attribute, which can display a message box on the victim's browser.
This type of attack is known as a Cross-Site Scripting (XSS) attack, and it can be used to steal sensitive information or take control of the victim's browser.
Cross-Site Scripting
Cross-Site Scripting is a type of cyberattack where attackers inject malicious scripts into websites, which are then executed in the browsers of users who visit the website. This type of attack targets the visitors to the injected website, not the host.
An XSS attack can be performed by injecting client-side scripts into web applications, which can be done through various methods such as inserting malicious code into URL parameters, POST parameters, or HTTP headers.
To find tags, events, and payloads for XSS, you can refer to Port Swigger's XSS Cheat Sheet. This resource provides a comprehensive list of tags, events, and payloads that can be used to perform XSS attacks.
If a website uses AngularJS with CSP (ng-csp), you may need to bypass it by using a focus event (ng-focus) to perform XSS. This involves modifying the code to use a focus event instead of the $eval function or quotes.
Websites may sanitize inputs to prevent malicious code, but you can still circumvent this by modifying your code. For example, you can convert JavaScript code to a Base64 string and insert it into the "eval" function.
Here are some common ways to inject XSS attacks:
• Inserting malicious code into URL parameters
• Inserting malicious code into POST parameters
• Inserting malicious code into HTTP headers
• Using a focus event (ng-focus) to bypass CSP (ng-csp)
It's worth noting that some websites may use charcode to prevent XSS attacks, but you can still use charcode to generate payloads for XSS attacks.
Types of XSS
There are at least four types of XSS attacks. Let's look at each.
Reflected XSS attacks occur when malicious scripts are reflected off a web server, typically through vulnerable input fields like search forms. These attacks are non-persistent and only execute when the malicious link is clicked by a user.
Reflected XSS attacks are delivered to victims via another route, such as in an e-mail message, or on some other website. When a user is tricked into clicking on a malicious link, submitting a specially crafted form, or even just browsing to a malicious site, the injected code travels to the vulnerable web site, which reflects the attack back to the user's browser.
DOM-based XSS attacks exploit client-side JavaScript vulnerabilities by injecting malicious scripts into the Document Object Model (DOM). These attacks occur when a website processes and writes unstructured data directly from the URL or other client-side input back into the web page.
There are at least four types of XSS attacks: Reflected XSS, Stored XSS, DOM-based XSS, and other less well-known types.
Cross-Site Scripting (XSS) Attack Types
There are at least four types of XSS attacks. Let's look at each.
One type is reflected XSS, where malicious scripts are reflected off a web server, typically through vulnerable input fields like search forms. These attacks are non-persistent and only execute when the malicious link is clicked by a user.
Another type is stored XSS, which occurs when user input is stored on the server and then reflected back to the user's browser. This type of attack is more persistent and can be executed by multiple users.
Dalfox is a tool used to perform XSS attacks, and it can be used to inject malicious scripts into web applications. It can be used to send a callback URL and even to send POST data.
XSStrike is another tool used to perform XSS attacks, and it can be used to send GET and POST requests to vulnerable web applications. It can also be used to send data as JSON.
The OWASP XSS Filter Evasion Cheat Sheet provides guidance on how to evade XSS filters and how to find tags, events, and payloads for XSS attacks. It also provides information on how to use Port Swigger's XSS Cheat Sheet to find tags, events, and payloads for XSS attacks.
In addition to these tools and resources, it's worth noting that XSS attacks can also be performed using AngularJS with CSP (ng-csp). To bypass these attacks, it's possible to use a focus event (ng-focus) to retrieve the victim user's cookie in the access log of the attacker's website.
Spaces and Meta Chars Before JavaScript
Spaces and Meta Chars Before JavaScript can be a game-changer in XSS attacks.
In reality, you can have any char from 1-32 in decimal between the quote and the javascript: keyword. This is because those characters are considered space or meta characters.
The pattern match often doesn't take into account spaces in the word javascript, making the assumption that you can't have a space between the quote and the javascript: keyword. This is incorrect.
In fact, the actual reality is that you can have any of these space or meta characters, making it a useful technique for XSS attacks.
Non-Alpha-Non-Digit
Non-Alpha-Non-Digit XSS is a type of attack that exploits the way browsers parse HTML.
The Firefox HTML parser assumes that a non-alpha-non-digit character is not valid after an HTML keyword, considering it a whitespace or non-valid token after an HTML tag.
This can be used to bypass some XSS filters, which assume that the tag they are looking for is broken up by whitespace. For example, \
The Gecko rendering engine allows for any character other than letters, numbers, or encapsulation chars between the event handler and the equals sign, making it easier to bypass cross site scripting blocks.
The grave accent char can also be used for this purpose, as seen in:
Yair Amit pointed out that there is a difference in behavior between the IE and Gecko rendering engines, allowing a slash between the tag and the parameter with no spaces.
XSS Impacts and Vulnerabilities
XSS attacks can have severe consequences, including hijacking user accounts, performing unauthorized actions, stealing sensitive information, downloading malware, and stealing credentials.
Here are some possible damages of XSS attacks:
- Hijacking user accounts: Attackers can extract session cookies and hijack user accounts.
- Stealing sensitive information: Attackers can leak sensitive information, such as login credentials, banking details, contact information, and purchase histories.
- Downloading malware & spreading botnet attacks: Malicious links can redirect users to download malware or create and spread botnets from compromised accounts.
- Stealing credentials: Attackers can create clone login pages using XSS vulnerabilities to steal user credentials.
To identify vulnerabilities, you can manually test using attack payloads or use an automated approach with web vulnerability scanners.
Impacts
XSS attacks can have serious consequences for your organization. Hijacking user accounts is a common outcome, where attackers extract session cookies and mimic legitimate users to perform unauthorized actions.
This can lead to sensitive information being stolen, such as login credentials, banking details, contact information, and purchase histories. Attackers can also leak this information to the public.
Unauthorized actions can be performed if attackers control an administrator account, allowing them to view user details, access databases, and modify code. This can be catastrophic for an organization.
Here are some possible damages of XSS attacks on your organization:
- Hijacking user accounts
- Performing unauthorized actions
- Stealing sensitive information
- Downloading malware & spreading botnet attacks
- Stealing credentials
These consequences can be devastating for an organization, and it's essential to take preventative measures to protect against XSS attacks.
Related Vulnerabilities
Improper data validation can be a significant contributor to XSS attacks. It's essential to validate user input to prevent malicious code from being injected into a website.
Types of Cross-Site Scripting are a major concern for web developers. They include Stored XSS, Reflected XSS, and DOM-based XSS, each with its unique challenges.
OWASP Development Guide has an excellent article on Data Validation that provides actionable tips on how to validate user input. It's a must-read for anyone looking to secure their web application.
OWASP Development Guide also has an article on Phishing that highlights the importance of secure data handling practices. By following these guidelines, developers can significantly reduce the risk of XSS attacks.
Some related vulnerabilities to be aware of include:
- Improper Data Validation
- Types of Cross-Site Scripting
Preventing XSS
Preventing XSS is crucial to protect your website from malicious attacks. Sanitize outputs properly by using a suitable escaping technique like HTML escape, CSS escape, JavaScript escape, URL escape, etc. This can be achieved by using a trusted and verified library to escape HTML inputs.
Input validation is also essential to prevent XSS attacks. For example, validate URLs containing safe protocols like HTTP and HTTPS, validate numerics to ensure the input does not contain unnecessary characters, etc.
To further prevent XSS attacks, enforce the Content Security Policy (CSP) to allow users to only load client-side resources, such as JavaScript and CSS, from trusted sources. You can also set the HttpOnly flag in the cookies to prevent JavaScript code from accessing the cookies.
Here are some additional security measures to prevent XSS attacks:
- Use the X-XSS-Protection header to prevent reflected XSS attacks.
- Implement Web Application Firewalls (WAFs) to check for specific attack strings and block them.
- Regularly update and patch your website's software and plugins to prevent known vulnerabilities.
Preventing Cross-Site Scripting Vulnerabilities
Preventing Cross-Site Scripting Vulnerabilities is crucial to protect your website from attacks. Sanitize outputs properly by using a suitable escaping technique like HTML escape, CSS escape, JavaScript escape, URL escape, etc. This will prevent malicious code from being injected into your website.
Input validation is also essential to prevent XSS attacks. Validate URLs containing safe protocols like HTTP and HTTPS, and validate numerics to ensure the input does not contain unnecessary characters.
To further secure your website, enforce the Content Security Policy (CSP). This policy allows users to only load client-side resources, such as JavaScript and CSS, from trusted sources.
Here are some key techniques to prevent XSS attacks:
- Sanitize outputs properly
- Input validation
- Enforce Content Security Policy (CSP)
- Set the HttpOnly flag in cookies
- Use the X-XSS-Protection header
- Implement Web Application Firewalls (WAFs)
By implementing these techniques, you can significantly reduce the risk of XSS attacks on your website.
XSS Consequences and Related Topics
An XSS attack can have severe consequences, including account compromise and disclosure of sensitive information.
The most severe XSS attacks involve disclosure of the user's session cookie, allowing an attacker to hijack the user's session and take over the account.
Other damaging attacks include disclosure of end user files, installation of Trojan horse programs, and redirecting the user to some other page or site.
XSS can also be used to modify presentation of content, which could have serious consequences, such as affecting a company's stock price or lessening consumer confidence.
Related attacks include Invoking untrusted mobile code and Cross Site History Manipulation (XSHM).
Some related vulnerabilities include Improper Data Validation and Types of Cross-Site Scripting.
Here are some related attacks and vulnerabilities:
- XSS Attacks
- Invoking untrusted mobile code
- Cross Site History Manipulation (XSHM)
- Improper Data Validation
- Types of Cross-Site Scripting
Sources
- https://exploit-notes.hdks.org/exploit/web/security-risk/xss/
- https://www.splunk.com/en_us/blog/learn/cross-site-scripting-xss-attacks.html
- https://aszx87410.github.io/beyond-xss/en/ch1/know-xss-a-bit-more/
- https://3os.org/penetration-testing/cheatsheets/xss-cheatsheet/
- https://owasp.org/www-community/attacks/xss/
Featured Images: pexels.com