5~10 questions are updated daily

directory

Has been finished

  • The difference and use of symmetric encryption and asymmetric encryption
  • What is the CSP?
  • Carrier hijacking
  • Can you talk about XSS attacks?
  • Can you talk a little bit about CSRF attacks?
  • What do you understand about HTTPS man-in-the-middle attacks?

The difference and use of symmetric encryption and asymmetric encryption

  • Symmetric encryption: Use the same secret key for encryption and decryption
  • Asymmetric encryption: different from symmetric encryption algorithms, asymmetric encryption algorithms require two keys: a publickey and a privatekey.

Symmetric encryption algorithm

Key is short, difficult to decipher, in addition to the data encryption Standard (DES), another symmetric key encryption system is the international data encryption algorithm (IDEA), it is better than the encryption of DES, and the computer performance requirements are not so high.

Advantages:

The algorithm is open, the computation is small, the encryption speed is fast, the encryption efficiency is high

Disadvantages:

Before data can be transmitted, the sender and receiver must agree on the secret key, and then enable both parties to keep the secret key. Secondly, if one party’s secret key is leaked, the encrypted information is not secure. In addition, each pair of users needs to use a unique secret key unknown to others every time they use a symmetric encryption algorithm, which makes the number of keys owned by the receiver and sender huge, and key management becomes a burden for both parties.

Common symmetric encryption algorithms include DES, 3DES, Blowfish, IDEA, RC4, RC5, RC6, and AES

Asymmetric encryption algorithm

The public key and private key are a pair. If the public key is used to encrypt data, only the corresponding private key can be used to decrypt data. If data is encrypted with a private key, it can only be decrypted with the corresponding public key. Because encryption and decryption use two different keys, the algorithm is called asymmetric encryption.

The basic process of asymmetric encryption algorithm to realize confidential information exchange is as follows: Party A generates a pair of keys and discloses one of them to other parties as a public key; Party B who has obtained the public key encrypts the confidential information with the public key and then sends it to Party A. Party A then uses another special key saved by itself to decrypt the encrypted information. Party A can only use its private key to decrypt any information encrypted by its public key.

Advantages:

security

Disadvantages:

The slower

Common asymmetric encryption algorithms include RSA, ECC (for mobile devices), Diffie-Hellman, El Gamal, AND DSA (for digital signature).

Hash algorithm (digest algorithm)

The Hash algorithm is unique in that it is a one-way algorithm. Users can use the Hash algorithm to generate a unique Hash value of a specific length for the target information, but cannot use the Hash value to retrieve the target information. Therefore, the Hash algorithm is commonly used for irrecoverable password storage and information integrity verification.

The common summarization algorithms are MD2, MD4, MD5, HAVAL and SHA

What is the CSP?

Content Security Policy (CSP)

A CSP is essentially a whitelist where the developer explicitly tells the browser which external resources can be loaded and executed. We just need to configure the rules, how to intercept is up to the browser implementation. We can minimize XSS attacks in this way

There are usually two ways to start a CSP

  • One is to set the HTTP headerContent-Security-Policy
  • One way is to set the meta tag<meta http-equiv="Content-Security-Policy">

Here is an example of setting HTTP headers

  • Only site resources are allowed to be loaded
Content-Security-Policy: default-src 'self'
Copy the code

Only HTTPS images can be loaded

Content-Security-Policy: img-src https://*
Copy the code
  • Allows loading of frames from any source
Content-Security-Policy: child-src 'none'
Copy the code

There are many more properties to set. See the MDN documentation for more Settings

In this case, as long as the developer has configured the right rules, the attacker cannot execute the site's attack code even if the site is vulnerable, and CSP compatibility is good

Carrier hijacking

Network hijacking is the main carrier level of hijacking. We all deal with it all the time, so to speak.

There are two main types of carrier hijacking:

  • DNS hijacking: This kind of hijacking redirects you to other sites, as we are familiar with phishing sites. But because of its illegality, it is now heavily regulated and rarely seen.
  • HTTP hijacking: DNS hijacking is regulated, but HTTP hijacking still exists! When carriers find out that your request is an HTTP request, they insert some weird ads. And this phenomenon is very common, you can try to open a random web page, take a closer look and you will find a little tail, this is HTTP hijacked.

Why is that?

HTTP requests are transmitted in plaintext over the network and must go through the carrier, so the carrier level can easily modify the HTTP content of the response. For example, add a javascript section to the returned HTML, which can be executed directly on the client side to achieve any desired effect.

