Solved

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

Posted on 2004-04-20
14
9,502 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:sholodak
Comment Utility
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
Comment Utility
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
Comment Utility
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 10

Accepted Solution

by:
eternal_21 earned 154 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:mobana
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

14 Experts available now in Live!

Get 1:1 Help Now