一、前言
package burp;
import java.io.PrintWriter;
import java.util.List;
public class BurpExtender implements IHttpListener{
private IBurpExtenderCallbacks callbacks;
private PrintWriter stdout;
private IExtensionHelpers helpers;
public void registerExtenderCallbacks(IBurpExtenderCallbacks callbacks){
// 存储callback的引用
this.callbacks = callbacks;
this.helpers = callbacks.getHelpers();
// 设置callback的扩展的名称
callbacks.setExtensionName("devops burp ext");
// 设置输出引用
stdout = new PrintWriter(callbacks.getStdout(), true);
//注册http的引用
callbacks.registerHttpListener(this);
}
// 处理http捕获到的数据
public void processHttpMessage(int toolFlag, boolean messageIsRequest, IHttpRequestResponse messageInfo) {
if(messageIsRequest){
hookRequest(messageInfo);
}else{
hookResponse(messageInfo);
}
}
}
//更新 body页面的内容
private byte[] updateBodyContent(byte[] payload,byte[] newBodyContent){
IResponseInfo ir = helpers.analyzeResponse(payload);
List headers = ir.getHeaders();
for(int i = 0; i< headers.size();++i){
if(headers.get(i).indexOf("Content-Length") != -1){
headers.set(i,"Content-Length: "+newBodyContent.length);
break;
}
}
return helpers.buildHttpMessage(headers,newBodyContent);
}
//增加get参数
private byte[] addGetPara(byte[] payload,String key,String val){
IParameter ip = helpers.buildParameter(key,val,IParameter.PARAM_URL);
return helpers.addParameter(payload,ip);
}
//增加post参数
private byte[] addPostPara(byte[] payload,String key,String val){
IParameter ip = helpers.buildParameter(key,val,IParameter.PARAM_BODY);
return helpers.addParameter(payload,ip);
}
//更新get参数
private byte[] updateGetPara(byte[] payload,String key,String val){
IParameter ip = helpers.buildParameter(key,val,IParameter.PARAM_URL);
return helpers.updateParameter(payload,ip);
}
//更新post 参数
private byte[] updatePostPara(byte[] payload,String key,String val){
IParameter ip = helpers.buildParameter(key,val,IParameter.PARAM_BODY);
return helpers.updateParameter(payload,ip);
}
//新增一个cookie
private byte[] addCookie(byte[] payload,String key,String val){
IParameter ip = helpers.buildParameter(key,val,IParameter.PARAM_COOKIE);
return helpers.addParameter(payload,ip);
}
//更新一个cookie
private byte[] updateCookie(byte[] payload,String key,String val){
IParameter ip = helpers.buildParameter(key,val,IParameter.PARAM_COOKIE);
return helpers.updateParameter(payload,ip);
}
//更新一个header,如果不存在,则增加
private byte[] updateHeader(byte[] payload,String key,String val){
IResponseInfo ir = helpers.analyzeResponse(payload);
int bodyOffset = ir.getBodyOffset();
List headers = ir.getHeaders();
byte[] bodyByte = new byte[payload.length - bodyOffset];
System.arraycopy(payload,bodyOffset,bodyByte,0,payload.length - bodyOffset);
boolean exist = false;
for(int i = 0; i< headers.size();++i){
if(headers.get(i).indexOf(key) != -1){
headers.set(i,key + ": "+val);
exist = true;
break;
}
}
if(!exist){
headers.add(key + ": "+val);
}
return helpers.buildHttpMessage(headers,bodyByte);
}