Get SSL certificates expiration date using powershell on ubuntu machine - powershell

I'm trying to execute the following script on ubuntu machine using PS mode but it shows me nothing, I want to know how can I do have this date.
Below is the script that I have wrote:
$var = (Get-ChildItem /etc/ssl/certs)[0].NotAfter
Write-Host $var

PowerShell doesn't magically parse certificate files for you. Use the openssl command.
$cert = (Get-ChildItem /etc/ssl/certs)[0].FullName
$var = ((& openssl x509 -in $cert -dates -noout) -match 'notAfter').Split('=')[1]

Related

Update multiple Certificate friendly names using PowerShell

I am fairly new to PowerShell and I am currently updating a large list of Certificate Friendly names remotely using PowerShell.
I have done the below script which works fine if there is one certificate but it fails if there is multiple certificates in the store as I need to add a Loop into the script. When I am trying to add a loop in it doesn't seem to be working. Could someone help or point me in the right direction please?
Enter-PSSession –ComputerName Servername
Get-ChildItem -Path Cert:\LocalMachine\My
$CertStore = "Cert:\LocalMachine\My\"
$FriendlyName = 'Examplename'
$cert = Get-ChildItem $CertStore
$cert.FriendlyName = $FriendlyName
Thanks for any help.
Just add a Foreach Loop into the script.
Something like below:
$CertStore = "Cert:\LocalMachine\My\"
$FriendlyName = 'Examplename'
$cert = Get-ChildItem $CertStore | foreach {$_.FriendlyName = $FriendlyName}
And this will update multiple Certificates with friendly names.

How do I delete a user's certificate while remoting to his laptop as admin?

While remoting in using a PS Session and as a Remote Admin, I need to delete a certificate from the store: CurrentUser\My.
I've noticed that while there are similar questions already, they do NOT answer my question. None of them address the problem of inserting local user as the context for the command.
I have tried recursing through the directories of ...\Roaming\Microsoft\Crypto\RSA, changing the cert store to LocalMachine and inserting the username manually. Nothing works.
THIS WORKS
Get-ChildItem cert:"CurrentUser\My" | where {($_.Subject -like "*OU=CO*")} | remove-item -WhatIf
THIS DOES NOT (with remote session active)
PS C:\Users\Remote Admin: Get-ChildItem cert:"CurrentUser\My" | where {($_.Subject -like "*OU=CO*")} | remove-item -WhatIf
Certificates are stored in the Registry or in LocalAppData.
If you have administrative rights you can access the registry remotely via .Net for that user or the file system and remove the certificates as needed. Here's a test I ran.
First I created a dummy cert on a remote machine. This adds the cert to CurrentUser\My and CurrentUser\CA
New-SelfSignedCertificate -DnsName "www.fabrikam.com" -CertStoreLocation "Cert:\CurrentUser\My"
Thumbprint Subject
---------- -------
E5A33C1BB6FBA8A6DA397C6BFE2CE489F751AF10 CN=www.fabrikam.com
Next I remotely access and remove the cert by thumbprint from Registry CA store.
$computer = 'ComputerA'
$targetSID = 'S-1-5-21-1234567890-1234567890-1234567890-12345' #get-aduser username
$reg = [Microsoft.win32.registryKey]::OpenRemoteBaseKey('Users', $computer) #open remote registry
$avaliableSID = #($($reg.GetSubKeyNames() | Where-Object { $_ -match 'S-\d-\d+-(\d+-){1,14}\d+$' })) #get all users SIDS
if($avaliableSID -contains $targetSID ) #if it contains the one we want
{
$otherUserStore = $reg.OpenSubKey("$targetSID\software\microsoft\systemcertificates\ca\certificates", $true) #open profile for writing
$otherUserStore.DeleteSubKey('E5A33C1BB6FBA8A6DA397C6BFE2CE489F751AF10', $false) #delete key and suppress error if missing
}
I then remove it from the My store in %localappdata%
Remove-Item "\\$computer\c$\users\SOME.USER\appdata\roaming\microsoft\systemcertificates\my\certificates\$thumbprint" -Force #force required or you get access denied
Checking cert:\currentuser\my as that user shows no sign of the certificate. Of course you should add error checking, etc. but this example should help.

