Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added real-time packet sniffing feature to the CLI using TrafficFlowWorker #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,19 @@ task exeCMD(type: JavaExec){
}else{
jvmArgs '-Djava.library.path=jnetpcap/linux/jnetpcap-1.4.r1425'
}
//args = ["/home/yzhang29/0a/Capture/", "/home/yzhang29/0a/Capture/out/"]
args = ["/home/enigma/work/tsyp/3asba/CICFlowMeter/cap/", "/home/enigma/work/tsyp/3asba/CICFlowMeter/out/"]
}

task exeRealCMD(type: JavaExec){
main = "cic.cs.unb.ca.ifm.RealCmd" //main class
classpath = sourceSets.main.runtimeClasspath
String osName = System.getProperty('os.name').toLowerCase()
if(osName.contains('windows')){
jvmArgs '-Djava.library.path=jnetpcap/win/jnetpcap-1.4.r1425'
}else{
jvmArgs '-Djava.library.path=jnetpcap/linux/jnetpcap-1.4.r1425'
}
args = ["wlp49s0", "/home/enigma/work/tsyp/3asba/CICFlowMeter/out/"]
}


Expand Down
87 changes: 87 additions & 0 deletions src/main/java/cic/cs/unb/ca/ifm/RealCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package cic.cs.unb.ca.ifm;

import cic.cs.unb.ca.flow.FlowMgr;
import cic.cs.unb.ca.jnetpcap.*;
import cic.cs.unb.ca.jnetpcap.worker.FlowGenListener;
import cic.cs.unb.ca.jnetpcap.worker.TrafficFlowWorker;
import org.apache.commons.io.FilenameUtils;
import org.jnetpcap.PcapClosedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cic.cs.unb.ca.jnetpcap.worker.InsertCsvRow;
import swing.common.SwingUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static cic.cs.unb.ca.Sys.FILE_SEP;

public class RealCmd {

public static final Logger logger = LoggerFactory.getLogger(Cmd.class);
private static final String DividingLine = "-------------------------------------------------------------------------------";
private static String[] animationChars = new String[]{"|", "/", "-", "\\"};

public static void main(String[] args) {

long flowTimeout = 120000000L;
long activityTimeout = 5000000L;
String outPath;

// Checking if the interface argument is provided
if (args.length < 1) {
logger.info("Please select a network interface!");
return;
}

String networkInterface = args[0]; // Network interface name
logger.info("Selected interface: {}", networkInterface);

// Select output path for CSV
if (args.length < 2) {
logger.info("Please select output folder!");
return;
}
outPath = args[1];
File out = new File(outPath);
if (out == null || out.isFile()) {
logger.info("The output folder does not exist! -> {}", outPath);
return;
}

logger.info("Output folder: {}", outPath);

// Start the TrafficFlowWorker on the selected network interface
listenOnNetworkInterface(networkInterface, outPath, flowTimeout, activityTimeout);
}

private static void listenOnNetworkInterface(String networkInterface, String outPath, long flowTimeout, long activityTimeout) {
// Start capturing traffic on the provided network interface
TrafficFlowWorker worker = new TrafficFlowWorker(networkInterface);

// Attach a listener to handle flows and save them
worker.addPropertyChangeListener(event -> {
if (TrafficFlowWorker.PROPERTY_FLOW.equalsIgnoreCase(event.getPropertyName())) {
BasicFlow flow = (BasicFlow) event.getNewValue();
String flowDump = flow.dumpFlowBasedFeaturesEx();
List<String> flowStringList = new ArrayList<>();
flowStringList.add(flowDump);
InsertCsvRow.insert(FlowFeature.getHeader(), flowStringList, outPath, networkInterface + FlowMgr.FLOW_SUFFIX);


logger.info("Flow: {}", flowDump);
}
});

try {
worker.execute(); // Start the worker
worker.get();
} catch (Exception e) {
logger.error("Error during network interface capture: ", e);
}

logger.info("Network capture on {} completed.", networkInterface);
}

}