1

I have implemented a code to import data from a .pcap file. It works correctly. It reads the cap files and in the console displays the results.

The code for this is,

@SpringBootApplication

public class SpringBootSecurityJwtMongodbApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootSecurityJwtMongodbApplication.class, args);
    }


    @Bean
    public ApplicationRunner runner(FTPConfiguration.GateFile gateFile) {
        return args -> {
            List<File> files = gateFile.mget(".");
            for (File file : files) {
                System.out.println("Result:" + file.getAbsolutePath());
                run(file);
            }
        };
    }

    void run(File file) throws IOException {
        SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
        Pcap pcap = Pcap.openStream(file);
        pcap.loop(
                packet -> {
                    if (packet.hasProtocol(Protocol.TCP)) {
                        TCPPacket packet1 = (TCPPacket) packet.getPacket(Protocol.TCP);
                        String Time = formatter.format(new Date(packet1.getArrivalTime() / 1000));
                        String Source = packet1.getSourceIP();
                        String Destination = packet1.getDestinationIP();
                        String Protocol = packet1.getProtocol().toString();
                        Long Length = packet1.getTotalLength();
                        System.out.printf("%s | %s | %s | %s | %d \n", Time, Source, Destination, Protocol, Length);
                       } else if (packet.hasProtocol(Protocol.UDP)) {
                        UDPPacket packet1 = (UDPPacket) packet.getPacket(Protocol.UDP);
                        String Time = formatter.format(new Date(packet1.getArrivalTime() / 1000));
                        String Source = packet1.getSourceIP();
                        String Destination = packet1.getDestinationIP();
                        String Protocol = packet1.getProtocol().toString();
                        Long Length = packet1.getTotalLength();
                        System.out.printf("%s | %s | %s | %s | %d \n", Time, Source, Destination, Protocol, Length);
                    } else {
                        System.out.println("Not found protocol. | " + packet.getProtocol());
                    }
                   return packet.getNextPacket() != null;
                }
        );
    }
}

I want to get these data into a JSON Array. So that the output should be like the following.

[
  {"Destination":"116.50.76.245","Length":119,"Time":"03:41:08","Protocol":"udp","Source":"4.2.2.2"},
  {"Destination":"10.64.33.73","Length":92,"Time":"03:41:06","Protocol":"tcp","Source":"91.198.174.192"},
  {"Destination":"4.2.2.2","Length":74,"Time":"03:41:08","Protocol":"udp","Source":"10.64.43.166"},
  {"Destination":"4.2.2.2","Length":74,"Time":"03:41:08","Protocol":"udp","Source":"10.64.43.166"}
]

Can anyone help me to do this?

1 Answer 1

1

Just create an instance of JSONArray and pass it to run method, and create an instance of JSONObject inside packet -> {} method body, and populate and push the JSONObject to passed instance of JSONArray. Below is the minimal code:

@Bean
public ApplicationRunner runner(FTPConfiguration.GateFile gateFile) {
    ...
    JSONArray arr = new JSONArray();
    for (File file : files) {
        System.out.println("Result:" + file.getAbsolutePath());
        run(file, arr);
    }
    ...
}

void run(File file, JSONArray arr) throws IOException {
    ...
        packet -> {
            ...
            if (packet.hasProtocol(Protocol.TCP)) {
                ...
                JSONOBject obj = new JSONOBject();
                obj.put("Desitnation", destination);
                // set other properties
                arr.add(obj);
                ...
            }
            // same for else if block
            ...
        }
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.