如何创建一个简单的 c# http 监视器/拦截器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/924449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:19:29  来源:igfitidea点击:

How to create a simple c# http monitor/blocker?

c#httpmonitoringblocking

提问by Click Ok

I was reading that question (How to create a simple proxy in C#?) that is near of my wishes.

我正在阅读那个接近我希望的问题(如何在 C# 中创建一个简单的代理?)。

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).

我只是想开发 ac# 应用程序,例如,监控 Firefox、IE 等并记录所有导航页面。根据访问的页面,我想阻止该站点(如家长过滤器)。

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

代码片段/示例很好,但如果您能告诉我该类的使用方向,我将不胜感激。:-)

采纳答案by Bill

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

我会回答适合家长:ala“家长控制”

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#).

当孩子不到 10 岁时,您可以开始使用代理服务器。之后,他们将弄清楚如何绕过代理(或运行自己的绕过代理的客户端应用程序)。在青少年时期,您可以使用原始套接字。将此程序键入 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.

请注意,这只是一个“片段”,缺乏适当的设计和错误检查。[请不要使用“原样” - 您确实要求先行一步] 将 IP 地址更改为您的机器。运行程序作为管理员(在命令行中使用“运行方式”,或右键单击“以管理员身份运行”)。只有管​​理员才能在现代版本的 Windows 上创建和使用原始套接字。坐下来看节目。

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).

下一步是创建一些协议过滤器。了解各种 Internet 应用程序协议(假设您还不知道),修改程序以检查数据包。寻找 HTTP 协议,并保存适当的数据(如 GET 请求)。

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.

我个人为 AIM (AOL Instant Messenger)、HTTP、MSN messenger (Windows Live Messenger)、POP 和 SMTP 设置了过滤器。今天,HTTP 几乎可以得到一切,因为现在孩子们更喜欢 facebook 墙而不是 AIM。

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.).

随着孩子们进入他们的青少年早期到中期,您将需要退出监控。有人说这是为了让孩子“长大”,但真正的原因是“你不想知道”。我放弃只收集明文(pop、基本身份验证等)的 get 请求的 URL 和用户名/密码(用于紧急情况)。

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.

我的最后意见是,这个应用程序应该直接针对 Win32 API 用 C/C++ 编写,并作为在机器上以管理权限运行的服务安装。我认为这种类型的代码不适合托管 C#。您可以在 C# 中附加一个 UI 以进行监视和控制。您必须对代码进行工程设计,以便对系统产生零显着影响。

回答by sbeskur

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.

如果您在单独的服务器上执行此操作,则在 C#/托管代码中完成此操作会更容易,因为您只需编写客户端 PC 将指向的 C# 代理服务器。System.Net.Sockets 命名空间 TcpListener 和 TcpClient 将是您的朋友。

If, however, you are installing this on the same machine then take a look WinPcapand and SharpPCapand perhaps Fiddlerfor some ideas.

但是,如果您将它安装在同一台机器上,那么请查看WinPcapSharpPCap以及Fiddler以获得一些想法。

Hope that helps.

希望有帮助。