Files
HaE/src/main/java/hae/instances/http/HttpMessageActiveHandler.java

107 lines
4.8 KiB
Java
Raw Normal View History

2024-05-06 12:56:56 +08:00
package hae.instances.http;
import burp.api.montoya.MontoyaApi;
import burp.api.montoya.core.Annotations;
import burp.api.montoya.core.HighlightColor;
import burp.api.montoya.http.handler.*;
import burp.api.montoya.http.message.HttpRequestResponse;
import burp.api.montoya.http.message.requests.HttpRequest;
import hae.component.board.message.MessageTableModel;
import hae.instances.http.utils.MessageProcessor;
2024-05-24 15:00:49 +08:00
import hae.utils.ConfigLoader;
2024-08-12 10:34:26 +08:00
import hae.utils.http.HttpUtils;
2024-05-06 12:56:56 +08:00
import hae.utils.string.StringProcessor;
2024-06-19 22:16:57 +08:00
import javax.swing.*;
2024-05-12 19:02:38 +08:00
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
2024-05-06 12:56:56 +08:00
2024-11-16 18:06:49 +08:00
public class HttpMessageActiveHandler implements HttpHandler {
2024-05-06 12:56:56 +08:00
private final MontoyaApi api;
2024-05-23 12:00:13 +08:00
private final ConfigLoader configLoader;
2024-08-12 10:34:26 +08:00
private final HttpUtils httpUtils;
2024-05-12 19:02:38 +08:00
private final MessageTableModel messageTableModel;
2024-05-06 12:56:56 +08:00
private final MessageProcessor messageProcessor;
// Montoya API对HTTP消息的处理分为了请求和响应因此此处设置高亮和标记需要使用全局变量的方式以此兼顾请求和响应
// 同时采用 ThreadLocal 来保证多线程并发的情况下全局变量的安全性
2024-05-12 19:02:38 +08:00
private final ThreadLocal<String> host = ThreadLocal.withInitial(() -> "");
2024-05-06 12:56:56 +08:00
private final ThreadLocal<List<String>> colorList = ThreadLocal.withInitial(ArrayList::new);
private final ThreadLocal<List<String>> commentList = ThreadLocal.withInitial(ArrayList::new);
2024-11-16 18:06:49 +08:00
public HttpMessageActiveHandler(MontoyaApi api, ConfigLoader configLoader, MessageTableModel messageTableModel) {
2024-05-06 12:56:56 +08:00
this.api = api;
2024-05-23 12:00:13 +08:00
this.configLoader = configLoader;
2024-08-12 10:34:26 +08:00
this.httpUtils = new HttpUtils(api, configLoader);
2024-05-06 12:56:56 +08:00
this.messageTableModel = messageTableModel;
this.messageProcessor = new MessageProcessor(api);
}
@Override
public RequestToBeSentAction handleHttpRequestToBeSent(HttpRequestToBeSent httpRequestToBeSent) {
colorList.get().clear();
commentList.get().clear();
Annotations annotations = httpRequestToBeSent.annotations();
2024-06-19 22:16:57 +08:00
try {
host.set(StringProcessor.getHostByUrl(httpRequestToBeSent.url()));
} catch (Exception e) {
api.logging().logToError("handleHttpRequestToBeSent: " + e.getMessage());
2024-05-06 12:56:56 +08:00
}
return RequestToBeSentAction.continueWith(httpRequestToBeSent, annotations);
}
@Override
public ResponseReceivedAction handleHttpResponseReceived(HttpResponseReceived httpResponseReceived) {
Annotations annotations = httpResponseReceived.annotations();
2024-08-12 10:34:26 +08:00
HttpRequest request = httpResponseReceived.initiatingRequest();
HttpRequestResponse requestResponse = HttpRequestResponse.httpRequestResponse(request, httpResponseReceived);
String toolType = httpResponseReceived.toolSource().toolType().toolName();
boolean matches = httpUtils.verifyHttpRequestResponse(requestResponse, toolType);
if (!matches) {
try {
setColorAndCommentList(messageProcessor.processRequest(host.get(), request, true));
setColorAndCommentList(messageProcessor.processResponse(host.get(), httpResponseReceived, true));
if (!colorList.get().isEmpty() && !commentList.get().isEmpty()) {
2024-11-16 18:06:49 +08:00
HttpRequestResponse httpRequestResponse = HttpRequestResponse.httpRequestResponse(request, httpResponseReceived);
2024-08-12 10:34:26 +08:00
String color = messageProcessor.retrieveFinalColor(messageProcessor.retrieveColorIndices(colorList.get()));
annotations.setHighlightColor(HighlightColor.highlightColor(color));
String comment = StringProcessor.mergeComment(String.join(", ", commentList.get()));
annotations.setNotes(comment);
2024-08-23 22:03:31 +08:00
String method = request.method();
String url = request.url();
2024-08-12 10:34:26 +08:00
String status = String.valueOf(httpResponseReceived.statusCode());
String length = String.valueOf(httpResponseReceived.toByteArray().length());
new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() {
2024-12-21 15:34:45 +08:00
messageTableModel.add(httpRequestResponse, url, method, status, length, comment, color, true);
2024-08-12 10:34:26 +08:00
return null;
}
2024-08-23 22:03:31 +08:00
}.execute();
2024-08-12 10:34:26 +08:00
}
} catch (Exception e) {
api.logging().logToError("handleHttpResponseReceived: " + e.getMessage());
2024-05-06 12:56:56 +08:00
}
}
return ResponseReceivedAction.continueWith(httpResponseReceived, annotations);
}
private void setColorAndCommentList(List<Map<String, String>> result) {
2024-06-19 22:16:57 +08:00
if (result != null && !result.isEmpty()) {
2024-05-06 12:56:56 +08:00
colorList.get().add(result.get(0).get("color"));
commentList.get().add(result.get(1).get("comment"));
}
}
}