Skip to content

Commit

Permalink
Clean Sonar bugs and smells
Browse files Browse the repository at this point in the history
  • Loading branch information
amrabed committed Nov 28, 2018
1 parent 7a3484f commit 6c79cd9
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 164 deletions.
1 change: 0 additions & 1 deletion analyzer/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;

/**
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/edu/vt/rhids/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package edu.vt.rhids;

import edu.vt.rhids.main.RHIDS;
import edu.vt.rhids.util.Logger;

public class Main {

public static final RHIDS rhids = RHIDS.getInstance();

public static void main(String[] args) {
Logger.log(rhids.run(args), Logger.Verbosity.NONE);
}
}
61 changes: 21 additions & 40 deletions src/main/java/edu/vt/rhids/common/IndexMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,27 @@
* Lookup table for system call index
*
* @author AmrAbed
*
*/
public class IndexMap extends HashMap<String, Integer>
{
public class IndexMap extends HashMap<String, Integer> {
private static final long serialVersionUID = 1L;

private IndexMap()
{
private IndexMap() {
}

/**
* Build index map from count file
*
* @param reader
* @param reader BufferedReader of file to read from
*/
public IndexMap(BufferedReader reader)
{
try
{
public IndexMap(BufferedReader reader) {
try {
String line;
while ((line = reader.readLine()) != null)
{
while ((line = reader.readLine()) != null) {
String[] words = line.split("\t");
put(words[0], size());
}
reader.close();
}
catch (IOException e)
{
} catch (IOException e) {
Logger.log(e.getMessage(), Logger.Verbosity.NONE);
System.exit(-2);
}
Expand All @@ -48,46 +40,35 @@ public IndexMap(BufferedReader reader)
/**
* Build index map from first epoch
*
* @param reader
* @param epochSize
* @throws IOException
* @param reader BufferedReader of file to read from
* @param epochSize Epoch size
* @throws IOException if reading from file fails
*/
public IndexMap(BufferedReader reader, int epochSize) throws IOException
{
@SuppressWarnings("unused")
public IndexMap(BufferedReader reader, int epochSize) throws IOException {
IndexMap temp = new IndexMap();
for (int i = 0; i < epochSize; i++)
{
for (int i = 0; i < epochSize; i++) {
String syscall;
if ((syscall = SyscallParser.parse(reader)) != null)
{
if (temp.containsKey(syscall))
{
if ((syscall = SyscallParser.parse(reader)) != null) {
if (temp.containsKey(syscall)) {
temp.replace(syscall, temp.get(syscall) + 1);
}
else
{
} else {
temp.put(syscall, 1);
}
}
}

for (String syscall : temp.keySet())
{
if (temp.get(syscall) > 1)
{
for (String syscall : temp.keySet()) {
if (temp.get(syscall) > 1) {
put(syscall, size());
}
}
}

private int get(String syscall)
{
if (containsKey(syscall))
{
private int get(String syscall) {
if (containsKey(syscall)) {
return super.get(syscall);
}
else
{
} else {
return size();
}
}
Expand Down
28 changes: 11 additions & 17 deletions src/main/java/edu/vt/rhids/input/BoSC.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
package edu.vt.rhids.input;

import edu.vt.rhids.Main;

import java.util.ArrayList;
import java.util.Collections;

import edu.vt.rhids.main.RHIDS;

/**
* Bag of System Calls
*
* @author AmrAbed
*
* @author AmrAbed
*/
public class BoSC extends ArrayList<Byte>
{
public class BoSC extends ArrayList<Byte> {
private static final long serialVersionUID = 1L;

public BoSC(Window window)
{
super(Collections.nCopies(RHIDS.indexMap.size() + 1, (byte) 0));
for (String syscall : window)
{
int index = RHIDS.indexMap.get(syscall);
BoSC(Window window) {
super(Collections.nCopies(Main.rhids.getIndexMap().size() + 1, (byte) 0));
for (String syscall : window) {
int index = Main.rhids.getIndexMap().get(syscall);
Byte count = get(index);
set(index, ++count);
}
}

public BoSC(String string)
{
public BoSC(String string) {
string = string.replace("[", "");
string = string.replace("]", "");

for (String value : string.split(","))
{
for (String value : string.split(",")) {
add(Byte.parseByte(value.trim()));
}
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/edu/vt/rhids/input/SyscallParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package edu.vt.rhids.input;

import edu.vt.rhids.main.RHIDS;
import edu.vt.rhids.Main;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -22,19 +22,19 @@ public static String parse(BufferedReader reader) throws IOException {

Matcher matcher = Pattern.compile("STOP TRAINING").matcher(line);
if (matcher.find()) {
RHIDS.setDoneTraining(true);
Main.rhids.setDoneTraining(true);
return parse(reader);
}

matcher = Pattern.compile("START ATTACK").matcher(line);
if (matcher.find()) {
RHIDS.setUnderAttack(true);
Main.rhids.setUnderAttack(true);
return parse(reader);
}

matcher = Pattern.compile("END ATTACK").matcher(line);
if (matcher.find()) {
RHIDS.setUnderAttack(false);
Main.rhids.setUnderAttack(false);
return parse(reader);
}

Expand Down
20 changes: 11 additions & 9 deletions src/main/java/edu/vt/rhids/main/Classifier.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.vt.rhids.main;

import edu.vt.rhids.Main;
import edu.vt.rhids.common.Database;
import edu.vt.rhids.common.SimilarityVector;
import edu.vt.rhids.input.BoSC;
Expand Down Expand Up @@ -36,7 +37,7 @@ boolean trainUnconditionally() throws IOException {
BoSC bosc;
String syscall;

while (!RHIDS.isDoneTraining()) {
while (Main.rhids.isTrainingActive()) {
for (int i = 0; i < stats.getEpochSize(); i++) {
if ((syscall = SyscallParser.parse(reader)) != null) {
bosc = window.slide(syscall).getBoSC();
Expand All @@ -56,13 +57,14 @@ boolean trainUnconditionally() throws IOException {
return true;
}

@SuppressWarnings("unused")
public boolean train() throws IOException {
final SimilarityVector similarity = new SimilarityVector();
final Window window = new Window();
Database lastEpochChange = new Database(null);
String syscall;

while (!RHIDS.isDoneTraining()) {
while (Main.rhids.isTrainingActive()) {
Logger.log("\nEpoch " + stats.getTotalEpochs(), Verbosity.MEDIUM);
final Database currentEpochChange = new Database(null);
for (int i = 0; i < stats.getEpochSize(); i++) {
Expand Down Expand Up @@ -103,7 +105,7 @@ void test() throws IOException {

for (int i = 0; i < stats.getEpochSize(); i++) {
if ((syscall = SyscallParser.parse(reader)) != null) {
if (RHIDS.isUnderAttack()) {
if (Main.rhids.isUnderAttack()) {
isAnomalousEpoch = true;
}
final BoSC bosc = window.slide(syscall).getBoSC();
Expand Down Expand Up @@ -141,11 +143,11 @@ void test() throws IOException {
/**
* Test classifier in epochs
*
* @param reader
* @param epochSize
* @param testThreshold
* @param reader BufferedReader of file to read syscalls from
* @param epochSize Epoch size
* @param testThreshold Test threshold
* @return test results
* @throws IOException
* @throws IOException if reading from file fails
* @deprecated
*/
@Deprecated
Expand Down Expand Up @@ -189,9 +191,9 @@ public TestResult test(BufferedReader reader, double testThreshold, int epochSiz
/**
* Test classifier in sequences
*
* @param reader
* @param reader BufferedReader of file to read syscalls from
* @return percentage of mismatches
* @throws IOException
* @throws IOException if reading from file fails
* @deprecated
*/
@Deprecated
Expand Down
Loading

0 comments on commit 6c79cd9

Please sign in to comment.