Skip to content

Commit

Permalink
Merge pull request #195 from blackphreak/feature-import-from-json
Browse files Browse the repository at this point in the history
Implementation for #133
  • Loading branch information
CoreyD97 authored Oct 26, 2023
2 parents 4ae99b7 + 19aefdf commit 9330879
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import burp.api.montoya.http.message.responses.HttpResponse;
import burp.api.montoya.utilities.Base64DecodingOptions;
import burp.api.montoya.utilities.Base64Utils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.nccgroup.loggerplusplus.LoggerPlusPlus;
import com.nccgroup.loggerplusplus.logview.processor.EntryImportWorker;
import lombok.extern.log4j.Log4j2;
import org.apache.logging.log4j.util.Base64Util;
import com.google.gson.Gson;
import com.google.gson.JsonElement;

import javax.swing.*;
import java.io.BufferedReader;
Expand Down Expand Up @@ -78,15 +82,15 @@ public static ArrayList<String> readFile(String filename) {
public static ArrayList<HttpRequestResponse> importWStalker() {
ArrayList<String> lines;
ArrayList<HttpRequestResponse> requests = new ArrayList<>();

String filename = getLoadFile();
if ( filename.length() == 0 ) { // exit if no file selected
return new ArrayList<>();
}

lines = readFile(filename);
Iterator<String> i = lines.iterator();

while (i.hasNext()) {
try {
String line = i.next();
Expand All @@ -113,7 +117,7 @@ public static ArrayList<HttpRequestResponse> importWStalker() {
public static ArrayList<HttpRequestResponse> importZAP() {
ArrayList<String> lines = new ArrayList<String>();
ArrayList<HttpRequestResponse> requests = new ArrayList<HttpRequestResponse>();

String filename = getLoadFile();
if ( filename.length() == 0 ) { // exit if no file selected
return new ArrayList<HttpRequestResponse>();
Expand Down Expand Up @@ -178,7 +182,7 @@ public static ArrayList<HttpRequestResponse> importZAP() {
} catch (Exception e) {
log.error("importZAP: Wrong Path Format");
return new ArrayList<>();
}
}
}

// It's the beginning of a response
Expand All @@ -199,6 +203,60 @@ public static ArrayList<HttpRequestResponse> importZAP() {
return requests;
}

public static ArrayList<HttpRequestResponse> importFromExportedJson() {
ArrayList<HttpRequestResponse> requests = new ArrayList<>();

String filename = getLoadFile();
if ( filename.length() == 0 ) { // exit if no file selected
return new ArrayList<>();
}

BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException e) {
log.error("LoggerImport-readFile: Error Opening File " + filename);
return new ArrayList<>();
}

// declare all required variables for re-use in runtime
Gson gson = LoggerPlusPlus.gsonProvider.getGson();
JsonArray arr = gson.fromJson(reader, JsonElement.class).getAsJsonArray();
Base64Utils b64Decoder = LoggerPlusPlus.montoya.utilities().base64Utils();
JsonObject obj, req, res;
HttpService httpService;
HttpRequest httpRequest;
HttpResponse httpResponse;
HttpRequestResponse requestResponse;
String url;
String[] v = new String[2];

Iterator<JsonElement> i = arr.iterator();
while (i.hasNext()) {
obj = i.next().getAsJsonObject();
req = obj.getAsJsonObject("Request");
res = obj.getAsJsonObject("Response");

url = req.get("URL").getAsString();
v[0] = req.get("AsBase64").getAsString();
v[1] = res.get("AsBase64").getAsString();

try {
httpService = HttpService.httpService(url);
httpRequest = HttpRequest.httpRequest(httpService, b64Decoder.decode(v[0]));
httpResponse = HttpResponse.httpResponse(b64Decoder.decode(v[1]));
requestResponse = HttpRequestResponse.httpRequestResponse(httpRequest, httpResponse);

requests.add(requestResponse);
} catch (Exception e) {
log.error("LoggerImport-importFromExportedJson: Error Parsing Content", e);

}
}

return requests;
}

public static boolean loadImported(ArrayList<HttpRequestResponse> requests, Boolean sendToAutoExporters) {
EntryImportWorker importWorker = LoggerPlusPlus.instance.getLogProcessor().createEntryImportBuilder()
.setOriginatingTool(ToolType.EXTENSIONS)
Expand All @@ -210,10 +268,10 @@ public static boolean loadImported(ArrayList<HttpRequestResponse> requests, Bool
})
.setCallback(() -> {
//Optional
//Called when all entries have been imported.
}).build();
//Called when all entries have been imported.
}).build();
importWorker.execute();

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@ public void actionPerformed(ActionEvent e) {
}
}));

importGroup.add(new JButton(new AbstractAction("Import From Exported JSON") {
@Override
public void actionPerformed(ActionEvent e) {
ArrayList<HttpRequestResponse> requests = LoggerImport.importFromExportedJson();
if (LoggerPlusPlus.instance.getExportController().getEnabledExporters().size() > 0) {
int res = JOptionPane.showConfirmDialog(LoggerPlusPlus.instance.getLoggerFrame(),
"One or more auto-exporters are currently enabled. " +
"Do you want the imported entries to also be sent to the auto-exporters?",
"Auto-exporters Enabled", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
LoggerImport.loadImported(requests, res == JOptionPane.YES_OPTION);
} else {
LoggerImport.loadImported(requests, false);
}
}
}));

ComponentGroup exportGroup = new ComponentGroup(Orientation.HORIZONTAL);
HashMap<Class<? extends LogExporter>, LogExporter> exporters = LoggerPlusPlus.instance
.getExportController().getExporters();
Expand Down

0 comments on commit 9330879

Please sign in to comment.