我正在开发一个Burp Suite扩展程序。
我有一个BurpExtender类,它有公共静态字段。
public class BurpExtender implements IBurpExtender, IContextMenuFactory{
private IBurpExtenderCallbacks callbacks;
public static PrintWriter stdout;
public static IExtensionHelpers helpers;
...
@Override
public void registerExtenderCallbacks(IBurpExtenderCallbacks callbacks) {
this.callbacks = callbacks;
this.helpers = callbacks.getHelpers();
PrintWriter stdout = new PrintWriter(callbacks.getStdout(), true);
callbacks.setExtensionName("REQUESTSENDER");
callbacks.registerContextMenuFactory(BurpExtender.this);
stdout.println("Registered");
}
public List<JMenuItem> createMenuItems(final IContextMenuInvocation invocation) {
List<JMenuItem> menuItemList = new ArrayList<JMenuItem>();
JMenuItem item = new JMenuItem(new MyAction());
menuItemList.add(item);
return menuItemList;
}
在这个文件中我有另一个类MyAction:
private class MyAction extends AbstractAction{
public MyAction(){
super("Name");
}
public void actionPerformed(ActionEvent e) {
//Here i want to use BurpExtender.helpers, but i cant figure out, how to.
//BurpExtender.stdout doesnt work here. Dunno why, NullPoinerException.
}
}
我有另一个解决方案,当我尝试像JMenuItem项目=新的JMenuItem(新的AbstractAction(“123”){...}那样结果时,它是相同的
答案 0 :(得分:1)
您需要初始化helper
课程中的stdout
和BurpExtender
个对象。
由于这些是静态字段,相应的位置将在声明它们或在类中的静态块内初始化它们。
例如:
- 在声明时:
醇>
public static PrintWriter stdout = System.out;
public static IExtensionHelpers helpers = new ExtensionHelperImpl();// something like this.
- 或在静态区块内
醇>
public static PrintWriter stdout;
public static IExtensionHelpers helpers;
static {
stdout = System.out;
helpers = new ExtensionHelperImpl();// something like this.
}
如果没有此初始化,stdout
和helpers
引用将指向null
。当您尝试使用时,这会导致NullPointerException
BurpExtender.stdout
或BurpExtender.helpers
。
<强>更新强>
在MyAction
类中声明一个引用以保存IContextMenuInvocation invocation
对象。有点像这样:
private class MyAction extends AbstractAction{
private IContextMenuInvocation invocation;
public MyAction(IContextMenuInvocation invocation){
super("Name");
this.invocation = invocation;
}
public void actionPerformed(ActionEvent e) {
//Here you can use BurpExtender.helpers and IContextMenuInvocation invocation also.
BurpExtender.helpers.doSomething();
invocation.invoke();// for example..
}
}
然后在你的外部类中,改变createMenuItems
方法,如下所示:
public List<JMenuItem> createMenuItems(final IContextMenuInvocation invocation) {
List<JMenuItem> menuItemList = new ArrayList<JMenuItem>();
JMenuItem item = new JMenuItem(new MyAction(invocation));// this is the change
menuItemList.add(item);
return menuItemList;
}
希望这有帮助!