Common attack
Cookie holding
HTTP is a stateless protocol that introduces cookies and sessions to maintain and track user status. Cookies contain the user credentials of the browser client and are relatively small. Sessions are maintained on the server and are used to maintain relatively large user information. You can think of cookies as passwords, and sessions as safes. Because HTTP is transmitted in clear text, cookies can be easily stolen. If stolen, someone can impersonate your identity, open your safe, get your information, and use your money, which is very dangerous.
The relationship between Cookie and Session can be seen in this article: A Brief Introduction to the relationship between Session and Cookie
1. The harm
Steal cookie information, impersonate the identity of others, steal information.
2. The defense
- to
cookie
addHttpOnly
Property, which can only be set inhttp
Pass in the request, in the script,document.cookie
Could not get thiscookie
Value, which defends against XSS attacks, but still leaks against network interception. - in
cookie
To add verification information that is related to the current user’s external environment, for exampleip
,user agent
And so oncookie
When hijacked, the verification value is changed on the server. Therefore, users are required to log in again to avoid this problemcookie
Hijacking. cookie
In thesession id
The regular replacement, letsession id
At the same time, the operation is transparent to users, which ensures the consistency of service experience.
XSS cross-site scripting attacks
An attacker inserts HTML or script tags into a Web page maliciously. When the user browses the page, the malicious code will be executed to achieve the purpose of attack. XSS leverages the user’s trust in a given site.
Type 1.
-
Reflective (non-persistent) : The attacker makes the attack link in advance and needs to trick the user to click the link to trigger the XSS code. The so-called reflective XSS is to reflect the JS script entered by the malicious user to the browser for execution.
-
Stored (persistent) : The attacker’s data is stored on the server, and the attack will always exist with the attack data. Whenever the user visits the page, the code execution will be triggered.
-
DOM: Vulnerability based on the document Object model.
The most classic stored XSS vulnerability is the message board, when user A leaves A JS code , the back end is directly stored in the database without filtering, when normal users browse to his message, this JS code will be executed, can be used to steal cookies.
Harm 2.
- Steal web browsing
cookie
Value,cookie
Achieve no password login, steal user information. - Hijack access to achieve malicious jump.
- Cooperate with CSRF attack to complete malicious request.
3. Defense methods
- Label filtering, for example
<script>
,<img>
,<a>
Labels etc. - Encoding, pair of characters
<
、>
,&
,"
、'
、+
,/
And so on. cookie
Guard against theft,cookie
Set tohttp-only
.js
The script will not be able to readcookie
Information.- Pure front-end rendering, clear
innerText
,setAttribute
,style
To separate code from data. - Avoid untrusted data concatenated into strings passed to these
API
, such asDOM
Inline event listeners in,location
,onclick
,onload
,onmouseover
, etc.<a>
Of the labelhref
Properties,JavaScript
theeval()
,setTimeout()
,setInterval()
Etc., can run strings as code.
CSRF cross-site request forgery
As the name implies, it is an attack method that allows users to complete unintended operations with their own identities by forging connection requests without their knowledge. CSRF takes advantage of a web site’s trust in the browser.
Principle 1.
- The user
C
Browse and log in to trusted websitesA
To producecookie
- The user
C
Not exiting the siteA
, in the same browser dangerous access to the siteB
- Web site
B
The page contains some offensive code that will issue a visitA
The request of - The browser receives the request and carries it without the user’s knowledge
cookie
Visit the web siteA
A
If you don’t know who sent the request, the browser will pick up the usercookie
, soA
It will be processed according to the user’s permissionB
Request made. That would accomplish the purpose of the attack.
2. The defense
- Verification code: Add verification code to sensitive operations to force users to interact with the site
- use
POST
Request, avoid usingGET
, reduce the risk of attack,post
The requesting attacker needs to construct oneform
A form can initiate a requestget
Request (img
thesrc
.a
Of the labelhref
And so on) are a bit more sophisticated, relatively risk-reducing, but not deterring. - check
HTTP
In thereferer
Field, which recordsHTTP
The source address of the request - Add to the request header
token
Validation fields, which the browser does not automatically carryToken
To request, andToken
Can carry a coded piece ofjwt
For authentication, do thisCSRF
When only passedcookie
, does not identify the user, and the site rejects the attack request. - in
http
To customize the attributes and validate.
Click on the hostage
-
ClickJacking
When you visit a website, use CSS to hide the page that the attacker actually wants you to click on, and then display something at the back of the page to induce you to click on it, which will do something without the user knowing it. This is ClickJacking.
-
The iframe cover
Third party websites embed a website through iframe, and set the IFrame to be transparent and invisible, and overlay it on other camouflaged DOM. The camouflaged clickable DOM (button, etc.) is the same as the actual clickable DOM of the embedded website. When the user clicks the camouflaged DOM, You actually click on the DOM of the webpage embedded in the IFrame to trigger the request action.
defense
-
Javascript disables embedding: When a page is not embedded with iframe, top and window are equal; When a page is embedded, top and window are not equal; You can add the following judgments on the page of this website:
<script> if(top.location ! =window.location) { // If no, iframe is used. You can perform related operations } </script> Copy the code
This is not a panacea, however, because the sandbox property in the iframe tag can disable scripts for embedded pages:
<iframe sandbox='allow-forms' src='... '></iframe>Copy the code
-
Setting the HTTP response header x-frame-options is by far the most reliable method. X-frame-options is an HTTP header proposed by Microsoft to defend against clickjacking attacks using nested iframe.
DENY // SAMEORIGIN is prohibited // Only allow-from within the same domain name is allowed // Specify the address that can be embeddedCopy the code
- Some auxiliary means, such as adding captcha, raise users’ awareness of precautions