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.engagement;
import opensuite.core.HttpMessage;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Pulls the comments and scripts out of a response, for the Find comments and
* Find scripts engagement tools. Pure text work with no Swing in it.
*/
public final class ContentFinder {
private ContentFinder() {}
private static final Pattern SCRIPT = Pattern.compile(
"<script\\b([^>]*)>(.*?)</script\\s*>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
private static final Pattern SRC = Pattern.compile(
"\\bsrc\\s*=\\s*(?:\"([^\"]*)\"|'([^']*)'|([^\\s>]+))", Pattern.CASE_INSENSITIVE);
private static final Pattern HTML_COMMENT = Pattern.compile("<!--(.*?)-->", Pattern.DOTALL);
/** True when the response is itself a script rather than a page. */
static boolean isScript(HttpMessage resp) {
String ct = resp.contentType();
String c = ct == null ? "" : ct.toLowerCase();
return c.contains("javascript") || c.contains("ecmascript");
}
private static boolean isText(HttpMessage resp) {
if (resp == null || resp.body().length == 0) return false;
String ct = resp.contentType();
String c = ct == null ? "" : ct.toLowerCase();
return c.isEmpty() || c.contains("html") || c.contains("xml") || c.contains("javascript")
|| c.contains("ecmascript") || c.contains("json") || c.contains("css") || c.startsWith("text/");
}
/**
* Every comment in the response: HTML comments, and the block and line
* comments inside its scripts (or the whole body when it is a script).
*/
public static List<String> comments(HttpMessage resp) {
List<String> out = new ArrayList<>();
if (!isText(resp)) return out;
String body = resp.bodyText();
if (isScript(resp)) {
scriptComments(body, out);
return out;
}
// Scripts are scanned with the script rules and cut out of the page, so
// an "<!--" inside a string literal is not taken for an HTML comment.
StringBuilder page = new StringBuilder();
Matcher m = SCRIPT.matcher(body);
int last = 0;
List<String> inScripts = new ArrayList<>();
while (m.find()) {
page.append(body, last, m.start());
last = m.end();
scriptComments(m.group(2), inScripts);
}
page.append(body.substring(last));
Matcher c = HTML_COMMENT.matcher(page);
while (c.find()) add(out, c.group(1));
out.addAll(inScripts);
return out;
}
/**
* Scan script source for {@code /* *}{@code /} and {@code //} comments,
* stepping over string and template literals so their contents (a URL's
* {@code //}, for one) are not mistaken for comments.
*/
static void scriptComments(String src, List<String> out) {
int i = 0, n = src.length();
while (i < n) {
char ch = src.charAt(i);
if (ch == '"' || ch == '\'' || ch == '`') {
i = skipString(src, i);
} else if (ch == '/' && i + 1 < n && src.charAt(i + 1) == '*') {
int end = src.indexOf("*/", i + 2);
if (end < 0) end = n;
add(out, src.substring(i + 2, end));
i = Math.min(n, end + 2);
} else if (ch == '/' && i + 1 < n && src.charAt(i + 1) == '/') {
int end = i + 2;
while (end < n && src.charAt(end) != '\n' && src.charAt(end) != '\r') end++;
add(out, src.substring(i + 2, end));
i = end;
} else {
i++;
}
}
}
private static int skipString(String src, int start) {
char quote = src.charAt(start);
int i = start + 1;
while (i < src.length()) {
char ch = src.charAt(i);
if (ch == '\\') { i += 2; continue; }
if (ch == quote) return i + 1;
// An unterminated quote ends at the line, except in a template literal.
if (quote != '`' && (ch == '\n' || ch == '\r')) return i;
i++;
}
return src.length();
}
private static void add(List<String> out, String text) {
String t = text.strip();
if (!t.isEmpty()) out.add(t);
}
/** One script found in a response: an external reference, or inline source. */
public record Script(String src, String code) {
public boolean external() { return src != null; }
/** What the results table shows for this script. */
public String summary() {
if (external()) return src;
String c = code.strip().replaceAll("\\s+", " ");
return c.length() > 120 ? c.substring(0, 120) + "..." : c;
}
}
/**
* The scripts in a response: each script element (its src, or its inline
* source), or the whole body when the response is itself a script.
*/
public static List<Script> scripts(HttpMessage resp) {
List<Script> out = new ArrayList<>();
if (!isText(resp)) return out;
String body = resp.bodyText();
if (isScript(resp)) {
if (!body.isBlank()) out.add(new Script(null, body));
return out;
}
Matcher m = SCRIPT.matcher(body);
while (m.find()) {
Matcher s = SRC.matcher(m.group(1));
if (s.find()) {
String v = s.group(1) != null ? s.group(1) : s.group(2) != null ? s.group(2) : s.group(3);
out.add(new Script(v, ""));
} else if (!m.group(2).isBlank()) {
out.add(new Script(null, m.group(2)));
}
}
return out;
}
/**
* True when the response links to the target: it mentions the full URL, or
* the path (with a boundary after it, so /a does not match /ab) while
* coming from the same host. A bare "/" target only counts in full-URL
* form, since every page contains a slash.
*/
public static boolean refersTo(HttpMessage resp, boolean sameHost, String fullUrl, String path) {
if (!isText(resp)) return false;
String body = resp.bodyText();
if (fullUrl != null && !fullUrl.isEmpty() && containsBounded(body, fullUrl)) return true;
if (!sameHost || path == null || path.length() < 2) return false;
return containsBounded(body, path);
}
private static boolean containsBounded(String body, String needle) {
int at = 0;
while ((at = body.indexOf(needle, at)) >= 0) {
int end = at + needle.length();
if (end >= body.length() || needle.endsWith("/")) return true;
char next = body.charAt(end);
if (!Character.isLetterOrDigit(next) && next != '-' && next != '_' && next != '.' && next != '/') return true;
at = end;
}
return false;
}
}