<p>J’ai un problème avec l’authentification par certificat WinRM sur une image Windows Server 2022 avec CIS STIGs.</p>
<p>Le serveur exécute le service WinRM, et je me connecte depuis un client Ubuntu Pro 20.04 FIPS en utilisant pywinrm (v0.5.0). Les deux certificats sont auto-signés (ACM PCA) et le certificat CA est ajouté aux magasins de confiance à la fois sur Windows et Ubuntu.</p>
<h2><a name="p-34275-commandes-et-configuration-1" class="anchor" href="#p-34275-commandes-et-configuration-1" aria-label="Heading link"></a>Commandes et configuration :</h2>
<p><strong>Sur Windows Server 2022 :</strong><br>
> Get-ChildItem -Path Cert:\CurrentUser\Root<br>
| Where-Object { $_.Thumbprint -eq “5A2F4E63BEBEDB186ED84BF722B54207E6664469” }<br>
| Select-Object Subject, Thumbprint</p>
<p>Subject Thumbprint</p>
<hr>
<p>L=TA, <a href="http://CN=example.com" rel="noopener nofollow ugc">CN=example.com</a>, S=TA, OU=RnD, O=Company, C=IL 5A2F4E63BEBEDB186ED84BF722B54207E6664469</p>
<blockquote>
<p>winrm enumerate winrm/config/service/certmapping<br>
CertMapping<br>
URI = *<br>
Subject = 50612F90702F2FEF1B777E987B7CD974DC99CE51<br>
Issuer = 5A2F4E63BEBEDB186ED84BF722B54207E6664469<br>
UserName = Administrator<br>
Enabled = true<br>
Password</p>
</blockquote>
<blockquote>
<p>winrm g winrm/config<br>
Config<br>
MaxEnvelopeSizekb = 500<br>
MaxTimeoutms = 1800000<br>
MaxBatchItems = 32000<br>
MaxProviderRequests = 4294967295<br>
Client<br>
NetworkDelayms = 5000<br>
URLPrefix = wsman<br>
AllowUnencrypted = false [Source=“GPO”]<br>
Auth<br>
Basic = false [Source=“GPO”]<br>
Digest = false [Source=“GPO”]<br>
Kerberos = true<br>
Negotiate = true<br>
Certificate = true<br>
CredSSP = false<br>
DefaultPorts<br>
HTTP = 5985<br>
HTTPS = 5986<br>
TrustedHosts<br>
Service<br>
RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)(A;;GR;;;IU)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)<br>
MaxConcurrentOperations = 4294967295<br>
MaxConcurrentOperationsPerUser = 1500<br>
EnumerationTimeoutms = 240000<br>
MaxConnections = 300<br>
MaxPacketRetrievalTimeSeconds = 120<br>
AllowUnencrypted = false [Source=“GPO”]<br>
Auth<br>
Basic = false [Source=“GPO”]<br>
Kerberos = true<br>
Negotiate = true<br>
Certificate = true<br>
CredSSP = true<br>
CbtHardeningLevel = Relaxed<br>
DefaultPorts<br>
HTTP = 5985<br>
HTTPS = 5986<br>
IPv4Filter = *<br>
IPv6Filter = *<br>
EnableCompatibilityHttpListener = false<br>
EnableCompatibilityHttpsListener = true<br>
CertificateThumbprint<br>
AllowRemoteAccess = true<br>
Winrs<br>
AllowRemoteShellAccess = true<br>
IdleTimeout = 7200000<br>
MaxConcurrentUsers = 2147483647<br>
MaxShellRunTime = 2147483647<br>
MaxProcessesPerShell = 2147483647<br>
MaxMemoryPerShellMB = 1024<br>
MaxShellsPerUser = 2147483647</p>
</blockquote>
<blockquote>
<p>winrm g winrm/config/service<br>
Service<br>
RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)(A;;GR;;;IU)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)<br>
MaxConcurrentOperations = 4294967295<br>
MaxConcurrentOperationsPerUser = 1500<br>
EnumerationTimeoutms = 240000<br>
MaxConnections = 300<br>
MaxPacketRetrievalTimeSeconds = 120<br>
AllowUnencrypted = false [Source=“GPO”]<br>
Auth<br>
Basic = false [Source=“GPO”]<br>
Kerberos = true<br>
Negotiate = true<br>
Certificate = true<br>
CredSSP = true<br>
CbtHardeningLevel = Relaxed<br>
DefaultPorts<br>
HTTP = 5985<br>
HTTPS = 5986<br>
IPv4Filter = *<br>
IPv6Filter = *<br>
EnableCompatibilityHttpListener = false<br>
EnableCompatibilityHttpsListener = true<br>
CertificateThumbprint = 13F3C1844B7617270D1331BEB02AD347FAB74D9C<br>
AllowRemoteAccess = true</p>
</blockquote>
<pre><code class="lang-auto">
**Sur Ubuntu Pro 20.04** :$ cert_path="/etc/nginx/certs/controller.example.com.crt.pem"
$ thumbprint=$(openssl x509 -in "$cert_path" -noout -fingerprint -sha1 | sed 's/://g' | awk -F= '{print $2}')
$ echo $thumbprint
50612F90702F2FEF1B777E987B7CD974DC99CE51
$ issuer_thumbprint=$(tac /etc/ssl/certs/ca-certificates.crt | awk 'BEGIN {c=0} /END CERTIFICATE/ {c++} {if (c==1) print}' | tac | openssl x509 -in /dev/stdin -noout -fingerprint -sha1 | sed 's/://g' | awk -F= '{print $2}')
$ echo $issuer_thumbprint
5A2F4E63BEBEDB186ED84BF722B54207E6664469
</code></pre>
<h2><a name="p-34275-test-de-connexion-2" class="anchor" href="#p-34275-test-de-connexion-2" aria-label="Heading link"></a>Test de connexion :</h2>
<p>Je teste la connexion en utilisant pywinrm avec HTTPS et l’authentification par certificat. Voici la commande et le message d’erreur Invalid Credentials :</p>
<p><code>$ python -c "import winrm; winrm.Session('https://winrm.example.com:5986/wsman', auth=(None, None), transport='certificate', cert_key_pem='/etc/nginx/certs/controller.example.com.key.pem', cert_pem='/etc/nginx/certs/controller.example.com.crt.pem', server_cert_validation='validate', ca_trust_path='/etc/ssl/certs/ca-certificates.crt').run_cmd('ipconfig', ['/all']).std_out.decode()"</code><br>
`Traceback (most recent call last):<br>
File “/opt/venv3.11/lib/python3.11/site-packages/winrm/transport.py”, line 342, in send_message_request<br>
response.raise_for_status()<br>
File “/opt/venv3.11/lib/python3.11/site-packages/requests/models.py”, line 1024, in raise_for_status<br>
raise HTTPError(http_error_msg, response=self)<br>
requests.exceptions.HTTPError: 401 Client Error: for url:<br>
<a href="https://winrm.example.com:5986/wsman" class="onebox" target="blank" rel="noopener nofollow ugc">https://winrm.example.com:5986/wsman</a></p>
<p>During handling of the above exception, an invalid credentials exception occurred:</p>
<p>Traceback (most recent call last):<br>
File “”, line 1, in <br>
File “/opt/venv3.11/lib/python3.11/site-packages/winrm/<strong>init</strong>.py”, line 44, in run_cmd<br>
shell_id = self.protocol.open_shell()<br>
File “/opt/venv3.11/lib/python3.11/site-packages/winrm/protocol.py”, line 193, in open_shell<br>
res = self.send_message(xmltodict.unparse(req))<br>
File “/opt/venv3.11/lib/python3.11/site-packages/winrm/protocol.py”, line 263, in send_message<br>
resp = self.transport.send_message(message)<br>
File “/opt/venv3.11/lib/python3.11/site-packages/winrm/transport.py”, line 336, in send_message<br>
response = self._send_message_request(session, prepared_request)<br>
File “/opt/venv3.11/lib/python3.11/site-packages/winrm/transport.py”, line 346, in _send_message_request<br>
raise InvalidCredentialsError(“the specified credentials were rejected by the server”)<br>
winrm.exceptions.InvalidCredentialsError:</p>
<p>the specified credentials were rejected by the server</p>
<pre><code class="lang-auto">
Journaux du service WinRM :
Sur le service WinRM, il y a un message d'erreur « accès refusé » erreur 5 :
</code></pre>
<blockquote>
<p>Get-WinEvent -LogName “Microsoft-Windows-WinRM/Operational” -MaxEvents 1 | Format-List *</p>
</blockquote>
<p>Message : The authorization of the user failed with error 5<br>
Id : 192<br>
RecordId : 5435<br>
ProviderName : Microsoft-Windows-WinRM<br>
LogName : Microsoft-Windows-WinRM/Operational<br>
ProcessId : 3320<br>
ThreadId : 3744<br>
MachineName : winrm<br>
UserId : S-1-5-20<br>
TimeCreated : 4/8/2025 8:44:41 PM</p>
<pre><code class="lang-auto">
Pourquoi la connexion avec l'authentification par certificat échoue-t-elle ? La poignée de main TLS semble correcte. J'ai également confirmé avec openssl s_client... obtenant verification OK.
Des suggestions ou idées s'il vous plaît !
Merci !!
Source : Server Fault</code></pre>