The Burp Suite User Forum was discontinued on the 1st November 2024.

Burp Suite User Forum

For support requests, go to the Support Center. To discuss with other Burp users, head to our Discord page.

SUPPORT CENTER DISCORD

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