Solutions:

  • If it is their own station hijacked, directly the whole station HTTPS bar! And in the promotion of the release with the protocol header will be effective, because port 80 301 also has a certain probability of hijacking;
  • If you are just a simple user, there is basically no solution; Try to visit the same station with different network environment, WIFI/4G cut off the operator, see if there will be such content; Only one carrier has it. Try calling the carrier to complain. (Many people say it works.)

Can you talk about XSS attacks?

What is an XSS attack?

XSS stands for Cross Site Scripting, so it is called XSS to differentiate it from CSS. An XSS attack is the execution of malicious scripts (whether cross-domain or co-domain) in a browser to retrieve user information.

These operations generally do the following

  1. Stealing cookies
  2. Monitor user behavior, such as entering the account password and sending it directly to the hacker server
  3. Modify DOM forgery login form
  4. Generate floating window ads in the page

Typically, XSS attacks are implemented in one of three ways

  • Storage type
  • reflective
  • The document type

Storage type

Stored, storing malicious scripts. Indeed, stored XSS stores the scripts in the server database and executes the scripts on the client side, thus implementing the attack

The common scenario is to submit a piece of code in the comment area, if the back and forth end has not done a good job of escaping, the comment content stored in the database, directly executed in the page rendering process, equivalent to the implementation of a section of location logic JS code, is very horrible. This is a stored XSS attack

reflective

Reflective XSS refers to malicious scripts as part of a network request such as my input

http://baidu.com?q=<script>alert(" You're screwed ")</script>Copy the code

The server takes the q argument and sends the content back to the browser, which parses it as part of the HTML, discovers it’s a script, executes it, and is attacked

It is called reflective because malicious scripts are parsed as parameters of a network request, passed through the server, and then reflected into an HTML document. Unlike the stored type, the server does not store these malicious scripts

The document type

Document-type XSS attacks do not pass through the server, but act as a middleman, hijacking network packets in the process of data transmission, and then modifying the HTML document inside. Such hijacking methods include wifi routing hijacking or local malware

Measures to prevent

Understand the principles of the three KINDS of XSS attacks, and find a common point: all malicious scripts can be directly executed in the browser, so to prevent it, is to avoid the execution of these script codes, in order to accomplish this, must do one belief, two use.

A belief

Never trust any user input! Transcoding or filtering of user input is performed both on the front-end and server side

Such as

