diff --git a/pom.xml b/pom.xml index 4a03337..bc8644a 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,12 @@ gremlin-core 3.2.3 + + + commons-cli + commons-cli + 1.3.1 + diff --git a/src/main/java/de/hsh/inform/orientdb_project/CommandLineInterface.java b/src/main/java/de/hsh/inform/orientdb_project/CommandLineInterface.java new file mode 100644 index 0000000..d5c239a --- /dev/null +++ b/src/main/java/de/hsh/inform/orientdb_project/CommandLineInterface.java @@ -0,0 +1,121 @@ +package de.hsh.inform.orientdb_project; + +import java.math.BigInteger; +import java.util.List; +import java.util.Scanner; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; + +import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; + +import de.hsh.inform.orientdb_project.model.EthernetFrameModel; +import de.hsh.inform.orientdb_project.model.Model; +import de.hsh.inform.orientdb_project.orientdb.OrientDbHelperService; +import de.hsh.inform.orientdb_project.repository.EthernetFrameRepository; +import de.hsh.inform.orientdb_project.repository.HostRepository; +import de.hsh.inform.orientdb_project.repository.TcpConnectionRepository; + +public class CommandLineInterface { + private static final Logger log = Logger.getLogger(CommandLineInterface.class.getName()); + private Options options = new Options(); + + private OrientDbHelperService odhs; + + private OrientGraphNoTx ogf; + + private TcpConnectionRepository tcpConnectionRepository; + private HostRepository hostRepository; + private EthernetFrameRepository ethernetFrameRepository; + + private boolean keepGoing; + + public CommandLineInterface(OrientDbHelperService odhs) { + this.odhs = odhs; + this.ogf = odhs.getOrientGraphNoTx(); + this.tcpConnectionRepository = new TcpConnectionRepository(this.ogf); + this.hostRepository = new HostRepository(this.ogf); + this.ethernetFrameRepository = new EthernetFrameRepository(this.odhs.getDatabaseDocument()); + + options.addOption("e", "ethernetFramesByBytes", false, "Find ethernet frames that contain a given byte sequence."); + + options.addOption("htoipp", "hostsByIpAndPort", false, "Find hosts that have tcp connections to a given ip address and port."); + options.addOption("htoex", "hostsByConnToExternalHosts", false, "Find hosts that have tcp connections to external hosts."); + options.addOption("hinw", "hostsWithIncomingOnWellKnownPorts", false, "Find hosts that have incoming tcp connections on well known ports."); + + options.addOption("ta", "tcpConnectionActiveAt", false, "Find tcp connections that were active at a given timestamp."); + options.addOption("tbpm", "tcpConnectionBytesPerMinuteBetween", false, "Get datavolume (bytes per minute) between two given ip addresses."); + + options.addOption("h", "help", false, "show help."); + options.addOption("q", "quit", false, "quit the program."); + } + + public void parse(String arguments) { + String[] args = arguments.split(" "); + CommandLineParser parser = new DefaultParser(); + CommandLine cmd = null; + try { + cmd = parser.parse(options, args); + if(cmd.hasOption("h")) + this.help(); + if(cmd.hasOption("q")) + this.quit(); + + if(cmd.hasOption("e")) { + String allBytes = ""; + for(String byteValue : cmd.getArgs()) { + allBytes += byteValue; + } + byte[] needle = new BigInteger(allBytes, 16).toByteArray(); + + List result = this.ethernetFrameRepository.findAllByRawData(needle); + System.out.println("EthernetFrames that contain the given bytes:"); + this.printResults(result); + } + + if(cmd.hasOption("htoipp")) { + + } + } catch (ParseException e) { + //log.log(Level.SEVERE, "Failed to parse comand line properties", e); + log.log(Level.SEVERE, "I did not understand that. Sorry."); + this.help(); + } + } + + private void printResults(List result) { + for(Object o : result) { + System.out.println(o); + } + System.out.println("End of result list."); + } + + private void quit() { + this.keepGoing = false; + System.out.println("Bye bye."); + } + + private void help() { + HelpFormatter formater = new HelpFormatter(); + formater.printHelp(" ", options); + } + + public void run() { + this.keepGoing = true; + Scanner s = new Scanner(System.in); + while(this.keepGoing) { + System.out.print("> "); + String arguments = s.nextLine(); + System.out.println(""); + this.parse(arguments); + } + System.out.println("End of Program"); + s.close(); + } +} diff --git a/src/main/java/de/hsh/inform/orientdb_project/Main.java b/src/main/java/de/hsh/inform/orientdb_project/Main.java index 63e9e80..1a5fb4d 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/Main.java +++ b/src/main/java/de/hsh/inform/orientdb_project/Main.java @@ -1,16 +1,8 @@ package de.hsh.inform.orientdb_project; -import java.util.List; - import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; -import de.hsh.inform.orientdb_project.model.EthernetFrameModel; -import de.hsh.inform.orientdb_project.model.HostModel; -import de.hsh.inform.orientdb_project.model.TcpConnectionModel; import de.hsh.inform.orientdb_project.orientdb.OrientDbHelperService; -import de.hsh.inform.orientdb_project.repository.EthernetFrameRepository; -import de.hsh.inform.orientdb_project.repository.HostRepository; -import de.hsh.inform.orientdb_project.repository.TcpConnectionRepository; import de.hsh.inform.orientdb_project.util.ConfigPropertiesReader; public class Main { @@ -22,8 +14,12 @@ public class Main { System.out.println("Using database: " + odhs.getDbUri(true)); // Get "handle" for database to pass to import service - OrientGraphNoTx ogf = odhs.getOrientGraphNoTx(); + //OrientGraphNoTx ogf = odhs.getOrientGraphNoTx(); + CommandLineInterface cli = new CommandLineInterface(odhs); + cli.run(); + + /* TcpConnectionRepository tcr = new TcpConnectionRepository(ogf); List result = tcr.findByActiveWhen(901713642); for(TcpConnectionModel m : result) { @@ -49,6 +45,7 @@ public class Main { for(EthernetFrameModel em : efrbyteResult) { System.out.println(em); } + */ // Done odhs.close(); diff --git a/src/main/java/de/hsh/inform/orientdb_project/model/ArpPacketModel.java b/src/main/java/de/hsh/inform/orientdb_project/model/ArpPacketModel.java index 503cbcb..2cdfdbd 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/model/ArpPacketModel.java +++ b/src/main/java/de/hsh/inform/orientdb_project/model/ArpPacketModel.java @@ -7,7 +7,7 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import com.tinkerpop.blueprints.impls.orient.OrientVertexType; // TODO: Not finished? -public class ArpPacketModel { +public class ArpPacketModel implements Model { public long ts; public int ms; diff --git a/src/main/java/de/hsh/inform/orientdb_project/model/EthernetFrameModel.java b/src/main/java/de/hsh/inform/orientdb_project/model/EthernetFrameModel.java index 1e8b81b..c0b6694 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/model/EthernetFrameModel.java +++ b/src/main/java/de/hsh/inform/orientdb_project/model/EthernetFrameModel.java @@ -8,7 +8,7 @@ import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import com.tinkerpop.blueprints.impls.orient.OrientVertexType; -public class EthernetFrameModel { +public class EthernetFrameModel implements Model { public long ts; public int ms; diff --git a/src/main/java/de/hsh/inform/orientdb_project/model/HostModel.java b/src/main/java/de/hsh/inform/orientdb_project/model/HostModel.java index 6d3e5e1..17245fd 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/model/HostModel.java +++ b/src/main/java/de/hsh/inform/orientdb_project/model/HostModel.java @@ -5,7 +5,7 @@ import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import com.tinkerpop.blueprints.impls.orient.OrientVertexType; -public class HostModel { +public class HostModel implements Model { public String ipAddress; public boolean internal; diff --git a/src/main/java/de/hsh/inform/orientdb_project/model/IcmpPacketModel.java b/src/main/java/de/hsh/inform/orientdb_project/model/IcmpPacketModel.java index 4add045..1bf6651 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/model/IcmpPacketModel.java +++ b/src/main/java/de/hsh/inform/orientdb_project/model/IcmpPacketModel.java @@ -6,7 +6,7 @@ import com.orientechnologies.orient.core.metadata.schema.OType; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import com.tinkerpop.blueprints.impls.orient.OrientVertexType; -public class IcmpPacketModel { +public class IcmpPacketModel implements Model { public long ts; public int ms; diff --git a/src/main/java/de/hsh/inform/orientdb_project/model/IpPacketModel.java b/src/main/java/de/hsh/inform/orientdb_project/model/IpPacketModel.java index b3a6124..bf5cf15 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/model/IpPacketModel.java +++ b/src/main/java/de/hsh/inform/orientdb_project/model/IpPacketModel.java @@ -6,7 +6,7 @@ import com.orientechnologies.orient.core.metadata.schema.OType; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import com.tinkerpop.blueprints.impls.orient.OrientVertexType; -public class IpPacketModel { +public class IpPacketModel implements Model { public long ts; public int ms; diff --git a/src/main/java/de/hsh/inform/orientdb_project/model/Model.java b/src/main/java/de/hsh/inform/orientdb_project/model/Model.java new file mode 100644 index 0000000..f5d6c36 --- /dev/null +++ b/src/main/java/de/hsh/inform/orientdb_project/model/Model.java @@ -0,0 +1,5 @@ +package de.hsh.inform.orientdb_project.model; + +public interface Model { + +} diff --git a/src/main/java/de/hsh/inform/orientdb_project/model/TcpConnectionModel.java b/src/main/java/de/hsh/inform/orientdb_project/model/TcpConnectionModel.java index 9686d67..bac891e 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/model/TcpConnectionModel.java +++ b/src/main/java/de/hsh/inform/orientdb_project/model/TcpConnectionModel.java @@ -10,7 +10,7 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import com.tinkerpop.blueprints.impls.orient.OrientVertexType; -public class TcpConnectionModel { +public class TcpConnectionModel implements Model { public long startTs; public int startMs; diff --git a/src/main/java/de/hsh/inform/orientdb_project/model/TcpPacketModel.java b/src/main/java/de/hsh/inform/orientdb_project/model/TcpPacketModel.java index 68d249c..7d4a8c9 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/model/TcpPacketModel.java +++ b/src/main/java/de/hsh/inform/orientdb_project/model/TcpPacketModel.java @@ -6,7 +6,7 @@ import com.orientechnologies.orient.core.metadata.schema.OType; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import com.tinkerpop.blueprints.impls.orient.OrientVertexType; -public class TcpPacketModel { +public class TcpPacketModel implements Model { public long ts; public int ms; diff --git a/src/main/java/de/hsh/inform/orientdb_project/model/UdpPacketModel.java b/src/main/java/de/hsh/inform/orientdb_project/model/UdpPacketModel.java index 64338d8..449b744 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/model/UdpPacketModel.java +++ b/src/main/java/de/hsh/inform/orientdb_project/model/UdpPacketModel.java @@ -6,7 +6,7 @@ import com.orientechnologies.orient.core.metadata.schema.OType; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import com.tinkerpop.blueprints.impls.orient.OrientVertexType; -public class UdpPacketModel { +public class UdpPacketModel implements Model { public long ts; public int ms; diff --git a/src/main/java/de/hsh/inform/orientdb_project/model/WellKnownPortModel.java b/src/main/java/de/hsh/inform/orientdb_project/model/WellKnownPortModel.java index c2a676a..21243dd 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/model/WellKnownPortModel.java +++ b/src/main/java/de/hsh/inform/orientdb_project/model/WellKnownPortModel.java @@ -4,7 +4,7 @@ import com.orientechnologies.orient.core.metadata.schema.OType; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import com.tinkerpop.blueprints.impls.orient.OrientVertexType; -public class WellKnownPortModel { +public class WellKnownPortModel implements Model { public int port; public String description;