Last active
January 6, 2023 15:35
-
-
Save david-botelho-mariano/50fbd071b4afa8bb039a6c19379bff2e to your computer and use it in GitHub Desktop.
pure file transfer using ssl and python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import socket | |
import ssl | |
#importando biblioteca para trabalhar com sockets SSL | |
host_addr = '127.0.0.1' | |
#definindo o endereço do servidor alvo | |
host_port = 8082 | |
#definindo a porta do servidor alvo | |
server_sni_hostname = 'example.com' | |
#definindo o nome do servidor alvo | |
server_cert = 'server.crt' | |
#definindo o certificado do servidor alvo | |
client_cert = 'client.crt' | |
#definindo o certificado do cliente | |
client_key = 'client.key' | |
#definindo a chave do cliente | |
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=server_cert) | |
#criando o contexto para o cliente | |
context.load_cert_chain(certfile=client_cert, keyfile=client_key) | |
#carregando o certificado e a chave do cliente | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
#criando o socket TCP | |
conn = context.wrap_socket(s, server_side=False, server_hostname=server_sni_hostname) | |
#criando a conexão SSL | |
conn.connect((host_addr, host_port)) | |
#conectando ao servidor alvo | |
print("Conexao SSL estabelecida: {}".format(conn.getpeercert())) | |
with open('tecnicas-integridade.pdf', 'rb') as f: | |
data = f.read() | |
conn.send(data) | |
print("Arquivo enviado") | |
#conn.send(bytes(usuario_senha, 'UTF-8')) | |
print("Fechando conexao") | |
conn.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import socket | |
from socket import AF_INET, SOCK_STREAM, SO_REUSEADDR, SOL_SOCKET, SHUT_RDWR | |
import ssl | |
#importando biblioteca para trabalhar com sockets SSL | |
listen_addr = '127.0.0.1' | |
#escuta conexoes vinda do proprio computador | |
listen_port = 8082 | |
#porta que o servidor vai escutar | |
server_cert = 'server.crt' | |
#arquivo com o certificado do servidor | |
server_key = 'server.key' | |
#arquivo com a chave privada do servidor | |
client_certs = 'client.crt' | |
#arquivo com o certificado do cliente | |
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) | |
#criando contexto para o servidor | |
context.verify_mode = ssl.CERT_REQUIRED | |
#verificando o certificado do cliente | |
context.load_cert_chain(certfile=server_cert, keyfile=server_key) | |
#carregando o certificado e a chave do servidor | |
context.load_verify_locations(cafile=client_certs) | |
#carregando o certificado do cliente | |
bindsocket = socket.socket() | |
#criando socket | |
bindsocket.bind((listen_addr, listen_port)) | |
#vinculando o socket a porta | |
bindsocket.listen(5) | |
#escutando conexoes | |
while True: | |
print("Aguardando cliente...") | |
#esperando conexoes | |
newsocket, fromaddr = bindsocket.accept() | |
#aceitando conexoes | |
print("Cliente se conectou: {}:{}".format(fromaddr[0], fromaddr[1])) | |
#mostrando o endereco do cliente | |
conn = context.wrap_socket(newsocket, server_side=True) | |
#criando conexao criptografada | |
print("Conexao SSL estabelecida: {}".format(conn.getpeercert())) | |
#mostrando o certificado do cliente | |
buf = b'' | |
#inicializando buffer para receber dados do cliente | |
try: | |
while True: | |
data = conn.recv(4096) | |
#recebendo dados do cliente | |
if data: | |
buf += data | |
#colocar no buffer os dados que o cliente enviou | |
else: | |
print("Recebi:", buf) | |
#mostrando os dados que o cliente enviou | |
#pegar conteudo do buffer e salvar em arquivo | |
with open('arquivo.pdf', 'wb') as f: | |
f.write(buf) | |
break | |
finally: | |
print("Fechando conexao") | |
#conn.shutdown(socket.SHUT_RDWR) | |
#fechando conexao | |
#conn.close() | |
#fechando socket |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment