jborean93 / pypsrp

PowerShell Remoting Protocol for Python
MIT License
324 stars 49 forks source link

Cannot connect to Exchange PowerShell with Pre-Release version #157

Closed bigdavidwong closed 1 year ago

bigdavidwong commented 2 years ago

Hi Jordan,

I've downloaded the pre-release version [v1.0.0b1] package and installed it, now I have some trouble when I am using it to connect to Exchange PowerShell;

here is my code:

def sync_rp(conn: psrp.ConnectionInfo) -> None:
    with psrp.SyncRunspacePool(conn) as rp:
        ps = psrp.SyncPowerShell(rp)
        ps.add_command("Get-Date")
        ps.add_statement()
        output = ps.invoke()

host = 'test-mailbox02.shizhuang-inc.com'
username='test@shizhuang-inc.com'
password='Test12345'
wsmanInfo = psrp.WSManInfo(server=host, username=username, password=password, path='PowerShell', configuration_name='Microsoft.Exchange', auth='kerberos', port=80)
sync_rp(wsmanInfo)

and I always get some errors like this:

_Traceback (most recent call last):
  File "C:/Users/davidwong/Documents/testProjects/main.py", line 32, in <module>
    sync_rp(wsmanInfo)
  File "C:/Users/davidwong/Documents/testProjects/main.py", line 18, in sync_rp
    with psrp.SyncRunspacePool(conn) as rp:
  File "D:\testProjects\lib\site-packages\psrp\_sync.py", line 510, in __enter__
    self.open()
  File "D:\testProjects\lib\site-packages\psrp\_sync.py", line 666, in open
    connection.create()
  File "D:\testProjects\lib\site-packages\psrp\_connection\wsman.py", line 543, in create
    resp = self._connection.post(self._shell.data_to_send())
  File "D:\testProjects\lib\site-packages\psrp\_io\wsman.py", line 1070, in post
    extensions=ext,
  File "D:\testProjects\lib\site-packages\httpx\_client.py", line 1143, in post
    extensions=extensions,
  File "D:\testProjects\lib\site-packages\httpx\_client.py", line 815, in request
    return self.send(request, auth=auth, follow_redirects=follow_redirects)
  File "D:\testProjects\lib\site-packages\httpx\_client.py", line 906, in send
    history=[],
  File "D:\testProjects\lib\site-packages\httpx\_client.py", line 933, in _send_handling_auth
    history=history,
  File "D:\testProjects\lib\site-packages\httpx\_client.py", line 967, in _send_handling_redirects
    response = self._send_single_request(request)
  File "D:\testProjects\lib\site-packages\httpx\_client.py", line 1003, in _send_single_request
    response = transport.handle_request(request)
  File "D:\testProjects\lib\site-packages\psrp\_io\wsman.py", line 665, in handle_request
    auth_resp = self._handle_sync_auth(request)
  File "D:\testProjects\lib\site-packages\psrp\_io\wsman.py", line 720, in _handle_sync_auth
    resp = self._connection.handle_request(req)
  File "D:\testProjects\lib\site-packages\httpcore\_sync\connection.py", line 57, in handle_request
    f"Attempted to send request to {request.url.origin} on connection to {self._origin}"
RuntimeError: Attempted to send request to http://test-mailbox02.shizhuang-inc.com:80 on connection to http://test-mailbox02.shizhuang-inc.com:5985_

I've tried to find out which step lead to this, and I think I've found it (but not sure..):

  1. The last error was raised by this image

  2. Then I found self.can_handle_request() returns a bool result: If the origin request url is equal to self._origin: image

  3. And I found that the port info in self._origin is not same to the woman info I created, and also not equal to http request url origin: image

I don't know why this happened, did I miss some arguments when I created the WsmanInfo?

That's all, looking forward to your reply.

End, thanks

bigdavidwong commented 2 years ago

Add some new found:

  1. This part create a HTTPConnection instance, and the url.port is got from http.URL instance, the port would be set to 5985 if the url port is None: image

  2. The http.URL instance copy the arguments from woman info, and in property method, it was set to None, then led to the finally error image

  3. If I add some code here, things work: image image

jborean93 commented 1 year ago

Thanks for the info and testing the pre-release version, I'll have to look into this and provide a fix unless you wish to push through a PR.

bigdavidwong commented 1 year ago

Thanks for the info and testing the pre-release version, I'll have to look into this and provide a fix unless you wish to push through a PR.

I have not had a deep understanding of this project, and I am not a professional programmer, so I should not have the ability to PR at present, I will wait patiently for you to improve this version, after all, version 0.8.1 is already very powerful for me!

But in the future, if I already have the ability to contribute to this project, I will definitely participate as a fan, haha~

jborean93 commented 1 year ago

Sorry for the delay in fixing this, the PR https://github.com/jborean93/pypsrp/pull/165 fixes this problem by correctly defining the port needed in this scenario. It also fixes a few other problems in relation to on prem Exchange and Exchange online.