Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send SYN packet using C# or Java

Tags:

c#

I want to send SYN packet to a remote host, now the problem is that if I use any of the classes of .NET framework then I cannot to do this; e.g. TCP Client takes Host ip and port number and establishes connection, same is the case with ClientSocket class in java.

I want to control the connection establishment my self; I mean I want to send the connection request (SYN packet) then wait for the connection reply and then send the packet. I know that it might not be easy without external libraries, so if any one can guide me how to do it in either C# or Java. .

like image 297
uyaseen Avatar asked Mar 12 '11 19:03

uyaseen


People also ask

Who sends SYN packet?

SYN packets are normally generated when a client attempts to start a TCP connection to a server, and the client and server exchange a series of messages, which normally runs like this: The client requests a connection by sending a SYN (synchronize) message to the server.

What is a SYN packet TCP?

Short for synchronize, SYN is a TCP packet sent to another computer requesting that a connection be established between them. If the SYN is received by the second machine, an SYN/ACK is sent back to the address requested by the SYN. Lastly, if the original computer receives the SYN/ACK, a final ACK is sent.

How many bytes is a SYN packet?

In the real-world, when SYN-ACK attacks were launched, packets that arrived at the victims' networks had a predictable length of 44 bytes.

Can a SYN packet have a payload?

The Syn Data Payload Protection enhancement will provide administrators the option to enable or disable SYN data payload checking within a zone protection profile. The scan of payloads during the three-way handshake will take place for both SYN and SYN-ACK packets.


3 Answers

Rather than commenting to both answers so far ... raw sockets have been restricted in Windows since XP (countermeasure to malware attacks). Read the following:

http://msdn.microsoft.com/en-us/library/ms740548(v=vs.85).aspx

Once that's done you can certainly build a raw socket following their rules based on Mat's answer for Java or Ritch's for C#.

It will be heavily restricted access, and very system dependent either way. I'm unaware of a method to complete the 3 way handshake and transmit data across TCP/UDP (though I'd be interested in hearing one).

I thought I should make one quick amendment to this answer. If you don't use winsock and instead use a different TCP stack, you can probably get what you want (but this is still not directly availalbe within the OS). Look at this as an example offering: http://www.komodia.com/tcpip-library-v5-1/ or this previous SO thread What is a popular, multi-platform, free and open source socket library

like image 162
iivel Avatar answered Oct 06 '22 23:10

iivel


What you're looking for is called raw sockets. I don't know about C#, but there are libraries in Java that let you do that, e.g. RockSaw. You cannot do it using pure Java as far as I know.

Be warned that on some operating systems (Linux at least), you need administrator privileges to be able to open raw sockets. And there are a bunch of restrictions on Windows. (See that page I linked.)

like image 45
Mat Avatar answered Oct 06 '22 23:10

Mat


Use the Socket class with a SocketType of raw, and the correct protocol type. I think you want TCP.

  var sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp);
like image 39
Ritch Melton Avatar answered Oct 06 '22 23:10

Ritch Melton