This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Hso = New-Object Net.HttpListener | |
$Hso.Prefixes.Add("http://+:8000/") | |
$Hso.Start() | |
While ($Hso.IsListening) { | |
$HC = $Hso.GetContext() | |
$HRes = $HC.Response | |
$HRes.Headers.Add("Content-Type","text/plain") | |
$Buf = [Text.Encoding]::UTF8.GetBytes((GC (Join-Path $Pwd ($HC.Request).RawUrl))) | |
$HRes.ContentLength64 = $Buf.Length | |
$HRes.OutputStream.Write($Buf,0,$Buf.Length) | |
$HRes.Close() | |
} | |
$Hso.Stop() |
So that wasn't painful, but not too helpful. How did we get here? Lets pipe the created object to Get-Member and explore the relevant properties and methods.
Now that we can put together a basic usage of the httplistener object, lets explore some of the properties of the request. Specifically, we need to know what part of the request we can use to find the name of the file. We can use the debugger that comes with the ISE to catch our loop and use the console to enumerate each of the properties until we find the one we want:
Now that we have everything put together, we just need roll it into a one-liner and encode it:
Finally, we have our encoded one-liner which will statically serve files from the present working directory.
We will need to have admin rights to grab a port, but otherwise it is pretty handy.
Thanks for reading about the basics of the httplistener class, maybe you will find it useful.
-Chris
No comments:
Post a Comment