Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write custom data to the TCP packet header options field with Java?

Tags:

java

tcp

options

As it is defined (see: http://www.freesoft.org/CIE/Course/Section4/8.htm) the TCP header has an 'Options' field. There are a couple of options already defined (see: www.iana.org/assignments/tcp-parameters/) but I want to come up with my own. (For experimenting/research.)

How can I get Java to write (and then read) some custom data to the options field?

Bonus question: if it cannot be done with Java. what kind of application can do this? (No, I don't really feel like messing with some kernel-level TCP/IP stack implementation, I want to keep it app level.)

like image 386
Matlabber Avatar asked Apr 16 '10 13:04

Matlabber


People also ask

What is option field in TCP header?

This option field is used in the SYN packet when the client establishes a connection with the server. “Kind=2” takes 1 bytes space, “length=4” takes 1-byte space and “MSS” takes 2-byte space. Thus, the total space consumed (length = 4) by the MSS option is 4 bytes.

What are the options used in TCP packet?

The TCP Options (MSS, Window Scaling, Selective Acknowledgements, Timestamps, Nop) are located at the end of the TCP Header which is also why they are covered last.

What is option and padding in TCP header?

The TCP header padding is used to ensure that the TCP header ends, and data begins, on a 32-bit boundary. The padding is composed of zeros.

What is the maximum size of the options field in a TCP header?

Maximum Segment Size(MSS) It should be seen only 3-way handshake in SYN and SYN/ACK packets. MSS option fields is 4 bytes long.


1 Answers

JNetPcap is a library that will allow you to change headers from low level layers including TCP. http://jnetpcap.com/node/29

Here is a quick example:

byte[] pktBytes = FormatUtils.toByteArray("0015c672234c90e6ba92661608004500002d358c4000800600000a000b050a090028c26e270fb8b256e3a2009f785018faf01f550000746573740a");
JMemoryPacket packet = new JMemoryPacket(pktBytes);

packet.scan(Ethernet.ID); //Need to be done before doing any edits

//Editing Ip layer
Ip4 ip = packet.getHeader(new Ip4());
ip.source(new byte[] {2,6,0,0}); //Source Ip 2.6.0.0
ip.destination(new byte[] {1,2,3,4}); //Dest Ip 1.2.3.4

//Editing Tcp layer
Tcp tcp = packet.getHeader(new Tcp());
tcp.destination(5555); //Port destination 5555

if (pcap.sendPacket(packet) != Pcap.OK) {
    System.err.println(pcap.getErr());
}
like image 78
h3xStream Avatar answered Oct 19 '22 12:10

h3xStream