From c84510e2bc3bbafa9bc52631b55ed74c30adb98c Mon Sep 17 00:00:00 2001 From: byt3bl33d3r Date: Mon, 14 Jul 2014 22:09:58 +0200 Subject: [PATCH 1/3] fixed list error converted to requsts --- python-msfrpc/msfrpc.py | 73 +++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/python-msfrpc/msfrpc.py b/python-msfrpc/msfrpc.py index 2ef7e88..e6566a4 100644 --- a/python-msfrpc/msfrpc.py +++ b/python-msfrpc/msfrpc.py @@ -1,17 +1,16 @@ -#!/usr/bin/env python +#! /usr/bin/env python # MSF-RPC - A Python library to facilitate MSG-RPC communication with Metasploit -# Ryan Linn - RLinn@trustwave.com +# Ryan Linn - RLinn@trustwave.com, Marcello Salvati - byt3bl33d3r@gmail.com # Copyright (C) 2011 Trustwave # This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - # You should have received a copy of the GNU General Public License along with this program. If not, see . +import requests import msgpack -import httplib class Msfrpc: + class MsfError(Exception): def __init__(self,msg): self.msg = msg @@ -21,47 +20,52 @@ def __str__(self): class MsfAuthError(MsfError): def __init__(self,msg): self.msg = msg - + def __init__(self,opts=[]): self.host = opts.get('host') or "127.0.0.1" - self.port = opts.get('port') or 55552 + self.port = opts.get('port') or "55552" self.uri = opts.get('uri') or "/api/" self.ssl = opts.get('ssl') or False - self.authenticated = False - self.token = False - self.headers = {"Content-type" : "binary/message-pack" } - if self.ssl: - self.client = httplib.HTTPSConnection(self.host,self.port) - else: - self.client = httplib.HTTPConnection(self.host,self.port) - - def encode(self,data): + self.token = None + self.headers = {"Content-type" : "binary/message-pack"} + + def encode(self, data): return msgpack.packb(data) - def decode(self,data): + + def decode(self, data): return msgpack.unpackb(data) - def call(self,meth,opts = []): - if meth != "auth.login": - if not self.authenticated: + def call(self, method, opts=[]): + if method != 'auth.login': + if self.token == None: raise self.MsfAuthError("MsfRPC: Not Authenticated") - if meth != "auth.login": - opts.insert(0,self.token) + if method != "auth.login": + opts.insert(0, self.token) - opts.insert(0,meth) - params = self.encode(opts) - self.client.request("POST",self.uri,params,self.headers) - resp = self.client.getresponse() - return self.decode(resp.read()) + if self.ssl == True: + url = "https://%s:%s%s" % (self.host, self.port, self.uri) + else: + url = "http://%s:%s%s" % (self.host, self.port, self.uri) - def login(self,user,password): - ret = self.call('auth.login',[user,password]) - if ret.get('result') == 'success': - self.authenticated = True - self.token = ret.get('token') + + opts.insert(0, method) + payload = self.encode(opts) + + r = requests.post(url, data=payload, headers=self.headers) + + opts[:] = [] #Clear opts list + + return self.decode(r.content) + + def login(self, user, password): + auth = self.call("auth.login", [user, password]) + try: + if auth['result'] == 'success': + self.token = auth['token'] return True - else: - raise self.MsfAuthError("MsfRPC: Authentication failed") + except: + raise self.MsfAuthError("MsfRPC: Authentication failed") if __name__ == '__main__': @@ -81,4 +85,3 @@ def login(self,user,password): ret = client.call('module.compatible_payloads',[mod['modules'][0]]) for i in (ret.get('payloads')): print "\t%s" % i - From 8f1c3215d3491080043e9581bdb2cc01f5b1a362 Mon Sep 17 00:00:00 2001 From: byt3bl33d3r Date: Mon, 14 Jul 2014 22:12:36 +0200 Subject: [PATCH 2/3] Update README --- README | 1 + 1 file changed, 1 insertion(+) diff --git a/README b/README index 62aeea6..b122392 100644 --- a/README +++ b/README @@ -1,6 +1,7 @@ ===================== Metasploit MSGRPC Modules Ryan Linn +Marcello Salvati http://www.trustwave.com ===================== ----------------------------------------------------- From a298706806e637e85aabb2746c1321db71059dcd Mon Sep 17 00:00:00 2001 From: byt3bl33d3r Date: Mon, 14 Jul 2014 22:13:28 +0200 Subject: [PATCH 3/3] Update README --- README | 1 - 1 file changed, 1 deletion(-) diff --git a/README b/README index b122392..62aeea6 100644 --- a/README +++ b/README @@ -1,7 +1,6 @@ ===================== Metasploit MSGRPC Modules Ryan Linn -Marcello Salvati http://www.trustwave.com ===================== -----------------------------------------------------