Skip to content

Commit 765db20

Browse files
author
Will Strei
committed
Added button to test regex
1 parent e6b037a commit 765db20

File tree

5 files changed

+143
-92
lines changed

5 files changed

+143
-92
lines changed

.idea/workspace.xml

Lines changed: 36 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
2.81 KB
Binary file not shown.

src/burp/Extractor.java

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import java.io.PrintWriter;
44
import java.net.URL;
5-
import java.util.regex.Matcher;
6-
import java.util.regex.Pattern;
75

86
public class Extractor implements IHttpListener {
97
private ExtractorMainTab extractorMainTab;
@@ -44,23 +42,16 @@ public void processHttpMessage(int toolFlag, boolean messageIsRequest, burp.IHtt
4442
if (!extractedData.equals("")
4543
&& !requestSelectionRegex[0].equals("")
4644
&& !requestSelectionRegex[1].equals("")) {
47-
logger.debug("Performing replacement...");
48-
49-
Matcher beforeMatcher = Pattern.compile(requestSelectionRegex[0]).matcher(request);
50-
if (beforeMatcher.find()) {
51-
int endOfBefore = beforeMatcher.end();
52-
Matcher afterMatcher = Pattern.compile(requestSelectionRegex[1]).matcher(request);
53-
if (afterMatcher.find(endOfBefore)) {
54-
logger.debug("Found a match");
55-
int startOfAfter = afterMatcher.start();
56-
request = request.substring(0, endOfBefore)
57-
+ extractedData
58-
+ request.substring(startOfAfter, request.length());
59-
edited = true;
60-
logger.debug("Finished replacement.");
61-
}
62-
63-
}
45+
logger.debug("Attempting replacement...");
46+
int[] selectionBounds = Utils.getSelectionBounds(request, requestSelectionRegex[0], requestSelectionRegex[1]);
47+
if (selectionBounds != null) {
48+
logger.debug("Found a match");
49+
request = request.substring(0, selectionBounds[0])
50+
+ extractedData
51+
+ request.substring(selectionBounds[1], request.length());
52+
edited = true;
53+
logger.debug("Finished replacement");
54+
}
6455
}
6556
}
6657
}
@@ -87,17 +78,14 @@ public void processHttpMessage(int toolFlag, boolean messageIsRequest, burp.IHtt
8778

8879
// Grab text from response
8980
if (responseSelectionRegex[0] != "" && responseSelectionRegex[1] != "") {
90-
Matcher beforeMatcher = Pattern.compile(responseSelectionRegex[0]).matcher(response);
91-
if (beforeMatcher.find()) {
92-
int endOfBefore = beforeMatcher.end();
93-
Matcher afterMatcher = Pattern.compile(responseSelectionRegex[1]).matcher(response);
94-
if (afterMatcher.find(endOfBefore)) {
95-
logger.debug("Found a match");
96-
int startOfAfter = afterMatcher.start();
97-
extractorTab.setDataToInsert(response.substring(endOfBefore, startOfAfter));
98-
}
99-
}
100-
}
81+
int[] selectionBounds = Utils.getSelectionBounds(response, responseSelectionRegex[0], responseSelectionRegex[1]);
82+
if (selectionBounds != null) {
83+
logger.debug("Found a match");
84+
extractorTab.setDataToInsert(response.substring(selectionBounds[0], selectionBounds[1]));
85+
}
86+
} else {
87+
logger.debug("Before and after regex not defined");
88+
}
10189
}
10290
}
10391
}

src/burp/ExtractorEditor.java

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import javax.swing.border.Border;
55
import java.awt.*;
66
import java.awt.event.*;
7+
import java.io.PrintWriter;
78
import java.util.Arrays;
89
import java.util.HashMap;
910

