无需管理员帐户即可捕获原始套接字 (SOCK_RAW)
我有以下代码,它捕获一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 MSDN 有关
SIO_RCVALL
的文章,这是IOControlCode.ReceiveAll
:要回答你的问题,没有管理员权限就无法完成。
Per MSDN's article on
SIO_RCVALL
, which is the unmanaged equivalent ofIOControlCode.ReceiveAll
:To answer your question, it can't be done without Administrator privileges.