How send raw ethernet packet with C#? [closed]
Asked Answered
T

3

31

Is there a way to send raw packet Ethernet to other host via C#? In Windows 7 if it makes difference.

Templetempler answered 18/10, 2010 at 22:43 Comment(3)
What do you expect to happen on the other end? Raw too? Making your own protocol? What's wrong with the ones we all use, your hardware knows how to route and your LAN admin knows how to support?Pediatrician
I just want send ethernet packet with some changed fields like MAC addressTempletempler
SharpPcap and Pcap.net are the way to go. You need a WinPcap wrapper framework because Windows doesn't allow access to lower level protocol headers for 'security reasons'. WinPcap provides its own networking driver that allows you to bypass that restriction.Afghan
V
20

Based on suggestion by Saint_pl:

I found probably better solution - similar to SharpPcap. It's Pcap.Net - .NET wrapper for WinPcap. Now I can modify my packets whatever I want.


I have some resources for you that maybe helpful. I don't try that solutions in Windows 7 but maybe it contains some good info to start.

Raw Ethernet Packet Manipulation or mirror on CodeProject

This purpose of this article is to explain how to send a raw Ethernet packet using C# on a Microsoft platform. A raw Ethernet packet is the complete Layer 2 network frame that is sent to the physical wire. Sending a frame like this allows you to manipulate the target and source MAC addresses and the Layer 3 protocol fields.

Also some info on raw sockets (just in case you interesting too):

Client (and Server) Sockets Communication take a look on whole chapter but here key parts:

Not sending packets but maybe interesting: A Network Sniffer in C#, SharpPcap - A Packet Capture Framework for .NET

Vashtivashtia answered 18/10, 2010 at 23:18 Comment(9)
I think Raw Ethernet Packet Sending from CodeProject will help me. And btw I suppose it's one and only way that I was found to send ethernet packet with e.g changed mac address via C#.Templetempler
Raw ethernet manipulation is pretty low level. To complete your task at good level you certainly need some knowledge about Network Driver Interface Specification and understanding device drivers (and its API). Don't skip it. C# alone don't give you full power in that task.Vashtivashtia
I found probably better solution - similar to SharpPcap. It's pcapdotnet.codeplex.com - .NET wrapper for WinPcap. Now I can modify my packets whatever I want. Thanx :)Templetempler
Welcome :) Glad to see that you have nice solution for your task.Vashtivashtia
@NickMartyshchenko do i have to install winpcap to use pcap.net?Telltale
@Telltale Pcap.Net is a .NET wrapper for WinPcap, so it's a managed interface to WinPCap and requires WinPcap installed to function properly. Here some details how to run it pcapdotnet.codeplex.com/…Vashtivashtia
Does it support tagged vlans ?Northey
@Northey Pcap.Net's description says support for Ethernet + VLAN tagging (802.1Q) but I never tried myselfVashtivashtia
Raw ethernet frames use a length (<= 1500) vs. an EtherType field, and the payload starts with 0xFFFF. Today, we normally use Ethernet II with IP, rather than Novell.Softener
F
0

iphelper API has some low level stuff - but probably not quite as low as you want to get

Flattish answered 18/10, 2010 at 22:57 Comment(0)
G
-2
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("10.25.184.11"), 4456);

server.Connect(ip);

byte[] sendData = new byte[] { 0, 8, 32, 64 };
server.Send(sendData);

//done. now let's listen for data

byte[] receiveData = new byte[1024];
int receivedDataLength = server.Receive(receiveData);

//if the response is a string message
string stringData = Encoding.ASCII.GetString(receiveData, 0, receivedDataLength);
Console.WriteLine(stringData);
Grog answered 18/10, 2010 at 22:51 Comment(3)
This is a TCP packet, not an ethernet packet... You're 2 layers too high on the stack ;)Josiah
Ah I see. Comprehension fail.Grog
Also this would not work on Windows 7. Only Server versions of windows allow use of raw TCP sockets due to malware abuse in the past.Noletta

© 2022 - 2024 — McMap. All rights reserved.