<script>alert(' You're done! ')</script>Copy the code

After transcoding, it becomes:

&lt; script&gt; alert(&#39; You're screwed. &lt; /script&gt;Copy the code

This code cannot be executed during HTML parsing and can also be used to remove script tags by keyword filtering. So all that’s left is… Nothing at all

Using the CSP

Content-security-policy (CSP) is an HTTP response header that describes allowing a page to control which resources a user agent can load for a specified page, preventing XSS attacks

CSP is a content security policy in the browser. The core idea of CSP is that the server decides which resources the browser loads. Specifically, it can accomplish the following functions:

  1. Restrict resource loading in other domains
  2. Disallow data submission to other domains
  3. Provide a reporting mechanism to help us detect XSS attacks in a timely manner

Using the HttpOnly

Many XSS attack scripts are used to steal cookies. After setting the HttpOnly attribute of cookies, JavaScript cannot read the value of cookies, which is also a good defense against XSS attacks.

conclusion

An XSS attack is a malicious script executed in a browser and then executed with user information. It is mainly divided into storage, reflection and document. Preventive measures include:

  • One belief: don’t trust user input; transcode or filter input to make it unexecutable
  • Two uses: using CSP, using the HttpOnly attribute of Cookie

Can you talk a little bit about CSRF attacks?

What is a CSRF attack?

Cross-site Request Forgery (CSRF) refers to a cross-site request initiated by a hacker who induces a user to click a link to open the hacker’s website, and then takes advantage of the user’s current login status. For example, if you click on an image on a website carefully selected by a hacker, you click and go to a new page. Well, congratulations on being attacked! You may be curious about being attacked out of the blue, so here’s the breakdown: What was the hacker doing behind the scenes when you clicked on the link? There are three things you might do:

  • A hacker’s web page may contain a code like this:
<img src="https://xxx.com/info?user=hhh&count=77">
Copy the code

Get request will be automatically sent after entering the page. It is worth noting that this request will automatically carry the cookie information about XXx.com (assuming that you have logged in xxx.com website). If the server does not have the corresponding authentication mechanism, it may think that the request is a normal user, because it carries the corresponding cookie. Undertake corresponding all sorts of operations next, can be transfer money remittance and other malicious operation

  • 2. Automatic POST Request The hacker may have filled out a form and written a script for automatic submission.
<form id='hacker-form' action="https://xxx.com/info" method="POST"> <input type="hidden" name="user" value="hhh" /> <input type="hidden" name="count" value="100" /> </form> <script>document.getElementById('hacker-form').submit(); </script>Copy the code

It will also carry the corresponding user cookie information, so that the server mistakenly thinks that it is a normal user in the operation, so that all kinds of malicious operations become possible

  • On a hacker’s website, a link may be placed that drives you to click
<a href="https://xxx/info? User =hhh&count=100" count= "_blank">Copy the code

This is the principle of CSRF attack. Compared with XSS attack, CSRF attack does not need to inject malicious code into the HTML document of the user’s current page. Instead, it jumps to a new page. The authentication vulnerability of the server and the login status of the user are used to simulate the operation of the user

Measures to prevent

  1. Use of cookiesSameSiteattribute

An important part of a CSRF attack is to automatically send a Cookie under the target site, and then this Cookie simulates the user’s identity. Therefore, Posting on cookies is the best defense. As it happens, there is one key field in cookies that places some restrictions on how cookies can be carried in requests: SameSite.

SameSite can be set to three values, Strict, Lax, and None.

  • 1. In Strict mode, the browser completely forbids third-party requests to carry cookies. For example, a request to zhufeng.com can only be made in the domain name of zhufeng.com to carry cookies, but not in other websites.
  • 2. In Lax mode, the Cookie can only be carried if the GET method submits a form condition or the A tag sends a GET request. Otherwise, the Cookie cannot be carried.
  • 3. In None mode, which is the default mode, requests automatically carry cookies.
  1. Verify the source site

This requires two fields in the request header: Origin and Referer. Origin contains only domain name information, while Referer contains the specific URL path. Of course, both of these can be forged by customizing the request headers in Ajax, which is less secure.

  1. CSRF Token

Django is a backend framework for Python. If you’ve developed with Django, you’ll know that its template is often accompanied by a line of code like this:

{% csrf_token %}
Copy the code

This is a typical application of the CSRF Token. So how does it work?

First, when the browser sends a request to the server, the server generates a string and implants it into the returned page. Then the browser, if it wants to send a request, must carry the string, and the server verifies that it is valid and does not respond if it is not. This string is known as a CSRF Token, which is usually not available to third-party sites and is therefore rejected by the server

  1. Verification code

Almost 100% defense, but slightly worse experience.

conclusion

Cross-site Request Forgery (CSRF) refers to a situation in which a hacker induces a user to click on a link to open the hacker’s website, and then the hacker initiates a cross-site request using the user’s current login status. CSRF attacks are generally carried out in three ways:

  • Automatic GET request
  • Automatic POST request
  • Induced clicks send GET requests

Precautions: Use the SameSite attribute of the Cookie, verify the source site, CSRF Token, and verification code.

What do you understand about HTTPS man-in-the-middle attacks?

https

You’ve probably heard that THE HTTPS protocol is secure because it encrypts the transmitted data using asymmetric encryption. However, IN fact, HTTPS uses symmetric encryption for content transmission. Asymmetric encryption only applies to certificate verification.

The HTTPS process consists of certificate authentication and data transmission. The interaction process is as follows:

① Certificate verification phase

  1. The browser initiates an HTTPS request

  2. Server returns HTTPS certificate (including public key)

  3. The client verifies whether the certificate is valid. If the certificate is invalid, an alarm is generated

② Data transmission stage

  1. When the certificate is valid, a random number is generated locally

  2. The public key encrypts the random number and transmits the encrypted random number to the server

  3. The server decrypts random numbers using private keys

  4. The server uses the random number from the client to construct a symmetric encryption algorithm, encrypts the returned result and transmits it

Why is data transmitted symmetrically encrypted?

First of all, the efficiency of asymmetric encryption is very low, and there is usually a lot of end-to-end interaction in HTTP application scenarios, so the efficiency of asymmetric encryption is unacceptable.

In the HTTPS scenario, only the server saves the private key, and a pair of public and private keys can only realize one-way encryption and decryption. Therefore, the content transmission encryption in HTTPS adopts symmetric encryption instead of asymmetric encryption.

Why do I need a CA to issue a certificate?

HTTP is considered insecure because the transmission process is easy to be tapped by listeners and forged servers, while HTTPS mainly solves the security problem of network transmission.

First, we assume that there is no certification authority and that anyone can create a certificate, which presents a security risk known as the classic “man-in-the-middle” problem.

Process principle:

  1. Local requests are hijacked (e.g., DNS hijacking) and all requests are sent to the middleman’s server

  2. The middleman server returns the middleman’s own certificate

  3. The client creates a random number, encrypts the random number using the public key of the middleman certificate, and sends the random number to the middleman. Symmetric encryption is constructed based on the random number to encrypt and transmit the transmitted content

  4. Because the middleman has the random number of the client, it can decrypt the content through the symmetric encryption algorithm

  5. The middleman sends a request to the official website with the content requested by the client

  6. Because the process of communication between the middleman and the server is legal, the legitimate website returns encrypted data through a secure channel established

  7. Middlemen decrypt content using symmetric encryption algorithms established with legitimate websites

  8. The middleman encrypts and transmits the data returned by the regular content through the symmetric encryption algorithm established with the client

  9. The client decrypts the returned result data through a symmetric encryption algorithm established with the middleman

Due to the lack of certificate verification, although the client initiates an HTTPS request, the client is completely unaware that its network has been intercepted and the transmitted content is stolen by a middleman.

How does the browser ensure that the CA certificate is valid?

  1. What information does the certificate contain?
  • Information of issuing Authority
  • The public key
  • Your company information
  • The domain name
  • The period of validity
  • The fingerprint
  • .
  1. What is the validity of the certificate?

Above all, authoritative orgnaization should have attestation, not just an orgnaization is qualified to issue a certificate, otherwise also not be called authoritative orgnaization.

In addition, the credibility of the certificate is based on the trust system, and the authority needs to endorse the certificate issued by the authority. As long as the certificate generated by the authority, we consider it legitimate. Therefore, the authority will review the information of the applicant, and the requirements of the authority of different levels are not the same, so the certificate is also divided into free, cheap and expensive.

  1. How does the browser verify the validity of the certificate?

When the browser initiates an HTTPS request, the server returns the website’s SSL certificate. The browser needs to verify the certificate as follows:

  1. Verify that the domain name and validity period are correct. All certificates contain these information, which is easier to complete verification;
  2. Determine whether the certificate source is valid. Each issued certificate can be found according to the verification chainRoot certificate, the operating system (OS) and browser store the root certificate of the local authority. The local root certificate can be used to authenticate the source of the certificate issued by the corresponding authority.

  1. Determine whether the certificate has been tampered with. Verification with the CA server is required.

  2. Determine whether the certificate has been revoked. Through Certificate Revocation List (CRL) and Online Certificate Status Protocol (OCSP), OCSP can be used in step 3 to reduce interaction with the CA server and improve verification efficiency

The browser considers the certificate valid only if any of the preceding steps are met.

Here’s a question I’ve been thinking about for a long time with a simple answer:

  • Since the certificate is public, if I want to launch a man-in-the-middle attack, I download a certificate from the official website as my server certificate, and the client will definitely agree that the certificate is legitimate. How can I avoid the situation of fake certificate?

In fact, this is the use of public and private keys in unencrypted symmetry. Although the middleman can get the certificate, the private key cannot be obtained. It is impossible to calculate the corresponding private key of a public key.

  1. Can only certification authorities generate certificates?

If you want the browser to avoid security risks, you can only use the certificate issued by the certification authority. However, browsers usually just warn of security risks and do not restrict access to websites, so technically anyone can generate a certificate that can complete HTTPS transfer of a website. For example, early 12306 uses the form of manually installing private certificates to implement HTTPS access.

What if local random numbers are stolen?

Asymmetric encryption is used for certificate authentication, but symmetric encryption is used for transmission. Important random numbers in the symmetric encryption algorithm are generated and stored locally. How does HTTPS prevent random numbers from being stolen?

In fact, HTTPS does not guarantee the security of random numbers. HTTPS only guarantees the security of transmission, and random numbers are stored locally. Local security belongs to another security category.

Can I get caught using HTTPS?

HTTPS data is encrypted. Generally, packets captured by the packet capture tool are in the encrypted state and cannot be viewed.

HTTPS only prevents communication from being monitored without the user’s knowledge. If the user initiates the communication, a “man in the middle” network can be constructed, and proxy software can decrypt the content of the transmission.

Methods to prevent

After the server adds a CA certificate to the public key of the browser, the browser can verify the validity of the CA certificate. (Existing HTTPS is hard to hijack unless the hijacker’s CA certificate is trusted).