views:

1934

answers:

5

I was reading that question (http://stackoverflow.com/questions/226784/how-to-create-a-simple-proxy-in-c) that is near of my wishes.

I simply want develop a c# app that, by example, monitors Firefox, IE, etc and logs all navigated pages. Depending of the visited page, I want to block the site (like a parental filter).

Code snippets/samples are good, but if you can just tell me some direction of that classes to use I will be grateful. :-)

+6  A: 

Your approach will depend on whether or not you are installing this application on the same box you are using to browse or on a separate proxy server.

If you are doing this on a separate server it will be easier to accomplish this in C# / managed code, as you will be simply writing a C# proxy server that client pc's will have point to. System.Net.Sockets namespace TcpListener and TcpClient will be your friend.

If, however, you are installing this on the same machine then take a look WinPcap and and SharpPCap and perhaps Fiddler for some ideas.

Hope that helps.

sbeskur
+7  A: 

I’ll answer appropriate for a parent: ala "Parental Controls"

You can start out with a proxy server when the kids are less than about 10 years old. After that, they will figure out how to get around the proxy (or run their own client applications that bypass the proxy). In the early teen years, you can use raw sockets. Type this program into Visual Studio (C#).

class Program
{
    static void Main(string[] args)
    {
        try
        {
            byte[] input = BitConverter.GetBytes(1);
            byte[] buffer = new byte[4096];
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
            s.Bind(new IPEndPoint(IPAddress.Parse("192.168.1.91"), 0));
            s.IOControl(IOControlCode.ReceiveAll, input, null);

            int bytes = 0;
            do
            {
                bytes = s.Receive(buffer);
                if (bytes > 0)
                {
                    Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, bytes));
                }
            } while (bytes > 0);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
}

Note that this is just a “snippet”, lacking appropriate design and error checking. [Please do not use 'as-is' - you did request just a head start] Change the IP address to your machine. Run the program AS Administrator (use “runas” on the command line, or right-click “Run as Administrator”). Only administrators can create and use raw sockets on modern versions of windows. Sit back and watch the show.

All network traffic is delivered to your code (displayed on the screen, which will not look nice, with this program).

Your next step is to create some protocol filters. Learn about the various internet application protocols (assuming you don't already know), modify the program to examine the packets. Look for HTTP protocol, and save the appropriate data (like GET requests).

I personally have setup filters for AIM (AOL Instant Messenger), HTTP, MSN messenger (Windows Live Messenger), POP, and SMTP. Today, HTTP gets just about everything since the kids prefer the facebook wall to AIM nowadays.

As the kids reach their early-to-mid teenage years, you will want to back-off on the monitoring. Some say this is to enable the kids to “grow up”, but the real reason is that “you don’t wanna know”. I backed off to just collecting URLs of get requests, and username/passwords (for emergency situations) that are in clear text (pop, basic auth, etc.).

I don't know what happens in late teen years; I cannot image things getting much worse, but I am told that "I have not seen anything yet".

Like someone earlier said, this only works when run on the target machine (I run a copy on all of the machines in the house). Otherwise, for simple monitoring check your router - mine has some nice logging features.

My final comment is that this application should be written in C/C++ against the Win32 API directly, and installed as a service running with administrative rights on the machine. I don't think this type of code is appropriate for managed c#. You can attach a UI in C# for monitoring and control. You have to engineer the code so as to have zero noticeable effect on the system.

Bill
You shouldn't use `Console.WriteLine(ex);` - it ha no sense. Either `ex.Message` (or something like that) or don't use try-catch block at all. Exception's message will be written to the output automatically.
abatishchev
+1  A: 

Hi Bill,

I am developing a software that will log the visited websites URL. The code snippet that you wrote here returns a lot of information. I only want to parse the Http header to get the URL. How can I get the URL from the information. Please help me in this regard. It will be highly appreciated that if you send me some code that will log the URLs of visited websites.

Kind Regards, Rizwan Ahmed

Rizwan Ahmed
Parse the packets then determine which packets are using the HTTP protocol.
Chris T
+1  A: 

Hi Rizwanz Ahmed,replace the

class Program

{
    static void Main(string[] args)
    {
        try
        {
            byte[] input = BitConverter.GetBytes(1);
            byte[] buffer = new byte[4096];
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
            s.Bind(new IPEndPoint(IPAddress.Parse("xxx.xxx.xx.x"), 0));//replace xxx.xxx.xx.x with your ip address.

            s.IOControl(IOControlCode.ReceiveAll, input, null);

            int bytes = 0;
            do
            {
                bytes = s.Receive(buffer);
                if (bytes > 0)
                {


                    System.Text.ASCIIEncoding ascii = new ASCIIEncoding();


                    string url=(ascii.GetString(buffer, 0, bytes));
                    string host = url;



                    try
                    {
                        url = url.Substring(url.IndexOf("GET") + 4, url.IndexOf("HTTP") - (url.IndexOf("GET") + 3) - 1);
                        host = host.Substring(host.IndexOf("Host") + 6, host.IndexOf("User-Agent") - (host.IndexOf("Host") + 6));

                        Console.WriteLine("http//:"+host.Substring(0,host.Length-2)+url);

                    }
                    catch
                    { 
                    }

                }
            } while (bytes > 0);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

    }
}

Arsalan,WABZ

www.wabztech.com

Arsalan
+1  A: 

How can I block the http request ?

Minhaj