在Python中接收原始TLS数据包

时间:2018-01-07 17:44:08

标签: python sockets ssl

我在Python 2.7中编写一个简单的SSL套接字服务器和客户端。我试图从客户端发送一个字节数组并从服务器读取相同的字节数组。这是代码:

服务器

import socket
import ssl
import threading

class SocketServer(object):
    def __init__(self, host, port):
        self.context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        self.context.load_cert_chain(certfile="D:\plugnplay\openssl\cert.pem")
        self.host = host
        self.port = port
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind((self.host, self.port))

    def listen(self):
        self.sock.listen(5)
        while True:
            sslsoc = None
            try:
                client, address = self.sock.accept()
                sslsoc = self.context.wrap_socket(client, server_side=True)
                threading.Thread(target = self.listenToClient,args = (client,address)).start()
            except Exception,ex:
                print (('Connection error : client - {0}, error - {1}').format(address, ex))
                try:
                    if sslsoc != None:
                        sslsoc.close()
                    else:
                        client.close()
                except:
                    pass


    def listenToClient(self, sslsoc, address):
        sslsoc.settimeout(60)
        size = 1024
        full_data = []
        while True:
            try:
                data = sslsoc.recv(size)
                byte_data = bytearray()
                byte_data.extend(data)
                if data:
                    for d in byte_data:
                        full_data.append(int(d))
                else:
                    raise Exception('Client disconnected')
            except Exception,ex:
                sslsoc.close()
                print full_data
                return

if __name__ == "__main__":
    port_num = 7656
    SocketServer('',port_num).listen()

客户端

import socket, ssl
import time

port = 7656
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.load_cert_chain(certfile="D:\plugnplay\openssl\cert.pem")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = context.wrap_socket(s, server_side = False)
ssl_sock.connect(('127.0.0.1', port))

data = bytearray()
data.append(1)
data.append(2)
data.append(3)
data.append(4)
ssl_sock.write(data)

ssl_sock.close()

问题是,我将以下数组作为服务器的输出 -

[23,3,3,0,28,83,69,220,108,217,65,85,25,96,230,134,63,153,137,21,226,71,162, 89,86,203,141,178,171,103,72,216,79]

但我想将[1,2,3,4]作为输出。每次运行客户端时,这些值都会发生变化,前5个字节除外 - 23,3,3,0,28。

我正在使用Win 10和Python 2.7.14。我使用Openssl使用以下命令生成SSL证书 -

openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem

我做错了什么?

2 个答案:

答案 0 :(得分:1)

  

[23,3,3,0

这是TLS帧的开始,即ContentType 23(应用程序数据),后跟TLS版本(3.3表示TLS 1.2)。这意味着您从普通套接字而不是SSL套接字读取。

原因是您使用普通套接字nodes调用listenToClient但函数实际上需要SSL套接字client

sslsoc

答案 1 :(得分:0)

(代表作者提问)

我将函数'listenToClient'的参数从'client'更改为'sslsoc',正如Steffen建议的那样。现在一切正常。