Skip to content

Commit

Permalink
Initial implementation of custom regex columns #153.
Browse files Browse the repository at this point in the history
  • Loading branch information
CoreyD97 committed Aug 3, 2023
1 parent 12ad2fa commit 8a605f6
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import javax.swing.table.TableColumn;
import java.lang.reflect.Type;
import java.util.regex.Pattern;

public class LogTableColumn extends TableColumn implements Comparable<LogTableColumn>{

Expand All @@ -43,8 +44,6 @@ public void setWidth(int width){
this.setPreferredWidth(width);
}

@Override
public LogEntryField getIdentifier() { return (LogEntryField) this.identifier; }
public String getName() {
return name;
}
Expand Down Expand Up @@ -102,8 +101,8 @@ public static class ColumnSerializer implements JsonDeserializer<LogTableColumn>
//id, name, enabled, defaultVisibleName, visibleName, width, type, readonly, order, visible, description, isRegEx, regExData
@Override
public JsonElement serialize(LogTableColumn column, Type type, JsonSerializationContext jsonSerializationContext) {
//TODO Allow saving of custom regex columns.
JsonObject object = new JsonObject();
object.addProperty("id", String.valueOf(column.identifier));
object.addProperty("order", column.order);
object.addProperty("name", column.name);
object.addProperty("defaultVisibleName", column.defaultVisibleName);
Expand All @@ -117,10 +116,10 @@ public JsonElement serialize(LogTableColumn column, Type type, JsonSerialization

@Override
public LogTableColumn deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
LogTableColumn column = null;
LogTableColumn column = new LogTableColumn();
JsonObject object = jsonElement.getAsJsonObject();
column = new LogTableColumn();
column.identifier = LogEntryField.getByFullyQualifiedName(object.get("id").getAsString());

column.identifier = LogEntryField.getByFullyQualifiedName(object.get("id").getAsString());
column.name = object.get("name").getAsString();
column.order = object.get("order").getAsInt();
column.defaultVisibleName = object.get("defaultVisibleName").getAsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;
import java.util.*;
import java.util.regex.Pattern;

//The visible columns are stored in the underlying class.

Expand Down Expand Up @@ -62,6 +63,23 @@ private void initialize(){

}

public void addCustomColumn(String name, int order, boolean matchRequest, boolean matchResponse, Pattern regex){
RegexLogColumn column = new RegexLogColumn();
column.setVisible(true);
column.setVisibleName(name);
column.setName("Custom");
column.setOrder(order);
column.setIdentifier(null);
column.setPattern(regex);
column.setMatchRequest(matchRequest);
column.setMatchResponse(matchResponse);
addColumn(column);
}

public void refreshColumn(LogTableColumn column){
this.fireColumnMoved(new TableColumnModelEvent(this, column.getModelIndex(), column.getModelIndex()));
}

@Override
public int getColumnCount() {
return tableColumns.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.util.*;
import java.util.regex.Matcher;

/* Extending AbstractTableModel to design the logTable behaviour based on the array list */
public class LogTableModel extends AbstractTableModel implements ColorFilterListener, TagListener {
Expand Down Expand Up @@ -104,7 +105,24 @@ public Object getValueAt(int rowIndex, int colModelIndex) {
return rowIndex + 1;
}

Object value = entries.get(rowIndex).getValueByKey(column.getIdentifier());
LogEntry entry = entries.get(rowIndex);

if(column instanceof RegexLogColumn){
Matcher m;
if(((RegexLogColumn) column).isMatchRequest()){ //TODO Fix this ugly mess.
m = ((RegexLogColumn) column).getPattern().matcher(entry.getRequest().toString());
if(m.find()){
return m.groupCount() > 0 ? m.group(1) : m.group(0);
}
}else if(((RegexLogColumn) column).isMatchResponse()){
m = ((RegexLogColumn) column).getPattern().matcher(entry.getResponse().toString());
if(m.find()){
return m.groupCount() > 0 ? m.group(1) : m.group(0);
}
}
}

Object value = entries.get(rowIndex).getValueByKey((LogEntryField) column.getIdentifier());

if (value instanceof Date) {
return LogProcessor.LOGGER_DATE_FORMAT.format(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Burp Suite Logger++
//
// Released as open source by NCC Group Plc - https://www.nccgroup.trust/
//
// Developed by Soroush Dalili (@irsdl)
//
// Project link: http://www.github.com/nccgroup/BurpSuiteLoggerPlusPlus
//
// Released under AGPL see LICENSE for more information
//

package com.nccgroup.loggerplusplus.logview.logtable;

// To define a structure for table headers
// This will provide a high degree of customisation
// a sample JSON object which will be converted to this object is as follows:
// "{'columnsDefinition':[{'id':'number','visibleName':'#','width':50,'type':'int','readonly':true,'order':1,'visible':true,'description':'Item index number','isRegEx':false,'regExData':{'regExString':'','regExCaseSensitive':false}}]}";

import com.google.gson.*;
import com.nccgroup.loggerplusplus.logentry.LogEntryField;
import lombok.Getter;
import lombok.Setter;

import javax.swing.table.TableColumn;
import java.lang.reflect.Type;
import java.util.regex.Pattern;

public class RegexLogColumn extends LogTableColumn implements Comparable<LogTableColumn>{

@Getter @Setter
Pattern pattern;
@Getter @Setter
boolean matchRequest;
@Getter @Setter
boolean matchResponse;
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public String getToolTipText(MouseEvent e) {
} catch (NullPointerException | ArrayIndexOutOfBoundsException ex) {
retStr = "";
}
if (retStr.length() < 1) {
if (retStr == null || retStr.length() < 1) {
retStr = super.getToolTipText(e);
}
return retStr;
Expand Down
Loading

0 comments on commit 8a605f6

Please sign in to comment.