Setting up a Powershell Listener using 443 with certificate. appID

Viewed 32

A while back I made a powershell listener for 80/443 with a certificate. Might need a sort if cert matches more than one.

This example serves google search from a front site. If you don't have a cert match up I think it will error but port 80 will work.

appId was the hardest part to get netsh sslcert part working with the shell the code is ran in. A job, function or process.

My question: is there a better Encoding to use than utf8 for bringing the raw html from invoke-webrequest? I have used [System.Text.Encoding]::utf8 before. I don't get an exact duplicate of the back-site and think it's all due to no css, but wonder if anyone has an idea.

$appid = "appid={"+(get-host).InstanceId.guid+"}"
$certhash = ls Cert:\LocalMachine\my | where {$_.EnhancedKeyUsageList -Match 'Server' -and $_.subject -match (hostname)}|select -expand Thumbprint -last 1
$cmdline='netsh http add sslcert ipport=0.0.0.0:443 certhash=' + $certhash + ' "' + $appid + '"'

netsh http delete sslcert ipport=0.0.0.0:443 
Invoke-Expression $cmdline

[Net.ServicePointManager]::SecurityProtocol =  ([Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::ssl3, [Net.SecurityProtocolType]::Tls12)
$listener.dispose()
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add(("http://+:80/"))
$Listener.Prefixes.Add("https://+:443/")
$listener.Start()

$FQDN = [System.Net.Dns]::GetHostByName((hostname)).HostName

while($Listener.IsListening){
$Context = $listener.GetContext()
$request = $context.request
$response = $context.response
$webRequest = Invoke-WebRequest -Uri ('https://www.google.com/search?q='+$context.Request.RawUrl.Substring(1))

$buffer = [System.Text.Encoding]::utf8.getbytes( ($webRequest|select -expand Content) )
$response.contentlength64 = $buffer.length
$response.OutputStream.Write($buffer,0,$buffer.length)
}

Thank you

0 Answers
Related