From 69f765b2476b77967588c0751be8afda7dc3adaf Mon Sep 17 00:00:00 2001 From: RaNaN Date: Sun, 31 May 2009 15:09:58 +0200 Subject: new socket encryption --- module/remote/ClientHandler.py | 22 ++++++++++++++++++++++ module/remote/ClientSocket.py | 26 +++++++++++++++++--------- module/remote/RequestHandler.py | 16 +++++++--------- module/remote/SocketServer.py | 10 +++++----- 4 files changed, 51 insertions(+), 23 deletions(-) create mode 100644 module/remote/ClientHandler.py (limited to 'module/remote') diff --git a/module/remote/ClientHandler.py b/module/remote/ClientHandler.py new file mode 100644 index 000000000..0037a874e --- /dev/null +++ b/module/remote/ClientHandler.py @@ -0,0 +1,22 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +""" +authored by: RaNaN + +this module handels the incoming requests + +""" +import hashlib + +from Crypto.Cipher import Blowfish +from RequestHandler import RequestHandler + +class ClientHandler(RequestHandler): + def __init__(self, client): + self.client = client + key = hashlib.sha256("pwhere") + self.bf = Blowfish.new(key.hexdigest(), Blowfish.MODE_ECB) + + def proceed(self, data): + obj = self.decrypt(data) + return self.encrypt(obj) \ No newline at end of file diff --git a/module/remote/ClientSocket.py b/module/remote/ClientSocket.py index 74f66f0be..30eaf20e0 100644 --- a/module/remote/ClientSocket.py +++ b/module/remote/ClientSocket.py @@ -8,23 +8,31 @@ socket for connecting to the core's server """ import asynchat import socket -from RequestHandler import RequestHandler + +from ClientHandler import ClientHandler class ClientSocket(asynchat.async_chat): def __init__(self, client): - asynchat.async_chat.__init__(self) - self.client = client + asynchat.async_chat.__init__(self) + self.client = client self.data = "" - self.handler = RequestHandler(None) - self.set_terminator("\n") + self.handler = ClientHandler(None) + self.set_terminator("\n") self.create_socket(socket.AF_INET, socket.SOCK_STREAM) def handle_connect(self): - print "connected" - + print "connected" + + def handle_close(self): + print "Disconnected from", self.getpeername() + self.close() + def collect_incoming_data(self, data): + print "data arrived" self.data += data def found_terminator(self): - pass - #process + obj = self.handler.proceed(data) + self.push(obj) + print "pushed" + data = "" diff --git a/module/remote/RequestHandler.py b/module/remote/RequestHandler.py index 212f65b1c..ec0bf0414 100644 --- a/module/remote/RequestHandler.py +++ b/module/remote/RequestHandler.py @@ -8,29 +8,27 @@ this module handels the incoming requests """ import base64 +import hashlib import random import string import cPickle -from Crypto.Cipher import AES -from Crypto.Hash import MD5 -from Crypto.Hash import SHA +from Crypto.Cipher import Blowfish from RequestObject import RequestObject class RequestHandler: def __init__(self, core): self.core = core - key = SHA.new("pwhere") #core.config['remotepassword'] - key = MD5.new(key.hexdigest()) - self.aes = AES.new(key.hexdigest(), AES.MODE_ECB) + key = hashlib.sha256("pwhere") #core.config['remotepassword'] + self.bf = Blowfish.new(key.hexdigest(), Blowfish.MODE_ECB) def proceed(self, data): obj = self.decrypt(data) if obj.command == "exec": func = getattr(self.core, obj.function) - obj.response = func(*obj.args) + obj.response = func( * obj.args) else: obj.response = "error happend" @@ -40,7 +38,7 @@ class RequestHandler: def decrypt(self, dec_str): try: dec_str = base64.standard_b64decode(dec_str) - dec_str = self.aes.decrypt(dec_str) + dec_str = self.bf.decrypt(dec_str) dec_str = dec_str[:-(int(dec_str[-1], 16) + 1)] obj = cPickle.loads(dec_str) @@ -60,7 +58,7 @@ class RequestHandler: p_str += hex(len(p_str)).replace("0x", "") enc_str += p_str - enc_str = self.aes.encrypt(enc_str) + enc_str = self.bf.encrypt(enc_str) enc_str = base64.standard_b64encode(enc_str) return enc_str diff --git a/module/remote/SocketServer.py b/module/remote/SocketServer.py index 5bd6ba3d0..817e956aa 100644 --- a/module/remote/SocketServer.py +++ b/module/remote/SocketServer.py @@ -45,16 +45,16 @@ class SecondaryServerSocket(asynchat.async_chat): def __init__(self, socket, pycore): print 'initing SSS' asynchat.async_chat.__init__(self, socket) - self.pycore = pycore - self.handler = RequestHandler(pycore) - self.set_terminator('\n') + self.pycore = pycore + self.handler = RequestHandler(pycore) + self.set_terminator('\n') self.data = "" def collect_incoming_data(self, data): self.data += data def found_terminator(self): - print "data arrived" rep = self.handler.proceed(self.data) - self.push(rep) + self.sendall(rep+"\n") + print "push" self.data = "" #having fun with the data def handle_close(self): -- cgit v1.2.3