How do I pull the thumbprint out of a SSL certificate FILE (not the windows cert store)?

I understand how to get the thumbprint of a certificate that's installed to a certificate store, however I'm hoping there is a way to get that information from a certificate FILE.
So for example I'd have c:\temp\mycert.com.cer... how would I get the thumbprint from that file? Is it even possible? Google isn't being very helpful. I've been doing this in powershell as such to get this from the certificate store, but again - I need to get this info from a certificate FILE.
$certCN = mysite.com
$cert = Get-ChildItem cert:\LocalMachine\My -Recurse |
where {$_.subject -like "*CN=$certCN*"} |
where {$_.ExpiringInDays -lt "91"}
$thumbprint = $cert.thumbprint
Thank you in advance!!
Without using a third party library you can rely on x509certificate2 cryptography class of .NET framework:
$content = [System.IO.File]::ReadAllBytes("D:\mycertificate.cer")
$cer = [system.security.cryptography.x509certificates.x509certificate2]::new($content)
$cer.SignatureAlgorithm.FriendlyName
$cer.Thumbprint
$cer.Subject
Then you will receive a result like:
sha1RSA
5A6008B61ABADE6412BEE4704C2407D5DE5DAA34
C=GB, O=University College London, OU=Computer Science, CN=FTAM Service

Import certificate to TrustedPublisher from driver for silent driver installation

I would like to install the Balloon driver for running my Windows in KVM without user any interaction (silent installation).
I'm using powershell to extract the certificate form the driver to some temporary file and then import it to TrustedPublisher using certutil.exe:
$cert = (Get-AuthenticodeSignature "D:\Balloon\2k12R2\amd64\blnsvr.exe").SignerCertificate; [System.IO.File]::WriteAllBytes("c:\redhat.cer", $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert));
certutil.exe -f -addstore "TrustedPublisher" c:\redhat.cer
Then I can install the driver without bothering the user by confirmation:
pnputil -i -a "d:\Balloon\2k12R2\amd64\*.inf"
How can I improve this task to do it all in powershell - without extracting the certificate to temporary file and using certutil.exe to import it?
You can store cert data in variable, and add it directly to desired store. For example, using your path/target:
$Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$Cert.Import((((Get-AuthenticodeSignature "D:\Balloon\2k12R2\amd64\blnsvr.exe").SignerCertificate).Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert)))
$store = Get-Item "cert:\LocalMachine\TrustedPublisher"
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]"ReadWrite")
$store.Add($Cert)
$store.Close()
I take the signature from the .cat file from all virtio drivers and import it directly to the store:
$DriverPath = Get-Item "D:\tmp\virtio-win-0.1.173\*\2k12r2\amd64"
$CertStore = Get-Item "cert:\LocalMachine\TrustedPublisher"
$CertStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
Get-ChildItem -Recurse -Path $DriverPath -Filter "*.cat" | % {
$Cert = (Get-AuthenticodeSignature $_.FullName).SignerCertificate
Write-Host ( "Added {0}, {1} from {2}" -f $Cert.Thumbprint,$Cert.Subject,$_.FullName )
$CertStore.Add($Cert)
}
$CertStore.Close()

How do I show the certificate thumbnail for a newly created certificate using certreq in powershell?

I am using the following command in PowerShell which works fine and good:
certreq -accept -machine "c:\my_csr_response.crt"
This command processes a CA response to a SSL CSR.
My question is how do I get the thumbnail of the newly created certificate generated by this command?
Create an X509Certificate2 object from the file and grab the thumbprint from there.
$CertPath = "C:\my_csr_response.crt"
$Cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::CreateFromCertFile($CertPath)
$Thumbprint = $Cert.GetCertHashString()
Find it in the cert store with:
Get-ChildItem cert:\ -Recurse |Where-Object {$_.Thumbprint -eq $Thumbprint}