Search code examples
python-3.xmetasploit

How do you emplent metasploit with python?


I am exploring pymetasploit3 module but I am having trouble connecting to the background process of 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 )

I tried to change the port number, use msfadmin as the password , but it does not seem to work

[*] 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, url, **httplib_request_kw)
  File "/usr/lib/python3.9/http/client.py", line 1255, in request
    self._send_request(method, url, 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, encode_chunked=encode_chunked)
  File "/usr/lib/python3.9/http/client.py", 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, url, error=e, _pool=self,
  File "/home/kali/.local/lib/python3.9/site-packages/urllib3/util/retry.py", line 399, in increment
    raise MaxRetryError(_pool, url, 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', url, 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'))


Solution

  • specify the ip address for the server to connect to it trying to connect to 127.0.0.1 but the background process is using 0.0.0.0

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