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

Adds exit codes for detections/errors and machine-readable CSV output with an option #59

Open
wants to merge 3 commits 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
2 changes: 1 addition & 1 deletion src/main/java/me/cortex/jarscanner/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static void createAndDisplayGui() {
};

Results run = Main.run(4, searchDir, true, logOutput);
Main.outputRunResults(run, logOutput);
Main.outputRunResults(run, logOutput, null);
textArea.append("Done scanning!");
} catch (Exception ex) {
if (ex instanceof InterruptedException || ex instanceof RejectedExecutionException) {
Expand Down
40 changes: 36 additions & 4 deletions src/main/java/me/cortex/jarscanner/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Main {
* Main method. Checks arguments and scans the specified directory for malicious code signatures. Outputs the
* results of the scan to the console.
*
* @param args the command line arguments (number of threads, directory to scan, and whether to emit walk errors)
* @param args the command line arguments (number of threads, directory to scan, whether to emit walk errors, and whether to output machine-readable CSV)
*/
public static void main(String[] args) {
// Check arguments
Expand All @@ -45,16 +45,32 @@ public static void main(String[] args) {
int nThreads = Integer.parseInt(args[0]);
Path dirToCheck = new File(args[1]).toPath();
boolean emitWalkErrors = false;
boolean machineReadableOutput = false;
if (args.length > 2) {
emitWalkErrors = Boolean.parseBoolean(args[2]);
}

if (args.length > 3) {
machineReadableOutput = Boolean.parseBoolean(args[3]);
}

// Create log output function
Function<String, String> logOutput = outputString -> {
System.out.println(outputString);
return outputString;
};

// Create CSV logging function
Function<String, String> logCSV = null;

//make this not null if CSV output requested
if(machineReadableOutput) {
logCSV = outputString -> {
System.err.println(outputString);
return outputString;
};
}

// Output scan start
logOutput.apply("Starting scan...");

Expand All @@ -65,22 +81,26 @@ public static void main(String[] args) {
} catch (IOException e) {
logOutput.apply("An error occurred while scanning the directory: " + dirToCheck);
e.printStackTrace();
System.exit(1); //exit code for actual errors, distinct from detections
} catch (InterruptedException e) {
logOutput.apply("An error occurred while waiting for the scan to complete.");
e.printStackTrace();
System.exit(1);
}

// Output scan completion and results
outputRunResults(results, logOutput);
outputRunResults(results, logOutput, logCSV);
}

/**
* Output the results of a scan to the specified log output function.
*
* @param results the resulting from {@link #run(int, Path, boolean, Function)}.
* @param logOutput the function to use for logging output
* @param logOutput the function to use for logging human-readable output
* @param logCSV the function to use for logging machine-readable CSV output, pass null for no CSV output
*
*/
public static void outputRunResults(Results results, Function<String, String> logOutput) {
public static void outputRunResults(Results results, Function<String, String> logOutput, Function<String, String> logCSV) {
if (results == null) {
logOutput.apply("Scan failed. Unable to display results.");
} else {
Expand All @@ -106,6 +126,18 @@ public static void outputRunResults(Results results, Function<String, String> lo
logOutput.apply(Constants.ANSI_RED + "[" + stage2InfectionNumber + "] " + Constants.ANSI_WHITE + stage2Infection + Constants.ANSI_RESET);
}
}
if(logCSV != null) //if CSV output isn't wanted, just pass in null
{
for (int i = 0; i < stage1Detections.size(); i++) {
String stage1Infection = stage1Detections.get(i);
logCSV.apply("1," + stage1Infection);
}
for (int i = 0; i < stage2Detections.size(); i++) {
String stage2Infection = stage2Detections.get(i);
logCSV.apply("2," + stage2Infection);
}
}
System.exit(2); //nonzero exit code to indicate infections found
}
}
}
Expand Down