C# 原始套接字 ARP 回复
C# raw socket ARP reply
我是一名学生,想了解更多有关 C# 中的 ARP 和套接字的信息
为此,我尝试使用 C# 中的原始 Socket
发送 ARP 请求和回复。
我已经在一个字节数组中手动重建了一个 ARP 回复,我正在尝试使用 Socket.Send
方法发送它。
static void Main(string[] args)
{
// Create a raw socket
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Raw);
// Setup ARP headers
byte[] buffer = new byte[]
{
0x34, 0x97, 0xf6, 0x22, 0x04, 0xe8, // Ethernet Destination mac
0x70, 0x1c, 0xe7, 0x51, 0x94, 0x0b, // Ethernet source mac
0x08, 0x06, // Type: ARP
00, 0x01, // Hardware type: Ethernet
0x08, 0x00, // Protocol type: IPv4
0x06, // Hardware size: 6
0x04, // Protocol size: 4
00, 0x02, // Opcode: Reply
0x70, 0x1c, 0xe7, 0x51, 0x94, 0x0b, // Sender mac addr
0xc0, 0xa8, 0x01, 0x34, // Sender IP addr 192.168.1.52
0x34, 0x97, 0xf6, 0x22, 0x04, 0xe8, // Target mac addr
0xc0, 0xa8, 0x01, 0x01 // Target ip addr 192.168.1.1
};
// Send ARP reply
socket.Send(buffer, buffer.Length, SocketFlags.None);
Console.ReadKey();
}
当我尝试 运行 这段代码时,应用程序抛出 SocketException
:
A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
不过,据我所知,我已经在请求中提供了目的地 MAC 地址。
我应该如何使用 Socket
正确发送 ARP 回复(/请求)?
PS: 我知道可能有一个库,但我想我不会在使用库时学到太多关于套接字和 ARP 的知识。
过了一会儿我忘记了这个问题,但从 an article my teacher showed me about Raw Sockets in a WinSock annex on MSDN 那里得到了答案。
这篇文章的一个部分特别有趣关于这个问题:
原始套接字的限制
在 Windows 7、Windows Vista、Windows 带有 Service Pack 2 (SP2) 的 XP 和 Windows 带有 Service Pack 3 (SP3) 的 XP 上,通过原始套接字发送流量的能力在几个方面受到限制:
- 无法通过原始套接字发送 TCP 数据。
- 源地址无效的 UDP 数据报无法通过原始套接字发送。任何传出 UDP 数据报的 IP 源地址必须存在于网络接口上,否则数据报将被丢弃。进行此更改是为了限制恶意代码创建分布式拒绝服务攻击的能力,并限制发送欺骗性数据包(TCP/IP 具有伪造源 IP 地址的数据包)的能力。
- 不允许使用 IPPROTO_TCP 协议的原始套接字调用绑定函数。
UDP datagrams with an invalid source address cannot be sent over raw sockets. The IP source address for any outgoing UDP datagram must exist on a network interface or the datagram is dropped. This change was made to limit the ability of malicious code to create distributed denial-of-service attacks and limits the ability to send spoofed packets (TCP/IP packets with a forged source IP address).
所以这个问题的简单答案是:用普通的 C# .net Framework 发送这样的数据包是不可能的。
对于任何想使用 C# 发送数据包的人:您可以使用 sharppcap library。
我是一名学生,想了解更多有关 C# 中的 ARP 和套接字的信息
为此,我尝试使用 C# 中的原始 Socket
发送 ARP 请求和回复。
我已经在一个字节数组中手动重建了一个 ARP 回复,我正在尝试使用 Socket.Send
方法发送它。
static void Main(string[] args)
{
// Create a raw socket
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Raw);
// Setup ARP headers
byte[] buffer = new byte[]
{
0x34, 0x97, 0xf6, 0x22, 0x04, 0xe8, // Ethernet Destination mac
0x70, 0x1c, 0xe7, 0x51, 0x94, 0x0b, // Ethernet source mac
0x08, 0x06, // Type: ARP
00, 0x01, // Hardware type: Ethernet
0x08, 0x00, // Protocol type: IPv4
0x06, // Hardware size: 6
0x04, // Protocol size: 4
00, 0x02, // Opcode: Reply
0x70, 0x1c, 0xe7, 0x51, 0x94, 0x0b, // Sender mac addr
0xc0, 0xa8, 0x01, 0x34, // Sender IP addr 192.168.1.52
0x34, 0x97, 0xf6, 0x22, 0x04, 0xe8, // Target mac addr
0xc0, 0xa8, 0x01, 0x01 // Target ip addr 192.168.1.1
};
// Send ARP reply
socket.Send(buffer, buffer.Length, SocketFlags.None);
Console.ReadKey();
}
当我尝试 运行 这段代码时,应用程序抛出 SocketException
:
A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
不过,据我所知,我已经在请求中提供了目的地 MAC 地址。
我应该如何使用 Socket
正确发送 ARP 回复(/请求)?
PS: 我知道可能有一个库,但我想我不会在使用库时学到太多关于套接字和 ARP 的知识。
过了一会儿我忘记了这个问题,但从 an article my teacher showed me about Raw Sockets in a WinSock annex on MSDN 那里得到了答案。
这篇文章的一个部分特别有趣关于这个问题:
原始套接字的限制
在 Windows 7、Windows Vista、Windows 带有 Service Pack 2 (SP2) 的 XP 和 Windows 带有 Service Pack 3 (SP3) 的 XP 上,通过原始套接字发送流量的能力在几个方面受到限制:
- 无法通过原始套接字发送 TCP 数据。
- 源地址无效的 UDP 数据报无法通过原始套接字发送。任何传出 UDP 数据报的 IP 源地址必须存在于网络接口上,否则数据报将被丢弃。进行此更改是为了限制恶意代码创建分布式拒绝服务攻击的能力,并限制发送欺骗性数据包(TCP/IP 具有伪造源 IP 地址的数据包)的能力。
- 不允许使用 IPPROTO_TCP 协议的原始套接字调用绑定函数。
UDP datagrams with an invalid source address cannot be sent over raw sockets. The IP source address for any outgoing UDP datagram must exist on a network interface or the datagram is dropped. This change was made to limit the ability of malicious code to create distributed denial-of-service attacks and limits the ability to send spoofed packets (TCP/IP packets with a forged source IP address).
所以这个问题的简单答案是:用普通的 C# .net Framework 发送这样的数据包是不可能的。
对于任何想使用 C# 发送数据包的人:您可以使用 sharppcap library。