2

Here is the code to capture the ICMP packets and store in txt file, but the storing information is in the format of binary. Can any one please tell me, how to capture the ICMP packet's source address and size [if possible MAC address] in clear text file or db file for processing.

import java.net.InetAddress;
import jpcap.packet.*;
import jpcap.*;
import jpcap.packet.EthernetPacket;
import jpcap.packet.IPPacket;
import jpcap.packet.TCPPacket;
import java.util.Scanner;

class capture
{
        public static void main(String[] args) throws java.io.IOException{
        //Get the Device information - Start

            //Obtain the list of network interfaces
            NetworkInterface[] devices = JpcapCaptor.getDeviceList();

            //for each network interface
            for (int i = 0; i < devices.length; i++) {
              //print out its name and description
              System.out.println(i+": "+devices[i].name + "(" + devices[i].description+")");

              //print out its datalink name and description
              System.out.println(" datalink: "+devices[i].datalink_name + "(" + devices[i].datalink_description+")");

              //print out its MAC address
              System.out.print(" MAC address:");
              for (byte b : devices[i].mac_address)
                System.out.print(Integer.toHexString(b&0xff) + ":");
              System.out.println();

              //print out its IP address, subnet mask and broadcast address
              for (NetworkInterfaceAddress a : devices[i].addresses)
                System.out.println(" address:"+a.address + " " + a.subnet + " "+ a.broadcast);
            }
        //Get the Device information - End

//Capture the packets

                System.out.println("\n \n ");
                System.out.println("Please Enter the Device Name to Capture the Packet");
                Scanner in = new Scanner(System.in);
                int a = in.nextInt();
                if(a <= devices.length)
                {
                int index=a;  // set index of the interface that you want to open.

                //Open an interface with openDevice(NetworkInterface intrface, int snaplen, boolean promics, int to_ms)
                JpcapCaptor captor=JpcapCaptor.openDevice(devices[index], 65535, false, 20);
                captor.setFilter("icmp",true);
                for(int i=0;i<50;i++){
                  //capture a single packet and print it out
                  System.out.println(captor.getPacket());
                  JpcapWriter writer=JpcapWriter.openDumpFile(captor,"s.txt");

                }

                }
                else
                System.out.println("Please Enter the correct value");
            }
}
Vinoth Kumar
  • 413
  • 6
  • 18
  • 1
    i think http://stackoverflow.com/questions/9443288/get-ip-adress-of-interface-in-linux-using-pcap can help you – MaVRoSCy Oct 10 '12 at 08:09
  • Its written in C as well, it will capture its ip address and port number. If you run my above code in one machine and run this command in another machine [open command prompt and enter ping ip-address-of-the-code-running-system -t -l 65500]. So the programming running system will capture the ICMP packets and store in binary format. But i need it in the exact format like IP, Packet size, MAC address. – Vinoth Kumar Oct 10 '12 at 09:37

2 Answers2

4

Call the looppacket function after opening the device and setting the icmp filter: jpcap.loopPacket(-1, new capture());

Declare this function in your capture class:

public void receivePacket(Packet pkt) {
        IPPacket pac = (IPPacket) pkt;
            System.out.println("Src: " + pac.src_ip + " Dest: " + pac.dst_ip);
    }
Shubham Saini
  • 738
  • 3
  • 8
  • 18
0

I haven't tested it, but according to documentation this should work in getting the Source IP address

System.out.println((ICMPPacket)captor.getPacket().src_ip);

Once you get the correct ip address then its easy to get the MAC address using this code

        InetAddress ip;       
        ip = InetAddress.getLocalHost();
        NetworkInterface network = NetworkInterface.getByInetAddress(ip);
        byte[] mac = network.getHardwareAddress();
        System.out.print("Current MAC address : ");

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
          sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));    
        }
        System.out.println(sb.toString());

Thanks to mkyong

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
  • I tried with this statement, System.out.println((ICMPPacket)captor.getPacket().src_ip); but got error in src_ip. Any solution – Vinoth Kumar Oct 10 '12 at 14:02
  • cannot compile it on my 64bit machine. According to documentation src_id is of type InetAddress. Try this `System.out.println((ICMPPacket)captor.getPacket().src_ip.getHostAddress());` – MaVRoSCy Oct 16 '12 at 13:13
  • still am getting error as, symbol: variable src_ip location: class Packet – Vinoth Kumar Oct 17 '12 at 16:52
  • How to block or drop the ICMP packets.. is there any function to drop the packets from ethernet level ? I want it by coding not by using Firewalls. – Vinoth Kumar Mar 06 '13 at 06:59