This article discusses an interesting question: how to crack HTTPS? As you know, almost the entire Internet now uses HTTPS, and some browsers will warn you if you don’t. HTTPS is often asked in the interview. This article will go into the principle of HTTPS and talk about how to crack HTTPS.
HTTPS
To crack HTTPS, you must first understand the principle of HTTPS. Let’s talk about the principle of HTTPS first.
Public and private key
HTTPS has two secret keys, a public key and a private key. The website has its own private key, and the user has a public key. The website uses its own private key to encrypt data and send it to the user, and the user uses the public key to decrypt the data. When a user wants to send a message, the user encrypts the data with a public key, and the website decrypts the data with a private key. This type of encryption and decryption using different secret keys is called asymmetric encryption. This process is A little bit round, the following example to illustrate, assuming that website A has HTTPS enabled, Xiao Ming will visit this website (the following example is only to explain the use of public and private key, not HTTPS real process, real process is the following “HTTPS handshake process” section) :
- With HTTPS enabled, site A naturally has A pair of secret keys, the private key and the public key. The private key is hidden and the public key is available to anyone who accesses it
- Xiao Ming visited website A and got the public key of A
- When Xiao Ming wants to send A message to Website A, he encrypts the message with A public key and sends it to website A
- After website A gets the ciphertext, it decrypts the message with its own private key
- Website A should reply to Xiaoming, encrypt the information with its private key and send it to Xiaoming
- After xiao Ming gets the ciphertext, he decrypts the information with his own public key
As can be seen from the above process, since the public key is public, the information encrypted by the private key of the website can be unlocked by all users. ** At this stage, you are actually protecting the data that the user sends to the server, because the data encrypted by the user must be unlocked by the server’s private key. ** Here is an interesting question: since all users can get the public key, is it possible that Xiao Ming’s encrypted information can be unlocked by Xiao Hong, because Xiao Hong also has a public key? If Xiao Hong can unlock it, then xiao Hong knows the content as long as she intercepts Xiao Ming’s traffic, doesn’t it? To simplify the question, can ** information encrypted with a public key be unlocked with the same public key? The answer is no! ** To understand this, you need to know the RSA algorithm, which we’ll talk about later, step by step.
The digital certificate
The process of Xiao Ming visiting website A is hidden danger and can be attacked. Suppose Xiao Hong is A middleman hacker and wants to attack Xiao Ming. She secretly changes the public key of website A into her own:
- Xiao Ming visits website A. Website A sends A public key to Xiao Ming, but this step is attacked by Xiao Hong
- Xiao Hong hijacks Xiao Ming’s traffic and replaces the public key sent by website A with her own
- Xiaoming got the wrong public key and used the public key to encrypt his own information, which may contain his user name, password and other sensitive information
- Xiao Ming sends an encrypted message to website A, and the traffic is intercepted by Xiao Hong
- Because the ciphertext is encrypted with Xiao Hong’s public key, Xiao Hong decrypts it with the corresponding private key to obtain Xiao Ming’s password, and the attack is complete
It can be seen that the public and private keys alone cannot cope with traffic hijacking by middlemen, and the information intercepted during transmission will still be cracked. The key to the success of this attack is that Xiao Ming got the wrong public key. Therefore, A mechanism is needed to ensure that Xiao Ming got the right public key of website A, and this mechanism is the digital certificate. The digital certificate is very simple, he has only one core thing, is the public key of website A. Website A put its public key into the digital certificate and sent it to Xiao Ming. Xiao Ming saw that the public key was authenticated by the certificate and trusted, so he used it. Even if Xiao Hong replaced the public key, because xiao Hong’s public key has no certificate authentication, Ming can still identify the fake.
The security of the digital certificate is how to ensure it, Xiao Hong forged a digital certificate is not on the line? This is about CA (CertificateAuthority), CA is the organization that issues digital certificates, CA has its own public and private keys. The CA uses its private key to encrypt A message, which is the public key of website A, and then sends it to the user. The user uses the CA public key to decrypt the message, and then gets the correct public key of website A. So, the digital certificate is actually the public key of the website encrypted by the CA private key. Without the CA’s private key, Hong could not forge the website’s digital certificate and replace Ming’s public key. Therefore, the digital certificate actually guarantees the correctness of the public key of the website, and CA guarantees the security of the digital certificate.
Since CAS guarantee the security of digital certificates, who guarantees the security of CAS? If there’s something X that guarantees CA security, who’s going to guarantee X security? It feels like this chain of trust could go on forever… In reality, THE security level of CA is very high, and its security is not only technical means, but also legal and physical measures. On the other hand, getting back to the subject of this article, cracking HTTPS, we actually have the first idea here: hack CA! You can replace the public key of all its certificates with your own, decrypting all sites that use its certificate.
Charles can decrypt HTTPS, which is the same principle xiao Hong used to attack Xiao Ming. Charles can decrypt HTTPS only if you install his certificate. By installing his certificate, you trust Charles, a fake CA. Attack flow change the front red to Charles.
The session keys
Public key encryption and decryption is indeed secure, but it is slow. If every message is encrypted in this way, it will affect the overall communication efficiency. Therefore, when we establish a connection with HTTPS, there is only one message exchanged through the public key: the session key. The session key is not asymmetrically encrypted, but symmetrically encrypted. Symmetric encryption is very common in some film and television works: a hero gets a treasure map, suffering from the treasure map is written in password, do not understand, all kinds of helpless, think of the ancestral so-and-so book, get a comparison, the book can decrypt the treasure map password. The book is actually a cipher book, which was used to encrypt many messages in World War II. Encryption and decryption with the password book, in fact, is the use of the same secret key, this is symmetric encryption. In computer parlance, this password is essentially a hash function that maps one character to another. For example, our encrypted hash function moves the character backwards by three Spaces, such as a -> d or b -> e, so “hello” becomes:
h -> k
e -> h
l -> o
l -> o
o -> r
“Hello” becomes “khoor”, so an attacker just needs to know your algorithm, work it back up, and move three places forward to decrypt it. So symmetric encryption is relatively insecure, but if I can guarantee that his passbook (i.e. secret key) is secure, symmetric encryption can be secure, too. So how is a symmetric secret key secure? Encrypt it again with the public and private keys! Therefore, after an HTTPS connection, there is only one information exchanged between the public and private keys, that is, the symmetric encryption key, that is, the session key. Symmetric encryption algorithm is a hash function, encryption and decryption relatively faster, this design is from the perspective of efficiency.
A digital signature
Digital signatures are actually very simple and are used to ensure the integrity and correctness of information:
- Ming first generates a digest from the plaintext information using the digest algorithm, which is similar to MD5, SHA-1, sha-2, and is a hash function that cannot be inversely solved
- Xiao Ming encrypts the abstract with a public key
- Ming appended the signature to the content and sent it to the server
- After receiving the signature, the server decrypts the digest with the private key
- The server performs the same summarization algorithm on the content to get a summary
- If the server calculates a summary as in the signature, it is complete and has not been tampered with
HTTPS Handshake Process
Now that you’ve covered the key points of HTTPS, let’s summarize the HTTPS handshake process:
- Xiaoming makes A request to website A
- Site A returns the CA digital certificate containing the public key of site A to the client
- Xiao Ming decrypts the certificate through the CA public key built into his computer and gets the public key of website A (CA public key built into the browser).
- Ming generates a random symmetric secret key, known as a session secret key. The session secret key must be generated by the client, because as mentioned before, the public and private key can only ensure the security of the information sent by the client to the website, and the information encrypted by the public key can only be unlocked by the private key. The private key is hidden in the website, so other people can not unlock the information. But if the site generates a session secret key and encrypts it with its private key, then everyone has a public key and everyone can unlock it.
- Xiao Ming encrypts the session secret key through the public key of website A and sends it to website A
- Next, website A and Xiaoming use the session secret key for HTTP communication
RSA algorithm
As mentioned earlier, information encrypted with public keys cannot be decrypted with the same public key, but can only be decrypted with the private key. This is actually the core secret of asymmetric encryption. Let’s talk about how to achieve this secret, which is actually RSA algorithm. The calculation flow of RSA algorithm is as follows:
- Pick two prime numbers p and q at random
- Calculate n = pq
- Calculate φ(n) = (p-1)(Q-1)
- Find a small odd number e that is mutually prime with φ(n), where the common divisor of two numbers is only 1
- For modulo φ(n), compute the multiplicative inverse d of e, that is, find a d so that the following equation is true :(e*d) mod φ(n) = 1
- Public key: (e, n), private key: (d, n)
- Encryption process: c = (m^e) mod n, (c is encrypted ciphertext, m is original text)
- Decryption process: m = (c^d) mod n
In the seventh step, m to the power of e, m is the text we send, it can be text, JSON, picture, although in various forms, but in the computer is binary 01, so it can be converted to a number to power. Let’s find two numbers to try out the algorithm:
- Pick any two prime numbers 23 and 61
- Calculate n = 23 * 61 = 1403
- Calculate φ(n) = (23-1) * (61-1) = 22 * 60 = 1320
- Find a small odd number e that is mutually compatible with φ(n). Let’s pick 7
- So let’s take the multiplicative inverse d, and I figured out that d is equal to 943. Those of you who are interested in multiplicative inverses can do an online search, but I won’t go into it because it’s not the subject of this article.
- Get public key (7, 1403), private key (943, 1403)
- C = (m^e) mod n = (5^7) % 1403 = 78125%1403 = 960
- M = (c^d) mod n = (960^943) % 1403 = 5, (960^943) m = (c^d) mod n = (960^943) % 1403 = 5, (960^943) m = (960^943) % 1403 = 5, (960^943)
- Try private key encryption: c = (m^d) mod n = (5^943) % 1403 = 283
- Public key decryption: m = (c^e) mod n = (283 ^ 7) % 1403 = 5
Knowing the algorithm, we can answer the previous question, why the public key encryption of their own data can not be solved? Notice that the encryption algorithm (m^e) mod n is a modular operation, and modular operations can’t be solved backwards. For example, if you take 5 and modulo 4, 5%, 4 is 1, but if you go the other way, if you know x%4 is 1, find x. There could be an infinite number of x’s, 5,9,13,17… So even if you have the public key (e,n) and ciphertext (C), you don’t know what the value of m^e is, and you can’t invert it, and that’s the core secret of asymmetric encryption, private key encryption, and you can’t invert it yourself.
Thinking of RSA cracking
The so-called decryption of RSA is to deduce the hidden information from the public information. Specifically, the public key (e, n) is known to obtain the private key (D,n), that is, to obtain D. To solve this equation, we have to know φ(n), because φ(n) = p-1 (q-1), so we have to know p and q. We know that n is equal to pq, and we know n, so it’s still possible to know p and q. So cracking RSA is a simple matter: n is known, and you just split n into the product of two prime numbers. Easy to say, very difficult to do! N is 2048 bits or even 4096 bits. This number can be hundreds or thousands of bits long in decimal. For comparison, JS integers can support up to 53 bits… So in reality there are two ways to break RSA:
- Find an algorithm that can efficiently split a large number N into two prime numbers. Unfortunately, mathematics has not yet found this algorithm.
- If you don’t have a good way to do it, you do it all the way, starting at 2 and going through p and q until their product is n. It is said that it took 5 months for someone to work out a 512 bits n, then they changed the secret key and RSA was upgraded to 1024 bits…
conclusion
- HTTPS is HTTP+RSA+ digital certificate + session key
- RSA implements asymmetric encryption, allowing the public key to be distributed arbitrarily, and even if the private key is lost, a pair of public and private keys can be quickly replaced. Solved the symmetric encryption password book vulnerability.
- Digital certificates guarantee that distributed public keys cannot be tampered with.
- The CA ensures the security of digital certificates.
- Who guarantees CA security is a metaphysics
- Session key is symmetric encryption to speed up encryption and decryption
- Essence of RSA algorithm:
- Encryption uses modular operations and cannot be reversed at all
- N to take a superlarge number, beyond the theoretical limits of mathematics and the industrial limits of computers
- Three ways to decrypt HTTPS:
- Hack the CA and replace the public key of its certificate with yours.
- The god of mathematics possessed, find efficient large number decomposition algorithm, calculate p, Q in minutes
- Turing had developed a super-fast quantum computer that could calculate p and Q in seconds
- Own website did not open HTTPS quickly go back to open, remember to look for a reliable CA to buy a certificate.
At the end of this article, thank you for your precious time to read this article. If this article gives you a little help or inspiration, please do not spare your thumbs up and GitHub stars. Your support is the motivation of the author’s continuous creation.
Welcome to follow my public numberThe big front end of the attackThe first time to obtain high quality original ~
“Front-end Advanced Knowledge” series:Juejin. Im/post / 5 e3ffc…
“Front-end advanced knowledge” series article source code GitHub address:Github.com/dennis-jian…