Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XSS Security Implementation and Ongoing SQL Injection Hardening #481

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

LyesRocker
Copy link

Hi @preetkaran20,

I hope you're doing well. I’ve implemented secure functionalities for both stored and reflected XSS:

Reflected XSS:

Level 8: Using CSP and escaping HTML entities
Level 9: Using MIME type
Level 10: Using hashed paths
Stored XSS:

Level 8: Escaping HTML and removing all script and img tags
Level 9: Using a regex to remove all HTML tags
Level 10: Escaping HTML and removing all JS calls, e.g., javascript:alert(1);
Let me know your thoughts. If everything is fine, I’ve already started working on the SQL Union injection.
In the meantime, I am documenting each level I secure or don't secure, and I’ll be providing a document on how to escape each security vulnerability (if not secured), to help users understand the application.

Thanks,
Lyes

@@ -0,0 +1,108 @@
package org.sasanlabs.service.vulnerability.sampleVulnerability;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please remove SampleVulnerability related classes. Add them to .gitignore as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes i'll try to do it.

queryParams,
LevelConstants.LEVEL_8,
post -> StringEscapeUtils.escapeHtml4(
post.replaceAll("(?i)<script.*?>.*?</script>", "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With escapeHtml4., do we even need escaping of script tag?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean there is some flaws i found, for example you can trigger an xss if the charset is explicitly set to UTF-7 you can use a payload like this one :
+ADw-script+AD4-alert(document.location)+ADw-/script+AD4-

Let me think what do you think

@RequestParam Map<String, String> queryParams) {
Function<String, String> function =
(post) -> {
String sanitizedPost = post.replaceAll("<.*?>", ""); // Delete all the html balises
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with html4 escape, do we need replaceall?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you are right, we dont actually need that replace.

description = "XSS_HASH_VALIDATION")
@VulnerableAppRequestMapping(
value = LevelConstants.LEVEL_10,
variant = Variant.SECURE,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to add insecure level for this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the XSS in img tag ?

htmlTemplate = "LEVEL_1/XSS")
public ResponseEntity<String> getVulnerablePayloadLevelSecure4(@RequestParam(PARAMETER_NAME) String imageLocation) {
String hashedValue = hashSHA256(imageLocation);
if (hashedValue.equals("bd473875115034776f0fed141a0b6f8cbd46989e0ff1d52864f88a4e48882c75") ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this hashed values of zap image or owasp image? if so can you compute it on runtime. Why because it will help if we update the images, we don't need to change code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can do that but the ressources requested needs to be defined before running the function.

description = "XSS_MIME_TYPE_VALIDATION")
@VulnerableAppRequestMapping(
value = LevelConstants.LEVEL_9,
variant = Variant.SECURE,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure that this cannot be broken?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting a MIME type in the backend cannot be bypassed. If the user submits anything other than an image, the requested resource does not appear.
In the other hand if the user submit something like a file, yes the mime type can be bypassed.

htmlTemplate = "LEVEL_1/XSS")
public ResponseEntity<String> getVulnerablePayloadLevelSecure2(@RequestParam(PARAMETER_NAME) String imageLocation) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Security-Policy", "default-src 'self'; img-src 'self'");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a level which is insecure where csp header is lenient if already that level is not there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll add an insecure level with a weak CSP.

mimeType = URLConnection.guessContentTypeFromName(file.getName());
}
if (mimeType != null && mimeType.startsWith("image/")) {
String vulnerablePayloadWithPlaceHolder = "<img src=\"%s\" width=\"400\" height=\"300\"/>";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to create a string constant for this line as it is being used multiple times in all the vulnerability levels.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted !

return new ResponseEntity<>("Invalid Image Hash", HttpStatus.BAD_REQUEST);
}

private String hashSHA256(String input) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move it to utils file so that we can utilise it at other places.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted !

}
}

private String bytesToHex(byte[] bytes) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't there any utility already whcih does this for us?

@@ -0,0 +1,23 @@
function addingEventListenerToFetchData() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove all sampleVulnerabilitu related files.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes i'll look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants