44import javax .swing .border .Border ;
55import java .awt .*;
66import java .awt .event .*;
7+ import java .io .PrintWriter ;
78import java .util .Arrays ;
89import 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
0 commit comments