Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a SSL socket connection in Python

I'm trying to establish a secure socket connection in Python, and i'm having a hard time with the SSL bit of it. I've found some code examples of how to establish a connection with SSL, but they all involve key files. The server i'm trying to connect with doesn't need to receive any keys or certificates. My question is how do I essentially wrap a python socket connection with SSL. I know for a fact that the cipher i'm suppose to use is ADH-AES256-SHA, and the protocol is TLSv1. This is what i've been trying:

import socket import ssl  # SET VARIABLES packet, reply = "<packet>SOME_DATA</packet>", "" HOST, PORT = 'XX.XX.XX.XX', 4434  # CREATE SOCKET sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(10)  # WRAP SOCKET ??? ssl.wrap_socket(sock, ssl_version="TLSv1", ciphers="ADH-AES256-SHA")  # CONNECT AND PRINT REPLY sock.connect((HOST, PORT)) sock.send(packet) print sock.recv(1280)  # CLOSE SOCKET CONNECTION sock.close() 

When I run this code, I don't get any errors, but I get a blank response. When trying to debug this code in the command line, by typing in python in the terminal and pasting in code line by line, I get what i'm assuming is a status code when running sock.send(packet). The integer response I get is 26. If anyone knows what this means, or can help in anyway it would be greatly appreciated. Thanks in advance!

like image 520
Raffi Avatar asked Nov 10 '14 19:11

Raffi


People also ask

How do I open a socket in Python?

It is very simple to create a socket client using the Python's socket module function. The socket. connect(hosname, port ) opens a TCP connection to hostname on the port. Once you have a socket open, you can read from it like any IO object.

How do I use SSL certificate in Python?

To install certifi Python on Microsoft Windows: Type cmd in the search bar and hit Enter to open the command line. Type python3 -m pip install certifi in the command line and hit Enter again. This installs certifi for your default Python installation.


1 Answers

Ok, I figured out what was wrong. It was kind of foolish of me. I had two problems with my code. My first mistake was when specifying the ssl_version I put in TLSv1 when it should have been ssl.PROTOCOL_TLSv1. The second mistake was that I wasn't referencing the wrapped socket, instead I was calling the original socket that I have created. The below code seemed to work for me.

import socket import ssl  # SET VARIABLES packet, reply = "<packet>SOME_DATA</packet>", "" HOST, PORT = 'XX.XX.XX.XX', 4434  # CREATE SOCKET sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(10)  # WRAP SOCKET wrappedSocket = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1, ciphers="ADH-AES256-SHA")  # CONNECT AND PRINT REPLY wrappedSocket.connect((HOST, PORT)) wrappedSocket.send(packet) print wrappedSocket.recv(1280)  # CLOSE SOCKET CONNECTION wrappedSocket.close() 

Hope this can help somebody!

like image 66
Raffi Avatar answered Sep 19 '22 02:09

Raffi