Getting started with Python Plugins

This article provides a step-by-step guide on how to write a simple Python plugin for Cutter.

Create a python file, called myplugin.py for example, and add the following contents:

  1. import cutter
  2.  
  3. class MyCutterPlugin(cutter.CutterPlugin):
  4. name = "My Plugin"
  5. description = "This plugin does awesome things!"
  6. version = "1.0"
  7. author = "1337 h4x0r"
  8.  
  9. def setupPlugin(self):
  10. pass
  11.  
  12. def setupInterface(self, main):
  13. pass
  14.  
  15. def terminate(self):
  16. pass
  17.  
  18. def create_cutter_plugin():
  19. return MyCutterPlugin()

This is the most basic code that makes up a plugin.Python plugins in Cutter are regular Python modules that are imported automatically on startup.In order to load the plugin, Cutter will call the function create_cutter_plugin() locatedin the root of the module and expects it to return an instance of cutter.CutterPlugin.Normally, you shouldn’t have to do anything else in this function.

Note

The Cutter API is exposed through the cutter module.This consists mostly of direct bindings of the original C++ classes, generated with Shiboken2.For more detail about this API, see the Cutter C++ code or API Reference.

The CutterPlugin subclass contains some meta-info and two callback methods:

  • setupPlugin() is called right after the plugin is loaded and can be used to initialize the plugin itself.
  • setupInterface() is called with the instance of MainWindow as an argument and should create and register any UI components.
  • terminate() is called on shutdown and should clean up any resources used by the plugin.

Copy this file into the python subdirectory located under the plugins directory of Cutter and start the application.You should see an entry for your plugin in the list under Edit -> Preferences -> Plugins.Here, the absolute path to the plugins directory is shown too if you are unsure where to put your plugin:../_images/preferences-plugins.png

Note

As mentioned, plugins are Python modules. This means, instead of only a single .py file, you can alsouse a directory containing multiple python files and an init.py file that defines or imports thecreate_cutter_plugin() function.

Note

If you are working on a Unix-like system, instead of copying, you can also symlink your plugin into the pluginsdirectory, which lets you store the plugin somewhere else without having to copy the files over and over again.

Creating a Widget

Next, we are going to add a simple dock widget. Extend the code as follows:

  1. import cutter
  2.  
  3. from PySide2.QtWidgets import QAction, QLabel
  4.  
  5. class MyDockWidget(cutter.CutterDockWidget):
  6. def __init__(self, parent, action):
  7. super(MyDockWidget, self).__init__(parent, action)
  8. self.setObjectName("MyDockWidget")
  9. self.setWindowTitle("My cool DockWidget")
  10.  
  11. label = QLabel(self)
  12. self.setWidget(label)
  13. label.setText("Hello World")
  14.  
  15. class MyCutterPlugin(cutter.CutterPlugin):
  16. # ...
  17.  
  18. def setupInterface(self, main):
  19. action = QAction("My Plugin", main)
  20. action.setCheckable(True)
  21. widget = MyDockWidget(main, action)
  22. main.addPluginDockWidget(widget, action)
  23.  
  24. # ...

We are subclassing cutter.CutterDockWidget, which is the base class for all dock widgets in Cutter,and adding a label to it.

Note

You can access the whole Qt5 API from Python, which is exposed by PySide2. For more information about this, refer to theDocumentation of Qt and PySide2.

In our setupInterface() method, we create an instance of our dock widget and an action to beadded to the menu for showing and hiding the widget.MainWindow provides a helper method called addPluginDockWidget() to easily register these.

When running Cutter now, you should see the widget:../_images/mydockwidget.png… as well as the action:../_images/mydockwidget-action.png

Fetching Data

Next, we want to show some actual data from the binary in our widget.As an example, we will display the instruction and instruction size at the current position.Extend the code as follows:

  1. # ...
  2.  
  3. class MyDockWidget(cutter.CutterDockWidget):
  4. def __init__(self, parent, action):
  5. # ...
  6.  
  7. label = QLabel(self)
  8. self.setWidget(label)
  9.  
  10. disasm = cutter.cmd("pd 1").strip()
  11.  
  12. instruction = cutter.cmdj("pdj 1")
  13. size = instruction[0]["size"]
  14.  
  15. label.setText("Current disassembly:\n{}\nwith size {}".format(disasm, size))
  16.  
  17. # ...

