From cdb36b39d29efd774a2b4bbe524181a87b34adb0 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 13 Dec 2016 12:00:36 +0100 Subject: [PATCH] Implement the missing queries --- .../de/hsh/inform/orientdb_project/Main.java | 14 ++++++++ .../model/EthernetFrameModel.java | 17 +++++----- .../orientdb/OrientDbHelperService.java | 6 +++- .../repository/EthernetFrameRepository.java | 34 ++++++++++--------- .../repository/HostRepository.java | 20 ++++++----- 5 files changed, 57 insertions(+), 34 deletions(-) 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 04a4c17..63e9e80 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/Main.java +++ b/src/main/java/de/hsh/inform/orientdb_project/Main.java @@ -4,9 +4,11 @@ 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; @@ -36,6 +38,18 @@ public class Main { System.out.println(hm); } + for(HostModel hm : hr.findAllByConnectionsToOutsideHosts()) { + System.out.println(hm); + } + + EthernetFrameRepository efr = new EthernetFrameRepository(odhs.getDatabaseDocument()); + List efrbyteResult = efr.findAllByRawData(new byte[] { + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF + }); + for(EthernetFrameModel em : efrbyteResult) { + System.out.println(em); + } + // Done odhs.close(); } 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 49d0d78..1e8b81b 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 @@ -3,6 +3,7 @@ package de.hsh.inform.orientdb_project.model; import org.pcap4j.packet.EthernetPacket; import com.orientechnologies.orient.core.metadata.schema.OType; +import com.orientechnologies.orient.core.record.impl.ODocument; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import com.tinkerpop.blueprints.impls.orient.OrientVertexType; @@ -47,14 +48,14 @@ public class EthernetFrameModel { this.microseconds = ms; } - public EthernetFrameModel(Vertex v) { - this.ts = v.getProperty("timestamp"); - this.ms = v.getProperty("microseconds"); - this.sourceMac = v.getProperty("sourceMac"); - this.targetMac = v.getProperty("targetMac"); - this.rawData = v.getProperty("rawData"); - this.size = v.getProperty("size"); - this.payloadSize = v.getProperty("payloadSize"); + public EthernetFrameModel(ODocument doc) { + this.ts = doc.field("timestamp"); + this.ms = doc.field("microseconds"); + this.sourceMac = doc.field("sourceMac"); + this.targetMac = doc.field("targetMac"); + this.rawData = doc.field("rawData"); + this.size = doc.field("size"); + this.payloadSize = doc.field("payloadSize"); this.timestamp = ts; this.microseconds = ms; } diff --git a/src/main/java/de/hsh/inform/orientdb_project/orientdb/OrientDbHelperService.java b/src/main/java/de/hsh/inform/orientdb_project/orientdb/OrientDbHelperService.java index 7360a11..8857c70 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/orientdb/OrientDbHelperService.java +++ b/src/main/java/de/hsh/inform/orientdb_project/orientdb/OrientDbHelperService.java @@ -1,9 +1,9 @@ package de.hsh.inform.orientdb_project.orientdb; import java.io.IOException; -import java.util.List; import com.orientechnologies.orient.client.remote.OServerAdmin; +import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; import com.orientechnologies.orient.core.intent.OIntentMassiveInsert; import com.tinkerpop.blueprints.impls.orient.OrientConfigurableGraph.THREAD_MODE; import com.tinkerpop.blueprints.impls.orient.OrientEdgeType; @@ -38,6 +38,10 @@ public class OrientDbHelperService { this.factory = null; } + public ODatabaseDocumentTx getDatabaseDocument() { + return this.factory.getDatabase(); + } + public OrientGraphFactory getOrientGraphFactory() { if(this.factory == null) { diff --git a/src/main/java/de/hsh/inform/orientdb_project/repository/EthernetFrameRepository.java b/src/main/java/de/hsh/inform/orientdb_project/repository/EthernetFrameRepository.java index 3a2682a..bd4862c 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/repository/EthernetFrameRepository.java +++ b/src/main/java/de/hsh/inform/orientdb_project/repository/EthernetFrameRepository.java @@ -3,30 +3,32 @@ package de.hsh.inform.orientdb_project.repository; import java.util.ArrayList; import java.util.List; -import com.tinkerpop.blueprints.GraphQuery; -import com.tinkerpop.blueprints.Vertex; -import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; +import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; +import com.orientechnologies.orient.core.iterator.ORecordIteratorClass; +import com.orientechnologies.orient.core.record.impl.ODocument; import de.hsh.inform.orientdb_project.model.EthernetFrameModel; public class EthernetFrameRepository { - private OrientGraphNoTx ogf; + private ODatabaseDocumentTx db; - public EthernetFrameRepository(OrientGraphNoTx ogf) { - this.ogf = ogf; + public EthernetFrameRepository(ODatabaseDocumentTx oDatabaseDocumentTx) { + this.db = oDatabaseDocumentTx; } - public List findAllByRawData(byte[] content) { - GraphQuery gq = this.ogf.query(); - gq = gq.has("@class", "EthernetFrame"); - // TODO - return this.getListFromVertices(gq.vertices()); - } - - private List getListFromVertices(Iterable vertices) { + public List findAllByRawData(byte[] needle) { + ORecordIteratorClass resultIterator = db.browseClass("EthernetFrame"); List result = new ArrayList(); - for(Vertex v : vertices) { - result.add(new EthernetFrameModel(v)); + for(ODocument doc : resultIterator) { + int found = -1; + byte[] rawData = (byte[]) doc.field("rawData"); + // Manually compare bytes... yay! \o/ + String bigStr = new String(rawData); + String smallStr = new String(needle); + found = bigStr.indexOf(smallStr); + if(found != -1) { + result.add(new EthernetFrameModel(doc)); + } } return result; } diff --git a/src/main/java/de/hsh/inform/orientdb_project/repository/HostRepository.java b/src/main/java/de/hsh/inform/orientdb_project/repository/HostRepository.java index 9acc594..be5a833 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/repository/HostRepository.java +++ b/src/main/java/de/hsh/inform/orientdb_project/repository/HostRepository.java @@ -29,18 +29,20 @@ public class HostRepository { } public List findAllByConnectionsToOutsideHosts() { - GraphQuery gq = this.ogf.query(); - gq = gq.has("@class", "Host"); - // TODO - return this.getListFromVertices(gq.vertices()); + String sql = "" + + "SELECT EXPAND(DISTINCT(out)) FROM (SELECT out('hasSourceHost') AS out FROM TcpConnection WHERE out('hasTargetHost').internal = false);"; + @SuppressWarnings("unchecked") // We know. + Iterable vertices = (Iterable) this.ogf.command(new OCommandSQL(sql)).execute(); + return this.getListFromVertices(vertices); } - public List findByIncomingConnectionOnPort(int port) { - GraphQuery gq = this.ogf.query(); - gq = gq.has("@class", "Host"); - // TODO - return this.getListFromVertices(gq.vertices()); + public List findAllByIncomingConnectionOnWellKnownPort() { + String sql = "" + + "SELECT EXPAND(DISTINCT(out)) FROM (SELECT out('hasTargetHost') FROM TcpConnection WHERE targetPort IN (SELECT port FROM WellKnownPort))"; + @SuppressWarnings("unchecked") // We know. + Iterable vertices = (Iterable) this.ogf.command(new OCommandSQL(sql)).execute(); + return this.getListFromVertices(vertices); } private List getListFromVertices(Iterable vertices) {