无需管理员帐户即可捕获原始套接字 (SOCK_RAW)

发布于 2024-12-11 09:10:36 字数 1128 浏览 0 评论 0原文

我有以下代码,它捕获一些 TCP 数据包。但它要求该程序以管理员身份运行。我如何修改此代码,以便该程序即使使用非管理员帐户也可以运行?

    public void startSniffer()
    {
        bContinueCapturing = true;

        mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);

        mainSocket.Bind(new IPEndPoint(IPAddress.Parse(Properties.Settings.Default.IPaddr), 0));
        mainSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);

        byte[] byTrue = new byte[4] { 1, 0, 0, 0 };
        byte[] byOut = new byte[4] { 1, 0, 0, 0 }; 


        mainSocket.IOControl(IOControlCode.ReceiveAll, byTrue, byOut);

        mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);

    }

    public void OnReceive(IAsyncResult ar)
    {
        int nReceived = mainSocket.EndReceive(ar);

        ParseData(byteData, nReceived);

        if (bContinueCapturing)
        {
            byteData = new byte[4096];

            mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
        }

    }

I have the following code, which captures some TCP Packets. But it requires that the program is run as Administrator. How can I modify this code, so the program would work even with a non-administrator account?

    public void startSniffer()
    {
        bContinueCapturing = true;

        mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);

        mainSocket.Bind(new IPEndPoint(IPAddress.Parse(Properties.Settings.Default.IPaddr), 0));
        mainSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);

        byte[] byTrue = new byte[4] { 1, 0, 0, 0 };
        byte[] byOut = new byte[4] { 1, 0, 0, 0 }; 


        mainSocket.IOControl(IOControlCode.ReceiveAll, byTrue, byOut);

        mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);

    }

    public void OnReceive(IAsyncResult ar)
    {
        int nReceived = mainSocket.EndReceive(ar);

        ParseData(byteData, nReceived);

        if (bContinueCapturing)
        {
            byteData = new byte[4096];

            mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流心雨 2024-12-18 09:10:36

根据 MSDN 有关 SIO_RCVALL 的文章,这是 IOControlCode.ReceiveAll

设置此 IOCTL 需要本地计算机上的管理员权限。

要回答你的问题,没有管理员权限就无法完成。

Per MSDN's article on SIO_RCVALL, which is the unmanaged equivalent of IOControlCode.ReceiveAll:

Setting this IOCTL requires Administrator privilege on the local computer.

To answer your question, it can't be done without Administrator privileges.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文