Showing posts with label Microsoft Windows. Show all posts
Showing posts with label Microsoft Windows. Show all posts

Convert SSL certificate from PFX to PEM (OpenSSL)

Install OpenSSL
Open PowerShell and cd to OpenSSL install location - 

C:\Program Files\OpenSSL-Win64\bin

Extract private key with password (encrypted) - 

.\openssl pkcs12 -in certificate.pfx -nocerts -out privatekey.pem

OR

Extract private key without password (unencrypted) - 

.\openssl rsa -in privatekey.pem -out privatekey-nopass.pem

Extract certificate - 

.\openssl pkcs12 -in certificate.pfx -clcerts -nokeys -out certificate.pem

Combine Certificate and Private key (if required) - 

Get-Content privatekey_unencrypted.pem, certificate.pem | Set-Content fullcertificate.pem

Bulk nslookup script

USAGE:

.\Resolve-IPs.ps1 -InputPath .\ips.txt -OutputPath .\resolved.csv


SCRIPT - 

param(
    [Parameter(Mandatory=$true)]
    [string]$InputPath,
    [Parameter(Mandatory=$true)]
    [string]$OutputPath
)

# Ensure input exists
if (-not (Test-Path -Path $InputPath)) {
    Write-Error "Input file not found: $InputPath"
    exit 1
}

# Create output directory if needed
$dir = Split-Path -Path $OutputPath -Parent
if ($dir -and -not (Test-Path $dir)) {
    New-Item -ItemType Directory -Path $dir | Out-Null
}

# Read IPs (ignore empty lines and lines starting with #)
$ips = Get-Content -Path $InputPath | ForEach-Object { $_.Trim() } | Where-Object { $_ -and ($_ -notmatch '^\s*#') }

# Prepare results
$results = @()

foreach ($ip in $ips) {
    $hostname = $null
    $status = "OK"

    try {
        # Reverse lookup
        $entry = [System.Net.Dns]::GetHostEntry($ip)
        $hostname = $entry.HostName
    }
    catch {
        $hostname = ""
        $status = "NXDOMAIN/No PTR or lookup failed"
    }

    $results += [PSCustomObject]@{
        IP       = $ip
        Hostname = $hostname
        Status   = $status
    }
}

# Export CSV
$results | Export-Csv -NoTypeInformation -Path $OutputPath -Encoding UTF8

Write-Host "Done. Results written to $OutputPath"