|
| 1 | +from burp import IBurpExtender, IContextMenuFactory, IExtensionHelpers |
| 2 | +from burp import IContextMenuInvocation |
| 3 | +from javax.swing import JMenuItem, JOptionPane |
| 4 | +import random |
| 5 | +import string |
| 6 | + |
| 7 | +class BurpExtender(IBurpExtender, IContextMenuFactory): |
| 8 | + |
| 9 | + def registerExtenderCallbacks(self, callbacks): |
| 10 | + # Set up the extension |
| 11 | + self._callbacks = callbacks |
| 12 | + self._helpers = callbacks.getHelpers() |
| 13 | + callbacks.setExtensionName("Add Random Text Extension") |
| 14 | + |
| 15 | + # Register the context menu factory |
| 16 | + callbacks.registerContextMenuFactory(self) |
| 17 | + |
| 18 | + def createMenuItems(self, invocation): |
| 19 | + menu_items = [] |
| 20 | + |
| 21 | + # Add context menu item for request editor context |
| 22 | + if invocation.getInvocationContext() == IContextMenuInvocation.CONTEXT_MESSAGE_EDITOR_REQUEST: |
| 23 | + menu_item = JMenuItem("Add Random Text to Request", actionPerformed=lambda x, inv=invocation: self.addTextToRequest(inv)) |
| 24 | + menu_items.append(menu_item) |
| 25 | + |
| 26 | + return menu_items |
| 27 | + |
| 28 | + def addTextToRequest(self, invocation): |
| 29 | + # Ensure we are working with a request editor context |
| 30 | + if invocation.getInvocationContext() != IContextMenuInvocation.CONTEXT_MESSAGE_EDITOR_REQUEST: |
| 31 | + self._callbacks.printError("This option is only available for request editors.") |
| 32 | + return |
| 33 | + |
| 34 | + selected_message = invocation.getSelectedMessages()[0] |
| 35 | + request_info = self._helpers.analyzeRequest(selected_message) |
| 36 | + |
| 37 | + # Prompt user for the amount of kilobytes they want to insert |
| 38 | + kb_input = JOptionPane.showInputDialog("Enter the number of KB to insert:") |
| 39 | + try: |
| 40 | + kb = int(kb_input) |
| 41 | + if kb <= 0: |
| 42 | + raise ValueError("The KB size must be greater than 0.") |
| 43 | + except ValueError: |
| 44 | + self._callbacks.printError("Invalid input. Please enter a valid number.") |
| 45 | + return |
| 46 | + |
| 47 | + # Generate random letters based on the user input (1 KB = 1024 bytes) using random.choice() |
| 48 | + text_size = kb * 1024 |
| 49 | + text_data = "bullet='{}'".format(''.join([random.choice(string.ascii_letters) for _ in range(text_size)])) |
| 50 | + |
| 51 | + # Get the current request in bytes and convert it to a string |
| 52 | + original_request = selected_message.getRequest() |
| 53 | + original_request_str = self._helpers.bytesToString(original_request) |
| 54 | + |
| 55 | + # Get the selection bounds (cursor position) in the request editor |
| 56 | + selection_bounds = invocation.getSelectionBounds() |
| 57 | + |
| 58 | + if selection_bounds: |
| 59 | + # If there is a selection, insert the generated text at the selected position |
| 60 | + cursor_start = selection_bounds[0] |
| 61 | + cursor_end = selection_bounds[1] |
| 62 | + else: |
| 63 | + # If no selection is made, insert the text at the end of the request |
| 64 | + cursor_start = len(original_request_str) |
| 65 | + cursor_end = len(original_request_str) |
| 66 | + |
| 67 | + # Insert the random text into the request string |
| 68 | + modified_request_str = original_request_str[:cursor_start] + text_data + original_request_str[cursor_end:] |
| 69 | + |
| 70 | + # Convert the modified request back to bytes |
| 71 | + modified_request = self._helpers.stringToBytes(modified_request_str) |
| 72 | + |
| 73 | + # Update the request with the modified text |
| 74 | + selected_message.setRequest(modified_request) |
0 commit comments