使用jnetpcap创建ARP数据包

时间:2016-05-27 21:37:31

标签: java jnetpcap

我正在尝试使用jnetpcap API创建ARP请求/回复数据包。我不知道什么是错的,但我的代码不起作用。

这是在数据包中添加ARP标头的方法的片段。

public void addARPHeader(String macSource, byte[] sourceip, byte[] destIp, int type){
    packet.scan( Ethernet.ID );
    Arp arp=packet.getHeader( new Arp() );
    byte[] macS=FormatUtils.toByteArray( macSource.replace( ":", "" ) );
    arp.setUByte( 0, 0x01);
    arp.setUByte( 2, 0x800 );
    arp.setUByte(4, 0x04 );
    arp.setUByte( 5, 0x04);
    arp.setUByte( 6, type );
    arp.setByteArray( 8, macS );
    arp.setByteArray( 14, sourceip ); //statement that raise exception
    arp.setByteArray( 18, macS );
    arp.setByteArray( 24, destIp );

}

当我尝试将一个arp标头添加到我的数据包JmemoryPacket时,我得到了这些错误:

Exception in thread "main" java.nio.BufferUnderflowException    at
org.jnetpcap.nio.JBuffer.check(Unknown Source)  at
org.jnetpcap.nio.JBuffer.setByteArray(Unknown Source)   at
com.upec.securityProtocols.communications.Packet.addARPHeader(Packet.java:53)
    at test.MyTest1.main(MyTest1.java:13)

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

ARP Packet Format

上图显示了ARP数据包格式

这个想法是在java

中实现这个结构

代码:

import org.jnetpcap.packet.JMemoryPacket;
import org.jnetpcap.packet.JPacket;
import org.jnetpcap.protocol.JProtocol;

public class ARPPacket {
    private static final String hardwareType = "0001";// 2 bytes
    private static final String protocolType = "0800";// IPv4 protocol
    private static final String hardwareAddressLength = "06";// xx-xx-xx-xx-xx-xx  48 bits => 6bytes
    private static final String protocolAdressLength = "04";// yyy.yyy.yyy.yyy 32 bits => 4 bytes
    private static final String operationCode = "0001";// Arp request 0001 ; Arp reply 0002

    public ARPPacket() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) {
        ARPPacket arp = new ARPPacket();
        // 192.168.1.2 in hex is C0A80102
        // broadcast address is FFFFFFFFFFFF
        // 192.168.1.2 in hex is C0A80101
        //Assume that we will send an arp request to 192.168.1.1
        JPacket arpPacket = arp.createArp("000012345678", "C0A80102", "FFFFFFFFFFFF", "C0A80101 ");
        System.out.println(arpPacket);

    }

    private JPacket createArp(String MAC_SOURCE, String IP_SOURCE, String MAC_DESTINATION, String IP_DESTINATION) {
        String ARPPacket = hardwareType + protocolType + hardwareAddressLength + protocolAdressLength + operationCode
                + MAC_SOURCE + IP_SOURCE + MAC_DESTINATION + IP_DESTINATION;
        JPacket arpRequest = new JMemoryPacket(JProtocol.ARP_ID, ARPPacket);
        return arpRequest;
    }
}

输出:

Frame:
Frame:                                  number = 0
Frame:                               timestamp = 2016-07-01 09:40:03.218
Frame:                             wire length = 28 bytes
Frame:                         captured length = 28 bytes
Frame:
Arp:  ******* Arp offset=0 (0x0) length=28 
Arp: 
Arp:                             hardwareType = 1 [ETHERNET]
Arp:                             protocolType = 0x800 (2048) [IP]
Arp:                            hardware size = 6 bytes
Arp:                            protocol size = 4 bytes
Arp:                                  op code = 1 [REQUEST]
Arp:                               sender MAC = 00:00:12:34:56:78
Arp:                                sender IP = 192.168.1.2
Arp:                               target MAC = ff:ff:ff:ff:ff:ff
Arp:                                target IP = 192.168.1.1
Arp: