• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

[C#] Raw sockets packet sending

xlink

New Member
Joined
Jan 22, 2010
Messages
6 (0.00/day)
So I made a sniffer to suit my needs but now I want to try something more ambitious, I want to send back a packet that I filtered exactly the same, this is trickier than I thought.

Code:
                    IPAddress ip = IPAddress.Parse("192.168.2.100");

                    m_clientSocket = new Socket(ip.AddressFamily, SocketType.Raw, ProtocolType.Ip);                    

                    m_clientSocket.Bind(new IPEndPoint(ip, 0));

                    m_clientSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);

Once I captured a packet that I want to resend I first write it to be sure that i got it and then try to send it.

Code:
                    byte[] dat = tcph.Data;

                    if (tcph.DataLength > 0)
                    {


                        char x = (char)dat[tcph.DataLength - 2];
                        string y = "" + x;
                        this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { y + "\n" });
                     

                    IPEndPoint ipEnd = new IPEndPoint(header.DestinationAddress, 8687);

                    try
                    {
                        m_clientSocket.SendTo(data, ipEnd);
                    }
                    catch (SocketException e)
                    {
                        MessageBox.Show(e.Message);
                    }
                    }


Thing is it doesn't send any packets nor does it result in an error, i have no clue what's the problem and there seems to be no documentation on C# raw packet sending on the internet.
 
Joined
Aug 5, 2008
Messages
557 (0.10/day)
Location
Hampshire, UK
System Name If you name your systems, get a boy/girlfriend...
Processor i7 4770k
Motherboard Asus Maximus VI Formula
Cooling Custom waterloop around Black Ice GTX 360
Memory 16GB DDR3
Video Card(s) GTX 1080 FE
Storage Samsung 850 Pro 1TB
Case HAF 932
Audio Device(s) Onboard
Power Supply Corsair HX750
Software Windows 10 x64
Hi,

I'm no C# expert, but I worked with M$ Sockets for a while.

I'm not sure whether it's affecting you, but MS locked the possibility of "forging" packets using Socket API, to prevent kids from writing spoofing tools easily :)

The move is plainly stupid, because you can still forge packets using lower-level access to the network driver (NDIS - in case of Windows)
Well... that's what NIC is for... creating packets... Don't ask me to explain Microsoft's reasoning....

The easiest way to achieve this is to use API that will talk to the NDIS driver for you.
Use WINPCAP. It's free and fairly easy to use.

Cheers,
 

xlink

New Member
Joined
Jan 22, 2010
Messages
6 (0.00/day)
Thanks, i knew about winpcap but i thought i could do it just with raw sockets cuz winpcap isn't native for c#.

Anyway i'll look into a winpcap wrapper of some sort.
 
Top