我正在使用 python flask 和 python3 编写一个 web 应用程序,并且想使用 metasploit API。使用 python2 编写代码时,一切正常(因为 lib 是为 python2 编写的)。但是,当尝试在 python 3 中使用它时,我得到了这个错误:
File "/usr/local/lib/python3.6/dist-packages/msfrpc.py", line 64, in login
raise self.MsfAuthError("MsfRPC: Authentication failed")
msfrpc.MsfAuthError: 'MsfRPC: Authentication failed'
python2和python 3版本的msfrpc.py文件唯一的区别是py2版本包含“httplib”并使用“httplib.HTTPSConnection”连接msgrpc服务,而py3版本包含“http.client”和使用“http.client.HTTPConnection”连接到服务。
有谁知道为什么会发生这个错误?
这里是 msfrpc.py 的源代码: import msgpack import http.client
class Msfrpc:
class MsfError(Exception):
def __init__(self,msg):
self.msg = msg
def __str__(self):
return repr(self.msg)
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.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 = http.client.HTTPConnection(self.host,self.port)
else:
self.client = http.client.HTTPConnection(self.host,self.port)
def encode(self,data):
return msgpack.packb(data)
def decode(self,data):
return msgpack.unpackb(data)
def call(self,meth,opts = []):
if meth != "auth.login":
if not self.authenticated:
raise self.MsfAuthError("MsfRPC: Not Authenticated")
if meth != "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())
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')
return True
else:
raise self.MsfAuthError("MsfRPC: Authentication failed")