A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.
Java application-securityappsecbug-bountyburp-alternativeburp-suitedasthttp-proxyinfosecintercepting-proxyjavaowaspowasp-zappenetration-testingpentestingsecuritysecurity-toolsvulnerability-scannerweb-application-securityweb-security
package opensuite.core;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Settings > Project > Logging: requests and responses written to log
* files as the tools make them. Each tick box on that page has a file of its
* own; "All tools" logs every tool's traffic to its file as well.
*/
public final class TrafficLog implements MessageStore.Listener {
/** The tools the page lists, by the name their settings use. */
public static final String[] TOOLS = { "all-tools", "proxy", "scanner", "intruder", "repeater", "sequencer", "extensions" };
public static String key(String tool, boolean response) {
return "logging." + tool + (response ? ".responses" : ".requests");
}
public static String fileKey(String tool, boolean response) { return key(tool, response) + ".file"; }
private static final String RULE = "======================================================\r\n";
private final Settings settings;
/** File writes are kept off the threads that carry traffic, and in order. */
private final ExecutorService writer = Executors.newSingleThreadExecutor(r -> {
Thread t = new Thread(r, "traffic-log");
t.setDaemon(true);
return t;
});
private final Set<LogEntry> requestDone = Collections.synchronizedSet(Collections.newSetFromMap(new WeakHashMap<>()));
private final Set<LogEntry> responseDone = Collections.synchronizedSet(Collections.newSetFromMap(new WeakHashMap<>()));
private volatile String lastError = "";
public TrafficLog(Settings settings) { this.settings = settings; }
/** The settings name of the tool an entry came from. */
static String toolOf(String entryTool) {
if (entryTool == null) return "";
if (ProjectOptions.AUDIT_TOOL.equals(entryTool)) return "scanner";
if (entryTool.startsWith("extension")) return "extensions";
return entryTool;
}
@Override public void entryAdded(LogEntry e) { log(e); }
@Override public void entryUpdated(LogEntry e) { log(e); }
private void log(LogEntry e) {
if (!anyOn()) return;
String tool = toolOf(e.tool());
if (requestDone.add(e)) write(tool, false, e.service(), e.time(), e.remoteIp(), e.request());
HttpMessage resp = e.response();
if (resp != null && resp.length() > 0 && responseDone.add(e)) write(tool, true, e.service(), new Date(), e.remoteIp(), resp);
}
/** Log an exchange a tool made without keeping it in the history. */
public void record(String tool, HttpService svc, HttpMessage request, HttpMessage response) {
if (!anyOn()) return;
Date now = new Date();
if (request != null) write(tool, false, svc, now, "", request);
if (response != null) write(tool, true, svc, now, "", response);
}
/** A quick answer for the common case that nothing is being logged. */
private boolean anyOn() {
for (String t : TOOLS) {
if ("true".equals(settings.get(key(t, false), "false")) || "true".equals(settings.get(key(t, true), "false"))) return true;
}
return false;
}
private void write(String tool, boolean response, HttpService svc, Date when, String ip, HttpMessage message) {
if (message == null) return;
for (String t : new String[] { "all-tools", tool }) {
if (!"true".equals(settings.get(key(t, response), "false"))) continue;
String path = settings.get(fileKey(t, response), "");
if (path.isBlank()) continue;
byte[] block = block(svc, when, ip, message);
writer.execute(() -> append(new File(path), block));
}
}
static byte[] block(HttpService svc, Date when, String ip, HttpMessage message) {
StringBuilder head = new StringBuilder(RULE);
head.append(new SimpleDateFormat("h:mm:ss a").format(when)).append(" ").append(svc == null ? "" : svc.url());
if (ip != null && !ip.isEmpty()) head.append(" [").append(ip).append("]");
head.append("\r\n").append(RULE);
byte[] raw = message.raw();
return Bytes.concat(Bytes.concat(head.toString().getBytes(StandardCharsets.UTF_8), raw),
("\r\n" + RULE + "\r\n\r\n").getBytes(StandardCharsets.UTF_8));
}
private void append(File f, byte[] block) {
try (OutputStream out = new FileOutputStream(f, true)) {
out.write(block);
} catch (IOException ex) {
// Reported once per file and cause, not once per request.
String what = f + ": " + ex.getMessage();
if (!what.equals(lastError)) { lastError = what; Debug.error("could not write the traffic log " + f, ex); }
}
}
/** Wait for the writes queued so far; for tests and for shutdown. */
public void flush() {
try { writer.submit(() -> { }).get(); } catch (Exception ignored) { }
}
}