-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathLoggerImport.java
More file actions
319 lines (264 loc) · 11.4 KB
/
Copy pathLoggerImport.java
File metadata and controls
319 lines (264 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//
// Burp Suite Logger++
//
// Released as open source by NCC Group Plc - https://www.nccgroup.trust/
//
// Originally Developed by Soroush Dalili (@irsdl)
// Maintained by Corey Arthur (@CoreyD97)
//
// Project link: http://www.github.com/nccgroup/BurpSuiteLoggerPlusPlus
//
// Released under AGPL see LICENSE for more information
//
package com.nccgroup.loggerplusplus.imports;
import burp.api.montoya.core.ByteArray;
import burp.api.montoya.core.ToolType;
import burp.api.montoya.http.HttpService;
import burp.api.montoya.http.message.HttpRequestResponse;
import burp.api.montoya.http.message.requests.HttpRequest;
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.logentry.ImportingLogEntryHttpRequestResponse;
import com.nccgroup.loggerplusplus.logview.processor.EntryImportWorker;
import lombok.extern.log4j.Log4j2;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Iterator;
@Log4j2
public class LoggerImport {
private static final String COMMENT_IMPORTED_MARKER = "[Imported from JSON]";
public static String getLoadFile() {
JFileChooser chooser = null;
chooser = new JFileChooser();
chooser.setDialogTitle("Import File");
int val = chooser.showOpenDialog(null);
if (val == JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile().getAbsolutePath();
}
return "";
}
public static ArrayList<String> readFile(String filename) {
BufferedReader reader;
ArrayList<String> lines = new ArrayList<String>();
try {
reader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException e) {
log.error("LoggerImport-readFile: Error Opening File " + filename);
return new ArrayList<String>();
}
try {
String line;
while ( (line = reader.readLine()) != null ) {
lines.add(line);
}
} catch (IOException e) {
log.error("LoggerImport-readFile: Error Reading Line");
return new ArrayList<String>();
}
return lines;
}
public static ArrayList<ImportingLogEntryHttpRequestResponse> importWStalker() {
ArrayList<String> lines;
ArrayList<ImportingLogEntryHttpRequestResponse> 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();
String[] v = line.split(","); // Format: "base64(request),base64(response),url"
String url = v[3];
Base64Utils b64Decoder = LoggerPlusPlus.montoya.utilities().base64Utils();
HttpService httpService = HttpService.httpService(url);
HttpRequest httpRequest = HttpRequest.httpRequest(httpService, b64Decoder.decode(v[0], Base64DecodingOptions.URL));
HttpResponse httpResponse = HttpResponse.httpResponse(b64Decoder.decode(v[1], Base64DecodingOptions.URL));
requests.add(new ImportingLogEntryHttpRequestResponse(
HttpRequestResponse.httpRequestResponse(httpRequest, httpResponse)
));
} catch (Exception e) {
log.error("LoggerImport-importWStalker: Error Parsing Content");
return new ArrayList<>();
}
}
return requests;
}
public static ArrayList<ImportingLogEntryHttpRequestResponse> importZAP() {
ArrayList<String> lines = new ArrayList<String>();
ArrayList<ImportingLogEntryHttpRequestResponse> requests = new ArrayList<ImportingLogEntryHttpRequestResponse>();
String filename = getLoadFile();
if ( filename.length() == 0 ) { // exit if no file selected
return new ArrayList<ImportingLogEntryHttpRequestResponse>();
}
lines = readFile(filename);
Iterator<String> i = lines.iterator();
// Format:
// ===[0-9]+ ==========
// REQUEST
// <empty>
// RESPONSE
String reSeparator = "^=+ ?[0-9]+ ?=+$";
String reResponse = "^HTTP/[0-9]\\.[0-9] [0-9]+ .*$";
// Ignore first line, since it should be a separator
if ( i.hasNext() ) {
i.next();
}
boolean isRequest = true;
String requestBuffer = "";
String responseBuffer = "";
String url = "";
// Loop lines
while (i.hasNext()) {
String line = i.next();
// Request and Response Ready
if ( line.matches(reSeparator) || !i.hasNext() ) {
// TODO: Remove one or two \n at the end of requestBuffer
HttpService httpService = HttpService.httpService(url);
HttpRequest httpRequest = HttpRequest.httpRequest(httpService, requestBuffer);
HttpResponse httpResponse = HttpResponse.httpResponse(responseBuffer);
requests.add(new ImportingLogEntryHttpRequestResponse(
HttpRequestResponse.httpRequestResponse(httpRequest, httpResponse)
));
// Reset content
isRequest = true;
requestBuffer = "";
responseBuffer = "";
url = "";
continue;
}
// It's the beginning of a request
if ( requestBuffer.length() == 0 ) {
try {
// Expected format: "GET https://whatever/whatever.html HTTP/1.1"
String[] x = line.split(" ");
url = x[1];
URL u = new URL(url);
String path = u.getPath();
line = x[0] + " " + path + " " + x[2]; // fix the path in the request
} catch (Exception e) {
log.error("importZAP: Wrong Path Format");
return new ArrayList<>();
}
}
// It's the beginning of a response
if ( line.matches(reResponse) ) {
isRequest = false;
}
// Add line to the corresponding buffer
if ( isRequest ) {
requestBuffer += line;
requestBuffer += "\n";
} else {
responseBuffer += line;
responseBuffer += "\n";
}
}
return requests;
}
public static ArrayList<ImportingLogEntryHttpRequestResponse> importFromExportedJson() {
ArrayList<ImportingLogEntryHttpRequestResponse> 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, jsonEntry;
HttpService httpService;
HttpRequest httpRequest;
HttpResponse httpResponse;
HttpRequestResponse requestResponse = null;
String url;
String[] v = new String[2];
ImportingLogEntryHttpRequestResponse logEntry;
Iterator<JsonElement> iter = arr.iterator();
StringBuilder comment = new StringBuilder();
while (iter.hasNext()) {
obj = iter.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);
} catch (Exception e) {
log.error("LoggerImport-importFromExportedJson: Error Parsing Content", e);
}
logEntry = new ImportingLogEntryHttpRequestResponse(requestResponse);
logEntry.setRequestTime(req.get("Time").getAsString());
logEntry.setResponseTime(res.get("Time").getAsString());
// might not exist
if (req.has("Tool")) {
logEntry.setTool(req.get("Tool").getAsString());
}
if (res.has("RTT")) {
logEntry.setRTT(res.get("RTT").getAsInt());
}
jsonEntry = obj.getAsJsonObject("Entry");
if (jsonEntry.has("ListenInterface")) {
logEntry.setListenInterface(jsonEntry.get("ListenInterface").getAsString());
}
comment.setLength(0); // empty the string
if (req.has("Comment")) {
comment.append(req.get("Comment").getAsString());
// prevent duplicated 'imported' marker
if (comment.indexOf(COMMENT_IMPORTED_MARKER) == -1)
{
comment.insert(0, " ");
comment.insert(0, COMMENT_IMPORTED_MARKER);
}
}
else {
comment.insert(0, COMMENT_IMPORTED_MARKER);
}
logEntry.setComment(comment.toString());
requests.add(logEntry);
}
return requests;
}
//TODO Integrate progress bar with SwingWorkerWithProgressDialog
public static boolean loadImported(ArrayList<ImportingLogEntryHttpRequestResponse> requests, Boolean sendToAutoExporters) {
EntryImportWorker importWorker = LoggerPlusPlus.instance.getLogProcessor().createEntryImportBuilder()
.setOriginatingTool(ToolType.EXTENSIONS)
.setHttpEntries(requests)
.setSendToAutoExporters(sendToAutoExporters)
.setInterimConsumer(integers -> {
//Optional
//Outputs chunks of integers representing imported indices
//May be used to update progress bar for example
})
.setCallback(() -> {
//Optional
//Called when all entries have been imported.
}).build();
importWorker.execute();
return true;
}
}