Burp Suite User Forum

Create new post

Getting the highlighted text in a HttpRequest or Response

Tyler | Last updated: Mar 07, 2024 05:10PM UTC

Hey, Is it possible with Montoya or any other method in Java to get the highlighted text of a Request/response? I'm wanting to pass/get some highlighted text into a contextmenu event. Appreciate any help!

Hannah, PortSwigger Agent | Last updated: Mar 08, 2024 09:39AM UTC

Hi.

Yes, this is possible with the Montoya API - you can find an example below. In the example, it simply prints the offsets of the selected text. However, you could manipulate this further to retrieve the contents of the text.

import burp.api.montoya.BurpExtension;
import burp.api.montoya.MontoyaApi;
import burp.api.montoya.core.Range;
import burp.api.montoya.ui.contextmenu.ContextMenuEvent;
import burp.api.montoya.ui.contextmenu.ContextMenuItemsProvider;

import javax.swing.*;
import java.awt.*;
import java.util.List;

import static burp.api.montoya.ui.contextmenu.InvocationType.*;
import static java.util.Collections.emptyList;

public class Extension implements BurpExtension
{
    @Override
    public void initialize(MontoyaApi api)
    {
        api.extension().setName("Get selection offsets");

        api.userInterface().registerContextMenuItemsProvider(new ContextMenuItemsProvider()
        {
            @Override
            public List<Component> provideMenuItems(ContextMenuEvent event)
            {
                if (event.isFrom(MESSAGE_EDITOR_REQUEST, MESSAGE_EDITOR_RESPONSE, MESSAGE_VIEWER_REQUEST, MESSAGE_VIEWER_RESPONSE))
                {
                    if (event.messageEditorRequestResponse().isPresent() && event.messageEditorRequestResponse().get().selectionOffsets().isPresent())
                    {
                        JMenuItem menuItem = new JMenuItem("Get selected text");
                        menuItem.addActionListener(l -> {
                             Range selectionOffsets = event.messageEditorRequestResponse().get().selectionOffsets().get();

                             api.logging().logToOutput(String.format("Start index: %s\r\nEnd index: %s", selectionOffsets.startIndexInclusive(), selectionOffsets.endIndexExclusive()));
                        });

                        return List.of(menuItem);
                    }
                }

                return emptyList();
            }
        });
    }
}

Tyler | Last updated: Mar 08, 2024 10:25AM UTC

Amazing, thank you Hannah!

Hannah, PortSwigger Agent | Last updated: Mar 08, 2024 10:50AM UTC

Just as a note - this retrieves selected text from the message editor, that has been highlighted through manual selection. This is not retrieving highlighted text from the search bar at the bottom of a message editor.

You must be an existing, logged-in customer to reply to a thread. Please email us for additional support.