We can access the data by calling radare2 commands and utilizing their output.This is done by using the two functions cmd() and cmdj(), which behave just as theydo in r2pipe.

Many commands in radare2 can be suffixed with a j to return JSON output.cmdj() will automatically deserialize the JSON into python dicts and lists, so theinformation can be easily accessed.

Warning

When fetching data that is not meant to be used only as readable text, always use the JSON variant of a command!Regular command output is not meant to be parsed and is subject to change at any time, which will break your code.

In our case, we use the two commands pd (Print Disassembly) and pdj (Print Disassembly as JSON)with a parameter of 1 to fetch a single line of disassembly.

Note

To try out commands, you can use the Console widget in Cutter. Almost all commands support a ? suffix, like inpd?, to show help and available sub-commands.To get a general overview, enter a single ?.

The result will look like the following:../_images/disasm-static.pngOf course, since we only fetch the info once during the creation of the widget, the content never updates.We are going to change that in the next section.

Reacting to Events

We want to update the content of our widget on every seek.This can be done like the following:

  1. # ...
  2.  
  3. from PySide2.QtCore import QObject, SIGNAL
  4.  
  5. # ...
  6.  
  7. class MyDockWidget(cutter.CutterDockWidget):
  8. def __init__(self, parent, action):
  9. # ...
  10.  
  11. self._label = QLabel(self)
  12. self.setWidget(self._label)
  13.  
  14. QObject.connect(cutter.core(), SIGNAL("seekChanged(RVA)"), self.update_contents)
  15. self.update_contents()
  16.  
  17. def update_contents(self):
  18. disasm = cutter.cmd("pd 1").strip()
  19.  
  20. instruction = cutter.cmdj("pdj 1")
  21. size = instruction[0]["size"]
  22.  
  23. self._label.setText("Current disassembly:\n{}\nwith size {}".format(disasm, size))

First, we move the update code to a separate method.Then we call cutter.core(), which returns the global instance of CutterCore.This class provides the Qt signal seekChanged(RVA), which is emitted every time the current seek changes.We can simply connect this signal to our method and our widget will update as we expect it to:../_images/disasm-dynamic.pngFor more information about Qt signals and slots, refer to https://doc.qt.io/qt-5/signalsandslots.html.

Full Code

  1. import cutter
  2.  
  3. from PySide2.QtCore import QObject, SIGNAL
  4. from PySide2.QtWidgets import QAction, QLabel
  5.  
  6. class MyDockWidget(cutter.CutterDockWidget):
  7. def __init__(self, parent, action):
  8. super(MyDockWidget, self).__init__(parent, action)
  9. self.setObjectName("MyDockWidget")
  10. self.setWindowTitle("My cool DockWidget")
  11.  
  12. self._label = QLabel(self)
  13. self.setWidget(self._label)
  14.  
  15. QObject.connect(cutter.core(), SIGNAL("seekChanged(RVA)"), self.update_contents)
  16. self.update_contents()
  17.  
  18. def update_contents(self):
  19. disasm = cutter.cmd("pd 1").strip()
  20.  
  21. instruction = cutter.cmdj("pdj 1")
  22. size = instruction[0]["size"]
  23.  
  24. self._label.setText("Current disassembly:\n{}\nwith size {}".format(disasm, size))
  25.  
  26.  
  27. class MyCutterPlugin(cutter.CutterPlugin):
  28. name = "My Plugin"
  29. description = "This plugin does awesome things!"
  30. version = "1.0"
  31. author = "1337 h4x0r"
  32.  
  33. def setupPlugin(self):
  34. pass
  35.  
  36. def setupInterface(self, main):
  37. action = QAction("My Plugin", main)
  38. action.setCheckable(True)
  39. widget = MyDockWidget(main, action)
  40. main.addPluginDockWidget(widget, action)
  41.  
  42. def terminate(self):
  43. pass
  44.  
  45. def create_cutter_plugin():
  46. return MyCutterPlugin()