Skip to content

Commit

Permalink
Moar inheritence
Browse files Browse the repository at this point in the history
  • Loading branch information
CoreyD97 committed Jan 25, 2023
1 parent 48667ce commit 70fa963
Show file tree
Hide file tree
Showing 39 changed files with 1,997 additions and 2,102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import burp.api.montoya.ui.contextmenu.ContextMenuEvent;
import burp.api.montoya.ui.contextmenu.ContextMenuItemsProvider;
import burp.api.montoya.ui.contextmenu.MessageEditorHttpRequestResponse;
import com.nccgroup.loggerplusplus.filter.colorfilter.ColorFilter;
import com.nccgroup.loggerplusplus.filter.logfilter.LogFilter;
import com.nccgroup.loggerplusplus.filter.parser.ParseException;
import com.nccgroup.loggerplusplus.filter.colorfilter.TableColorRule;
import com.nccgroup.loggerplusplus.logentry.LogEntryField;
import com.nccgroup.loggerplusplus.logview.logtable.LogTable;
import com.nccgroup.loggerplusplus.util.userinterface.dialog.ColorFilterDialog;
Expand Down Expand Up @@ -120,19 +118,13 @@ public void actionPerformed(ActionEvent actionEvent) {
filterMenu.add(addToCurrentFilter);
}

