Transport Layer Security (TLS)
Transport Layer Security (TLS)
Task1:TLS Client
Task 1.a: TLS handshake
Use the following code, in order to print out the various algorithms used by TLS, you can add the
pprint.pprint(ssock.cipher()) statement on the basis of the experimental manual code:
#!/usr/bin/python3
hostname = sys.argv[1]
port = 443
cadir = '/etc/ssl/certs'
context.load_verify_locations(capath=cadir)
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
sock.connect((hostname, port))
do_handshake_on_connect=False)
pprint.pprint(ssock.getpeercert())
pprint.pprint(context.get_ca_certs())
ssock.shutdown(socket.SHUT_RDWR)
ssock.close()
The first step is to create a TLS context object. The object saves our preference settings
for certificate authentication and encryption algorithm selection. The context part of the
code.
The second step is to establish a TCP handshake protocol, make a TCP connection, and
the code sock part.
The third step is to call the context object created in the first step and use the
wrap_socket() method on it, which means that the OpenSLL library is responsible for
controlling our TCP connection. Then exchange the necessary handshake information
with the communicating party and establish an encrypted link. The ssock
=context.wrap_socket... part of the code
The last step is to use the ssl_sock object returned by the wrap_socket() call for all
subsequent communications. Subsequent communication needs to use a format similar to
ssock.method name.
Question
1. print out
These files are root CA files, which are used to verify the validity of the certificate sent
back by the server when establishing a TLS connection.
4. Use wireshark to capture relevant information about the TLS connection with
www.baidu.com, get a series of data packets, and analyze them one by one:
TCP handshake:
The TCP three-way handshake protocol, it can be seen that the first TCP handshake is
intuitive. TLS is based on the transport layer, so it should be based on the TCP
connection. Of course, there are also based on UDP, called DTLS. .
TLS handshake
The TLS packets in the picture are all TLS handshake related data packets. The following
analysis is one by one:
The client first sends out Client Hello related data, including all cipher suites supported
by the client, client random numbers (used as Nonce), uh... a lot of information
The server replies to the client Server Hello related data:
And the certificate (maybe the amount of certificate data is too large, it will be transmitted
separately from the above):
Subsequently, the client verifies the validity of the certificate. If it is valid, the client
sends a Key Exchange Client Key Exchange and a Change Cipher Spec (change cipher
specification message) to the server, and the client handshake ends:
The
server replies to Change Cipher Spec (change cipher specification message) and a New
Session value, and the handshake ends. Sometimes these two will be sent separately, that
is, they may not be sent together:
1. In the above task, we print out the information of the certificate sent back by the server
and find the issuer information in the certificate information:
2.
Go
to the /etc/ssl/certs directory to find the .pem file that matches the
organizationalUnitName field or commonName field of the certificate sent back by the
server. As for when to choose which field, I don't know what is needed when testing
www.baidu.com The commonName field of the file corresponds to the
GlobalSign_Root_CA.pem file in the /etc/ssl/certs directory, and in the subsequent
www.jd.com test, we found that the file matching the organizationalUnitName field
information is valid... As for the specific matching mechanism , I don’t know...On this
point, I took a lot of detours. Finally...
4. Subsequently, modify the cadir=’./certs’ in the previous code to test and find that the
results are consistent, so task1a is the same, the results can print out the corresponding
information of the certificate, and the connection is successful.
Direct test, because ./cert is not configured with the corresponding certificate, I get the error
message:
At this point, modify cadir=’./certs’ in the code as the host storage certificate directory:
cadir=‘etc/ssl/certs’ to get the corresponding certificate issuer information:
Find the corresponding .pem file in the etc/ssl/certs directory, move it to the ./cert file, perform
hash calculation, soft link processing, and then change back to cadir='./certs' to get the correct
return result ,connection succeeded.
Question 1:
According to the experimental requirements, we need to find the corresponding certificate file in
the /etc/ssl/certs directory to verify the certificate returned by the server, but how to find the
correct .pem file?
In response to this problem, the first thing that comes to mind is to look for the .pem field
information in the server certificate printed by the code. However, the .pem format field did not
appear in the certificate field, so I wondered: Is there any function or method in the ssl library
that can print out the name of the .pem file that needs to be matched, so a search...Look at the ssl
source code, Although I didn’t understand much, I still didn’t find anything usable. After
spending a long time, forget it, and don’t find it, I tried to find a few names and the subject field
in the certificate in the etc/ssl/certs directory. I found several certificate files with the best match,
but the GlobalSign_Root_CA.pem file looks the best match. I tried it, hey. . . Okay
Maybe there is no such method. . . The certificate file name directly matches the hash
value. . . I split...
Question 2:
In the experiment manual, all the .crt files are manipulated, and the .pem files in the etc/ssl/certs
directory are all .pem files, this...can it be successful? What is the difference between the two
files?
According to the soft link, two files with the same file name and different suffixes were found.
These two files were originally linked to each other. You can find them using the ls -l command
in the etc/ssl/certs directory and move them to the experiment folder.
Use the diff command to compare and find that the file content is the same:
Access to information: .pem is a file in a specific format encoded by BASE64. There are fixed
strings at the beginning and end of the file, such as -BEGIN-... and .crt files are common in
UNIX systems, and most of them are files in PEM format.
The hash value generated by using the two file names is also the same, and both can be verified
by the certificate
…Bingo!
Task 1.c
Test the www.sdu.edu.cn website, and perform the following sub-case tests after configuring
/etc/hosts:
False: No hostname verification is performed, and the wrong certificate can be verified
successfully.
True: Perform host name verification, and the wrong certificate cannot be verified successfully.
Question:
If hostname verification is not performed, then the attacker can use the legitimate certificate of
other websites to impersonate the legitimate certificate of his forged website and send it back to
the user for verification. Since the hostname verification is ignored, the user will not notice it.
Achieve the website's purpose of deceiving users.
Return the response data to the www.baidu.com website to the baidu.html file and open it with a
browser.
(2). Modify the HTTP request line code to make the program return an image file data,...
Tried to modify,
Make up later...
Make up...
repair…
Task2:TLS Server
#!/usr/bin/python3
html = """
"""
SERVER_CERT = '../lab_pki/server.crt'#seedlab Crypto_PKI The certificate file of
SEEDPKILab.com obtained in the experimentSERVER_PRIVATE =
'../lab_pki/server.key'#seedlab Crypto_PKIThe private key file of SEEDPKILab.com obtained in
the experiment
context.load_cert_chain(SERVER_CERT, SERVER_PRIVATE)
sock.bind(('0.0.0.0', 4433))
sock.listen(5)
while True:
try :
pprint.pprint("Request: {}".format(data))
ssock.close()
except Exception:
continue
In the server.py code, we use sock.bind(('0.0.0.0', 4433)) to bind the listening port 4433,
so we need to make some modifications based on the client code in task1, that is, modify
the port number (Otherwise the client program and server program will not run
successfully):
We need to use SEEDPKILab.com for the host name parameter when running the code
on the client, so we need to configure the static IP in the local /etc/hosts file and add the
127.0.0.1 SEEDPKILab.com entry so that the client will access it locally. .
In the server.py program, the sock.bind(('0.0.0.0', 4433)) code sentence, the IP address of
the binding code is 0.0.0.0, which means that for any IP address on the machine,
server.py will accept , And then reply to the data, this is very important: it corresponds to
the role of the above configuration IP, so that the server and the client program can
connect.
Two Terminals need to be opened, one to run the server.py program and the other to run
the client program. The client gets the following results:
In the process of debugging the server.py program, we may encounter the following situations:
This is a port occupancy problem. Although the server.py program is closed at this time,
0.0.0.0:4433 is still in the listening state. You can use the following statement to close it for
subsequent debugging.
netstat -tunlp
This is a combined command, -t, -u, -n, -l, -p. It can print out linux network status information,
tcp, udp port, IP address and other information, check the 0.0.0.0 in Local Address: 4433 status,
if it is running, after getting the corresponding PID, use the following command to kill:
Google it and saw a blog written. For a social-oriented CA issuing organization, the country and
province in front of the certificate issued by it can be set at will, but when issuing a certificate
with a private CA, it needs to be filled in with the server's certificate. The same, otherwise the
signing is unsuccessful, and the reason is not said, remember...
Run the server.py program in one Terminal, and run the client program in task1 in the other
Terminal, and use the host name www.csdn2020.com in the alt_name field to access (because it
is tested on this machine, it is necessary to configure a static IP in the etc/hosts file , Add
127.0.0.1 www.csdn2020.com entry:
After testing), the following results are obtained:
It can also be tested in the browser. Since we have imported the private ca.crt certificate in the
browser, and the server certificate we generated is issued by this ca.crt, it will pass the
verification:
Of course, we can also test on other hosts, just need to configure a static IP in the etc/hosts file to
make an IP modification.