@@ -21,10 +22,12 @@ public class ExtractorEditor {
2122
private JTextField endRegex;
2223
private boolean keyListenerSet;
2324
private final int SELECTION_BUFFER = 15;
25+
private Logger logger;
2426

2527
public ExtractorEditor(final IBurpExtenderCallbacks callbacks) {
2628
this.pane = new JPanel();
2729
this.helpers = callbacks.getHelpers();
30+
this.logger = new Logger(new PrintWriter(callbacks.getStdout(), true));
2831
this.pane.setLayout(new GridBagLayout());
2932

3033
// Add buttons to panel
@@ -41,20 +44,6 @@ public ExtractorEditor(final IBurpExtenderCallbacks callbacks) {
4144
private void addButtons(JPanel pane) {
4245
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
4346

44-
// Add radio button for scope
45-
this.useScope = new JRadioButton("Use suite scope");
46-
buttonPanel.add(this.useScope);
47-
48-
// Add radio button for target host
49-
this.useCustomHost = new JRadioButton("Use specified target host");
50-
buttonPanel.add(this.useCustomHost);
51-
52-
// Create button group and select suite scope by default
53-
ButtonGroup scopeSelection = new ButtonGroup();
54-
scopeSelection.add(this.useScope);
55-
scopeSelection.add(this.useCustomHost);
56-
this.useScope.setSelected(true);
57-
5847
// Create tool selection
5948
toolSelectors = new HashMap<Integer, ToolMenuItem>();
6049
JButton toolSelectionBar = new JButton("Select in-scope tools");
@@ -81,6 +70,26 @@ public void mouseClicked(MouseEvent e) {
8170
});
8271
buttonPanel.add(toolSelectionBar);
8372

73+
// Create button for testing regex
74+
JButton testRegexButton = new JButton("Test defined selection");
75+
testRegexButton.addActionListener(new ActionListener() {
76+
@Override
77+
public void actionPerformed(ActionEvent e) {
78+
String matchResult = getTestRegexMatch();
79+
JPopupMenu popup = new JPopupMenu();
80+
JLabel contents = new JLabel();
81+
if (matchResult == null) {
82+
contents.setText("Did not find a match for the defined start and end regex!");
83+
} else {
84+
contents.setText("Found match: " + matchResult);
85+
}
86+
contents.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
87+
popup.add(contents);
88+
popup.show(testRegexButton, 0, testRegexButton.getHeight());
89+
}
90+
});
91+
buttonPanel.add(testRegexButton);
92+
8493
GridBagConstraints constraints = new GridBagConstraints();
8594
constraints.gridx = 0;
8695
constraints.gridy = 0;
@@ -179,30 +188,44 @@ private void addTextFields(JPanel pane) {
179188
GridBagConstraints constraints = new GridBagConstraints();
180189

181190
// Add label for target host
182-
JLabel targetLabel = new JLabel("Target host: ");
183-
constraints.gridx = 0;
184-
constraints.gridy = 1;
185-
constraints.gridwidth = 1;
186-
constraints.fill = GridBagConstraints.NONE;
187-
constraints.weightx = 0;
188-
this.pane.add(targetLabel, constraints);
191+
JPanel targetPanel = new JPanel(new GridBagLayout());
192+
GridBagConstraints targetConstraints = new GridBagConstraints();
193+
194+
// Add radio button for scope
195+
this.useScope = new JRadioButton("Use suite scope ");
196+
targetConstraints.gridx = 0;
197+
targetPanel.add(this.useScope, targetConstraints);
198+
199+
// Add radio button for target host
200+
this.useCustomHost = new JRadioButton("Use specified target host: ");
201+
targetConstraints.gridx += 1;
202+
targetPanel.add(this.useCustomHost, targetConstraints);
203+
204+
// Create button group and select suite scope by default
205+
ButtonGroup scopeSelection = new ButtonGroup();
206+
scopeSelection.add(this.useScope);
207+
scopeSelection.add(this.useCustomHost);
208+
this.useScope.setSelected(true);
189209

190210
// Add text field for target host
191211
this.targetHost = new JTextField();
192-
constraints.gridx = 1;
193-
constraints.gridwidth = 3;
194-
constraints.gridy = 1;
195-
constraints.fill = GridBagConstraints.HORIZONTAL;
196-
constraints.weightx = 1;
197-
this.pane.add(this.targetHost, constraints);
212+
targetConstraints.gridx += 1;
213+
targetConstraints.weightx = 1;
214+
targetConstraints.fill = GridBagConstraints.HORIZONTAL;
215+
targetPanel.add(this.targetHost, targetConstraints);
198216

199217
// Add regex checkBox
200218
this.regexCheckBox = new JCheckBox("Regex");
201-
constraints.gridx = 3;
219+
targetConstraints.gridx += 1;
220+
targetConstraints.weightx = 0;
221+
targetConstraints.fill = GridBagConstraints.NONE;
222+
targetPanel.add(this.regexCheckBox, targetConstraints);
223+
224+
constraints.gridx = 0;
225+
constraints.gridwidth = 4;
202226
constraints.gridy = 1;
203-
constraints.fill = GridBagConstraints.NONE;
204-
constraints.weightx = 0;
205-
this.pane.add(this.regexCheckBox, constraints);
227+
constraints.fill = GridBagConstraints.HORIZONTAL;
228+
this.pane.add(targetPanel, constraints);
206229

207230
// Add label for startRegex
208231
JLabel regexLabel = new JLabel("Regex Start: ");
@@ -295,6 +318,21 @@ private String[] buildSelectionRegex() {
295318
}
296319
}
297320

321+
private String getTestRegexMatch() {
322+
String toMatch = helpers.bytesToString(textSelector.getText());
323+
int[] selectionBounds = Utils.getSelectionBounds(toMatch,
324+
startRegex.getText(),
325+
endRegex.getText());
326+
logger.debug("Testing regex...");
327+
logger.debug("String to match: " + toMatch);
328+
logger.debug("Start regex: " + startRegex.getText());
329+
logger.debug("End regex: " + endRegex.getText());
330+
if (selectionBounds == null) {
331+
return null;
332+
}
333+
return toMatch.substring(selectionBounds[0], selectionBounds[1]);
334+
}
335+
298336
// I hope that all necessary characters are escaped here, but I'm no regex pro so this could be faulty
299337
private String escapeRegex(String regex) {
300338

src/burp/Utils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package burp;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
public class Utils {
7+
public static int[] getSelectionBounds(String request, String beforeRegex, String afterRegex) {
8+
int[] selectionBounds = new int[2];
9+
Matcher beforeMatcher = Pattern.compile(beforeRegex).matcher(request);
10+
if (beforeMatcher.find()) {
11+
selectionBounds[0] = beforeMatcher.end();
12+
Matcher afterMatcher = Pattern.compile(afterRegex).matcher(request);
13+
if (afterMatcher.find(selectionBounds[0])) {
14+
selectionBounds[1] = afterMatcher.start();
15+
return selectionBounds;
16+
}
17+
}
18+
return null;
19+
}
20+
}

0 commit comments

Comments
 (0)