Solved

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

Posted on 2004-04-20
14
9,550 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:mobana
  • 4
  • 4
  • 2
  • +1
14 Comments
 
LVL 10

Expert Comment

by:eternal_21
ID: 10874854
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?
0
 

Author Comment

by:mobana
ID: 10874892
i am trying download a web page, but want to set my own IP Header on Packet sending.  
0
 
LVL 2

Expert Comment

by:sholodak
ID: 10875540
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
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 2

Expert Comment

by:sholodak
ID: 10875558
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)
0
 

Author Comment

by:mobana
ID: 10875656
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...
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10876053
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 154 total points
ID: 10876054
If you mean changing the browser headers, such as User-Agent, here is an example:

    static void Main(string[] args)
    {
      System.Net.WebClient wc;
      wc = new System.Net.WebClient();
      wc.Headers["User-Agent"] = ""; //TODO: Change User-Agent string e.g.: "Mozilla/4.0"

      System.IO.StreamReader webReader;
      webReader = new System.IO.StreamReader(wc.OpenRead("http://pgl.yoyo.org/http/browser-headers.php"));

      string webPage = webReader.ReadToEnd();

      Console.WriteLine(webPage);
      Console.ReadLine();
    }
0
 

Author Comment

by:mobana
ID: 10876134
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
0
 
LVL 2

Expert Comment

by:sholodak
ID: 10883372
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
0
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 153 total points
ID: 10885810
0
 

Author Comment

by:mobana
ID: 10898172
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.  
0
 
LVL 2

Assisted Solution

by:sholodak
sholodak earned 153 total points
ID: 10902322
First thing I've noticed is that your code doesn't mention anything about TCP.  You won't be able to successfully connect to a web server because it relies on TCP/IP.  It look as though you're just sending an IP header and an HTTP header, in which case the packet would never even make it to the application level of the web server.  

Also, make sure you are testing with your real address.  You might get an error (I believe at the TCP level) if you are sending from a spoofed address because TCP will require acknowledgements which will never arrive.  If I were you, I would set up a web server in your private network and put ettercap on it to monitor the activity.  It will let you see exactly what it is receiving and will probably indicate whether your packets are properly formed.  If your solution is working, it should show you the details of the HTTP request.  If it doesn't, your packet probably has a bad/missing header.

I think the TCP acknowledgement/sliding window problems will arise when spoofing your address unless you set up another machine to send back valid ack packets to your machine (spoofing the origin to be your actual destination).  Or I suppose you could ignore the errors on your end once your sure the packets are getting across.

I would monitor what your program is sending remotely (on a test server) & see what it reveals.  Since you are using TCP to get at a web site, and TCP creates the illusion of a connection-oriented, reliable protocol, you should expect some difficulties keeping the TCP handler on your end happy, unless of course you are just creating the TCP packets yourself and there is nothing to handle them.

Scott
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
System.Security 2 27
SqlDataBase 7 46
Reference issues in Visual Studio 3 29
updating order of the rows, N+1 query issue, C#, asp.net, MVC, SQL Server 3 25
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now