Catagorizing CSS Attacks - Stored | Reflected | DOM Based Attacks
XSS Mitigation
Content Security Policy
Writing CSP Policy Examples
Implementing CSP - Testing Policy and Report Enabling
Cross Site Scripting XSS
A type of injection, in which malicious scripts are injected into websites
Stored XSS Attacks
Where the injected malicious script is permanently stored on the target servers [Persistant]
Reflected XSS Attacks
Where the injected malicious script is reflected off of a web application to the victim’s browser. [None Persistant]
Other XSS Attacks:
DOM Based XSS-attackers smuggle malicious JS into a user's web page via the URI fragment.
Good Video on XSS
Example 1 (Reflected XSS)
Data is read directly from the HTTP request and reflected back in the HTTP response. Reflected XSS exploits occur when an attacker causes a user to supply dangerous content to a vulnerable web application, which is then reflected back to the user and executed by the web browser. The most common mechanism for delivering malicious content is to include it as a parameter in a URL that is posted publicly or e-mailed directly to victims. URLs constructed in this manner constitute the core of many phishing schemes, whereby an attacker convinces victims to visit a URL that refers to a vulnerable site. After the site reflects the attacker’s content back to the user, the content is executed and proceeds to transfer private information, such as cookies that may include session information, from the user’s machine to the attacker or perform other nefarious activities.
Example 2(Stored XSS)
The application stores dangerous data in a database or other trusted data store. The dangerous data is subsequently read back into the application and included in dynamic content. Stored XSS exploits occur when an attacker injects dangerous content into a data store that is later read and included in dynamic content. From an attacker’s perspective, the optimal place to inject malicious content is in an area that is displayed to either many users or particularly interesting users. Interesting users typically have elevated privileges in the application or interact with sensitive data that is valuable to the attacker. If one of these users executes malicious content, the attacker may be able to perform privileged operations on behalf of the user or gain access to sensitive data belonging to the user.
Performing Reflected Cross Scripting Attack
We can do this using default parameters, where we add an equal sign and the default value at the end of the parameter or structure the URL to execute JS code that makes a HTTP request to desired location.
// 1) test browser for execution of JS code
// 2) Formulate URL to execute JS code as endpoint
<script type="text/javascript">
var information = '../hackinfo?getCookies=' + escape(document.cookie);
</script>
Mitigating both Reflected and Stored
Escaping control characters in dynamic content that the website interpolates into HTML
Modern templating and front-end frameworks implement this feature
Helpful tips for finding XSS Attacks
Test URL Endpoints
Test the attack in a browser
Implement a Content Security Policy!!
Content Security Policy (CSP)
CSP makes it possible for server administrators to reduce or eliminate the vectors by which XSS can occur by specifying the domains that the browser should consider to be valid sources of executable scripts
What Does CSP Prevent??
XSS - CSP automatically blocks the code from sending sensitive information to the hacker's domain.
Browser Hijacking & Ad Injection - Client-side malware causes unwanted ads to appear on your users' browsers. A CSP prevents these ads from loading when affected users go on your website.
Unauthorized Piggyback Tags - One tag could also be loading multiple tags from vendors you have not authorized. A CSP eliminates this security risk..
Implementing a CSP
// 1) Via the server: Example Below
Content-Security-Policy: default-src 'self' trusted.com *.trusted.com
// 2) Meta tag with head of site
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; img-src https://*; child-src 'none';" />
// 3) We can use the set method of the Express Response object
res.set("Content-Security-Policy", "default-src 'self'");
Consideration 1 Implement via the server. If you have access
// 1) Server Implementation:
Content-Security-Policy-Report-Only: default-src https:; report-uri /csp-violation-report-endpoint/
Implementation Implemented via the server. You will need to set up your server to receive the reports; it can store or process them in whatever manner you determine is appropriate.
Implementation Implemented via the server. You will need to set up your server to receive the reports; it can store or process them in whatever manner you determine is appropriate.