JMenuItem colorFilterItem = new JMenuItem(new AbstractAction("Set Selection as Color LogFilter") {
JMenuItem colorFilterItem = new JMenuItem(new AbstractAction("Set Selection as Color Filter") {
@Override
public void actionPerformed(ActionEvent actionEvent) {
try {
ColorFilter colorFilter = new ColorFilter();
colorFilter.setFilter(new LogFilter(LoggerPlusPlus.instance.getLibraryController(),
context.getFullLabel() + " CONTAINS \"" + selectedText + "\""));
HashMap<UUID,ColorFilter> colorFilters = LoggerPlusPlus.instance.getPreferencesController().getPreferences().getSetting(PREF_COLOR_FILTERS);
colorFilters.put(colorFilter.getUUID(), colorFilter);
new ColorFilterDialog(LoggerPlusPlus.instance.getLibraryController()).setVisible(true);
} catch (ParseException e) {
return;
}
TableColorRule tableColorRule = new TableColorRule("New Filter", context.getFullLabel() + " CONTAINS \"" + selectedText + "\"");
HashMap<UUID, TableColorRule> colorFilters = LoggerPlusPlus.instance.getPreferencesController().getPreferences().getSetting(PREF_COLOR_FILTERS);
colorFilters.put(tableColorRule.getUuid(), tableColorRule);
new ColorFilterDialog(LoggerPlusPlus.instance.getLibraryController()).setVisible(true);
}
});
filterMenu.add(colorFilterItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.coreyd97.BurpExtenderUtilities.Preferences;
import com.nccgroup.loggerplusplus.LoggerPlusPlus;
import com.nccgroup.loggerplusplus.filter.logfilter.LogFilter;
import com.nccgroup.loggerplusplus.filter.logfilter.LogTableFilter;
import com.nccgroup.loggerplusplus.filter.parser.ParseException;
import com.nccgroup.loggerplusplus.logentry.LogEntry;
import com.nccgroup.loggerplusplus.logentry.LogEntryField;
Expand Down Expand Up @@ -46,7 +46,7 @@ public class ElasticExporter extends AutomaticLogExporter implements ExportPanel

RestHighLevelClient httpClient;
ArrayList<LogEntry> pendingEntries;
LogFilter logFilter;
LogTableFilter logFilter;
private List<LogEntryField> fields;
private String indexName;
private ScheduledFuture indexTask;
Expand Down Expand Up @@ -97,7 +97,7 @@ void setup() throws Exception {

if (!StringUtils.isBlank(filterString)) {
try {
logFilter = new LogFilter(LoggerPlusPlus.instance.getLibraryController(), filterString);
logFilter = new LogTableFilter(filterString);
} catch (ParseException ex) {
logger.error("The log filter configured for the Elastic exporter is invalid!", ex);
}
Expand Down Expand Up @@ -143,15 +143,15 @@ void setup() throws Exception {
@Override
public void exportNewEntry(final LogEntry logEntry) {
if(logEntry.getStatus() == Status.PROCESSED) {
if (logFilter != null && !logFilter.matches(logEntry)) return;
if (logFilter != null && !logFilter.getFilterExpression().matches(logEntry)) return;
pendingEntries.add(logEntry);
}
}

@Override
public void exportUpdatedEntry(final LogEntry updatedEntry) {
if(updatedEntry.getStatus() == Status.PROCESSED) {
if (logFilter != null && !logFilter.matches(updatedEntry)) return;
if (logFilter != null && !logFilter.getFilterExpression().matches(updatedEntry)) return;
pendingEntries.add(updatedEntry);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.coreyd97.BurpExtenderUtilities.PanelBuilder;
import com.coreyd97.BurpExtenderUtilities.Preferences;
import com.nccgroup.loggerplusplus.LoggerPlusPlus;
import com.nccgroup.loggerplusplus.filter.logfilter.LogFilter;
import com.nccgroup.loggerplusplus.filter.logfilter.LogTableFilter;
import com.nccgroup.loggerplusplus.filter.parser.ParseException;
import com.nccgroup.loggerplusplus.filterlibrary.FilterLibraryController;
import com.nccgroup.loggerplusplus.logentry.LogEntryField;
Expand Down Expand Up @@ -221,9 +221,8 @@ public void windowClosing(WindowEvent e) {
String logFilter = preferences.getSetting(PREF_ELASTIC_FILTER);

if (!StringUtils.isBlank(logFilter)) {
FilterLibraryController libraryController = LoggerPlusPlus.instance.getLibraryController();
try {
new LogFilter(libraryController, logFilter);
new LogTableFilter(logFilter);
} catch (ParseException ex) {
JOptionPane.showMessageDialog(ElasticExporterConfigDialog.this,
"Cannot save Elastic Exporter configuration. The chosen log filter is invalid: \n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.nccgroup.loggerplusplus.filter;

import com.google.gson.annotations.JsonAdapter;
import com.nccgroup.loggerplusplus.filter.colorfilter.TableColorRule;
import lombok.Getter;
import lombok.Setter;

import java.awt.*;

public abstract class ColorizingFilterRule extends FilterRule {

@Getter @Setter
private Color backgroundColor;
@Getter @Setter
private Color foregroundColor;
@Getter @Setter
private short priority;
@Getter @Setter
private boolean enabled;

protected ColorizingFilterRule(String name){
super(name);
}

protected ColorizingFilterRule(String name, FilterExpression filterExpression){
super(name, filterExpression);
}

protected ColorizingFilterRule(String name, String filter){
super(name, filter);
}

@Override
public boolean equals(Object obj) {
if(obj instanceof ColorizingFilterRule){
return ((TableColorRule) obj).getUuid().equals(this.getUuid());
}else{
return super.equals(obj);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.nccgroup.loggerplusplus.filter;

import com.nccgroup.loggerplusplus.LoggerPlusPlus;
import com.nccgroup.loggerplusplus.filter.parser.*;
import com.nccgroup.loggerplusplus.logentry.LogEntry;
import com.nccgroup.loggerplusplus.logentry.LogEntryField;
import lombok.Getter;

import java.util.HashSet;

public class FilterExpression {

@Getter
protected ASTExpression ast;

@Getter
protected HashSet<String> snippetDependencies;

public FilterExpression(String filterString) throws ParseException {
this.ast = FilterParser.parseFilter(filterString);
this.snippetDependencies = FilterParser.checkAliasesForSanity(LoggerPlusPlus.instance.getLibraryController(), this.ast);
}

public boolean matches(LogEntry entry){
FilterEvaluationVisitor visitor = new FilterEvaluationVisitor(LoggerPlusPlus.instance.getLibraryController());
return visitor.visit(ast, entry);
}

public void addConditionToFilter(LogicalOperator logicalOperator, LogEntryField field,
ComparisonOperator booleanOperator, String value) throws ParseException {
String existing;
if (this.ast.getLogicalOperator() != null && !this.ast.getLogicalOperator().equals(logicalOperator)) {
existing = "(" + this.ast.getFilterString() + ")";
} else {
existing = this.ast.getFilterString();
}

this.ast = FilterParser.parseFilter(String.format("%s %s %s %s %s", existing, logicalOperator.toString(), field.toString(), booleanOperator, value));
this.snippetDependencies = FilterParser.checkAliasesForSanity(LoggerPlusPlus.instance.getLibraryController(), this.ast);
}

@Override
public String toString() {
return ast.getFilterString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.nccgroup.loggerplusplus.filter;

import com.google.gson.*;
import com.nccgroup.loggerplusplus.filter.parser.ParseException;

import java.lang.reflect.Type;

public class FilterExpressionSerializer implements JsonSerializer<FilterExpression>, JsonDeserializer<FilterExpression> {

@Override
public JsonElement serialize(FilterExpression filter, Type type, JsonSerializationContext jsonSerializationContext) {
JsonObject object = new JsonObject();
object.addProperty("filter", filter.toString());
return object;
}

@Override
public FilterExpression deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
FilterExpression filter = null;
try {
filter = new FilterExpression(jsonElement.getAsJsonObject().get("filter").getAsString());
} catch (ParseException e) {
}
return filter;
}
}
64 changes: 64 additions & 0 deletions src/main/java/com/nccgroup/loggerplusplus/filter/FilterRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.nccgroup.loggerplusplus.filter;

import com.google.gson.annotations.SerializedName;
import com.nccgroup.loggerplusplus.filter.parser.ParseException;
import lombok.Getter;

import java.util.UUID;

public abstract class FilterRule {

@Getter @SerializedName("uid")
private UUID uuid;
@Getter
private String name;
@Getter
private String filterString;
@Getter @SerializedName("filter")
private FilterExpression filterExpression;

public FilterRule(String name){
this.name = name;
this.uuid = UUID.randomUUID();
}

public FilterRule(String name, FilterExpression filterExpression){
this(name);
this.filterExpression = filterExpression;
this.filterString = filterExpression.toString();
}

public FilterRule(String name, String filterString){
this(name);
trySetFilter(filterString);
}

protected void setUuid(UUID uuid) {
this.uuid = uuid;
}

public void setFilter(FilterExpression expression) {
this.filterExpression = expression;
if(expression != null) this.filterString = expression.toString();
}

public void setName(String name){
this.name = name;
}

public boolean trySetFilter(String filterString){
this.filterString = filterString;
try{
parseAndSetFilter(filterString);
}catch (ParseException e){
return false;
}

return true;
}

public void parseAndSetFilter(String filterString) throws ParseException {
this.filterString = filterString;
this.filterExpression = new FilterExpression(filterString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.nccgroup.loggerplusplus.filter;

import com.google.gson.*;
import com.nccgroup.loggerplusplus.filter.savedfilter.SavedFilter;

import java.lang.reflect.Type;
import java.util.UUID;

public class SavedFilterSerializer implements JsonSerializer<SavedFilter>, JsonDeserializer<SavedFilter> {

@Override
public JsonElement serialize(SavedFilter tag, Type type, JsonSerializationContext context) {
JsonObject object = new JsonObject();
object.addProperty("uid", tag.getUuid().toString());
object.addProperty("name", tag.getName());
object.add("filter", context.serialize(tag.getFilterExpression()));
return object;
}

@Override
public SavedFilter deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject obj = jsonElement.getAsJsonObject();
String uid = obj.get("uid").getAsString();
String name = obj.get("name").getAsString();
FilterExpression filterExpression = context.deserialize(obj.get("filter"), FilterExpression.class);

SavedFilter tag = new SavedFilter(name, filterExpression);
tag.setUuid(UUID.fromString(uid));
return tag;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.nccgroup.loggerplusplus.filter;

import com.google.gson.*;
import com.nccgroup.loggerplusplus.filter.colorfilter.TableColorRule;

import java.awt.*;
import java.lang.reflect.Type;
import java.util.UUID;

public class TableColorRuleSerializer implements JsonSerializer<TableColorRule>, JsonDeserializer<TableColorRule> {

@Override
public JsonElement serialize(TableColorRule tag, Type type, JsonSerializationContext context) {
JsonObject object = new JsonObject();
object.addProperty("uid", tag.getUuid().toString());
object.addProperty("name", tag.getName());
object.add("filter", context.serialize(tag.getFilterExpression()));
object.add("foregroundColor", context.serialize(tag.getForegroundColor()));
object.add("backgroundColor", context.serialize(tag.getBackgroundColor()));
object.addProperty("enabled", tag.isEnabled());
object.addProperty("priority", tag.getPriority());
return object;
}

@Override
public TableColorRule deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject obj = jsonElement.getAsJsonObject();
String uid = obj.get("uid").getAsString();
String name = obj.has("name") ? obj.get("name").getAsString() : "";
FilterExpression filterExpression = context.deserialize(obj.get("filter"), FilterExpression.class);
Color foregroundColor = context.deserialize(obj.get("foregroundColor"), Color.class);
Color backgroundColor = context.deserialize(obj.get("backgroundColor"), Color.class);
boolean enabled = obj.get("enabled").getAsBoolean();
short priority = obj.get("priority").getAsShort();

TableColorRule tag = new TableColorRule(name, filterExpression);
tag.setUuid(UUID.fromString(uid));
tag.setForegroundColor(foregroundColor);
tag.setBackgroundColor(backgroundColor);
tag.setEnabled(enabled);
tag.setPriority(priority);
return tag;
}
}
Loading

0 comments on commit 70fa963

Please sign in to comment.