Showing posts with label HTTP. Show all posts
Showing posts with label HTTP. Show all posts

Thursday, September 6, 2007

.NET Remoting articles


Three articles on remoting published on The Code Project website.
As Jeffrey Snover stated on his last interview with SearchWinIT.com  ('remoting' will be built into the heart of the PowerShell engine).
At the meantime here are some others techniques in .NET.



Just .NET Remoting

A simple .NET Remoting example in three parts: the interface, the server, and the client. The client asks for your name, then sends it to the server. The server returns a personal greeting.




Remote Administration Tool (RAT) in C# - Part 3 (Reverse Connection Shell)
How to access server behind a gateway firewall using reverse connection shell technique



RemoteConsole
Remote console that allows to send commands to remote computer located in the Internet.


All articles includes source file in C#

Friday, August 31, 2007

Query HTTP status codes and headers with PowerShell


I wrote this after a posting it in the PowerShell newsgroup. Three methods to the task:


1. Xml Http (com object)

$url = "http://www.cnn.com"
$xHTTP = new-object -com msxml2.xmlhttp;
$xHTTP.open("GET",$url,$false);
$xHTTP.send();
$xHTTP.ResponseText; # returns the html doc like downloadstring
$xHTTP.status # returns the status code

 

PS C:\Scripts> $xHTTP.getAllResponseHeaders()
Date: Fri, 31 Aug 2007 19:05:39 GMT
Server: Apache
Accept-Ranges: bytes
Cache-Control: max-age=60, private
Expires: Fri, 31 Aug 2007 19:06:31 GMT
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 30296
Content-Type: text/html
Keep-Alive: timeout=5, max=64
Connection: Keep-Alive

PS C:\Scripts> $xHTTP.getResponseHeader("Content-Length")
30296
PS C:\Scripts> $xHTTP.status
200
PS C:\Scripts> $xHTTP.statusText
OK

# $xHTTP.responseText will return the document text.

 

 

2. System.Net.HttpWebRequest

$url = "http://www.cnn.com"
$req=[system.Net.HttpWebRequest]::Create($url);
$res = $req.getresponse();
$stat = $res.statuscode;
$res.Close();

 

PS C:\Scripts> $res

IsMutuallyAuthenticated : False
Cookies : {}
Headers : {Vary, X-Pad, Keep-Alive, Connection...}
ContentLength : 135531
ContentEncoding :
ContentType : text/html
CharacterSet : ISO-8859-1
Server : Apache
LastModified : 8/31/2007 7:01:46 PM
StatusCode : OK
StatusDescription : OK
ProtocolVersion : 1.1
ResponseUri : http://www.cnn.com/
Method : GET
IsFromCache : False
PS C:\Scripts> $res.Headers
Vary
X-Pad
Keep-Alive
Connection
Accept-Ranges
Content-Length
Cache-Control
Content-Type
Date
Expires
Server

PS C:\Scripts> $res.GetResponseHeader("Content-Length")
135531

-or-  $res.<HeaderString>

$res.ContentLength
135531

PS C:\Scripts> $res.StatusCode
OK
PS C:\Scripts> $res.StatusDescription
OK

 

3. System.Net.WebClient

$url="http://www.cnn.com"
$wc = new-object net.webclient
$html = $wc.DownloadString($url)

 

There's is still a lot to investigate on this classes and its methods and this post can be only the tip of the iceberg. Personally, I prefer to use the System.Net.WebClient DownloadString method. It's quick and short to type. I don't want to rely on any methods to query responses based on status codes.
In my scripts I choose to get the document text and query it for a matching string.
I do it because if the target web server redirects you (which happens in most cases) to another page using the Server.Transfer method (e.g., 404 - page not found) than you
wont get 404, you'll get back "OK" or 200.

 

Here is another example. Frequently, I need to check if one of my web server on the Internet is listed in CBL's (Composite Blocking List) database as a spam source. This site is one of the most important spam databases. If you're server is listed there it is more likely that you are listed on numerous spam databases on the web. Most of them will simply query CBL.

 

$ip = "<WebServerIP>";
$url = "http://cbl.abuseat.org/lookup.cgi?ip=$ip";
$lookup = "^IP Address $ip was not found.*";
$wc = (new-object net.webclient).DownloadString($url);

if($wc -match $lookup){
    "Listed";
} else{
    "Not Listed";
}

 

Enjoy,

Shay