diff options
-rw-r--r-- | Core.py | 12 | ||||
-rw-r--r-- | module/remote/RequestHandler.py | 63 | ||||
-rw-r--r-- | module/remote/RequestObject.py | 17 | ||||
-rw-r--r-- | module/remote/SocketServer.py | 8 | ||||
-rw-r--r-- | sockettest.py | 68 |
5 files changed, 108 insertions, 60 deletions
@@ -212,6 +212,18 @@ class Core(object): else: return False + def get_downloads(self): #only for debuging?!? + list = [] + for pyfile in self.thread_list.py_downloading: + download = {} + download['name'] = pyfile.status.filename + download['speed'] = pyfile.status.get_speed() + download['eta'] = pyfile.status.get_ETA() + list.append(download) + + return list + + def _test_print_status(self): if self.thread_list.py_downloading: diff --git a/module/remote/RequestHandler.py b/module/remote/RequestHandler.py index c72f6eaaa..09ad5b548 100644 --- a/module/remote/RequestHandler.py +++ b/module/remote/RequestHandler.py @@ -8,44 +8,57 @@ this module handels the incoming requests """ import base64 -import cPickle import random import string + +import cPickle from Crypto.Cipher import AES +from Crypto.Hash import MD5 from Crypto.Hash import SHA +from RequestObject import RequestObject -class RequestHandler(): +class RequestHandler: def __init__(self, core): - self.core = core - key = SHA.new(core.config['remotepassword']) - self.aes = AES.new(key.hexdigest()[:32], AES.MODE_ECB) + self.core = core + key = SHA.new(core.config['remotepassword']) + key = MD5.new(key.hexdigest()) + self.aes = AES.new(key.hexdigest(), AES.MODE_ECB) def proceed(self, data): - return self.encrypt({'befehl' : None , 'args':[1,2,3], 'test': 'lol'}) + obj = self.decrypt(data) - def decrypt(self, dec_str): - dec_str = base64.standard_b64decode(dec_str) - dec_str = self.aes.decrypt(dec_str) + if obj.command == "exec": + func = getattr(self.core, obj.function) + obj.response = func() + else: + obj.response = "antwort" + + return self.encrypt(obj) - dec_str = dec_str[:-(int(dec_str[-1],16)+1)] - obj = cPickle.loads(dec_str) - return obj + + def decrypt(self, dec_str): + dec_str = base64.standard_b64decode(dec_str) + dec_str = self.aes.decrypt(dec_str) + + dec_str = dec_str[:-(int(dec_str[-1], 16) + 1)] + obj = cPickle.loads(dec_str) + return obj def encrypt(self, obj): - enc_str = cPickle.dumps(obj, 1) - padding = len(enc_str) % 16 - padding = 16 - padding - - p_str = "" - for i in range(padding - 1): - p_str += random.choice(string.letters+string.digits) - p_str += hex(len(p_str)).replace("0x","") - enc_str += p_str - - enc_str = self.aes.encrypt(enc_str) - enc_str = base64.standard_b64encode(enc_str) - return enc_str + enc_str = cPickle.dumps(obj, 1) + padding = len(enc_str) % 16 + padding = 16 - padding + + p_str = "" + for i in range(padding - 1): + p_str += random.choice(string.letters + string.digits) + p_str += hex(len(p_str)).replace("0x", "") + enc_str += p_str + + enc_str = self.aes.encrypt(enc_str) + enc_str = base64.standard_b64encode(enc_str) + return enc_str diff --git a/module/remote/RequestObject.py b/module/remote/RequestObject.py new file mode 100644 index 000000000..77322663a --- /dev/null +++ b/module/remote/RequestObject.py @@ -0,0 +1,17 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +""" +authored by: RaNaN + +this module handels the incoming requests + +""" + +class RequestObject(object): + def __init__(self): + self.version = 0 + self.sender = "ip" + self.command = None + self.function = "" + self.args = [] + self.response = ""
\ No newline at end of file diff --git a/module/remote/SocketServer.py b/module/remote/SocketServer.py index 1252547ad..9000c1c71 100644 --- a/module/remote/SocketServer.py +++ b/module/remote/SocketServer.py @@ -48,13 +48,13 @@ class SecondaryServerSocket(asynchat.async_chat): self.pycore = pycore self.handler = RequestHandler(pycore) self.set_terminator('\n') - self.data = [] + self.data = "" def collect_incoming_data(self, data): - self.data.append(data) + self.data += data def found_terminator(self): - rep = self.handler.proceed(self.data) + rep = self.handler.proceed(self.data) self.push(rep) - self.data = [] + self.data = "" #having fun with the data def handle_close(self): print "Disconnected from", self.getpeername() diff --git a/sockettest.py b/sockettest.py index 30a4deb81..882895b57 100644 --- a/sockettest.py +++ b/sockettest.py @@ -12,56 +12,62 @@ import random import string from Crypto.Cipher import AES from Crypto.Hash import SHA +from Crypto.Hash import MD5 +from module.remote.RequestObject import RequestObject -class handler: + +class Handler: def __init__(self): - key = SHA.new("pwhere") - self.aes = AES.new(key.hexdigest()[:32], AES.MODE_ECB) + key = SHA.new("pwhere") + key = MD5.new(key.hexdigest()) + self.aes = AES.new(key.hexdigest(), AES.MODE_ECB) def proceed(self, data): - return self.decrypt(self.encrypt(str(("lol","mehrlol","pff")))) + + return self.decrypt(self.encrypt(str(("lol","mehrlol","pff")))) def decrypt(self, dec_str): - try: - dec_str = base64.standard_b64decode(dec_str) - dec_str = self.aes.decrypt(dec_str) + try: + dec_str = base64.standard_b64decode(dec_str) + dec_str = self.aes.decrypt(dec_str) - dec_str = dec_str[:-(int(dec_str[-1],16)+1)] - obj = cPickle.loads(dec_str) - except: - obj = None - return obj + dec_str = dec_str[:-(int(dec_str[-1],16)+1)] + obj = cPickle.loads(dec_str) + except: + obj = None + return obj def encrypt(self, obj): - enc_str = cPickle.dumps(obj, 1) - padding = len(enc_str) % 16 - padding = 16 - padding - - p_str = "" - for i in range(padding - 1): - p_str += random.choice(string.letters+string.digits) - p_str += hex(len(p_str)).replace("0x","") - enc_str += p_str + enc_str = cPickle.dumps(obj, 1) + padding = len(enc_str) % 16 + padding = 16 - padding + + p_str = "" + for i in range(padding - 1): + p_str += random.choice(string.letters+string.digits) + p_str += hex(len(p_str)).replace("0x","") + enc_str += p_str - enc_str = self.aes.encrypt(enc_str) - enc_str = base64.standard_b64encode(enc_str) - return enc_str + enc_str = self.aes.encrypt(enc_str) + enc_str = base64.standard_b64encode(enc_str) + return enc_str import socket -handler = handler() +handler = Handler() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(('localhost', 7272)) print "Connected to server" -data = """A few lines of data to test the operation of both server and client. -Und noch eine Zeile""" -for line in data.splitlines(): - sock.sendall(line+'\n') - print "Sent:", line + +obj = RequestObject() +obj.command = "exec" +obj.function = "get_downloads" + +sock.sendall(handler.encrypt(obj)+"\n") response = sock.recv(8192) -print "Received:", handler.decrypt(response) +print "Received:", handler.decrypt(response).response sock.close() |