如何在Java中实现PCA算法以对捕获的实时数据包进行特征提取(使用jpcap库?

问题描述 投票:0回答:1

[正如几篇研究论文[1] [2] [3]一样,使用主成分分析(PCA)算法进行特征提取似乎是合适的。为了实现这一目标,我使用jpcap库(下面的代码片段)在Java中制作了一个流量嗅探器程序,该程序能够从实时网络流量中提取各种功能。

但是我还没有找到任何实际实现此方法的方法,因为现有方法(下面的代码片段)似乎都可以在非实时的预先组织的数据集(例如ARFF文件的形式)上运行。

[我已经看到许多研究论文 [4] [5] [6],它们引用了结合jpcap组合库使用PCA算法的情况,说我无法找到关于这是怎么回事的解释。完成了吗?

嗅探器示例(使用Jpcap库)

Sniffer.java

import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.packet.Packet;
import jpcap.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.DatatypeConverter;
import java.util.List;
public class Sniffer {
    public static NetworkInterface[] NETWORK_INTERFACES;
    public static JpcapCaptor CAP;
    jpcap_thread THREAD;
    public static int INDEX = 0;
    public static int flag = 0;
    public static int COUNTER = 0;
    static boolean CaptureState = false;
    public static int No = 0;
    JpcapWriter writer = null;
    List<Packet> packetList = new ArrayList<>();
    public static ArrayList<Object[]> packetInfo = new ArrayList<>();
    public static void CapturePackets() {
        THREAD = new jpcap_thread() {
            public Object construct() {
                try {
                    CAP = JpcapCaptor.openDevice(NETWORK_INTERFACES[INDEX], 65535, false, 20);
                    // writer = JpcapWriter.openDumpFile(CAP, "captureddata");
                    if ("UDP".equals(filter_options.getSelectedItem().toString())) {
                        CAP.setFilter("udp", true);
                    } else if ("TCP".equals(filter_options.getSelectedItem().toString())) {
                        CAP.setFilter("tcp", true);
                    } else if ("ICMP".equals(filter_options.getSelectedItem().toString())) {
                        CAP.setFilter("icmp", true);
                    }
                    while (CaptureState) {
                        CAP.processPacket(1, new PacketContents());
                        packetList.add(CAP.getPacket());
                    }
                    CAP.close();
                } catch (Exception e) {
                    System.out.print(e);
                }
                return 0;
            }
            public void finished() {
                this.interrupt();
            }
        };
        THREAD.start();
    }
    public static void main(String[] args) {
        CaptureState = true;
        CapturePackets();
    }
    public void saveToFile() {
        THREAD = new jpcap_thread() {
            public Object construct() {
                writer = null;
                try {
                    CAP = JpcapCaptor.openDevice(NETWORK_INTERFACES[INDEX], 65535, false, 20);
                    writer = JpcapWriter.openDumpFile(CAP, "captured_data.txt");
                } catch (IOException ex) {
                    Logger.getLogger(Sniffer.class.getName()).log(Level.SEVERE, null, ex);
                }
                for (int i = 0; i < No; i++) {
                    writer.writePacket(packetList.get(i));
                }
                return 0;
            }
            public void finished() {
                this.interrupt();
            }
        };
        THREAD.start();
    }
}

PacketContents.java

import jpcap.PacketReceiver;
import jpcap.packet.Packet;
import javax.swing.table.DefaultTableModel;
import jpcap.packet.TCPPacket;
import jpcap.packet.UDPPacket;
import java.util.ArrayList;
import java.util.List;
import jpcap.packet.ICMPPacket;

public class PacketContents implements PacketReceiver {

    public static TCPPacket tcp;
    public static UDPPacket udp;
    public static ICMPPacket icmp;

    public void recievePacket(Packet packet) {
    }

    @Override
    public void receivePacket(Packet packet) {

        if (packet instanceof TCPPacket) {
            tcp = (TCPPacket) packet;

            Sniffer.packetInfo.add(new Object[] { sniffer.No, tcp.length, tcp.src_ip, tcp.dst_ip, "TCP", tcp.src_port,
                    tcp.dst_port, tcp.ack, tcp.ack_num, tcp.data, tcp.sequence, tcp.offset, tcp.header });

            sniffer.No++;

        } else if (packet instanceof UDPPacket) {

            udp = (UDPPacket) packet;

            Sniffer.packetInfo.add(new Object[] { sniffer.No, udp.length, udp.src_ip, udp.dst_ip, "UDP", udp.src_port,
                    udp.dst_port, udp.data, udp.offset, udp.header });

            sniffer.No++;

        } else if (packet instanceof ICMPPacket) {

            icmp = (ICMPPacket) packet;

            Sniffer.packetInfo.add(new Object[] { sniffer.No, icmp.length, icmp.src_ip, icmp.dst_ip, "ICMP",
                    icmp.checksum, icmp.header, icmp.offset, icmp.orig_timestamp, icmp.recv_timestamp,
                    icmp.trans_timestamp, icmp.data });

            sniffer.No++;

        }
    }
}

使用Weka库对ARFF文件执行PCA算法

WekaPCA.java

package project;

import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.core.converters.ConverterUtils;
import weka.core.converters.TextDirectoryLoader;
import java.io.File;
import org.math.plot.FrameView;
import org.math.plot.Plot2DPanel;
import org.math.plot.PlotPanel;
import org.math.plot.plots.ScatterPlot;
import weka.attributeSelection.PrincipalComponents;
import weka.attributeSelection.Ranker;

public class PCA {
    public static void main(String[] args) {
        try {
            // Load data
            String InputFilename = "kdd99.arff";

            ArffLoader loader = new ArffLoader();
            loader.setSource(new File(InputFilename));
            Instances data = loader.getDataSet();

            // Perform PCA
            PrincipalComponents pca = new PrincipalComponents();
            pca.setVarianceCovered(1.0);
            pca.setTransformBackToOriginal(false);
            pca.buildEvaluator(data);

            // Show transformed data
            Instances transformedData = pca.transformedData();
            System.out.println(transformedData);
        }
        catch (Exception e) {
            e.printStackTrace();
java weka pca packet pcap
1个回答
0
投票

您走在正确的轨道上。 Jpcap确实可以让您捕获ipv4和6软件包,您可以提取功能表。我认为您必须根据所引用的研究论文来实现算法逻辑。您可以使用以下链接来帮助您实现实施过程。

要捕获数据包,您可以使用以下教程:

https://www.codeproject.com/Tips/1062446/Using-Java-Codes-to-Capture-PIV-IPV-Packages

https://javatutorial.net/capture-network-packages-java

但是,算法将取决于您要分析的主要成分。有一个非常有用的库可能会帮助您进行分析。以下是链接:https://github.com/mkobos/pca_transform

© www.soinside.com 2019 - 2024. All rights reserved.