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

别说谁变了你拦得住时间么 提交于 2019-11-28 06:36:41

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());
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!