This repository has been archived by the owner on Apr 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
264 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package CEP.PortScanDetector; | ||
|
||
import Utilities.*; | ||
import com.espertech.esper.compiler.client.*; | ||
import com.espertech.esper.runtime.client.*; | ||
import org.pcap4j.packet.namednumber.*; | ||
|
||
import java.io.*; | ||
import java.net.*; | ||
|
||
public class BlockPortScanCEP { | ||
public BlockPortScanCEP(int alertPeriod, int interval) throws EPCompileException, EPDeployException, IOException, EPCompileException, EPDeployException { | ||
|
||
new EPAdapter().execute("add-horizontal-port-scan", "insert into BlockPortScanAlert\n" + | ||
"select hostPort from HorizontalPortScanAlert#expr(oldest_timestamp > newest_timestamp - 1000)"); | ||
|
||
new EPAdapter().execute("add-vertical-port-scan", "insert into BlockPortScanAlert\n" + | ||
"select hostAddr from VerticalPortScanAlert#expr(oldest_timestamp > newest_timestamp - 1000)"); | ||
|
||
new EPAdapter().execute("alert-block-port-scan", "select * from BlockPortScanAlert#time( " + alertPeriod + " seconds)#expr(oldest_timestamp > newest_timestamp - 1000)\n" + | ||
"where exists(select * from HorizontalPortScanAlert)\n" + | ||
"and exists(select * from VerticalPortScanAlert)\n" | ||
).addListener((newData, __, ___, ____) -> System.out.println("Alert a block scan:" | ||
+ " is happened!")); | ||
// new EPAdapter().execute("alert-block-port-scan", "on BlockPortScanAlert delete from BlockPortScanAlert\n"); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/CEP/PortScanDetector/BlockPortScanEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package CEP.PortScanDetector; | ||
|
||
import org.pcap4j.packet.namednumber.*; | ||
|
||
import java.net.*; | ||
|
||
public class BlockPortScanEvent { | ||
InetAddress hostAddr; | ||
Port hostPort; | ||
Long timestamp; | ||
|
||
public BlockPortScanEvent(InetAddress hostAddr) { | ||
this.hostAddr = hostAddr; | ||
} | ||
|
||
public BlockPortScanEvent(Port hostPort) { | ||
this.hostPort = hostPort; | ||
} | ||
|
||
public BlockPortScanEvent(Long timestamp) { | ||
this.timestamp = timestamp; | ||
} | ||
|
||
public InetAddress getHostAddr() { | ||
return hostAddr; | ||
} | ||
public Port getHostPort() { | ||
return hostPort; | ||
} | ||
public Long getTimestamp() { return timestamp; } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package CEP.PortScanDetector; | ||
|
||
import CEP.PortScanDetector.*; | ||
import Utilities.EPAdapter; | ||
import org.pcap4j.core.*; | ||
import org.pcap4j.packet.*; | ||
import org.pcap4j.util.*; | ||
|
||
import java.io.IOException; | ||
|
||
public class Detector { | ||
private static final int snapshotLength = 65536; // in bytes | ||
private static final int readTimeout = 100; // in milliseconds | ||
private static final int maxPackets = -1; | ||
private static final String filter = "tcp"; | ||
|
||
public static void main (String [] args) throws Exception { | ||
System.out.println("Please wait while I'm configuring the Port Scan... "); | ||
new VerticalPortScanCEP(20, 100); | ||
new HorizontalPortScanCEP(60, 2, 10); // set to 2 to test, use 5 or more in production | ||
new BlockPortScanCEP(20,10); | ||
|
||
PcapNetworkInterface device = getNetworkDevice(); | ||
System.out.println(device.getName() + "(" + device.getDescription() + ")"); | ||
System.out.println("You chose: " + device); | ||
|
||
// New code below here | ||
if (device == null) { | ||
System.out.println("No device chosen."); | ||
System.exit(1); | ||
} | ||
|
||
final PcapHandle handle = device.openLive(snapshotLength, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, readTimeout); | ||
|
||
// Set a filter to only listen for tcp packets on port 80 (HTTP) | ||
if (filter.length() != 0) { | ||
handle.setFilter(filter, BpfProgram.BpfCompileMode.OPTIMIZE); | ||
} | ||
|
||
// Tell the handle to loop using the listener we created | ||
handle.loop(maxPackets, (PacketListener) packet -> { | ||
|
||
try { | ||
IpV4Packet ipV4Packet = packet.get(IpV4Packet.class); | ||
TcpPacket tcpPacket = ipV4Packet.get(TcpPacket.class); | ||
int port = tcpPacket.getHeader().getSrcPort().valueAsInt(); | ||
TCPPacket evt = new TCPPacket( | ||
ipV4Packet.getHeader(), | ||
tcpPacket.getHeader() | ||
); | ||
if (port != 443 && port != 80 && port != 62078 && port != 22) { | ||
sendEvent(evt, TCPPacket.class.getSimpleName()); | ||
} | ||
} catch (Exception ignored) { | ||
|
||
} | ||
}); | ||
// Cleanup when complete | ||
handle.close(); | ||
} | ||
|
||
static <EventType> void sendEvent(EventType event, String eventType) { | ||
EPAdapter.runtime.getEventService().sendEventBean(event, eventType); | ||
} | ||
|
||
static PcapNetworkInterface getNetworkDevice() { | ||
PcapNetworkInterface device = null; | ||
try { | ||
device = new NifSelector().selectNetworkInterface(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return device; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/CEP/PortScanDetector/HorizontalPortScanCEP.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package CEP.PortScanDetector; | ||
|
||
import Utilities.*; | ||
import com.espertech.esper.compiler.client.*; | ||
import com.espertech.esper.runtime.client.*; | ||
import org.pcap4j.packet.namednumber.*; | ||
|
||
import java.io.*; | ||
|
||
public class HorizontalPortScanCEP { | ||
public HorizontalPortScanCEP(int alertPeriod, int consecutiveFailed, int interval) throws EPCompileException, EPDeployException, IOException, EPCompileException, EPDeployException { | ||
|
||
new EPAdapter().execute("get-horizontal-port-scan", "insert into HorizontalPortScanAlert\n" + | ||
"select tcpHeader.srcPort\n" + | ||
"from TCPPacket#time(" + alertPeriod + " seconds)#expr(oldest_timestamp > newest_timestamp - 10000)\n" + | ||
"group by tcpHeader.srcPort\n" + | ||
"having count(distinct ipHeader.dstAddr) >= " + consecutiveFailed + | ||
"output first every " + interval + " seconds" ); | ||
|
||
new EPAdapter().execute("alert-horizontal-port-scan", "select * from HorizontalPortScanAlert") | ||
.addListener((newData, __, ___, ____) -> { | ||
Port hostAddr = (Port) newData[0].get("hostPort"); | ||
System.out.println("Alert a horizontal scan: Port " + hostAddr.valueAsInt() | ||
+ " is under attack!"); | ||
}); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/CEP/PortScanDetector/HorizontalPortScanEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package CEP.PortScanDetector; | ||
|
||
import org.pcap4j.packet.namednumber.*; | ||
|
||
public class HorizontalPortScanEvent { | ||
Port hostPort; | ||
|
||
public HorizontalPortScanEvent(Port hostPort) { | ||
this.hostPort = hostPort; | ||
} | ||
|
||
public Port getHostPort() { | ||
return hostPort; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package CEP.PortScanDetector; | ||
|
||
import org.pcap4j.packet.*; | ||
|
||
public class TCPPacket { | ||
private TcpPacket.TcpHeader tcpHeader; | ||
private IpPacket.IpHeader ipHeader; | ||
|
||
public TCPPacket(IpPacket.IpHeader ipHeader, TcpPacket.TcpHeader tcpHeader) { | ||
this.ipHeader = ipHeader; | ||
this.tcpHeader = tcpHeader; | ||
} | ||
|
||
public IpPacket.IpHeader getIpHeader() { | ||
return ipHeader; | ||
} | ||
|
||
public TcpPacket.TcpHeader getTcpHeader() { | ||
return tcpHeader; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/CEP/PortScanDetector/VerticalPortScanCEP.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package CEP.PortScanDetector; | ||
|
||
|
||
import Utilities.*; | ||
import com.espertech.esper.compiler.client.*; | ||
import com.espertech.esper.runtime.client.*; | ||
|
||
import java.io.*; | ||
import java.net.*; | ||
|
||
public class VerticalPortScanCEP { | ||
public VerticalPortScanCEP(int alertPeriod, int consecutiveFailed) throws EPCompileException, EPDeployException, IOException, EPCompileException, EPDeployException { | ||
|
||
new EPAdapter().execute("get-vertical-port-scan", "insert into VerticalPortScanAlert\n" + | ||
"select ipHeader.dstAddr\n" + | ||
"from TCPPacket#time_batch(" + alertPeriod + " seconds)#expr(oldest_timestamp > newest_timestamp - 10000)\n" + | ||
"group by ipHeader.dstAddr\n" + | ||
"having count(distinct tcpHeader.dstPort) > " + consecutiveFailed + ""); | ||
|
||
new EPAdapter().execute("alert-vertical-port-scan", "select * from VerticalPortScanAlert") | ||
.addListener((newData, __, ___, ____) -> { | ||
InetAddress hostAddr = (InetAddress) newData[0].get("hostAddr"); | ||
System.out.println("Alert a vertical scan: IP " + hostAddr.getHostAddress() | ||
+ " is under attack!"); | ||
}); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/CEP/PortScanDetector/VerticalPortScanEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package CEP.PortScanDetector; | ||
|
||
import java.net.*; | ||
|
||
public class VerticalPortScanEvent { | ||
InetAddress hostAddr; | ||
|
||
public VerticalPortScanEvent(InetAddress hostAddr) { | ||
this.hostAddr = hostAddr; | ||
} | ||
|
||
public InetAddress getHostAddr() { | ||
return hostAddr; | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file modified
0
src/test/java/CEP/WebserverMonitor/NeptuneErrorLogEventTest.java
100644 → 100755
Empty file.