Link to home
Start Free TrialLog in
Avatar of mobana
mobana

asked on

""""Open HTTP Page With Raw Socket (C#)""""

I am new in Networking Programming.  Recently I am trying to make an application that will open an HTTP Page using Raw Socket.  I guess should prepare code for IP Header, UDP header, etc?? I have been searching the net but can't find any good example for it.  Anyone could give some suggestion? Thank you in advance.
Avatar of eternal_21
eternal_21

What are you trying to do, download a web page as a byte[] array, or to a string, or are you specifically trying to open and control a socket connected to port 80 of the web server?
Avatar of mobana

ASKER

i am trying download a web page, but want to set my own IP Header on Packet sending.  
If you're new to network programming, you probably don't want to worry about IP headers.  I think only Windows XP would theoretically allow you to alter IP headers, but even then, it would be through native API calls.  As far as I know, they didn't include these capabilities as part of the .NET Framework.  If that's really what you're trying to do, you'd have to declare an external native function.

If you want to download a web page, that is another issue...  Here are two pieces of code I used to get this working in C#:

  System.Net.WebClient wc = new System.Net.WebClient();

  // This downloads the web page to a local file (in /bin/debug or /bin/release if your in VS.NET)
  wc.DownloadFile(TARGET_URL, LOCAL_FILENAME);

  // This would download that same URL to a byte array and then convert it to a UTF8 (unicode) string.  
  byte[] data = wc.DownloadData(TARGET_URL);
  string s = System.Text.Encoding.UTF8.GetString(data);

I can also help you accomplish the same thing with Sockets via the .NET framework, but it requires understanding of the HTTP protocol and won't let you tamper with IP header information.  See this page for info: http://www.faqs.org/rfcs/rfc2616.html

Scott

Scott

Scott
One other thing... HTTP is a connection-oriented protocol.  It relies on TCP/IP, not UDP.  No need to worry about UDP for this problem.  Also, have a look at the Internet Protocol.  I really doubt you want to do anything to the IP headers unless you're trying to spoof your address or something like that.  http://www.tekelec.com/ss7/protocols/ip2.asp

(sorry for the 3 signatures above)
Avatar of mobana

ASKER

Thanks Sholodak.  some web browser now equipped to be able alter with User Agent, etc...things that heard taboo in early era of internet terms.  in future web browser might be equipped to be able change ip setting too,  i guess...  I am using WinXp and I heard it is possible to change IP Header of it.   it would be grateful if anyone could share some of his knowledge here...
ASKER CERTIFIED SOLUTION
Avatar of eternal_21
eternal_21

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mobana

ASKER

Thanks for the supporting post.  there is some examples about capturing raw socket, but can't one for sending raw socket.  the principle might be the same, but it was written in C language that unfamiliar for me...T.T
I tried to throw something together to try and get the WebClient effect with raw sockets.  For some reason the response body isn't showing (should be one blank line after the response headers).  I am getting 200 responses from remote servers, which tells me that the server is processing the request successfully.

This does NOT attempt to use Raw sockets, though in throwing this together, it does seem possible to do.  Whether support is available for Windows XP via .NET is still as of yet unknown to me.

// Prepare a TCP/IP Connection to www.yahoo.com port 80 (standard web server port)
// NOTE: If you want to use raw sockets, you will change System.Net.Sockets.SocketType.Stream to System.Net.Sockets.SocketType.Raw
//   this will require you to send your own valid IP header.  Search google for specifics on the IP protocol.
System.Net.Sockets.Socket s = new Socket(
  System.Net.Sockets.AddressFamily.InterNetwork,
  System.Net.Sockets.SocketType.Stream,
  System.Net.Sockets.ProtocolType.Tcp);
System.Net.IPEndPoint iep = new System.Net.IPEndPoint(
  System.Net.IPAddress.Parse(
    System.Net.Dns.Resolve("www.yahoo.com").AddressList[0].ToString()
  ),
  80);
s.Connect(iep);
                  
// Prepare a byte[] containing the request
// If you're trying to emulate a particular browser, you can either get a tool to view the headers or try Etherial/EtterCap
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("GET / HTTP/1.0\r\n");
sb.Append("Connection: close\r\n");
sb.Append("Host: www.yahoo.com\r\n");
sb.Append("User-Agent: Mozilla/4.0\r\n");
sb.Append("Accept: */*\r\n");
sb.Append("\r\n");
byte[] send = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
     
// Send the request to the web server
s.Send(send);

// Read back the byte[] from the web server and buffer it's string equivalent into a string builder
System.Text.StringBuilder resp = new System.Text.StringBuilder();
int ct = 0;
byte[] receive = new byte[4096];
while ((ct = s.Receive(receive)) > 0) {
  resp.Append(System.Text.Encoding.UTF8.GetString(receive));
  receive = new byte[4096];
}

// Show the response in a message box.
MessageBox.Show(resp.ToString());

You'll notice the WebClient solution was much more approachable, but less flexible.  I believe there is also an HttpRequest class in System.Net that you could probably use to assemble the request in a more OO-friendly way.  I think you could use that to transmit the request via sockets as I did manually above.

The second one seems like it might help, but you should probably focus on getting the fundamentals of network programming down before you try and dive into that article.  For starters, you should be aware that the HTTP is a top-level protocol.  It relies on TCP on top of IP.  If you are going to use raw sockets, I think you'll have to take care of creating both an IP header and a TCP header.  

If you are trying to spoof your IP address, you shouldn't have to change anything in the TCP headers or the HTTP headers.  If I'm not mistaken, you'll be sending out packets with another return address.  In other words, you can never expect to see a response from the HTTP server unless you use your actual IP address.  I would start off just trying to get it to work with your real IP before screwing around with it.  You should also be aware that there are some legal implications to hiding, and even trying to hide, the origin of network traffic in many states/countries.

Some additional resources:
HTTP/1.1 Request specification: http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
Info on Raw Sockets in C# (IP related): http://www.c-sharpcorner.com/Network/SimpleSnifferInCSLM.asp
Another example using raw (you don't want ICMP/IGMP) Sockets: http://www.codeproject.com/csharp/trace.asp

Lots of luck

Scott
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mobana

ASKER

Thousands of thanks to everyone here.  I'm still keep struggle these few days to find the answer.  Actually there is no problem when i sent a packet with ICMP protocol.  I guess the principle of sending IP Header should be the same too?  So i try this code :

Socket s= new Socket(AddressFamily.InterNetwork,
                     SocketType.Raw, ProtocolType.IP);
s.SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP,
                           System.Net.Sockets.SocketOptionName.HeaderIncluded, 1);
s.Connect(destination_ip);
s.Send(iphead_httphead, iphead_httphead.Length, 0);
int Result = s.Receive(ByteRecv, ByteRecv.Length, 0);

It display socket error that didn't get proper respon from connected host socket.  It succeed to connect with the remote host, but can't send the proper request packet?? I feel it just one step left to the right answer.  any suggestion for it?? thank you.  
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial