Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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# C语言中的ARP请求#_C#_Wifi_Pinvoke_Arp_Spoofing - Fatal编程技术网

C# C语言中的ARP请求#

C# C语言中的ARP请求#,c#,wifi,pinvoke,arp,spoofing,C#,Wifi,Pinvoke,Arp,Spoofing,我试图让我的Android设备认为我是路由器,使用C#中的一个简单ARP请求(用C#将ARP从我的笔记本发送到我的Android设备)。 我想如果我使用SendArp方法(来自iphlapi.dll),它会工作: SendArp(ConvertIPToInt32(IPAddress.Parse("myAndroidIP")), ConvertIPToInt32(IPAddress.Parse("192.168.1.1")), macAddr, ref macAddrLen) 但是我无法

我试图让我的Android设备认为我是路由器,使用C#中的一个简单ARP请求(用C#将ARP从我的笔记本发送到我的Android设备)。 我想如果我使用SendArp方法(来自iphlapi.dll),它会工作:

SendArp(ConvertIPToInt32(IPAddress.Parse("myAndroidIP")), 
   ConvertIPToInt32(IPAddress.Parse("192.168.1.1")), macAddr, ref macAddrLen)
但是我无法发送请求。*但是,如果我将编写“0”而不是
ConvertIPToInt32(IPAddress.Parse(“192.168.1.1”)

它将在以下方面发挥作用:

因此,如果源ip为“0”,则它可以工作,但如果源ip为路由器ip地址,则它不能工作

我正在使用此pinvoke方法发送ARP:

[System.Runtime.InteropServices.DllImport("Iphlpapi.dll", EntryPoint = "SendARP")]
internal extern static Int32 SendArp(Int32 destIpAddress, Int32 srcIpAddress,
byte[] macAddress, ref Int32 macAddressLength);
此方法用于将字符串IP转换为Int32:

private static Int32 ConvertIPToInt32(IPAddress pIPAddr)
{
 byte[] lByteAddress = pIPAddr.GetAddressBytes();
 return BitConverter.ToInt32(lByteAddress, 0);
}

谢谢。

我想您误解了第二个参数的含义

1) ARP请求不是通过特定IP(例如Android设备)发送的,而是通过广播发送到网络中的所有计算机

2) 请看的描述,第二个参数是接口IP,而不是目标IP。如果我理解正确,如果您的计算机中有多个LAN卡,您可以选择一个,它将发送ARP请求

SrcIP[in]发送方的源IPv4地址,格式为 IPAddr结构。此参数是可选的,用于选择 接口发送ARP条目请求。打电话的人可能 为此指定与INADDR\ U任何IPv4地址对应的零 参数


这是我使用的方法,似乎没有问题。
如其他答案所述,第二个参数是源IP的选择。
将其设置为0只会使用计算机上的任何接口

//You'll need this pinvoke signature as it is not part of the .Net framework
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int DestIP, int SrcIP, 
                                 byte[] pMacAddr, ref uint PhyAddrLen);

//These vars are needed, if the the request was a success 
//the MAC address of the host is returned in macAddr
private byte[] macAddr = new byte[6];
private uint macAddrLen;

//Here you can put the IP that should be checked
private IPAddress Destination = IPAddress.Parse("127.0.0.1");

//Send Request and check if the host is there
if (SendARP((int)Destination.Address, 0, macAddr, ref macAddrLen) == 0)
{
    //SUCCESS! Igor it's alive!
}

第一步是添加一些错误检查
SendARP
有一个可忽略的返回值。不要那样做。告诉我们返回的值是多少。请看页面顶部的C#签名。@C0LDSH3LL好的,我看了,有什么不同?pinvoke.net不是参考。事实上,它经常是错误的,或者至少是误导性的。pinvoke.net可以帮助您快速确定函数的.net签名,但是如果您不能/不阅读并理解函数的实际文档,那么您根本不应该使用pinvoke。很抱歉耽搁了这么久,我在过去几周进行了一些期末考试。我尝试使用SharpPcap发送请求,但仍然无法实现。C#中是否有ARP欺骗的解决方案?
//You'll need this pinvoke signature as it is not part of the .Net framework
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int DestIP, int SrcIP, 
                                 byte[] pMacAddr, ref uint PhyAddrLen);

//These vars are needed, if the the request was a success 
//the MAC address of the host is returned in macAddr
private byte[] macAddr = new byte[6];
private uint macAddrLen;

//Here you can put the IP that should be checked
private IPAddress Destination = IPAddress.Parse("127.0.0.1");

//Send Request and check if the host is there
if (SendARP((int)Destination.Address, 0, macAddr, ref macAddrLen) == 0)
{
    //SUCCESS! Igor it's alive!
}