你如何用 python 实现 metasploit?

问题描述

我正在探索pymetasploit3 模块,但我无法连接到 MSGRPC

的后台进程
import random,os 
from pymetasploit3.msfrpc import MsfRpcClient
passwrd = ''.join([ str(random.randrange(0,99)) for i in range(10) ]) 
print (f"Current password : {passwrd}" )
os.system(f'msfrpcd -P {passwrd} -S')
client = MsfRpcClient(passwrd,port=55553 )

我尝试更改端口号,使用 msfadmin 作为密码,但似乎不起作用

[*] MSGRPC starting on 0.0.0.0:55553 (NO SSL):Msg...
[*] MSGRPC backgrounding at 2021-07-20 07:54:03 -0400...
[*] MSGRPC background PID 1374
Traceback (most recent call last):
  File "/home/kali/.local/lib/python3.9/site-packages/urllib3/connection.py",line 158,in _new_conn
    conn = connection.create_connection(
  File "/home/kali/.local/lib/python3.9/site-packages/urllib3/util/connection.py",line 80,in create_connection
    raise err
  File "/home/kali/.local/lib/python3.9/site-packages/urllib3/util/connection.py",line 70,in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "/home/kali/.local/lib/python3.9/site-packages/urllib3/connectionpool.py",line 597,in urlopen
    httplib_response = self._make_request(conn,method,url,File "/home/kali/.local/lib/python3.9/site-packages/urllib3/connectionpool.py",line 354,in _make_request
    conn.request(method,**httplib_request_kw)
  File "/usr/lib/python3.9/http/client.py",line 1255,in request
    self._send_request(method,body,headers,encode_chunked)
  File "/usr/lib/python3.9/http/client.py",line 1301,in _send_request
    self.endheaders(body,encode_chunked=encode_chunked)
  File "/usr/lib/python3.9/http/client.py",line 1250,in endheaders
    self._send_output(message_body,line 1010,in _send_output
    self.send(msg)
  File "/usr/lib/python3.9/http/client.py",line 950,in send
    self.connect()
  File "/home/kali/.local/lib/python3.9/site-packages/urllib3/connection.py",line 181,in connect
    conn = self._new_conn()
  File "/home/kali/.local/lib/python3.9/site-packages/urllib3/connection.py",line 167,in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fe0816afcd0>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "/home/kali/.local/lib/python3.9/site-packages/requests/adapters.py",line 439,in send
    resp = conn.urlopen(
  File "/home/kali/.local/lib/python3.9/site-packages/urllib3/connectionpool.py",line 637,in urlopen
    retries = retries.increment(method,error=e,_pool=self,File "/home/kali/.local/lib/python3.9/site-packages/urllib3/util/retry.py",line 399,in increment
    raise MaxRetryError(_pool,error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1',port=55553): Max retries exceeded with url: /api/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fe0816afcd0>: Failed to establish a new connection: [Errno 111] Connection refused'))

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "/home/kali/Desktop/ooo.py",line 13,in <module>
    client = MsfRpcClient(passwrd,port=55553 )
  File "/home/kali/.local/lib/python3.9/site-packages/pymetasploit3/msfrpc.py",line 195,in __init__
    self.login(kwargs.get('username','msf'),password)
  File "/home/kali/.local/lib/python3.9/site-packages/pymetasploit3/msfrpc.py",line 229,in login
    auth = self.call(MsfRpcMethod.AuthLogin,[user,password])
  File "/home/kali/.local/lib/python3.9/site-packages/pymetasploit3/msfrpc.py",line 215,in call
    r = self.post_request(url,payload)
  File "<decorator-gen-2>",line 2,in post_request
  File "/home/kali/.local/lib/python3.9/site-packages/retry/api.py",line 73,in retry_decorator
    return __retry_internal(partial(f,*args,**kwargs),exceptions,tries,delay,max_delay,backoff,jitter,File "/home/kali/.local/lib/python3.9/site-packages/retry/api.py",line 33,in __retry_internal
    return f()
  File "/home/kali/.local/lib/python3.9/site-packages/pymetasploit3/msfrpc.py",line 226,in post_request
    return requests.post(url,data=payload,headers=self.headers,verify=False)
  File "/home/kali/.local/lib/python3.9/site-packages/requests/api.py",line 116,in post
    return request('post',data=data,json=json,**kwargs)
  File "/home/kali/.local/lib/python3.9/site-packages/requests/api.py",line 60,in request
    return session.request(method=method,url=url,**kwargs)
  File "/home/kali/.local/lib/python3.9/site-packages/requests/sessions.py",line 533,in request
    resp = self.send(prep,**send_kwargs)
  File "/home/kali/.local/lib/python3.9/site-packages/requests/sessions.py",line 646,in send
    r = adapter.send(request,**kwargs)
  File "/home/kali/.local/lib/python3.9/site-packages/requests/adapters.py",line 516,in send
    raise ConnectionError(e,request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1',port=55553): Max retries exceeded with url: /api/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fe0816afcd0>: Failed to establish a new connection: [Errno 111] Connection refused'))

解决方法

指定服务器连接到它的 IP 地址尝试连接到 127.0.0.1 但后台进程正在使用 0.0.0.0

os.system(f'msfrpcd -P {passwrd} -S  -a 127.0.0.1 ')

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...