Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在本地网络上嗅探android设备的TCP数据包_C#_Packet_Packet Sniffers_Packet Capture_Packetdotnet - Fatal编程技术网

C# 在本地网络上嗅探android设备的TCP数据包

C# 在本地网络上嗅探android设备的TCP数据包,c#,packet,packet-sniffers,packet-capture,packetdotnet,C#,Packet,Packet Sniffers,Packet Capture,Packetdotnet,我正在使用C#和PcapDotNet编写一个数据包嗅探器,我已经成功地实现了这个功能,并且我能够从我的笔记本电脑中捕获所有TCP数据包,问题是如果我以我的android设备为目标,我就根本没有数据包, 我是一个网络新手,所以我想我可能错过了一些东西 这是我的数据包处理程序代码,我使用ObjectListView和一个模型对象,该模型对象包含我想要列出的所有信息,而且我的网络适配器处于混杂模式,带有Berkeley过滤器,仅获取端口号为443和80的TCP数据包,其中包含数据(无SYN、FIN、A

我正在使用C#和PcapDotNet编写一个数据包嗅探器,我已经成功地实现了这个功能,并且我能够从我的笔记本电脑中捕获所有TCP数据包,问题是如果我以我的android设备为目标,我就根本没有数据包, 我是一个网络新手,所以我想我可能错过了一些东西

这是我的数据包处理程序代码,我使用ObjectListView和一个模型对象,该模型对象包含我想要列出的所有信息,而且我的网络适配器处于混杂模式,带有Berkeley过滤器,仅获取端口号为443和80的TCP数据包,其中包含数据(无SYN、FIN、ACK-only数据包)来自特定的Mac地址

代码:

private void PacketHandler(PcapDotNet.Packets.Packet-Packet)
{
if(packet==null){return;}
如果(packet.Ethernet==null){return;}
如果(packet.Ethernet.IpV4==null){return;}
如果(packet.Ethernet.IpV4.Tcp==null){return;}
如果(packet.Ethernet.IpV4.Tcp.Http==null){return;}
var acpacket=new AcceptedPacket();//模型对象
Packet=Packet;
尝试
{
HttpDatagram http=packet.Ethernet.IpV4.Tcp.http;
if(packet.Ethernet.Source.ToString()==targetmac)
{
if(http.IsRequest&&http.IsValid)
{
if(materialListView1.InvokeRequired)
{
materialListView1.BeginInvoke(新操作(()=>{
materialListView1.AddObject(acpacket);});
}
其他的
{
materialListView1.AddObject(acpacket);
}
已接受数据包列表。添加(acpacket);
}
}
}
捕获(例外情况除外)
{
MetroMessageBox.Show(例如消息“Error”,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
我是这样打开适配器的:

使用(通信器)=
选择设备。打开(65536,PacketDeviceOpenAttributes.滥交,
1000))
{
如果(communicator.DataLink.Kind!=
DataLinkKind(以太网)
{
if(MetroMessageBox.Show)(此“此操作仅支持以太网!”、“错误”、MessageBoxButtons.OK、MessageBoxIcon.Error)=DialogResult.OK)
{
返回;
}
}
使用(BerkeleyPacketFilter=communicator.CreateFilter($“tcp端口80和((ip[2:2]-((ip[0]&0xf)2))!=0)和以太src{targetmac.ToLower()}或tcp端口443和((ip[2:2]-((ip[0]&0xf)2))!=0)和以太src{targetmac ToLower()))
{
通信器。设置过滤器(过滤器);
}
flag=true;
数据包;
做
{
packetcommunicator接收结果=
通讯器。接收数据包(输出数据包);
开关(结果)
{
案例
PacketCommunicationorReceiverResult.超时:
继续;
案例
packetcommunicator接收结果。确定:
{
PacketHandler(PacketHandler);
}
打破
违约:
打破
}
}而(旗),;
}     
我也尝试过不使用任何过滤器,无法访问android设备数据包。 那么,除了我自己的设备之外,还有可能从网络上的其他设备捕获TCP数据包吗?或者我的实现可能有问题


编辑:在尝试了一点之后,我成功地从android设备获取了TCP数据包,但只有在同时实施ARP缓存中毒时,因为设备认为我是网关。所以我的问题是,这是否可以在没有ARP缓存中毒的情况下实现,因为我认为这对pa来说有点咄咄逼人cket嗅探器。

我终于能够通过应用ARP缓存中毒使其工作,而下面的代码能够将任何设备的数据包重定向到其目的地,这样您就可以捕获网络上任何设备的数据包,而不会丢失该设备的internet访问

代码:

这是捕获设备设置:

 try
    {
        snifferStarted = true;
        if (capturedevice != null)
        {
            capturedevice.Open(DeviceMode.Promiscuous, 1000);

                capturedevice.Filter = $"(ip and ether src {targetmac.ToLower()}) or (ip and ether src {gatewayMAC.ToLower()} and dst net {Target})";

            new Thread(() => { StartSniffer(); }).Start();
        }
        else
        {
            MetroMessageBox.Show(this, "No Capture Device is selected!", "Error", MessageBoxButtons.OK,
                MessageBoxIcon.Error);
        }
    }
    catch (Exception exception)
    {
        MetroMessageBox.Show(this, exception.Message, "Error", MessageBoxButtons.OK,
            MessageBoxIcon.Error);
    }

注意:这是使用
Packet.Net
而不是
PcapDotNet

完成的,我最终能够通过应用ARP缓存中毒使其工作,而下面的代码能够将任何设备的数据包重定向到其目的地,这样您就可以在不丢失internet的情况下捕获网络上任何设备的数据包此设备的访问权限

代码:

这是捕获设备设置:

 try
    {
        snifferStarted = true;
        if (capturedevice != null)
        {
            capturedevice.Open(DeviceMode.Promiscuous, 1000);

                capturedevice.Filter = $"(ip and ether src {targetmac.ToLower()}) or (ip and ether src {gatewayMAC.ToLower()} and dst net {Target})";

            new Thread(() => { StartSniffer(); }).Start();
        }
        else
        {
            MetroMessageBox.Show(this, "No Capture Device is selected!", "Error", MessageBoxButtons.OK,
                MessageBoxIcon.Error);
        }
    }
    catch (Exception exception)
    {
        MetroMessageBox.Show(this, exception.Message, "Error", MessageBoxButtons.OK,
            MessageBoxIcon.Error);
    }
注意:这是使用
Packet.Net
而不是
PcapDotNet
完成的