-
Notifications
You must be signed in to change notification settings - Fork 265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix for session clixml encoding on Python 3 #222
Conversation
+1 Just ran into this as well. Excited to see it merged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually fails when len(nodes) == 0. It attempts to encode the original message but fails because it is still bytes. This was actually circumvented previously by _strip_namespace failing. Since pre python 3.6 can't do fancy type checking the best fix would be to do this and let the exception hit then warn (seems best for backwards compatibility and to prevent type incompatibility from hiding).
assert len(nodes) != 0, "nodes length 0 falling back to original error message"
change:
return msg.encode('utf-8')
back to
return msg
I think I ran into the same encoding issue;
|
I ran into this issue myself, and I was wondering why it hasn't been integrated yet. Is it because the build is now failing for Python2? I'm not very sure about how to interpret the test output, so I may be wrong there. The try-except in _strip_namespace should be left in, correct? I'm interested in understanding why it was removed originally. Thanks for your time. |
+1 |
1 similar comment
👍 |
@lebonez thanks for the heads up, just updated the commit to include cases where stderr does not contain clixml. |
Anyone know when this will be merged? |
It's still not working for on python 3.6.6 (ubuntu 18.04) Getting this error message
Any ideas ? |
winrm/__init__.py
Outdated
@@ -86,18 +86,17 @@ def _clean_error_msg(self, msg): | |||
if len(new_msg): | |||
# remove leading and trailing whitespace while we are here | |||
msg = new_msg.strip() | |||
return msg | |||
return msg.encode('utf-8') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be return msg.decode('utf-8')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it expects msg
to be set to new_msg
above this but in some cases that's not what happens. Needs a bit more logic and thanks for the pickup.
The reason this is failing is when new_msg is an empty string it goes back to attempting to encode a bytes object as stated in the comment you can't decode a bytes object. But the comment will fail as well when msg is a string because it doesn't have a decode attribute. I would suggest the following be committed
I forgot about this after I overwrote the classes method to just return error msg. It might also be smart to add an except block for the else that way it catches an attribute error to just return the msg because it probably wasn't a bytes object as below. Just remove the else and do the following which is better in my opinion.
|
@aduzsardi @lebonez the latest ocmmit should fix up the latest issue |
Hi, thx for the update #!/usr/bin/env python3
import winrm
from getpass import getpass
username = input("Enter your AD username: ")
password = getpass("Enter your AD password: ")
ad_domain = "mydomain.lan"
upn = username + '@' + ad_domain
command = 'Get-ADUser ' + username
s = winrm.Session('admember.mydomain.lan',auth=(username,password),transport='ntlm')
pwsh = s.run_ps(command)
print(pwsh.std_out.decode('utf-8')) Am i doing something wrong ? |
I printed the
|
@aduzsardi you can but you either need to use CredSSP, Kerberos with delegation, or explicitly define the credential in your powershell script. |
yes that's true... but back to the code , thank you for the patches 👍 |
@aduzsardi Microsoft have documented numerous ways that you can overcome the double hop issue https://docs.microsoft.com/en-us/powershell/scripting/setup/ps-remoting-second-hop?view=powershell-6. Ultimately the biggest problem is that people expect network authentication like WinRM to act the same way as authentication through a local console or on RDP. Unless you use CredSSP or Kerberos with credential delegation, the new network login that is created does not have access to the user's credentials or a special token that can be used to authenticate with any further servers. Whereas a local or RDP login (uses CredSSP), means the login has access to the user's credentials which can be used to authenticate with further servers. With pywinrm you have the ability to do the following to get credential delegation working;
There's another option but unfortunately you cannot use pywinrm for this. This option is to create a custom PSSession Configuration endpoint that runs the script as a specific user. This can be combined with JEA to limit what cmdlets scripts can call for added security. If you are interested in this option, have a look at https://github.com/jborean93/pypsrp which is a Python library I've worked on that runs on the PowerShell Remoting layer rather than just WinRM. |
Hey @badcure |
Pinging @nitzmahone here. I remember him mentioning that he wanted to do some testing due to recent changes. With that said, let me know if you are comfortable with me releasing a new version. |
The output from WinRM is a byte string and on Python 3 the Session class tries to clear up the clixml that is produced on the stderr. Currently it expects a text/unicode string which is fine on Python 2 but doesn't work on 3. This PR fixes that issue and ensure the stderr is cleaned and outputted properly as per before.
Supersedes: