Example usage for org.apache.commons.httpclient URI getScheme

List of usage examples for org.apache.commons.httpclient URI getScheme

Introduction

In this page you can find the example usage for org.apache.commons.httpclient URI getScheme.

Prototype

public String getScheme() 

Source Link

Document

Get the scheme.

Usage

From source file:org.zaproxy.zap.extension.ascanrulesBeta.CrossDomainScanner.java

private void scanSilverlightCrossdomainPolicyFile(URI originalURI)
        throws IOException, XPathExpressionException {
    // retrieve the Silverlight client access policy file, and assess it.
    HttpMessage clientaccesspolicymessage = new HttpMessage(new URI(originalURI.getScheme(),
            originalURI.getAuthority(), "/" + SILVERLIGHT_CROSS_DOMAIN_POLICY_FILE, null, null));
    sendAndReceive(clientaccesspolicymessage, false);

    if (clientaccesspolicymessage.getResponseBody().length() == 0) {
        return;/*from   w  w w .j ava2s  .c  om*/
    }

    byte[] clientaccesspolicymessagebytes = clientaccesspolicymessage.getResponseBody().getBytes();

    // parse the file. If it's not parseable, it might have been because of a 404
    try {
        // work around the "no protocol" issue by wrapping the content in a ByteArrayInputStream
        Document silverlightXmldoc = docBuilder
                .parse(new InputSource(new ByteArrayInputStream(clientaccesspolicymessagebytes)));
        XPathExpression exprAllowFromUri = xpath
                .compile("/access-policy/cross-domain-access/policy/allow-from/domain/@uri"); // gets the uri attributes
        // check the "allow-from" policies
        NodeList exprAllowFromUriNodes = (NodeList) exprAllowFromUri.evaluate(silverlightXmldoc,
                XPathConstants.NODESET);
        for (int i = 0; i < exprAllowFromUriNodes.getLength(); i++) {
            String uri = exprAllowFromUriNodes.item(i).getNodeValue();
            if (uri.equals("*")) {
                // tut, tut, tut.
                if (log.isDebugEnabled())
                    log.debug("Bingo! " + SILVERLIGHT_CROSS_DOMAIN_POLICY_FILE
                            + ", at /access-policy/cross-domain-access/policy/allow-from/domain/@uri");
                bingo(getRisk(), Alert.CONFIDENCE_MEDIUM,
                        Constant.messages.getString(MESSAGE_PREFIX_SILVERLIGHT + "name"),
                        Constant.messages.getString(MESSAGE_PREFIX_SILVERLIGHT + "desc"),
                        clientaccesspolicymessage.getRequestHeader().getURI().getURI(), // the url field
                        "", // parameter being attacked: none.
                        "", // attack
                        Constant.messages.getString(MESSAGE_PREFIX_SILVERLIGHT + "extrainfo"), // extrainfo
                        Constant.messages.getString(MESSAGE_PREFIX_SILVERLIGHT + "soln"), // solution
                        "<domain uri=\"*\"", // evidence
                        clientaccesspolicymessage // the message on which to place the alert
                );
            }
        }

    } catch (SAXException | IOException e) {
        // Could well be a 404 or equivalent
        log.debug(
                "An error occurred trying to parse " + SILVERLIGHT_CROSS_DOMAIN_POLICY_FILE + " as XML: " + e);
    }
}

From source file:org.zaproxy.zap.extension.ascanrulesBeta.RemoteCodeExecutionCVE20121823.java

private static URI createAttackUri(URI originalURI, String attackParam) {
    StringBuilder strBuilder = new StringBuilder();
    strBuilder.append(originalURI.getScheme()).append("://").append(originalURI.getEscapedAuthority());
    strBuilder.append(originalURI.getRawPath() != null ? originalURI.getEscapedPath() : "/")
            .append(attackParam);//from w ww  . j  a  v a2s .  c  o m
    String uri = strBuilder.toString();
    try {
        return new URI(uri, true);
    } catch (URIException e) {
        log.warn("Failed to create attack URI [" + uri + "], cause: " + e.getMessage());
    }
    return null;
}

From source file:org.zaproxy.zap.extension.ascanrulesBeta.SessionFixation.java

/**
 * scans all GET, Cookie params for Session fields, and looks for SessionFixation
 * vulnerabilities/* w  w w  . j  ava 2 s  . c o  m*/
 */
@Override
public void scan() {

    // TODO: scan the POST (form) params for session id fields.
    try {
        boolean loginUrl = false;

        // Are we dealing with a login url in any of the contexts of which this uri is part
        URI requestUri = getBaseMsg().getRequestHeader().getURI();
        ExtensionAuthentication extAuth = (ExtensionAuthentication) Control.getSingleton().getExtensionLoader()
                .getExtension(ExtensionAuthentication.NAME);

        // using the session, get the list of contexts for the url
        List<Context> contextList = extAuth.getModel().getSession().getContextsForUrl(requestUri.getURI());

        // now loop, and see if the url is a login url in each of the contexts in turn...
        for (Context context : contextList) {
            URI loginUri = extAuth.getLoginRequestURIForContext(context);
            if (loginUri != null && requestUri.getPath() != null) {
                if (requestUri.getScheme().equals(loginUri.getScheme())
                        && requestUri.getHost().equals(loginUri.getHost())
                        && requestUri.getPort() == loginUri.getPort()
                        && requestUri.getPath().equals(loginUri.getPath())) {
                    // we got this far.. only the method (GET/POST), user details, query params,
                    // fragment, and POST params
                    // are possibly different from the login page.
                    loginUrl = true;
                    break;
                }
            }
        }

        // For now (from Zap 2.0), the Session Fixation scanner will only run for login pages
        if (loginUrl == false) {
            log.debug("For the Session Fixation scanner to actually do anything, a Login Page *must* be set!");
            return;
        }
        // find all params set in the request (GET/POST/Cookie)
        // Note: this will be the full set, before we delete anything.

        TreeSet<HtmlParameter> htmlParams = new TreeSet<>();
        htmlParams.addAll(getBaseMsg().getRequestHeader().getCookieParams()); // request cookies only. no response cookies
        htmlParams.addAll(getBaseMsg().getFormParams()); // add in the POST params
        htmlParams.addAll(getBaseMsg().getUrlParams()); // add in the GET params

        // Now add in the pseudo parameters set in the URL itself, such as in the following:
        // http://www.example.com/someurl;JSESSIONID=abcdefg?x=123&y=456
        // as opposed to the url parameters in the following example, which are already picked
        // up by getUrlParams()
        // http://www.example.com/someurl?JSESSIONID=abcdefg&x=123&y=456

        // convert from org.apache.commons.httpclient.URI to a String
        String requestUrl = "Unknown URL";
        try {
            requestUrl = new URL(requestUri.getScheme(), requestUri.getHost(), requestUri.getPort(),
                    requestUri.getPath()).toString();
        } catch (Exception e) {
            // no point in continuing. The URL is invalid.  This is a peculiarity in the Zap
            // core,
            // and can happen when
            // - the user browsed to http://www.example.com/bodgeit and
            // - the user did not browse to http://www.example.com or to http://www.example.com/
            // so the Zap GUI displays "http://www.example.com" as a node under "Sites",
            // and under that, it displays the actual urls to which the user browsed
            // (http://www.example.com/bodgeit, for instance)
            // When the user selects the node "http://www.example.com", and tries to scan it
            // with
            // the session fixation scanner, the URI that is passed is "http://www.example.com",
            // which is *not* a valid url.
            // If the user actually browses to "http://www.example.com" (even without the
            // trailing slash)
            // the web browser appends the trailing slash, and so Zap records the URI as
            // "http://www.example.com/", which IS a valid url, and which can (and should) be
            // scanned.
            //
            // In short.. if this happens, we do not want to scan the URL anyway
            // (because the user never browsed to it), so just do nothing instead.

            log.error("Cannot convert URI [" + requestUri + "] to a URL: " + e.getMessage());
            return;
        }

        // suck out any pseudo url parameters from the url
        Set<HtmlParameter> pseudoUrlParams = getPseudoUrlParameters(requestUrl);
        htmlParams.addAll(pseudoUrlParams);
        if (this.debugEnabled)
            log.debug("Pseudo url params of URL [" + requestUrl + "] : [" + pseudoUrlParams + "]");

        //// for each parameter in turn,
        // int counter = 0;
        for (Iterator<HtmlParameter> iter = htmlParams.iterator(); iter.hasNext();) {

            HttpMessage msg1Final;
            HttpMessage msg1Initial = getNewMsg();

            //// debug logic only.. to do first field only
            // counter ++;
            // if ( counter > 1 )
            //   return;

            HtmlParameter currentHtmlParameter = iter.next();

            // Useful for debugging, but I can't find a way to view this data in the GUI, so
            // leave it out for now.
            // msg1Initial.setNote("Message 1 for parameter "+ currentHtmlParameter);

            if (this.debugEnabled)
                log.debug("Scanning URL [" + msg1Initial.getRequestHeader().getMethod() + "] ["
                        + msg1Initial.getRequestHeader().getURI() + "], [" + currentHtmlParameter.getType()
                        + "] field [" + currentHtmlParameter.getName() + "] with value ["
                        + currentHtmlParameter.getValue() + "] for Session Fixation");

            if (currentHtmlParameter.getType().equals(HtmlParameter.Type.cookie)) {

                // careful to pick up the cookies from the Request, and not to include cookies
                // set in any earlier response
                TreeSet<HtmlParameter> cookieRequestParams = msg1Initial.getRequestHeader().getCookieParams();
                // delete the original cookie from the parameters
                cookieRequestParams.remove(currentHtmlParameter);
                msg1Initial.setCookieParams(cookieRequestParams);

                // send the message, minus the cookie parameter, and see how it comes back.
                // Note: do NOT automatically follow redirects.. handle those here instead.
                sendAndReceive(msg1Initial, false, false);

                /////////////////////////////
                // create a copy of msg1Initial to play with to handle redirects (if any).
                // we use a copy because if we change msg1Initial itself, it messes the URL and
                // params displayed on the GUI.

                msg1Final = msg1Initial;
                HtmlParameter cookieBack1 = getResponseCookie(msg1Initial, currentHtmlParameter.getName());
                long cookieBack1TimeReceived = System.currentTimeMillis(); // in ms.  when was the cookie received?
                // Important if it has a Max-Age directive
                Date cookieBack1ExpiryDate = null;

                HttpMessage temp = msg1Initial;

                int redirectsFollowed1 = 0;
                while (HttpStatusCode.isRedirection(temp.getResponseHeader().getStatusCode())) {

                    // Note that we need to clone the Request and the Response..
                    // we seem to need to track the secure flag now to make sure its set later
                    boolean secure1 = temp.getRequestHeader().isSecure();
                    temp = temp.cloneAll(); // clone the previous message

                    redirectsFollowed1++;
                    if (redirectsFollowed1 > 10) {
                        throw new Exception("Too many redirects were specified in the first message");
                    }
                    // create a new URI from the absolute location returned, and interpret it as
                    // escaped
                    // note that the standard says that the Location returned should be
                    // absolute, but it ain't always so...
                    URI newLocation = new URI(temp.getResponseHeader().getHeader(HttpHeader.LOCATION), true);

                    // and follow the forward url
                    // need to clear the params (which would come from the initial POST,
                    // otherwise)
                    temp.getRequestHeader().setGetParams(new TreeSet<HtmlParameter>());
                    temp.setRequestBody("");
                    temp.setResponseBody(""); // make sure no values accidentally carry from one iteration to
                    // the next
                    try {
                        temp.getRequestHeader().setURI(newLocation);
                    } catch (Exception e) {
                        // the Location field contents may not be standards compliant. Lets
                        // generate a uri to use as a workaround where a relative path was
                        // given instead of an absolute one
                        URI newLocationWorkaround = new URI(temp.getRequestHeader().getURI(),
                                temp.getResponseHeader().getHeader(HttpHeader.LOCATION), true);
                        // try again, except this time, if it fails, don't try to handle it
                        if (this.debugEnabled)
                            log.debug("The Location [" + newLocation
                                    + "] specified in a redirect was not valid. Trying workaround url ["
                                    + newLocationWorkaround + "]");
                        temp.getRequestHeader().setURI(newLocationWorkaround);
                    }
                    temp.getRequestHeader().setSecure(secure1);
                    temp.getRequestHeader().setMethod(HttpRequestHeader.GET);
                    temp.getRequestHeader().setContentLength(0); // since we send a GET, the body will be 0 long
                    if (cookieBack1 != null) {
                        // if the previous request sent back a cookie, we need to set that
                        // cookie when following redirects, as a browser would
                        if (this.debugEnabled)
                            log.debug("Adding in cookie [" + cookieBack1 + "] for a redirect");
                        TreeSet<HtmlParameter> forwardCookieParams = temp.getRequestHeader().getCookieParams();
                        forwardCookieParams.add(cookieBack1);
                        temp.getRequestHeader().setCookieParams(forwardCookieParams);
                    }

                    if (this.debugEnabled)
                        log.debug("DEBUG: Cookie Message 1 causes us to follow redirect to [" + newLocation
                                + "]");

                    sendAndReceive(temp, false, false); // do NOT redirect.. handle it here

                    // handle any cookies set from following redirects that override the cookie
                    // set in the redirect itself (if any)
                    // note that this will handle the case where a latter cookie unsets one set
                    // earlier.
                    HtmlParameter cookieBack1Temp = getResponseCookie(temp, currentHtmlParameter.getName());
                    if (cookieBack1Temp != null) {
                        cookieBack1 = cookieBack1Temp;
                        cookieBack1TimeReceived = System.currentTimeMillis(); // in ms.  record when we got the
                        // cookie.. in case it has a
                        // Max-Age directive
                    }

                    // reset the "final" version of message1 to use the final response in the
                    // chain
                    msg1Final = temp;
                }
                ///////////////////////////

                // if non-200 on the final response for message 1, no point in continuing. Bale
                // out.
                if (msg1Final.getResponseHeader().getStatusCode() != HttpStatusCode.OK) {
                    if (this.debugEnabled)
                        log.debug(
                                "Got a non-200 response code [" + msg1Final.getResponseHeader().getStatusCode()
                                        + "] when sending [" + msg1Initial.getRequestHeader().getURI()
                                        + "] with param [" + currentHtmlParameter.getName()
                                        + "] = NULL (possibly somewhere in the redirects)");
                    continue;
                }

                // now check that the response set a cookie. if it didn't, then either..
                // 1) we are messing with the wrong field
                // 2) the app doesn't do sessions
                // either way, there is not much point in continuing to look at this field..

                if (cookieBack1 == null || cookieBack1.getValue() == null) {
                    // no cookie was set, or the cookie param was set to a null value
                    if (this.debugEnabled)
                        log.debug("The Cookie parameter was NOT set in the response, when cookie param ["
                                + currentHtmlParameter.getName() + "] was set to NULL: " + cookieBack1);
                    continue;
                }

                //////////////////////////////////////////////////////////////////////
                // at this point, before continuing to check for Session Fixation, do some other
                // checks on the session cookie we got back
                // that might cause us to raise additional alerts (in addition to doing the main
                // check for Session Fixation)
                //////////////////////////////////////////////////////////////////////

                // Check 1: was the session cookie sent and received securely by the server?
                // If not, alert this fact
                if ((!msg1Final.getRequestHeader().isSecure())
                        || (!cookieBack1.getFlags().contains("secure"))) {
                    // pass the original param value here, not the new value, since we're
                    // displaying the session id exposed in the original message
                    String extraInfo = Constant.messages.getString(
                            "ascanbeta.sessionidsentinsecurely.alert.extrainfo", currentHtmlParameter.getType(),
                            currentHtmlParameter.getName(), currentHtmlParameter.getValue());
                    if (!cookieBack1.getFlags().contains("secure")) {
                        extraInfo += ("\n" + Constant.messages.getString(
                                "ascanbeta.sessionidsentinsecurely.alert.extrainfo.secureflagnotset"));
                    }

                    // and figure out the risk, depending on whether it is a login page
                    int risk = Alert.RISK_LOW;
                    if (loginUrl) {
                        extraInfo += ("\n" + Constant.messages
                                .getString("ascanbeta.sessionidsentinsecurely.alert.extrainfo.loginpage"));
                        // login page, so higher risk
                        risk = Alert.RISK_MEDIUM;
                    } else {
                        // not a login page.. lower risk
                        risk = Alert.RISK_LOW;
                    }

                    String attack = Constant.messages.getString(
                            "ascanbeta.sessionidsentinsecurely.alert.attack", currentHtmlParameter.getType(),
                            currentHtmlParameter.getName());
                    String vulnname = Constant.messages.getString("ascanbeta.sessionidsentinsecurely.name");
                    String vulndesc = Constant.messages.getString("ascanbeta.sessionidsentinsecurely.desc");
                    String vulnsoln = Constant.messages.getString("ascanbeta.sessionidsentinsecurely.soln");

                    // call bingo with some extra info, indicating that the alert is
                    // not specific to Session Fixation, but has its own title and description
                    // (etc)
                    // the alert here is "Session id sent insecurely", or words to that effect.
                    bingo(risk, Alert.CONFIDENCE_MEDIUM, vulnname, vulndesc,
                            getBaseMsg().getRequestHeader().getURI().getURI(), currentHtmlParameter.getName(),
                            attack, extraInfo, vulnsoln, getBaseMsg());

                    if (log.isDebugEnabled()) {
                        String logMessage = MessageFormat.format(
                                "A session identifier in {2} field: [{3}] may be sent "
                                        + "via an insecure mechanism at [{0}] URL [{1}]",
                                getBaseMsg().getRequestHeader().getMethod(),
                                getBaseMsg().getRequestHeader().getURI().getURI(),
                                currentHtmlParameter.getType(), currentHtmlParameter.getName());
                        log.debug(logMessage);
                    }
                    // Note: do NOT continue to the next field at this point..
                    // since we still need to check for Session Fixation.
                }

                //////////////////////////////////////////////////////////////////////
                // Check 2: is the session cookie that was set accessible to Javascript?
                // If so, alert this fact too
                if (!cookieBack1.getFlags().contains("httponly") && loginUrl) {
                    // pass the original param value here, not the new value, since we're
                    // displaying the session id exposed in the original message
                    String extraInfo = Constant.messages.getString(
                            "ascanbeta.sessionidaccessiblebyjavascript.alert.extrainfo",
                            currentHtmlParameter.getType(), currentHtmlParameter.getName(),
                            currentHtmlParameter.getValue());
                    String attack = Constant.messages.getString(
                            "ascanbeta.sessionidaccessiblebyjavascript.alert.attack",
                            currentHtmlParameter.getType(), currentHtmlParameter.getName());
                    String vulnname = Constant.messages
                            .getString("ascanbeta.sessionidaccessiblebyjavascript.name");
                    String vulndesc = Constant.messages
                            .getString("ascanbeta.sessionidaccessiblebyjavascript.desc");
                    String vulnsoln = Constant.messages
                            .getString("ascanbeta.sessionidaccessiblebyjavascript.soln");

                    extraInfo += ("\n" + Constant.messages
                            .getString("ascanbeta.sessionidaccessiblebyjavascript.alert.extrainfo.loginpage"));

                    // call bingo with some extra info, indicating that the alert is
                    // not specific to Session Fixation, but has its own title and description
                    // (etc)
                    // the alert here is "Session id accessible in Javascript", or words to that
                    // effect.
                    bingo(Alert.RISK_LOW, Alert.CONFIDENCE_MEDIUM, vulnname, vulndesc,
                            getBaseMsg().getRequestHeader().getURI().getURI(), currentHtmlParameter.getName(),
                            attack, extraInfo, vulnsoln, getBaseMsg());

                    if (log.isDebugEnabled()) {
                        String logMessage = MessageFormat.format(
                                "A session identifier in [{0}] URL [{1}] {2} field: "
                                        + "[{3}] may be accessible to JavaScript",
                                getBaseMsg().getRequestHeader().getMethod(),
                                getBaseMsg().getRequestHeader().getURI().getURI(),
                                currentHtmlParameter.getType(), currentHtmlParameter.getName());
                        log.debug(logMessage);
                    }
                    // Note: do NOT continue to the next field at this point..
                    // since we still need to check for Session Fixation.
                }

                //////////////////////////////////////////////////////////////////////
                // Check 3: is the session cookie set to expire soon? when the browser session
                // closes? never?
                // the longer the session cookie is valid, the greater the risk. alert it
                // accordingly
                String cookieBack1Expiry = null;
                int sessionExpiryRiskLevel;
                String sessionExpiryDescription = null;

                // check for the Expires header
                for (Iterator<String> i = cookieBack1.getFlags().iterator(); i.hasNext();) {
                    String cookieBack1Flag = i.next();
                    // if ( this.debugEnabled ) log.debug("Cookie back 1 flag (checking for
                    // Expires): "+ cookieBack1Flag);
                    // match in a case insensitive manner. never know what case various web
                    // servers are going to send back.
                    // if (cookieBack1Flag.matches("(?i)expires=.*")) {
                    if (cookieBack1Flag.toLowerCase(Locale.ENGLISH).startsWith("expires=")) {
                        String[] cookieBack1FlagValues = cookieBack1Flag.split("=");
                        if (cookieBack1FlagValues.length > 1) {
                            if (this.debugEnabled)
                                log.debug("Cookie Expiry: " + cookieBack1FlagValues[1]);
                            cookieBack1Expiry = cookieBack1FlagValues[1]; // the Date String
                            sessionExpiryDescription = cookieBack1FlagValues[1]; // the Date String
                            cookieBack1ExpiryDate = DateUtil.parseDate(cookieBack1Expiry); // the actual Date
                        }
                    }
                }

                // also check for the Max-Age header, which overrides the Expires header.
                // WARNING: this Directive is reported to be ignored by IE, so if both Expires
                // and Max-Age are present
                // and we report based on the Max-Age value, but the user is using IE, then the
                // results reported
                // by us here may be different from those actually experienced by the user! (we
                // use Max-Age, IE uses Expires)
                for (Iterator<String> i = cookieBack1.getFlags().iterator(); i.hasNext();) {
                    String cookieBack1Flag = i.next();
                    // if ( this.debugEnabled ) log.debug("Cookie back 1 flag (checking for
                    // Max-Age): "+ cookieBack1Flag);
                    // match in a case insensitive manner. never know what case various web
                    // servers are going to send back.
                    if (cookieBack1Flag.toLowerCase(Locale.ENGLISH).startsWith("max-age=")) {
                        String[] cookieBack1FlagValues = cookieBack1Flag.split("=");
                        if (cookieBack1FlagValues.length > 1) {
                            // now the Max-Age value is the number of seconds relative to the
                            // time the browser received the cookie
                            // (as stored in cookieBack1TimeReceived)
                            if (this.debugEnabled)
                                log.debug("Cookie Max Age: " + cookieBack1FlagValues[1]);
                            long cookie1DropDeadMS = cookieBack1TimeReceived
                                    + (Long.parseLong(cookieBack1FlagValues[1]) * 1000);

                            cookieBack1ExpiryDate = new Date(cookie1DropDeadMS); // the actual Date the cookie
                            // expires (by Max-Age)
                            cookieBack1Expiry = DateUtil.formatDate(cookieBack1ExpiryDate,
                                    DateUtil.PATTERN_RFC1123);
                            sessionExpiryDescription = cookieBack1Expiry; // needs to the Date String
                        }
                    }
                }
                String sessionExpiryRiskDescription = null;
                // check the Expiry/Max-Age details garnered (if any)

                // and figure out the risk, depending on whether it is a login page
                // and how long the session will live before expiring
                if (cookieBack1ExpiryDate == null) {
                    // session expires when the browser closes.. rate this as medium risk?
                    sessionExpiryRiskLevel = Alert.RISK_MEDIUM;
                    sessionExpiryRiskDescription = "ascanbeta.sessionidexpiry.browserclose";
                    sessionExpiryDescription = Constant.messages.getString(sessionExpiryRiskDescription);
                } else {
                    long datediffSeconds = (cookieBack1ExpiryDate.getTime() - cookieBack1TimeReceived) / 1000;
                    long anHourSeconds = 3600;
                    long aDaySeconds = anHourSeconds * 24;
                    long aWeekSeconds = aDaySeconds * 7;

                    if (datediffSeconds < 0) {
                        if (this.debugEnabled)
                            log.debug("The session cookie has expired already");
                        sessionExpiryRiskDescription = "ascanbeta.sessionidexpiry.timeexpired";
                        sessionExpiryRiskLevel = Alert.RISK_INFO; // no risk.. the cookie has expired already
                    } else if (datediffSeconds > aWeekSeconds) {
                        if (this.debugEnabled)
                            log.debug("The session cookie is set to last for more than a week!");
                        sessionExpiryRiskDescription = "ascanbeta.sessionidexpiry.timemorethanoneweek";
                        sessionExpiryRiskLevel = Alert.RISK_HIGH;
                    } else if (datediffSeconds > aDaySeconds) {
                        if (this.debugEnabled)
                            log.debug("The session cookie is set to last for more than a day");
                        sessionExpiryRiskDescription = "ascanbeta.sessionidexpiry.timemorethanoneday";
                        sessionExpiryRiskLevel = Alert.RISK_MEDIUM;
                    } else if (datediffSeconds > anHourSeconds) {
                        if (this.debugEnabled)
                            log.debug("The session cookie is set to last for more than an hour");
                        sessionExpiryRiskDescription = "ascanbeta.sessionidexpiry.timemorethanonehour";
                        sessionExpiryRiskLevel = Alert.RISK_LOW;
                    } else {
                        if (this.debugEnabled)
                            log.debug("The session cookie is set to last for less than an hour!");
                        sessionExpiryRiskDescription = "ascanbeta.sessionidexpiry.timelessthanonehour";
                        sessionExpiryRiskLevel = Alert.RISK_INFO;
                    }
                }

                if (!loginUrl) {
                    // decrement the risk if it's not a login page
                    sessionExpiryRiskLevel--;
                }

                // alert it if the default session expiry risk level is more than informational
                if (sessionExpiryRiskLevel > Alert.RISK_INFO) {
                    // pass the original param value here, not the new value
                    String cookieReceivedTime = cookieBack1Expiry = DateUtil
                            .formatDate(new Date(cookieBack1TimeReceived), DateUtil.PATTERN_RFC1123);
                    String extraInfo = Constant.messages.getString("ascanbeta.sessionidexpiry.alert.extrainfo",
                            currentHtmlParameter.getType(), currentHtmlParameter.getName(),
                            currentHtmlParameter.getValue(), sessionExpiryDescription, cookieReceivedTime);
                    String attack = Constant.messages.getString("ascanbeta.sessionidexpiry.alert.attack",
                            currentHtmlParameter.getType(), currentHtmlParameter.getName());
                    String vulnname = Constant.messages.getString("ascanbeta.sessionidexpiry.name");
                    String vulndesc = Constant.messages.getString("ascanbeta.sessionidexpiry.desc");
                    String vulnsoln = Constant.messages.getString("ascanbeta.sessionidexpiry.soln");
                    if (loginUrl) {
                        extraInfo += ("\n" + Constant.messages
                                .getString("ascanbeta.sessionidexpiry.alert.extrainfo.loginpage"));
                    }

                    // call bingo with some extra info, indicating that the alert is
                    // not specific to Session Fixation, but has its own title and description
                    // (etc)
                    // the alert here is "Session Id Expiry Time is excessive", or words to that
                    // effect.
                    bingo(sessionExpiryRiskLevel, Alert.CONFIDENCE_MEDIUM, vulnname, vulndesc,
                            getBaseMsg().getRequestHeader().getURI().getURI(), currentHtmlParameter.getName(),
                            attack, extraInfo, vulnsoln, getBaseMsg());

                    if (log.isDebugEnabled()) {
                        String logMessage = MessageFormat.format(
                                "A session identifier in [{0}] URL [{1}] {2} field: "
                                        + "[{3}] may be accessed until [{4}], unless the session is destroyed.",
                                getBaseMsg().getRequestHeader().getMethod(),
                                getBaseMsg().getRequestHeader().getURI().getURI(),
                                currentHtmlParameter.getType(), currentHtmlParameter.getName(),
                                sessionExpiryDescription);
                        log.debug(logMessage);
                    }
                    // Note: do NOT continue to the next field at this point..
                    // since we still need to check for Session Fixation.
                }

                if (!loginUrl) {
                    // not a login page.. skip
                    continue;
                }

                ////////////////////////////////////////////////////////////////////////////////////////////
                /// Message 2 - processing starts here
                ////////////////////////////////////////////////////////////////////////////////////////////
                // so now that we know the URL responds with 200 (OK), and that it sets a
                // cookie, lets re-issue the original request,
                // but lets add in the new (valid) session cookie that was just issued.
                // we will re-send it.  the aim is then to see if it accepts the cookie (BAD, in
                // some circumstances),
                // or if it issues a new session cookie (GOOD, in most circumstances)
                if (this.debugEnabled)
                    log.debug("A Cookie was set by the URL for the correct param, when param ["
                            + currentHtmlParameter.getName() + "] was set to NULL: " + cookieBack1);

                // use a copy of msg2Initial, since it has already had the correct cookie
                // removed in the request..
                // do NOT use msg2Initial itself, as this will cause both requests in the GUI to
                // show the modified data..
                // finally send the second message, and see how it comes back.
                HttpMessage msg2Initial = msg1Initial.cloneRequest();

                TreeSet<HtmlParameter> cookieParams2Set = msg2Initial.getRequestHeader().getCookieParams();
                cookieParams2Set.add(cookieBack1);
                msg2Initial.setCookieParams(cookieParams2Set);

                // resend the copy of the initial message, but with the valid session cookie
                // added in, to see if it is accepted
                // do not automatically follow redirects, as we need to check these for cookies
                // being set.
                sendAndReceive(msg2Initial, false, false);

                // create a copy of msg2Initial to play with to handle redirects (if any).
                // we use a copy because if we change msg2Initial itself, it messes the URL and
                // params displayed on the GUI.
                HttpMessage temp2 = msg2Initial;
                HttpMessage msg2Final = msg2Initial;
                HtmlParameter cookieBack2Previous = cookieBack1;
                HtmlParameter cookieBack2 = getResponseCookie(msg2Initial, currentHtmlParameter.getName());

                int redirectsFollowed2 = 0;
                while (HttpStatusCode.isRedirection(temp2.getResponseHeader().getStatusCode())) {

                    // clone the previous message
                    boolean secure2 = temp2.getRequestHeader().isSecure();
                    temp2 = temp2.cloneAll();

                    redirectsFollowed2++;
                    if (redirectsFollowed2 > 10) {
                        throw new Exception("Too many redirects were specified in the second message");
                    }

                    // create a new URI from the absolute location returned, and interpret it as
                    // escaped
                    // note that the standard says that the Location returned should be
                    // absolute, but it ain't always so...
                    URI newLocation = new URI(temp2.getResponseHeader().getHeader(HttpHeader.LOCATION), true);

                    // and follow the forward url
                    // need to clear the params (which would come from the initial POST,
                    // otherwise)
                    temp2.getRequestHeader().setGetParams(new TreeSet<HtmlParameter>());
                    temp2.setRequestBody("");
                    temp2.setResponseBody(""); // make sure no values accidentally carry from one iteration to
                    // the next

                    try {
                        temp2.getRequestHeader().setURI(newLocation);
                    } catch (Exception e) {
                        // the Location field contents may not be standards compliant. Lets
                        // generate a uri to use as a workaround where a relative path was
                        // given instead of an absolute one
                        URI newLocationWorkaround = new URI(temp2.getRequestHeader().getURI(),
                                temp2.getResponseHeader().getHeader(HttpHeader.LOCATION), true);

                        // try again, except this time, if it fails, don't try to handle it
                        if (this.debugEnabled)
                            log.debug("The Location [" + newLocation
                                    + "] specified in a redirect was not valid. Trying workaround url ["
                                    + newLocationWorkaround + "]");
                        temp2.getRequestHeader().setURI(newLocationWorkaround);
                    }
                    temp2.getRequestHeader().setSecure(secure2);
                    temp2.getRequestHeader().setMethod(HttpRequestHeader.GET);
                    temp2.getRequestHeader().setContentLength(0); // since we send a GET, the body will be 0 long
                    if (cookieBack2 != null) {
                        // if the previous request sent back a cookie, we need to set that
                        // cookie when following redirects, as a browser would
                        // also make sure to delete the previous value set for the cookie value
                        if (this.debugEnabled) {
                            log.debug("Deleting old cookie [" + cookieBack2Previous
                                    + "], and adding in cookie [" + cookieBack2 + "] for a redirect");
                        }
                        TreeSet<HtmlParameter> forwardCookieParams = temp2.getRequestHeader().getCookieParams();
                        forwardCookieParams.remove(cookieBack2Previous);
                        forwardCookieParams.add(cookieBack2);
                        temp2.getRequestHeader().setCookieParams(forwardCookieParams);
                    }

                    sendAndReceive(temp2, false, false); // do NOT automatically redirect.. handle redirects here

                    // handle any cookies set from following redirects that override the cookie
                    // set in the redirect itself (if any)
                    // note that this will handle the case where a latter cookie unsets one set
                    // earlier.
                    HtmlParameter cookieBack2Temp = getResponseCookie(temp2, currentHtmlParameter.getName());
                    if (cookieBack2Temp != null) {
                        cookieBack2Previous = cookieBack2;
                        cookieBack2 = cookieBack2Temp;
                    }

                    // reset the "final" version of message2 to use the final response in the
                    // chain
                    msg2Final = temp2;
                }
                if (this.debugEnabled)
                    log.debug("Done following redirects");

                // final result was non-200, no point in continuing. Bale out.
                if (msg2Final.getResponseHeader().getStatusCode() != HttpStatusCode.OK) {
                    if (this.debugEnabled)
                        log.debug(
                                "Got a non-200 response code [" + msg2Final.getResponseHeader().getStatusCode()
                                        + "] when sending [" + msg2Initial.getRequestHeader().getURI()
                                        + "] with a borrowed cookie (or by following a redirect) for param ["
                                        + currentHtmlParameter.getName() + "]");
                    continue; // to next parameter
                }

                // and what we've been waiting for.. do we get a *different* cookie being set in
                // the response of message 2??
                // or do we get a new cookie back at all?
                // No cookie back => the borrowed cookie was accepted. Not ideal
                // Cookie back, but same as the one we sent in => the borrowed cookie was
                // accepted. Not ideal
                if ((cookieBack2 == null) || cookieBack2.getValue().equals(cookieBack1.getValue())) {
                    // no cookie back, when a borrowed cookie is in use.. suspicious!

                    // use the cookie extrainfo message, which is specific to the case of
                    // cookies
                    // pretty much everything else is generic to all types of Session Fixation
                    // vulnerabilities
                    String extraInfo = Constant.messages.getString(
                            "ascanbeta.sessionfixation.alert.cookie.extrainfo", currentHtmlParameter.getName(),
                            cookieBack1.getValue(), (cookieBack2 == null ? "NULL" : cookieBack2.getValue()));
                    String attack = Constant.messages.getString("ascanbeta.sessionfixation.alert.attack",
                            currentHtmlParameter.getType(), currentHtmlParameter.getName());

                    if (loginUrl) {
                        extraInfo += ("\n" + Constant.messages
                                .getString("ascanbeta.sessionfixation.alert.cookie.extrainfo.loginpage"));
                    }

                    bingo(Alert.RISK_INFO, Alert.CONFIDENCE_MEDIUM,
                            msg2Initial.getRequestHeader().getURI().getURI(), currentHtmlParameter.getName(),
                            attack, extraInfo, msg2Initial);
                    logSessionFixation(msg2Initial, currentHtmlParameter.getType().toString(),
                            currentHtmlParameter.getName());
                }

                continue; // jump to the next iteration of the loop (ie, the next parameter)
            } // end of the cookie code.

            // start of the url parameter code
            // note that this actually caters for
            // - actual URL parameters
            // - pseudo URL parameters, where the sessionid was in the path portion of the URL,
            // in conjunction with URL re-writing
            if (currentHtmlParameter.getType().equals(HtmlParameter.Type.url)) {
                boolean isPseudoUrlParameter = false; // is this "url parameter" actually a url parameter, or was it
                // path of the path (+url re-writing)?
                String possibleSessionIdIssuedForUrlParam = null;
                // remove the named url parameter from the request..
                TreeSet<HtmlParameter> urlRequestParams = msg1Initial.getUrlParams(); // get parameters?
                if (!urlRequestParams.remove(currentHtmlParameter)) {
                    isPseudoUrlParameter = true;
                    // was not removed because it was a pseudo Url parameter, not a real url
                    // parameter.. (so it would not be in the url params)
                    // in this case, we will need to "rewrite" (ie hack) the URL path to remove
                    // the pseudo url parameter portion
                    // ie, we need to remove the ";jsessionid=<sessionid>" bit from the path
                    // (assuming the current field is named 'jsessionid')
                    // and replace it with ";jsessionid=" (ie, we nullify the possible "session"
                    // parameter in the hope that a new session will be issued)
                    // then we continue as usual to see if the URL is vulnerable to a Session
                    // Fixation issue
                    // Side note: quote the string to search for, and the replacement, so that
                    // regex special characters are treated as literals
                    String hackedUrl = requestUrl.replaceAll(
                            Pattern.quote(";" + currentHtmlParameter.getName() + "="
                                    + currentHtmlParameter.getValue()),
                            Matcher.quoteReplacement(";" + currentHtmlParameter.getName() + "="));
                    if (this.debugEnabled)
                        log.debug("Removing the pseudo URL parameter from [" + requestUrl + "]: [" + hackedUrl
                                + "]");
                    // Note: the URL is not escaped. Handle it.
                    msg1Initial.getRequestHeader().setURI(new URI(hackedUrl, false));
                }
                msg1Initial.setGetParams(urlRequestParams); // url parameters

                // send the message, minus the value for the current parameter, and see how it
                // comes back.
                // Note: automatically follow redirects.. no need to look at any intermediate
                // responses.
                // this was only necessary for cookie-based session implementations
                sendAndReceive(msg1Initial);

                // if non-200 on the response for message 1, no point in continuing. Bale out.
                if (msg1Initial.getResponseHeader().getStatusCode() != HttpStatusCode.OK) {
                    if (this.debugEnabled)
                        log.debug("Got a non-200 response code ["
                                + msg1Initial.getResponseHeader().getStatusCode() + "] when sending ["
                                + msg1Initial.getRequestHeader().getURI() + "] with param ["
                                + currentHtmlParameter.getName()
                                + "] = NULL (possibly somewhere in the redirects)");
                    continue;
                }

                // now parse the HTML response for urls that contain the same parameter name,
                // and look at the values for that parameter
                // if no values are found for the parameter, then
                // 1) we are messing with the wrong field, or
                // 2) the app doesn't do sessions
                // either way, there is not much point in continuing to look at this field..

                // parse out links in HTML (assume for a moment that all the URLs are in links)
                // this gives us a map of parameter value for the current parameter, to the
                // number of times it was encountered in links in the HTML
                SortedMap<String, Integer> parametersInHTMLURls = getParameterValueCountInHtml(
                        msg1Initial.getResponseBody().toString(), currentHtmlParameter.getName(),
                        isPseudoUrlParameter);
                if (this.debugEnabled)
                    log.debug("The count of the various values of the [" + currentHtmlParameter.getName()
                            + "] parameters in urls in the result of retrieving the url with a null value for parameter ["
                            + currentHtmlParameter.getName() + "]: " + parametersInHTMLURls);

                if (parametersInHTMLURls.isEmpty()) {
                    // setting the param to NULL did not cause any new values to be generated
                    // for it in the output..
                    // so either..
                    // it is not a session field, or
                    // it is a session field, but a session is only issued on authentication,
                    // and this is not an authentication url
                    // the app doesn't do sessions (etc)
                    // either way, the parameter/url combo is not vulnerable, so continue with
                    // the next parameter
                    if (this.debugEnabled)
                        log.debug("The URL parameter [" + currentHtmlParameter.getName()
                                + "] was NOT set in any links in the response, when "
                                + (isPseudoUrlParameter ? "pseudo/URL rewritten" : "") + " URL param ["
                                + currentHtmlParameter.getName()
                                + "] was set to NULL in the request, so it is likely not a session id field");
                    continue; // to the next parameter
                } else if (parametersInHTMLURls.size() == 1) {
                    // the parameter was set to just one value in the output
                    // so it's quite possible it is the session id field that we have been
                    // looking for
                    // caveat 1: check it is longer than 3 chars long, to remove false
                    // positives..
                    // we assume here that a real session id will always be greater than 3
                    // characters long
                    // caveat 2: the value we got back for the param must be different from the
                    // value we
                    // over-wrote with NULL (empty) in the first place, otherwise it is very
                    // unlikely to
                    // be a session id field
                    possibleSessionIdIssuedForUrlParam = parametersInHTMLURls.firstKey();
                    // did we get back the same value we just nulled out in the original
                    // request?
                    // if so, use this to eliminate false positives, and to optimise.
                    if (possibleSessionIdIssuedForUrlParam.equals(currentHtmlParameter.getValue())) {
                        if (this.debugEnabled)
                            log.debug((isPseudoUrlParameter ? "pseudo/URL rewritten" : "") + " URL param ["
                                    + currentHtmlParameter.getName()
                                    + "], when set to NULL, causes 1 distinct values to be set for it in URLs in the output, but the possible session id value ["
                                    + possibleSessionIdIssuedForUrlParam
                                    + "] is the same as the value we over-wrote with NULL. 'Sorry, kid. You got the gift, but it looks like you're waiting for something'");
                        continue; // to the next parameter
                    }
                    if (possibleSessionIdIssuedForUrlParam.length() > 3) {
                        // raise an alert here on an exposed session id, even if it is not
                        // subject to a session fixation vulnerability
                        // log.info("The URL parameter ["+ currentHtmlParameter.getName() + "]
                        // was set ["+
                        // parametersInHTMLURls.get(possibleSessionIdIssuedForUrlParam)+ "]
                        // times to ["+ possibleSessionIdIssuedForUrlParam + "] in links in the
                        // response, when "+ (isPseudoUrlParameter?"pseudo/URL rewritten":"")+ "
                        // URL param ["+ currentHtmlParameter.getName() + "] was set to NULL in
                        // the request. This likely indicates it is a session id field.");

                        // pass the original param value here, not the new value, since we're
                        // displaying the session id exposed in the original message
                        String extraInfo = Constant.messages.getString(
                                "ascanbeta.sessionidexposedinurl.alert.extrainfo",
                                currentHtmlParameter.getType(), currentHtmlParameter.getName(),
                                currentHtmlParameter.getValue());
                        String attack = Constant.messages
                                .getString("ascanbeta.sessionidexposedinurl.alert.attack",
                                        (isPseudoUrlParameter ? "pseudo/URL rewritten " : "")
                                                + currentHtmlParameter.getType(),
                                        currentHtmlParameter.getName());
                        String vulnname = Constant.messages.getString("ascanbeta.sessionidexposedinurl.name");
                        String vulndesc = Constant.messages.getString("ascanbeta.sessionidexposedinurl.desc");
                        String vulnsoln = Constant.messages.getString("ascanbeta.sessionidexposedinurl.soln");

                        if (loginUrl) {
                            extraInfo += ("\n" + Constant.messages
                                    .getString("ascanbeta.sessionidexposedinurl.alert.extrainfo.loginpage"));
                        }

                        // call bingo with some extra info, indicating that the alert is
                        // not specific to Session Fixation, but has its own title and
                        // description (etc)
                        // the alert here is "Session id exposed in url", or words to that
                        // effect.
                        bingo(Alert.RISK_MEDIUM, Alert.CONFIDENCE_MEDIUM, vulnname, vulndesc,
                                getBaseMsg().getRequestHeader().getURI().getURI(),
                                currentHtmlParameter.getName(), attack, extraInfo, vulnsoln, getBaseMsg());

                        if (log.isDebugEnabled()) {
                            String logMessage = MessageFormat.format(
                                    "An exposed session identifier has been found at "
                                            + "[{0}] URL [{1}] on {2} field: [{3}]",
                                    getBaseMsg().getRequestHeader().getMethod(),
                                    getBaseMsg().getRequestHeader().getURI().getURI(),
                                    (isPseudoUrlParameter ? "pseudo " : "") + currentHtmlParameter.getType(),
                                    currentHtmlParameter.getName());
                            log.debug(logMessage);
                        }
                        // Note: do NOT continue to the next field at this point..
                        // since we still need to check for Session Fixation.
                    } else {
                        if (this.debugEnabled)
                            log.debug((isPseudoUrlParameter ? "pseudo/URL rewritten" : "") + " URL param ["
                                    + currentHtmlParameter.getName()
                                    + "], when set to NULL, causes 1 distinct values to be set for it in URLs in the output, but the possible session id value ["
                                    + possibleSessionIdIssuedForUrlParam
                                    + "] is too short to be a real session id.");
                        continue; // to the next parameter
                    }
                } else {
                    // strange scenario: setting the param to null causes multiple different
                    // values to be set for it in the output
                    // it could still be a session parameter, but we assume it is *not* a
                    // session id field
                    // log it, but assume it is not a session id
                    if (this.debugEnabled)
                        log.debug((isPseudoUrlParameter ? "pseudo/URL rewritten" : "") + " URL param ["
                                + currentHtmlParameter.getName() + "], when set to NULL, causes ["
                                + parametersInHTMLURls.size()
                                + "] distinct values to be set for it in URLs in the output. Assuming it is NOT a session id as a consequence. This could be a false negative");
                    continue; // to the next parameter
                }

                ////////////////////////////////////////////////////////////////////////////////////////////
                /// Message 2 - processing starts here
                ////////////////////////////////////////////////////////////////////////////////////////////
                // we now have a plausible session id field to play with, so set it to a
                // borrowed value.
                // ie: lets re-send the request, but add in the new (valid) session value that
                // was just issued.
                // the aim is then to see if it accepts the session without re-issuing the
                // session id (BAD, in some circumstances),
                // or if it issues a new session value (GOOD, in most circumstances)

                // and set the (modified) session for the second message
                // use a copy of msg2Initial, since it has already had the correct session
                // removed in the request..
                // do NOT use msg2Initial itself, as this will cause both requests in the GUI to
                // show the modified data..
                // finally send the second message, and see how it comes back.
                HttpMessage msg2Initial = msg1Initial.cloneRequest();

                // set the parameter to the new session id value (in different manners,
                // depending on whether it is a real url param, or a pseudo url param)
                if (isPseudoUrlParameter) {
                    // we need to "rewrite" (hack) the URL path to remove the pseudo url
                    // parameter portion
                    // id, we need to remove the ";jsessionid=<sessionid>" bit from the path
                    // and replace it with ";jsessionid=" (ie, we nullify the possible "session"
                    // parameter in the hope that a new session will be issued)
                    // then we continue as usual to see if the URL is vulnerable to a Session
                    // Fixation issue
                    // Side note: quote the string to search for, and the replacement, so that
                    // regex special characters are treated as literals
                    String hackedUrl = requestUrl.replaceAll(
                            Pattern.quote(";" + currentHtmlParameter.getName() + "="
                                    + currentHtmlParameter.getValue()),
                            Matcher.quoteReplacement(";" + currentHtmlParameter.getName() + "="
                                    + possibleSessionIdIssuedForUrlParam));
                    if (this.debugEnabled)
                        log.debug("Changing the pseudo URL parameter from [" + requestUrl + "]: [" + hackedUrl
                                + "]");
                    // Note: the URL is not escaped
                    msg2Initial.getRequestHeader().setURI(new URI(hackedUrl, false));
                    msg2Initial.setGetParams(msg1Initial.getUrlParams()); // restore the GET params
                } else {
                    // do it via the normal url parameters
                    TreeSet<HtmlParameter> urlRequestParams2 = msg2Initial.getUrlParams();
                    urlRequestParams2.add(new HtmlParameter(Type.url, currentHtmlParameter.getName(),
                            possibleSessionIdIssuedForUrlParam));
                    msg2Initial.setGetParams(urlRequestParams2); // restore the GET params
                }

                // resend a copy of the initial message, but with the new valid session
                // parameter added in, to see if it is accepted
                // automatically follow redirects, which are irrelevant for the purposes of
                // testing URL parameters
                sendAndReceive(msg2Initial);

                // final result was non-200, no point in continuing. Bale out.
                if (msg2Initial.getResponseHeader().getStatusCode() != HttpStatusCode.OK) {
                    if (this.debugEnabled)
                        log.debug("Got a non-200 response code ["
                                + msg2Initial.getResponseHeader().getStatusCode() + "] when sending ["
                                + msg2Initial.getRequestHeader().getURI()
                                + "] with a borrowed session (or by following a redirect) for param ["
                                + currentHtmlParameter.getName() + "]");
                    continue; // next field!
                }

                // do the analysis on the parameters in link urls in the HTML output again to
                // see if the session id was regenerated
                SortedMap<String, Integer> parametersInHTMLURls2 = getParameterValueCountInHtml(
                        msg2Initial.getResponseBody().toString(), currentHtmlParameter.getName(),
                        isPseudoUrlParameter);
                if (this.debugEnabled)
                    log.debug("The count of the various values of the [" + currentHtmlParameter.getName()
                            + "] parameters in urls in the result of retrieving the url with a borrowed session value for parameter ["
                            + currentHtmlParameter.getName() + "]: " + parametersInHTMLURls2);

                if (parametersInHTMLURls2.size() != 1) {
                    // either no values, or multiple values, but not 1 value.  For a session
                    // that was regenerated, we would have expected to see
                    // just 1 new value
                    if (this.debugEnabled)
                        log.debug("The HTML has spoken. [" + currentHtmlParameter.getName()
                                + "] doesn't look like a session id field, because there are "
                                + parametersInHTMLURls2.size()
                                + " distinct values for this parameter in urls in the HTML output");
                    continue;
                }
                // there is but one value for this param in links in the HTML output. But is it
                // vulnerable to Session Fixation? Ie, is it the same parameter?
                String possibleSessionIdIssuedForUrlParam2 = parametersInHTMLURls2.firstKey();
                if (possibleSessionIdIssuedForUrlParam2.equals(possibleSessionIdIssuedForUrlParam)) {
                    // same sessionid used in the output.. so it is likely that we have a
                    // SessionFixation issue..

                    // use the url param extrainfo message, which is specific to the case of url
                    // parameters and url re-writing Session Fixation issue
                    // pretty much everything else is generic to all types of Session Fixation
                    // vulnerabilities
                    String extraInfo = Constant.messages.getString(
                            "ascanbeta.sessionfixation.alert.url.extrainfo", currentHtmlParameter.getName(),
                            possibleSessionIdIssuedForUrlParam, possibleSessionIdIssuedForUrlParam2);
                    String attack = Constant.messages.getString("ascanbeta.sessionfixation.alert.attack",
                            (isPseudoUrlParameter ? "pseudo/URL rewritten " : "")
                                    + currentHtmlParameter.getType(),
                            currentHtmlParameter.getName());

                    int risk = Alert.RISK_LOW;
                    if (loginUrl) {
                        extraInfo += ("\n" + Constant.messages
                                .getString("ascanbeta.sessionfixation.alert.url.extrainfo.loginpage"));
                        // login page, so higher risk
                        risk = Alert.RISK_MEDIUM;
                    } else {
                        // not a login page.. lower risk
                        risk = Alert.RISK_LOW;
                    }

                    bingo(risk, Alert.CONFIDENCE_MEDIUM, getBaseMsg().getRequestHeader().getURI().getURI(),
                            currentHtmlParameter.getName(), attack, extraInfo, getBaseMsg());
                    logSessionFixation(getBaseMsg(),
                            (isPseudoUrlParameter ? "pseudo " : "") + currentHtmlParameter.getType(),
                            currentHtmlParameter.getName());

                    continue; // jump to the next iteration of the loop (ie, the next parameter)

                } else {
                    // different sessionid used in the output.. so it is unlikely that we have a
                    // SessionFixation issue..
                    // more likely that the Session is being re-issued for every single request,
                    // or we have issues a login request, which
                    // normally causes a session to be reissued
                    if (this.debugEnabled)
                        log.debug("The " + (isPseudoUrlParameter ? "pseudo/URL rewritten" : "") + " parameter ["
                                + currentHtmlParameter.getName() + "] in url ["
                                + getBaseMsg().getRequestHeader().getMethod() + "] ["
                                + getBaseMsg().getRequestHeader().getURI()
                                + "] changes with requests, and so it likely not vulnerable to Session Fixation");
                }

                continue; // onto the next parameter
            } // end of the url parameter code.
        } // end of the for loop around the parameter list

    } catch (Exception e) {
        // Do not try to internationalise this.. we need an error message in any event..
        // if it's in English, it's still better than not having it at all.
        log.error("An error occurred checking a url for Session Fixation issues", e);
    }
}

From source file:org.zaproxy.zap.extension.ascanrulesBeta.SourceCodeDisclosureSVN.java

/**
 * finds the source code for the given file, using SVN metadata on the server (if this is
 * available)//from ww w  . j a v  a 2 s .  com
 *
 * @param uri the URI of a file, whose source code we want to find
 * @return Did we find the source code?
 */
private boolean findSourceCodeSVN(HttpMessage originalMessage) throws Exception {

    AlertThreshold alertThreshold = getAlertThreshold();

    // SVN formats 1-10 (format 11 is not used) are supported by this logic.
    // TODO: The SQLite based (and centralised, except for pre-release formats which we don't
    // plan to support) ".svn/wc.db" style used from SVN format 12 through to 31
    // (and possibly later formats) is not yet supported here. It's a work in progress.
    // It is fully supported in the Spider, however.

    URI uri = originalMessage.getRequestHeader().getURI();
    String path = uri.getPath();
    if (path == null)
        path = "";
    // String filename = path.substring( path.lastIndexOf('/')+1, path.length() );
    String urlfilename = uri.getName();

    String fileExtension = null;
    if (urlfilename.contains(".")) {
        fileExtension = urlfilename.substring(urlfilename.lastIndexOf(".") + 1);
        fileExtension = fileExtension.toUpperCase();
    }

    // do not recurse into a Subversion folder... this would cause infinite recursion issues in
    // Attack Mode. (which goes depth first!)
    // in any event, it doesn't make sense to do this.
    if (path.contains("/.svn/") || path.endsWith("/.svn")) {
        if (log.isDebugEnabled())
            log.debug(
                    "Nope. It doesn't make any sense to look for a Subversion repo *within* a Subversion repo");
        return false;
    }

    // Look for SVN < 1.7 metadata (ie internal SVN format < 29) containing source code
    // These versions all store the pristine copies in the the same format (insofar as the logic
    // here is concerned, at least)
    try {
        String pathminusfilename = path.substring(0, path.lastIndexOf(urlfilename));

        HttpMessage svnsourcefileattackmsg = new HttpMessage(new URI(uri.getScheme(), uri.getAuthority(),
                pathminusfilename + ".svn/text-base/" + urlfilename + ".svn-base", null, null));
        svnsourcefileattackmsg.setCookieParams(this.getBaseMsg().getCookieParams());
        // svnsourcefileattackmsg.setRequestHeader(this.getBaseMsg().getRequestHeader());
        sendAndReceive(svnsourcefileattackmsg, false); // do not follow redirects

        int attackmsgResponseStatusCode = svnsourcefileattackmsg.getResponseHeader().getStatusCode();

        if (shouldStop(alertThreshold, attackmsgResponseStatusCode)) {
            return false;
        }

        if (originalMessage.getResponseBody().toString()
                .equals(svnsourcefileattackmsg.getResponseBody().toString())) {
            if (log.isDebugEnabled()) {
                log.debug("Response bodies are exactly the same, so can not be the source code");
            }
        } else if (!UNWANTED_RESPONSE_CODES.contains(attackmsgResponseStatusCode)) { // If the response is wanted (not on the
            // unwanted list)

            String attackFilename = uri.getScheme() + "://" + uri.getAuthority() + pathminusfilename
                    + ".svn/text-base/" + urlfilename + ".svn-base";
            if (log.isDebugEnabled()) {
                log.debug("The contents for request '" + attackFilename
                        + "' do not return 404 or 3**, so we possibly have the source code using SVN < 1.7");
            }
            // check the contents of the output to some degree, if we have a file extension.
            // if not, just try it (could be a false positive, but hey)
            String evidence = findEvidenceForExtension(svnsourcefileattackmsg.getResponseBody().getBytes(),
                    fileExtension);
            if (evidence != null) {
                // if we get to here, is is very likely that we have source file inclusion
                // attack. alert it.
                bingo(Alert.RISK_MEDIUM, getConfidence(attackmsgResponseStatusCode), getName(),
                        getDescription(), getBaseMsg().getRequestHeader().getURI().getURI(), null,
                        attackFilename, getExtraInfo(urlfilename, attackFilename), getSolution(), evidence,
                        svnsourcefileattackmsg);
                // if we found one, do not even try the "super" method, which tries each of the
                // parameters,
                // since this is slow, and we already found an instance
                return true;
            } else {
                if (log.isDebugEnabled())
                    log.debug("The HTML output does not look like source code of type " + fileExtension);
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Got an unsuitable response code "
                        + svnsourcefileattackmsg.getResponseHeader().getStatusCode()
                        + ", so it looks like SVN < 1.7 source code file was not found");
            }
        }
    } catch (Exception e) {
        log.warn("Got an error trying to find source code using the format used by SVN < 1.7", e);
    }

    // try again, by assuming that SVN 1.7 or later is used.  These versions use a different
    // internal format, and store the source code in different locations compared to SVN < 1.7.
    // Note that it's not as simple this time around, because the name of the file that contains
    // the source code is based on a SHA1 hash of the file contents, rather than being based on
    // the source file name.
    // In other words, we can't guess the name of the internal SVN file, and we can't just
    // calculate it from the file name.  The good news is that the file name that we need is
    // contained in the centralised
    // "wc.db" SVN metadata file that is associated with SVN >= 1.7.
    // "wc.db" lives in ".svn/wc.db".  This file contains data for all of the files in the repo
    // (ie, it contains data for the root directory and all subdirectories of the repo).
    // The only real issue we have is the question of where within the web folder structure (or
    // mappings) that the "wc.db" file resides.
    // For instance, the ".svn" directory might have been deployed into
    // "http://www.example.com/.svn",
    // or it *might* have been deployed into "http://www.example.com/dir1/dir2/.svn".
    // If we're looking for the SVN >= 1.7 source for
    // "http://www.example.com/dir1/dir2/login.php", for instance, we need to check for the
    // "wc.db" file in the following locations:
    //   "http://www.example.com/dir1/dir2/.svn/wc.db"
    //   "http://www.example.com/dir1/.svn/wc.db"
    //   "http://www.example.com/.svn/wc.db"
    // ie, we need to traverse all the way back to the web root looking for it.
    // Once we've found the "wc.db" file, we use it as an index, looking up the name of the file
    // for which we're trying to get the source code.
    // That gives us the internal SVN file name (containing the SHA1 value), which we can (in
    // theory) then retrieve. If it works, we will retrieve the source code for the file!
    try {
        String pathminusfilename = path.substring(0, path.lastIndexOf(urlfilename));
        while (!pathminusfilename.equals("/")) {
            HttpMessage svnWCDBAttackMsg = new HttpMessage(
                    new URI(uri.getScheme(), uri.getAuthority(), pathminusfilename + ".svn/wc.db", null, null));
            svnWCDBAttackMsg.setCookieParams(this.getBaseMsg().getCookieParams());
            // svnsourcefileattackmsg.setRequestHeader(this.getBaseMsg().getRequestHeader());
            sendAndReceive(svnWCDBAttackMsg, false); // do not follow redirects

            int svnWCDBAttackMsgStatusCode = svnWCDBAttackMsg.getResponseHeader().getStatusCode();

            if (shouldStop(alertThreshold, svnWCDBAttackMsgStatusCode)) {
                return false;
            }

            if (originalMessage.getResponseBody().toString()
                    .equals(svnWCDBAttackMsg.getResponseBody().toString())) {
                if (log.isDebugEnabled()) {
                    log.debug("Response bodies are exactly the same, so can not be the source code");
                }
            } else if (!UNWANTED_RESPONSE_CODES.contains(svnWCDBAttackMsgStatusCode)) { // If the response is wanted (not on the
                // unwanted list)
                // calculate the path used to access the wc.db, as well as the matching relpath
                // to query the wc.db
                // since the relpath is calculated from the original message URL path, after
                // removing the base used in the wc.db url path
                String wcdbAttackFilename = uri.getScheme() + "://" + uri.getAuthority() + pathminusfilename
                        + ".svn/wc.db";
                String relPath = path.substring(path.indexOf(pathminusfilename) + pathminusfilename.length());
                if (log.isDebugEnabled()) {
                    log.debug("The contents for request '" + wcdbAttackFilename
                            + "' do not return 404 or 3**, so we found the '.svn/wc.db' file for SVN >= 1.7..");
                    log.debug("The relpath to query SQLite is '" + relPath + "'");
                }

                // so we found the wc.db file... handle it.
                // get the binary data, and put it in a temp file we can use with the SQLite
                // JDBC driver
                // Note: File is not AutoClosable, so cannot use a "try with resources" to
                // manage it
                File tempSqliteFile;
                tempSqliteFile = File.createTempFile("sqlite_svn_wc_db", null);
                tempSqliteFile.deleteOnExit();
                OutputStream fos = new FileOutputStream(tempSqliteFile);
                fos.write(svnWCDBAttackMsg.getResponseBody().getBytes());
                fos.close();

                if (log.isDebugEnabled()) {
                    org.sqlite.JDBC jdbcDriver = new org.sqlite.JDBC();
                    log.debug("Created a temporary SQLite database file '" + tempSqliteFile + "'");
                    log.debug("SQLite JDBC Driver is version " + jdbcDriver.getMajorVersion() + "."
                            + jdbcDriver.getMinorVersion());
                }

                // now load the temporary SQLite file using JDBC, and query the file entries
                // within.
                Class.forName("org.sqlite.JDBC");
                String sqliteConnectionUrl = "jdbc:sqlite:" + tempSqliteFile.getAbsolutePath();

                try (Connection conn = DriverManager.getConnection(sqliteConnectionUrl)) {
                    if (conn != null) {
                        Statement pragmaStatement = null;
                        PreparedStatement nodeStatement = null;
                        ResultSet rsSVNWCFormat = null;
                        ResultSet rsNode = null;
                        ResultSet rsRepo = null;
                        try {
                            pragmaStatement = conn.createStatement();
                            rsSVNWCFormat = pragmaStatement.executeQuery("pragma USER_VERSION");

                            // get the precise internal version of SVN in use
                            // this will inform how the scanner should proceed in an efficient
                            // manner.
                            int svnFormat = 0;
                            while (rsSVNWCFormat.next()) {
                                if (log.isDebugEnabled())
                                    log.debug("Got a row from 'pragma USER_VERSION'");
                                svnFormat = rsSVNWCFormat.getInt(1);
                                break;
                            }
                            if (svnFormat < 29) {
                                throw new Exception(
                                        "The SVN Working Copy Format of the SQLite database should be >= 29. We found "
                                                + svnFormat);
                            }
                            if (svnFormat > 31) {
                                throw new Exception("SVN Working Copy Format " + svnFormat
                                        + " is not supported at this time.  We support up to and including format 31 (~ SVN 1.8.5)");
                            }
                            if (log.isDebugEnabled()) {
                                log.debug("Internal SVN Working Copy Format for " + tempSqliteFile + " is "
                                        + svnFormat);
                                log.debug(
                                        "Refer to http://svn.apache.org/repos/asf/subversion/trunk/subversion/libsvn_wc/wc.h for more details!");
                            }

                            // allow future changes to be easily handled
                            switch (svnFormat) {
                            case 29:
                            case 30:
                            case 31:
                                nodeStatement = conn.prepareStatement(
                                        "select kind,local_relpath,'pristine/'||substr(checksum,7,2) || \"/\" || substr(checksum,7)|| \".svn-base\" from nodes where local_relpath = ? order by wc_id");
                                break;
                            }
                            // now set the parameter, and execute the query
                            nodeStatement.setString(1, relPath);
                            rsNode = nodeStatement.executeQuery();

                            // and get the internal name of the SVN file stored in the SVN repo
                            while (rsNode.next()) {
                                if (log.isDebugEnabled())
                                    log.debug("Got a Node from the SVN wc.db file (format " + svnFormat + ")");
                                // String kind = rsNode.getString(1);
                                // String filename = rsNode.getString(2);
                                String svnFilename = rsNode.getString(3);

                                if (svnFilename != null && svnFilename.length() > 0) {
                                    log.debug("Found " + relPath + " in the wc.db: " + svnFilename);

                                    // try get the source, using the internal SVN file path,
                                    // building the path back up correctly
                                    HttpMessage svnSourceFileAttackMsg = new HttpMessage(
                                            new URI(uri.getScheme(), uri.getAuthority(),
                                                    pathminusfilename + ".svn/" + svnFilename, null, null));
                                    svnSourceFileAttackMsg.setCookieParams(this.getBaseMsg().getCookieParams());
                                    // svnsourcefileattackmsg.setRequestHeader(this.getBaseMsg().getRequestHeader());
                                    sendAndReceive(svnSourceFileAttackMsg, false); // do not follow redirects

                                    int svnSourceFileAttackMsgStatusCode = svnSourceFileAttackMsg
                                            .getResponseHeader().getStatusCode();

                                    if (shouldStop(alertThreshold, svnSourceFileAttackMsgStatusCode)) {
                                        return false;
                                    }

                                    if (!UNWANTED_RESPONSE_CODES.contains(svnSourceFileAttackMsgStatusCode)) { // If the
                                        // response is
                                        // wanted (not
                                        // on the
                                        // unwanted
                                        // list)

                                        String attackFilename = uri.getScheme() + "://" + uri.getAuthority()
                                                + pathminusfilename + ".svn/" + svnFilename;
                                        if (log.isDebugEnabled()) {
                                            log.debug("The contents for request '" + attackFilename
                                                    + "' do not return 404 or 3**, so we possibly have the source code using SVN >= 1.7");
                                        }
                                        // check the contents of the output to some degree, if
                                        // we have a file extension.
                                        // if not, just try it (could be a false positive, but
                                        // hey)
                                        String evidence = findEvidenceForExtension(
                                                svnSourceFileAttackMsg.getResponseBody().getBytes(),
                                                fileExtension);
                                        if (evidence != null) {
                                            // if we get to here, is is very likely that we have
                                            // source file inclusion attack. alert it.
                                            bingo(Alert.RISK_MEDIUM,
                                                    getConfidence(svnSourceFileAttackMsgStatusCode), getName(),
                                                    getDescription(),
                                                    getBaseMsg().getRequestHeader().getURI().getURI(), null,
                                                    attackFilename, getExtraInfo(urlfilename, attackFilename),
                                                    getSolution(), evidence, svnSourceFileAttackMsg);
                                            // do not return.. need to tidy up first
                                        } else {
                                            if (log.isDebugEnabled())
                                                log.debug(
                                                        "The HTML output does not look like source code of type "
                                                                + fileExtension);
                                        }
                                    } else {
                                        if (log.isDebugEnabled()) {
                                            log.debug("Got an unsuitable response code "
                                                    + svnSourceFileAttackMsg.getResponseHeader().getStatusCode()
                                                    + ", so it looks like SVN >= 1.7 source code file was not found");
                                        }
                                    }

                                    break; // out of the loop. even though there should be just
                                    // 1 entry
                                }
                            }
                        } catch (SQLException sqlEx) {
                            StringBuilder errorSb = new StringBuilder(300);
                            errorSb.append("Error executing SQL on temporary SVN SQLite database '");
                            errorSb.append(sqliteConnectionUrl);
                            errorSb.append("': ");
                            errorSb.append(sqlEx);
                            errorSb.append("\nThe saved response likely wasn't a SQLite db.");
                            log.debug(errorSb);
                        } catch (Exception e) {
                            log.debug("An error has occurred, related to the temporary SVN SQLite DB. " + e);
                        } finally {
                            // the JDBC driver in use does not play well with "try with
                            // resource" construct. I tried!
                            if (rsRepo != null)
                                rsRepo.close();
                            if (rsNode != null)
                                rsNode.close();
                            if (rsSVNWCFormat != null)
                                rsSVNWCFormat.close();
                            if (pragmaStatement != null)
                                pragmaStatement.close();
                            if (nodeStatement != null)
                                nodeStatement.close();
                        }
                    } else
                        throw new SQLException("Could not open a JDBC connection to SQLite file "
                                + tempSqliteFile.getAbsolutePath());
                } catch (Exception e) {
                    // the connection will have been closed already, since we're used a try with
                    // resources
                    log.error("Error parsing temporary SVN SQLite database " + sqliteConnectionUrl);
                } finally {
                    // delete the temp file.
                    // this will be deleted when the VM is shut down anyway, but better to be
                    // safe than to run out of disk space.
                    tempSqliteFile.delete();
                }

                break; // out of the while loop
            } // non 404, 300, etc for "wc.db", for SVN >= 1.7
              // set up the parent directory name
            pathminusfilename = pathminusfilename.substring(0,
                    pathminusfilename.substring(0, pathminusfilename.length() - 1).lastIndexOf("/") + 1);
        }

    } catch (Exception e) {
        log.warn("Got an error trying to find source code using the format used by SVN >= 1.7", e);
    }
    return false;
}

From source file:org.zaproxy.zap.extension.ascanrulesBeta.UsernameEnumeration.java

/**
 * looks for username enumeration in the login page, by changing the username field to be a
 * valid / invalid user, and looking for differences in the response
 *///from   w ww  . jav  a  2  s.co  m
@Override
public void scan() {

    // the technique to determine if usernames can be enumerated is as follows, using a variant
    // of the Freiling+Schinzel method,
    // adapted to the case where we do not know which is the username field
    //
    // 1) Request the original URL n times. (The original URL is assumed to have a valid
    // username, if not a valid password). Store the results in A[].
    // 2) Compute the longest common subsequence (LCS) of A[] into LCS_A
    // 3) for each parameter in the original URL (ie, for URL params, form params, and cookie
    // params)
    //   4) Change the current parameter (which we assume is the username parameter) to an invalid
    // username (randomly), and request the URL n times. Store the results in B[].
    //   5) Compute the longest common subsequence (LCS) of B[] into LCS_B
    //   6) If LCS_A <> LCS_B, then there is a Username Enumeration issue on the current parameter

    try {
        boolean loginUrl = false;

        // Are we dealing with a login url in any of the contexts of which this uri is part
        URI requestUri = getBaseMsg().getRequestHeader().getURI();

        // using the session, get the list of contexts for the url
        List<Context> contextList = extAuth.getModel().getSession().getContextsForUrl(requestUri.getURI());

        // now loop, and see if the url is a login url in each of the contexts in turn...
        for (Context context : contextList) {
            URI loginUri = extAuth.getLoginRequestURIForContext(context);
            if (loginUri != null) {
                if (requestUri.getScheme().equals(loginUri.getScheme())
                        && requestUri.getHost().equals(loginUri.getHost())
                        && requestUri.getPort() == loginUri.getPort()
                        && requestUri.getPath().equals(loginUri.getPath())) {
                    // we got this far.. only the method (GET/POST), user details, query params,
                    // fragment, and POST params
                    // are possibly different from the login page.
                    loginUrl = true;
                    log.info(requestUri.toString()
                            + " falls within a context, and is the defined Login URL. Scanning for possible Username Enumeration vulnerability.");
                    break; // Stop checking
                }
            }
        }

        // the Username Enumeration scanner will only run for logon pages
        if (loginUrl == false) {
            if (this.debugEnabled) {
                log.debug(requestUri.toString() + " is not a defined Login URL.");
            }
            return; // No need to continue for this URL
        }

        // find all params set in the request (GET/POST/Cookie)
        TreeSet<HtmlParameter> htmlParams = new TreeSet<>();
        htmlParams.addAll(getBaseMsg().getRequestHeader().getCookieParams()); // request cookies only. no response cookies
        htmlParams.addAll(getBaseMsg().getFormParams()); // add in the POST params
        htmlParams.addAll(getBaseMsg().getUrlParams()); // add in the GET params

        int numberOfRequests = 0;
        if (this.getAttackStrength() == AttackStrength.INSANE) {
            numberOfRequests = 50;
        } else if (this.getAttackStrength() == AttackStrength.HIGH) {
            numberOfRequests = 15;
        } else if (this.getAttackStrength() == AttackStrength.MEDIUM) {
            numberOfRequests = 5;
        } else if (this.getAttackStrength() == AttackStrength.LOW) {
            numberOfRequests = 3;
        }

        // 1) Request the original URL n times. (The original URL is assumed to have a valid
        // username, if not a valid password). Store the results in A[].
        // make sure to manually handle all redirects, and cookies that may be set in response.
        // allocate enough space for the responses

        StringBuilder responseA = null;
        StringBuilder responseB = null;
        String longestCommonSubstringA = null;
        String longestCommonSubstringB = null;

        for (int i = 0; i < numberOfRequests; i++) {

            // initialise the storage for this iteration
            // baseResponses[i]= new StringBuilder(250);
            responseA = new StringBuilder(250);

            HttpMessage msgCpy = getNewMsg(); // clone the request, but not the response

            sendAndReceive(msgCpy, false, false); // request the URL, but do not automatically follow redirects.

            // get all cookies set in the response
            TreeSet<HtmlParameter> cookies = msgCpy.getResponseHeader().getCookieParams();

            int redirectCount = 0;
            while (HttpStatusCode.isRedirection(msgCpy.getResponseHeader().getStatusCode())) {
                redirectCount++;

                if (this.debugEnabled)
                    log.debug("Following redirect " + redirectCount + " for message " + i + " of "
                            + numberOfRequests + " iterations of the original query");

                // append the response to the responses so far for this particular instance
                // this will give us a complete picture of the full set of actual traffic
                // associated with following redirects for the request
                responseA.append(msgCpy.getResponseHeader().getHeadersAsString());
                responseA.append(msgCpy.getResponseBody().toString());

                // and manually follow the redirect
                // create a new message from scratch
                HttpMessage msgRedirect = new HttpMessage();

                // create a new URI from the absolute location returned, and interpret it as
                // escaped
                // note that the standard says that the Location returned should be absolute,
                // but it ain't always so...
                URI newLocation = new URI(msgCpy.getResponseHeader().getHeader(HttpHeader.LOCATION), true);
                try {
                    msgRedirect.getRequestHeader().setURI(newLocation);
                } catch (Exception e) {
                    // the Location field contents may not be standards compliant. Lets generate
                    // a uri to use as a workaround where a relative path was
                    // given instead of an absolute one
                    URI newLocationWorkaround = new URI(msgCpy.getRequestHeader().getURI(),
                            msgCpy.getResponseHeader().getHeader(HttpHeader.LOCATION), true);
                    // try again, except this time, if it fails, don't try to handle it
                    if (this.debugEnabled)
                        log.debug("The Location [" + newLocation
                                + "] specified in a redirect was not valid (not absolute?). Trying absolute workaround url ["
                                + newLocationWorkaround + "]");
                    msgRedirect.getRequestHeader().setURI(newLocationWorkaround);
                }
                msgRedirect.getRequestHeader().setMethod(HttpRequestHeader.GET); // it's always a GET for a redirect
                msgRedirect.getRequestHeader().setContentLength(0); // since we send a GET, the body will be 0 long
                if (cookies.size() > 0) {
                    // if a previous request sent back a cookie that has not since been
                    // invalidated, we need to set that cookie when following redirects, as a
                    // browser would
                    msgRedirect.getRequestHeader().setCookieParams(cookies);
                }

                if (this.debugEnabled)
                    log.debug("DEBUG: Following redirect to [" + newLocation + "]");
                sendAndReceive(msgRedirect, false, false); // do NOT redirect.. handle it here

                // handle scenario where a cookie is unset in a subsequent iteration, or where
                // the same cookie name is later re-assigned a different value
                // ie, in these cases, do not simply (and dumbly) accumulate cookie detritus.
                // first get all cookies set in the response
                TreeSet<HtmlParameter> cookiesTemp = msgRedirect.getResponseHeader().getCookieParams();
                for (Iterator<HtmlParameter> redirectSetsCookieIterator = cookiesTemp
                        .iterator(); redirectSetsCookieIterator.hasNext();) {
                    HtmlParameter cookieJustSet = redirectSetsCookieIterator.next();
                    // loop through each of the cookies we know about in cookies, to see if it
                    // matches by name.
                    // if so, delete that cookie, and add the one that was just set to cookies.
                    // if not, add the one that was just set to cookies.
                    for (Iterator<HtmlParameter> knownCookiesIterator = cookies.iterator(); knownCookiesIterator
                            .hasNext();) {
                        HtmlParameter knownCookie = knownCookiesIterator.next();
                        if (cookieJustSet.getName().equals(knownCookie.getName())) {
                            knownCookiesIterator.remove();
                            break; // out of the loop for known cookies, back to the next cookie
                            // set in the response
                        }
                    } // end of loop for cookies we already know about
                      // we can now safely add the cookie that was just set into cookies, knowing
                      // it does not clash with anything else in there.
                    cookies.add(cookieJustSet);
                } // end of for loop for cookies just set in the redirect

                msgCpy = msgRedirect; // store the last redirect message into the MsgCpy, as we
                // will be using it's output in a moment..
            } // end of loop to follow redirects

            // now that the redirections have all been handled.. was the request finally a
            // success or not?  Successful or Failed Logins would normally both return an OK
            // HTTP status
            if (!HttpStatusCode.isSuccess(msgCpy.getResponseHeader().getStatusCode())) {
                log.warn("The original URL [" + getBaseMsg().getRequestHeader().getURI()
                        + "] returned a non-OK HTTP status " + msgCpy.getResponseHeader().getStatusCode()
                        + " (after " + i + " of " + numberOfRequests
                        + " steps). Could be indicative of SQL Injection, or some other error. The URL is not stable enough to look at Username Enumeration");
                return; // we have not even got as far as looking at the parameters, so just
                // abort straight out of the method
            }

            if (this.debugEnabled)
                log.debug("Done following redirects!");

            // append the response to the responses so far for this particular instance
            // this will give us a complete picture of the full set of actual traffic associated
            // with following redirects for the request
            responseA.append(msgCpy.getResponseHeader().getHeadersAsString());
            responseA.append(msgCpy.getResponseBody().toString());

            // 2) Compute the longest common subsequence (LCS) of A[] into LCS_A
            // Note: in the Freiling and Schinzel method, this is calculated recursively. We
            // calculate it iteratively, but using an equivalent method

            // first time in, the LCS is simple: it's the first HTML result.. no diffing
            // required
            if (i == 0)
                longestCommonSubstringA = responseA.toString();
            // else get the LCS of the existing string, and the current result
            else
                longestCommonSubstringA = this.longestCommonSubsequence(longestCommonSubstringA,
                        responseA.toString());

            // optimisation step: if the LCS of A is 0 characters long already, then the URL
            // output is not stable, and we can abort now, and save some time
            if (longestCommonSubstringA.length() == 0) {
                // this might occur if the output returned for the URL changed mid-way. Perhaps
                // a CAPTCHA has fired, or a WAF has kicked in.  Let's abort now so.
                log.warn("The original URL [" + getBaseMsg().getRequestHeader().getURI()
                        + "] does not produce stable output (at " + i + 1 + " of " + numberOfRequests
                        + " steps). There is no static element in the output that can be used as a basis of comparison for the result of requesting URLs with the parameter values modified. Perhaps a CAPTCHA or WAF has kicked in!!");
                return; // we have not even got as far as looking at the parameters, so just
                // abort straight out of the method
            }
        }
        // get rid of any remnants of cookie setting and Date headers in the responses, as these
        // cause false positives, and can be safely ignored
        // replace the content length with a non-variable placeholder
        // replace url parameters with a non-variable placeholder to eliminate tokens in URLs in
        // the output
        longestCommonSubstringA = longestCommonSubstringA.replaceAll("Set-Cookie:[^\\r\\n]+[\\r\\n]{1,2}", "");
        longestCommonSubstringA = longestCommonSubstringA.replaceAll("Date:[^\\r\\n]+[\\r\\n]{1,2}", "");
        longestCommonSubstringA = longestCommonSubstringA.replaceAll("Content-Length:[^\\r\\n]+[\\r\\n]{1,2}",
                "Content-Length: XXXX\n");
        longestCommonSubstringA = longestCommonSubstringA
                .replaceAll("(?<=(&amp;|\\?)[^\\?\"=&;]+=)[^\\?\"=&;]+(?=(&amp;|\"))", "YYYY");

        if (this.debugEnabled)
            log.debug("The LCS of A is [" + longestCommonSubstringA + "]");

        // 3) for each parameter in the original URL (ie, for URL params, form params, and
        // cookie params)
        for (Iterator<HtmlParameter> iter = htmlParams.iterator(); iter.hasNext();) {

            HttpMessage msgModifiedParam = getNewMsg();
            HtmlParameter currentHtmlParameter = iter.next();

            if (this.debugEnabled)
                log.debug("Handling [" + currentHtmlParameter.getType() + "] parameter ["
                        + currentHtmlParameter.getName() + "], with value [" + currentHtmlParameter.getValue()
                        + "]");

            // 4) Change the current parameter value (which we assume is the username parameter)
            // to an invalid username (randomly), and request the URL n times. Store the results
            // in B[].

            // get a random user name the same length as the original!
            String invalidUsername = RandomStringUtils.random(currentHtmlParameter.getValue().length(),
                    RANDOM_USERNAME_CHARS);
            if (this.debugEnabled)
                log.debug("The invalid username chosen was [" + invalidUsername + "]");

            TreeSet<HtmlParameter> requestParams = null;
            if (currentHtmlParameter.getType().equals(HtmlParameter.Type.cookie)) {
                requestParams = msgModifiedParam.getRequestHeader().getCookieParams();
                requestParams.remove(currentHtmlParameter);
                requestParams.add(new HtmlParameter(currentHtmlParameter.getType(),
                        currentHtmlParameter.getName(), invalidUsername.toString())); // add in the invalid username
                msgModifiedParam.setCookieParams(requestParams);
            } else if (currentHtmlParameter.getType().equals(HtmlParameter.Type.url)) {
                requestParams = msgModifiedParam.getUrlParams();
                requestParams.remove(currentHtmlParameter);
                requestParams.add(new HtmlParameter(currentHtmlParameter.getType(),
                        currentHtmlParameter.getName(), invalidUsername.toString())); // add in the invalid username
                msgModifiedParam.setGetParams(requestParams);
            } else if (currentHtmlParameter.getType().equals(HtmlParameter.Type.form)) {
                requestParams = msgModifiedParam.getFormParams();
                requestParams.remove(currentHtmlParameter);
                requestParams.add(new HtmlParameter(currentHtmlParameter.getType(),
                        currentHtmlParameter.getName(), invalidUsername.toString())); // add in the invalid username
                msgModifiedParam.setFormParams(requestParams);
            }

            if (this.debugEnabled)
                log.debug("About to loop for " + numberOfRequests
                        + " iterations with an incorrect user of the same length");

            boolean continueForParameter = true;
            for (int i = 0; i < numberOfRequests && continueForParameter; i++) {

                // initialise the storage for this iteration
                responseB = new StringBuilder(250);

                HttpMessage msgCpy = msgModifiedParam; // use the message we already set up, with the
                // modified parameter value

                sendAndReceive(msgCpy, false, false); // request the URL, but do not automatically follow redirects.

                // get all cookies set in the response
                TreeSet<HtmlParameter> cookies = msgCpy.getResponseHeader().getCookieParams();

                int redirectCount = 0;
                while (HttpStatusCode.isRedirection(msgCpy.getResponseHeader().getStatusCode())) {
                    redirectCount++;

                    if (this.debugEnabled)
                        log.debug("Following redirect " + redirectCount + " for message " + i + " of "
                                + numberOfRequests + " iterations of the modified query");

                    // append the response to the responses so far for this particular instance
                    // this will give us a complete picture of the full set of actual traffic
                    // associated with following redirects for the request
                    responseB.append(msgCpy.getResponseHeader().getHeadersAsString());
                    responseB.append(msgCpy.getResponseBody().toString());

                    // and manually follow the redirect
                    // create a new message from scratch
                    HttpMessage msgRedirect = new HttpMessage();

                    // create a new URI from the absolute location returned, and interpret it as
                    // escaped
                    // note that the standard says that the Location returned should be
                    // absolute, but it ain't always so...
                    URI newLocation = new URI(msgCpy.getResponseHeader().getHeader(HttpHeader.LOCATION), true);
                    try {
                        msgRedirect.getRequestHeader().setURI(newLocation);
                    } catch (Exception e) {
                        // the Location field contents may not be standards compliant. Lets
                        // generate a uri to use as a workaround where a relative path was
                        // given instead of an absolute one
                        URI newLocationWorkaround = new URI(msgCpy.getRequestHeader().getURI(),
                                msgCpy.getResponseHeader().getHeader(HttpHeader.LOCATION), true);
                        // try again, except this time, if it fails, don't try to handle it
                        if (this.debugEnabled)
                            log.debug("The Location [" + newLocation
                                    + "] specified in a redirect was not valid (not absolute?). Trying absolute workaround url ["
                                    + newLocationWorkaround + "]");
                        msgRedirect.getRequestHeader().setURI(newLocationWorkaround);
                    }
                    msgRedirect.getRequestHeader().setMethod(HttpRequestHeader.GET); // it's always a GET for a redirect
                    msgRedirect.getRequestHeader().setContentLength(0); // since we send a GET, the body will be 0 long
                    if (cookies.size() > 0) {
                        // if a previous request sent back a cookie that has not since been
                        // invalidated, we need to set that cookie when following redirects, as
                        // a browser would
                        msgRedirect.getRequestHeader().setCookieParams(cookies);
                    }

                    sendAndReceive(msgRedirect, false, false); // do NOT redirect.. handle it here

                    // handle scenario where a cookie is unset in a subsequent iteration, or
                    // where the same cookie name is later re-assigned a different value
                    // ie, in these cases, do not simply (and dumbly) accumulate cookie
                    // detritus.
                    // first get all cookies set in the response
                    TreeSet<HtmlParameter> cookiesTemp = msgRedirect.getResponseHeader().getCookieParams();
                    for (Iterator<HtmlParameter> redirectSetsCookieIterator = cookiesTemp
                            .iterator(); redirectSetsCookieIterator.hasNext();) {
                        HtmlParameter cookieJustSet = redirectSetsCookieIterator.next();
                        // loop through each of the cookies we know about in cookies, to see if
                        // it matches by name.
                        // if so, delete that cookie, and add the one that was just set to
                        // cookies.
                        // if not, add the one that was just set to cookies.
                        for (Iterator<HtmlParameter> knownCookiesIterator = cookies
                                .iterator(); knownCookiesIterator.hasNext();) {
                            HtmlParameter knownCookie = knownCookiesIterator.next();
                            if (cookieJustSet.getName().equals(knownCookie.getName())) {
                                knownCookiesIterator.remove();
                                break; // out of the loop for known cookies, back to the next
                                // cookie set in the response
                            }
                        } // end of loop for cookies we already know about
                          // we can now safely add the cookie that was just set into cookies,
                          // knowing it does not clash with anything else in there.
                        cookies.add(cookieJustSet);
                    } // end of for loop for cookies just set in the redirect

                    msgCpy = msgRedirect; // store the last redirect message into the MsgCpy, as
                    // we will be using it's output in a moment..
                } // end of loop to follow redirects

                // now that the redirections have all been handled.. was the request finally a
                // success or not?  Successful or Failed Logins would normally both return an OK
                // HTTP status
                if (!HttpStatusCode.isSuccess(msgCpy.getResponseHeader().getStatusCode())) {
                    log.warn("The modified URL [" + msgModifiedParam.getRequestHeader().getURI()
                            + "] returned a non-OK HTTP status " + msgCpy.getResponseHeader().getStatusCode()
                            + " (after " + i + 1 + " of " + numberOfRequests + " steps for ["
                            + currentHtmlParameter.getType() + "] parameter " + currentHtmlParameter.getName()
                            + "). Could be indicative of SQL Injection, or some other error. The URL is not stable enough to look at Username Enumeration");
                    continueForParameter = false;
                    continue; // skip directly to the next parameter. Do not pass Go. Do not
                    // collect $200.
                }

                if (this.debugEnabled)
                    log.debug("Done following redirects!");

                // append the response to the responses so far for this particular instance
                // this will give us a complete picture of the full set of actual traffic
                // associated with following redirects for the request
                responseB.append(msgCpy.getResponseHeader().getHeadersAsString());
                responseB.append(msgCpy.getResponseBody().toString());

                // 5) Compute the longest common subsequence (LCS) of B[] into LCS_B
                // Note: in the Freiling and Schinzel method, this is calculated recursively. We
                // calculate it iteratively, but using an equivalent method

                // first time in, the LCS is simple: it's the first HTML result.. no diffing
                // required
                if (i == 0)
                    longestCommonSubstringB = responseB.toString();
                // else get the LCS of the existing string, and the current result
                else
                    longestCommonSubstringB = this.longestCommonSubsequence(longestCommonSubstringB,
                            responseB.toString());

                // optimisation step: if the LCS of B is 0 characters long already, then the URL
                // output is not stable, and we can abort now, and save some time
                if (longestCommonSubstringB.length() == 0) {
                    // this might occur if the output returned for the URL changed mid-way.
                    // Perhaps a CAPTCHA has fired, or a WAF has kicked in.  Let's abort now so.
                    log.warn("The modified URL [" + msgModifiedParam.getRequestHeader().getURI() + "] (for ["
                            + currentHtmlParameter.getType() + "] parameter " + currentHtmlParameter.getName()
                            + ") does not produce stable output (after " + i + 1 + " of " + numberOfRequests
                            + " steps). There is no static element in the output that can be used as a basis of comparison with the static output of the original query. Perhaps a CAPTCHA or WAF has kicked in!!");
                    continueForParameter = false;
                    continue; // skip directly to the next parameter. Do not pass Go. Do not
                    // collect $200.
                    // Note: if a CAPTCHA or WAF really has fired, the results of subsequent
                    // iterations will likely not be accurate..
                }
            }

            // if we didn't hit something with one of the iterations for the parameter (ie, if
            // the output when changing the parm is stable),
            // check if the parameter might be vulnerable by comparins its LCS with the original
            // LCS for a valid login
            if (continueForParameter == true) {
                // get rid of any remnants of cookie setting and Date headers in the responses,
                // as these cause false positives, and can be safely ignored
                // replace the content length with a non-variable placeholder
                // replace url parameters with a non-variable placeholder to eliminate tokens in
                // URLs in the output
                longestCommonSubstringB = longestCommonSubstringB
                        .replaceAll("Set-Cookie:[^\\r\\n]+[\\r\\n]{1,2}", "");
                longestCommonSubstringB = longestCommonSubstringB.replaceAll("Date:[^\\r\\n]+[\\r\\n]{1,2}",
                        "");
                longestCommonSubstringB = longestCommonSubstringB
                        .replaceAll("Content-Length:[^\\r\\n]+[\\r\\n]{1,2}", "Content-Length: XXXX\n");
                longestCommonSubstringB = longestCommonSubstringB
                        .replaceAll("(?<=(&amp;|\\?)[^\\?\"=&;]+=)[^\\?\"=&;]+(?=(&amp;|\"))", "YYYY");

                if (this.debugEnabled)
                    log.debug("The LCS of B is [" + longestCommonSubstringB + "]");

                // 6) If LCS_A <> LCS_B, then there is a Username Enumeration issue on the
                // current parameter
                if (!longestCommonSubstringA.equals(longestCommonSubstringB)) {
                    // calculate line level diffs of the 2 Longest Common Substrings to aid the
                    // user in deciding if the match is a false positive
                    // get the diff as a series of patches
                    Patch diffpatch = DiffUtils.diff(
                            new LinkedList<String>(Arrays.asList(longestCommonSubstringA.split("\\n"))),
                            new LinkedList<String>(Arrays.asList(longestCommonSubstringB.split("\\n"))));

                    int numberofDifferences = diffpatch.getDeltas().size();

                    // and convert the list of patches to a String, joining using a newline
                    // String diffAB = StringUtils.join(diffpatch.getDeltas(), "\n");
                    StringBuilder tempDiff = new StringBuilder(250);
                    for (Delta delta : diffpatch.getDeltas()) {
                        String changeType = null;
                        if (delta.getType() == Delta.TYPE.CHANGE)
                            changeType = "Changed Text";
                        else if (delta.getType() == Delta.TYPE.DELETE)
                            changeType = "Deleted Text";
                        else if (delta.getType() == Delta.TYPE.INSERT)
                            changeType = "Inserted text";
                        else
                            changeType = "Unknown change type [" + delta.getType() + "]";

                        tempDiff.append("\n(" + changeType + ")\n"); // blank line before
                        tempDiff.append("Output for Valid Username  : " + delta.getOriginal() + "\n"); // no blank lines
                        tempDiff.append("\nOutput for Invalid Username: " + delta.getRevised() + "\n"); // blank line before
                    }
                    String diffAB = tempDiff.toString();
                    String extraInfo = Constant.messages.getString(
                            "ascanbeta.usernameenumeration.alert.extrainfo", currentHtmlParameter.getType(),
                            currentHtmlParameter.getName(), currentHtmlParameter.getValue(), // original value
                            invalidUsername.toString(), // new value
                            diffAB, // the differences between the two sets of output
                            numberofDifferences); // the number of differences
                    String attack = Constant.messages.getString("ascanbeta.usernameenumeration.alert.attack",
                            currentHtmlParameter.getType(), currentHtmlParameter.getName());
                    String vulnname = Constant.messages.getString("ascanbeta.usernameenumeration.name");
                    String vulndesc = Constant.messages.getString("ascanbeta.usernameenumeration.desc");
                    String vulnsoln = Constant.messages.getString("ascanbeta.usernameenumeration.soln");

                    // call bingo with some extra info, indicating that the alert is
                    bingo(Alert.RISK_INFO, Alert.CONFIDENCE_LOW, vulnname, vulndesc,
                            getBaseMsg().getRequestHeader().getURI().getURI(), currentHtmlParameter.getName(),
                            attack, extraInfo, vulnsoln, getBaseMsg());

                } else {
                    if (this.debugEnabled)
                        log.debug("[" + currentHtmlParameter.getType() + "] parameter ["
                                + currentHtmlParameter.getName()
                                + "] looks ok (Invalid Usernames cannot be distinguished from Valid usernames)");
                }
            }
        } // end of the for loop around the parameter list

    } catch (Exception e) {
        // Do not try to internationalise this.. we need an error message in any event..
        // if it's in English, it's still better than not having it at all.
        log.error("An error occurred checking a url for Username Enumeration issues", e);
    }
}

From source file:org.zaproxy.zap.extension.bruteforce.ScanTarget.java

public ScanTarget(URI uri) {
    this.uri = copyURI(uri);

    this.scheme = uri.getScheme();

    try {/*from w w  w  .ja  v a  2 s  .  co  m*/
        this.host = uri.getHost();
    } catch (URIException e) {
        throw new IllegalArgumentException("Failed to get host from URI: " + e.getMessage(), e);
    }

    this.port = getPort(scheme, uri.getPort());

    try {
        this.uri.setPath(null);
        this.uri.setQuery(null);
        this.uri.setFragment(null);
    } catch (URIException ignore) {
        // It's safe to set the URI query, path and fragment components to null.
    }

    this.stringRepresentation = createHostPortString(host, port);
    buildHtmlStringRepresentation();
}

From source file:org.zaproxy.zap.extension.callgraph.CallGraphFrame.java

/**
 * sets up the graph by retrieving the nodes and edges from the history table in the database
 *
 * @param urlPattern/*w w w  .ja v a2s  .c  o m*/
 * @throws SQLException
 */
private void setupGraph(Pattern urlPattern) throws SQLException {
    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    Map<String, String> schemaAuthorityToColor = new HashMap<String, String>();
    // use some web safe colours. Currently, there are 24 colours.
    String[] colors = { "#FFFF00", "#FFCC00", "#FF9900", "#FF6600", "#FF3300", "#CCFF00", "#CCCC00", "#CC9900",
            "#CC6600", "#99FF00", "#999900", "#996600", "#CCFFCC", "#CCCCCC", "#99CCCC", "#9999CC", "#9966CC",
            "#66FFCC", "#6699CC", "#6666CC", "#33FFCC", "#33CCCC", "#3399CC", "#00FFCC" };
    int colorsUsed = 0;
    try {
        // Create a pattern for the specified

        // get a new connection to the database to query it, since the existing database classes
        // do not cater for
        // ad-hoc queries on the table
        /*
         * TODO Add-ons should NOT make their own connections to the db any more - the db layer is plugable
         * so could be implemented in a completely different way
         * TODO: how? There is currently no API to do this.
         */
        // Note: the db is a singleton instance, so do *not* close it!!
        Database db = Model.getSingleton().getDb();
        if (!(db instanceof ParosDatabase)) {
            throw new InvalidParameterException(db.getClass().getCanonicalName());
        }

        conn = ((ParosDatabaseServer) db.getDatabaseServer()).getNewConnection();

        // we begin adding stuff to the graph, so begin a "transaction" on it.
        // we will close this after we add all the vertexes and edges to the graph
        graph.getModel().beginUpdate();

        // prepare to add the vertices to the graph
        // this must include all URLs references as vertices, even if those URLs did not feature
        // in the history table in their own right

        // include entries of type 1 (proxied), 2 (spidered), 10 (Ajax spidered) from the
        // history
        st = conn.createStatement();
        rs = st.executeQuery(
                "select distinct URI from HISTORY where histtype in (1,2,10) union distinct select distinct  RIGHT(REGEXP_SUBSTRING (REQHEADER, 'Referer:.+') , LENGTH(REGEXP_SUBSTRING (REQHEADER, 'Referer:.+'))-LENGTH('Referer: ')) from HISTORY where REQHEADER like '%Referer%' and histtype in (1,2,10) order by 1");
        for (; rs.next();) {
            String url = rs.getString(1);

            // remove urls that do not match the pattern specified (all sites / one site)
            Matcher urlmatcher = urlPattern.matcher(url);
            if (urlmatcher.find()) {
                // addVertex(url , url);
                try {
                    URI uri = new URI(url, false);
                    String schemaAuthority = uri.getScheme() + "://" + uri.getAuthority();
                    String path = uri.getPathQuery();
                    if (path == null)
                        path = "/";
                    String color = schemaAuthorityToColor.get(schemaAuthority);
                    if (color == null) {
                        // not found already.. so assign this scheme and authority a color.
                        if (colorsUsed >= colors.length) {
                            throw new Exception("Too many scheme/authority combinations. Ne need more colours");
                        }
                        color = colors[colorsUsed++];
                        schemaAuthorityToColor.put(schemaAuthority, color);
                    }
                    addVertex(path, url, "fillColor=" + color);
                } catch (Exception e) {
                    log.error("Error graphing node for URL " + url, e);
                }
            } else {
                if (log.isDebugEnabled())
                    log.debug("URL " + url + " does not match the specified pattern " + urlPattern
                            + ", so not adding it as a vertex");
            }
        }
        // close the resultset and statement
        rs.close();
        st.close();

        // set up the edges in the graph
        st = conn.createStatement();
        rs = st.executeQuery(
                "select distinct RIGHT(REGEXP_SUBSTRING (REQHEADER, 'Referer:.+') , LENGTH(REGEXP_SUBSTRING (REQHEADER, 'Referer:.+'))-LENGTH('Referer: ')), URI from HISTORY where REQHEADER like '%Referer%' and histtype in (1,2,10) order by 2");

        mxGraphModel graphmodel = (mxGraphModel) graph.getModel();
        for (; rs.next();) {
            String predecessor = rs.getString(1);
            String url = rs.getString(2);

            // now trim back all urls from the base url
            // Matcher predecessorurlmatcher = urlpattern.matcher(predecessor);
            // if (predecessorurlmatcher.find()) {
            //   predecessor =  predecessorurlmatcher.group(1);
            //   }
            // Matcher urlmatcher = urlpattern.matcher(url);
            // if (urlmatcher.find()) {
            //   url =  urlmatcher.group(1);
            //   }

            // remove urls that do not match the pattern specified (all sites / one site)
            Matcher urlmatcher1 = urlPattern.matcher(predecessor);
            if (!urlmatcher1.find()) {
                if (log.isDebugEnabled())
                    log.debug("Predecessor URL " + predecessor + " does not match the specified pattern "
                            + urlPattern + ", so not adding it as a vertex");
                continue; // to the next iteration
            }
            Matcher urlmatcher2 = urlPattern.matcher(url);
            if (!urlmatcher2.find()) {
                if (log.isDebugEnabled())
                    log.debug("URL " + url + " does not match the specified pattern " + urlPattern
                            + ", so not adding it as a vertex");
                continue; // to the next iteration
            }

            // check that we have added the url as a vertex in its own right.. definitely should
            // have happened..
            mxCell predecessorVertex = (mxCell) graphmodel.getCell(predecessor);
            mxCell postdecessorVertex = (mxCell) graphmodel.getCell(url);
            if (predecessorVertex == null || postdecessorVertex == null) {
                log.warn("Could not find graph node for " + predecessor + " or for " + url + ". Ignoring it.");
                continue;
            }
            // add the edge (ie, add the dependency between 2 URLs)
            graph.insertEdge(parent, predecessorVertex.getId() + "-->" + postdecessorVertex.getId(), null,
                    predecessorVertex, postdecessorVertex);
        }

        // once all the vertices and edges are drawn, look for root nodes (nodes with no
        // incoming edges)
        // we will display the full URl for these, rather than just the path, to aid viewing the
        // graph
        Object[] vertices = graph.getChildVertices(graph.getDefaultParent());
        for (Object vertex : vertices) {
            Object[] incomingEdgesForVertex = graph.getIncomingEdges(vertex);
            if (incomingEdgesForVertex == null
                    || (incomingEdgesForVertex != null && incomingEdgesForVertex.length == 0)) {
                // it's a root node. Set it's value (displayed label) to the same as it's id
                // (the full URL)
                mxCell vertextCasted = (mxCell) vertex;
                vertextCasted.setValue(vertextCasted.getId());

                // now sort out the text metrics for the vertex, since the size of the displayed
                // text has been changed
                Dimension textsize = this.getTextDimension((String) vertextCasted.getValue(), this.fontmetrics);
                mxGeometry cellGeometry = vertextCasted.getGeometry();
                cellGeometry.setHeight(textsize.getHeight());
                cellGeometry.setWidth(textsize.getWidth());
                vertextCasted.setGeometry(cellGeometry);
            }
        }
    } catch (SQLException e) {
        log.error("Error trying to setup the graph", e);
        throw e;
    } finally {

        if (rs != null && !rs.isClosed())
            rs.close();
        if (st != null && !st.isClosed())
            st.close();
        if (conn != null && !conn.isClosed())
            conn.close();
        // mark the "transaction" on the graph as complete
        graph.getModel().endUpdate();
    }
}

From source file:org.zaproxy.zap.extension.invoke.InvokeAppWorker.java

@Override
protected Void doInBackground() throws Exception {

    String url = ""; // Full URL
    String host = ""; // Just the server name, e.g. localhost
    String port = ""; // the port
    String site = ""; // e.g. http://localhost:8080/
    String postdata = ""; // only present in POST ops
    String cookie = ""; // from the request header
    HistoryReference historyRef = msg.getHistoryRef();
    int msgid = -1;

    if (historyRef != null) {
        msgid = historyRef.getHistoryId();
    }/*from  ww  w  .  j a v a 2  s .  c  o m*/

    URI uri = msg.getRequestHeader().getURI();
    url = uri.toString();
    host = uri.getHost();
    site = uri.getScheme() + "://" + uri.getHost();
    if (uri.getPort() > 0) {
        port = String.valueOf(uri.getPort());
        site = site + ":" + port + "/";
    } else {
        if (uri.getScheme().equalsIgnoreCase("http")) {
            port = "80";
        } else if (uri.getScheme().equalsIgnoreCase("https")) {
            port = "443";
        }
        site = site + "/";
    }
    if (msg.getRequestBody() != null) {
        postdata = msg.getRequestBody().toString();
        postdata = postdata.replaceAll("\n", "\\n");
    }
    Vector<String> cookies = msg.getRequestHeader().getHeaders(HttpHeader.COOKIE);
    if (cookies != null && cookies.size() > 0) {
        cookie = cookies.get(0);
    }

    List<String> cmd = new ArrayList<>();
    cmd.add(command);
    if (parameters != null) {
        for (String parameter : parameters.split(" ")) {
            // Replace all of the tags
            String finalParameter = parameter.replace("%url%", url).replace("%host%", host)
                    .replace("%port%", port).replace("%site%", site).replace("%cookie%", cookie)
                    .replace("%postdata%", postdata).replace("%msgid%", String.valueOf(msgid));

            // Replace header tags
            Matcher headers = Pattern.compile("%header-([A-z0-9_-]+)%").matcher(finalParameter);
            while (headers.find()) {
                String headerValue = msg.getRequestHeader().getHeader(headers.group(1));
                if (headerValue == null) {
                    headerValue = "";
                }
                finalParameter = finalParameter.replace(headers.group(0), headerValue);
            }

            cmd.add(finalParameter);
        }
    }

    logger.debug("Invoking: " + cmd.toString());
    View.getSingleton().getOutputPanel().append("\n" + cmd.toString() + "\n");
    ProcessBuilder pb = new ProcessBuilder(cmd);
    if (workingDir != null) {
        pb.directory(workingDir);
    }
    pb.redirectErrorStream(true);
    Process proc;
    try {
        proc = pb.start();
    } catch (final Exception e) {
        View.getSingleton().getOutputPanel()
                .append(Constant.messages.getString("invoke.error") + e.getLocalizedMessage() + "\n");
        logger.warn("Failed to start the process: " + e.getMessage(), e);
        return null;
    }

    if (captureOutput) {
        try (BufferedReader brOut = new BufferedReader(new InputStreamReader(proc.getInputStream()))) {
            String line;
            boolean isOutput = false;
            StringBuilder sb = new StringBuilder();
            if (msg.getNote() != null) {
                sb.append(msg.getNote());
                sb.append('\n');
            }

            // Show any stdout/error messages
            while ((line = brOut.readLine()) != null) {
                View.getSingleton().getOutputPanel().append(line + "\n");
                sb.append(line);
                sb.append('\n');
                isOutput = true;
            }
            if (isOutput) {
                // Somethings been written, switch to the Output tab
                View.getSingleton().getOutputPanel().setTabFocus();
            }

            if (outputNote) {
                HistoryReference hr = msg.getHistoryRef();
                if (hr != null) {
                    hr.setNote(sb.toString());
                }
            }
        }
    }

    return null;
}

From source file:org.zaproxy.zap.extension.openapi.ExtensionOpenApi.java

public List<String> importOpenApiDefinition(final URI uri, final String siteOverride, boolean initViaUi) {
    Requestor requestor = new Requestor(HttpSender.MANUAL_REQUEST_INITIATOR);
    requestor.addListener(new HistoryPersister());
    try {/*from  w w  w.j  a va2  s .c  o  m*/
        return importOpenApiDefinition(Scheme.forValue(uri.getScheme().toLowerCase()), uri.getAuthority(),
                requestor.getResponseBody(uri), siteOverride, initViaUi);
    } catch (IOException e) {
        if (initViaUi) {
            View.getSingleton().showWarningDialog(Constant.messages.getString("openapi.io.error"));
        }
        LOG.warn(e.getMessage(), e);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
    return null;
}

From source file:org.zaproxy.zap.extension.pscanrulesAlpha.StrictTransportSecurityScanner.java

@Override
public void scanHttpResponseReceive(HttpMessage msg, int id, Source source) {
    long start = System.currentTimeMillis();
    Vector<String> stsOption = msg.getResponseHeader().getHeaders(STS_HEADER);
    String metaHSTS = getMetaHSTSEvidence(source);

    if (msg.getRequestHeader().isSecure()) { // No point reporting missing for non-SSL resources
        // Content available via both HTTPS and HTTP is a separate though related issue
        if (stsOption == null) { // Header NOT found
            boolean report = true;
            if (!this.getAlertThreshold().equals(AlertThreshold.LOW)
                    && HttpStatusCode.isRedirection(msg.getResponseHeader().getStatusCode())) {
                // Only report https redirects to the same domain at low threshold
                try {
                    String redirStr = msg.getResponseHeader().getHeader(HttpHeader.LOCATION);
                    URI srcUri = msg.getRequestHeader().getURI();
                    URI redirUri = new URI(redirStr, false);
                    if (redirUri.isRelativeURI() || (redirUri.getScheme().equalsIgnoreCase("https")
                            && redirUri.getHost().equals(srcUri.getHost())
                            && redirUri.getPort() == srcUri.getPort())) {
                        report = false;//from www  .ja va 2s . c om
                    }
                } catch (Exception e) {
                    // Ignore, so report the missing header
                }
            }
            if (report) {
                raiseAlert(VulnType.HSTS_MISSING, null, msg, id);
            }
        } else if (stsOption.size() > 1) { // More than one header found
            raiseAlert(VulnType.HSTS_MULTIPLE_HEADERS, null, msg, id);
        } else { // Single HSTS header entry
            String stsOptionString = stsOption.get(0);
            Matcher badAgeMatcher = BAD_MAX_AGE_PATT.matcher(stsOptionString);
            Matcher maxAgeMatcher = MAX_AGE_PATT.matcher(stsOptionString);
            Matcher malformedMaxAgeMatcher = MALFORMED_MAX_AGE.matcher(stsOptionString);
            Matcher wellformedMatcher = WELL_FORMED_PATT.matcher(stsOptionString);
            if (!wellformedMatcher.matches()) {
                // Well formed pattern didn't match (perhaps curly quotes or some other unwanted
                // character(s))
                raiseAlert(VulnType.HSTS_MALFORMED_CONTENT, STS_HEADER, msg, id);
            } else if (badAgeMatcher.find()) {
                // Matched BAD_MAX_AGE_PATT, max-age is zero
                raiseAlert(VulnType.HSTS_MAX_AGE_DISABLED, badAgeMatcher.group(), msg, id);
            } else if (!maxAgeMatcher.find()) {
                // Didn't find a digit value associated with max-age
                raiseAlert(VulnType.HSTS_MAX_AGE_MISSING, stsOption.get(0), msg, id);
            } else if (malformedMaxAgeMatcher.find()) {
                // Found max-age but it was malformed
                raiseAlert(VulnType.HSTS_MALFORMED_MAX_AGE, stsOption.get(0), msg, id);
            }
        }
    } else if (AlertThreshold.LOW.equals(this.getAlertThreshold()) && stsOption != null
            && !stsOption.isEmpty()) {
        // isSecure is false at this point
        // HSTS Header found on non-HTTPS response (technically there could be more than one
        // but we only care that there is one or more)
        raiseAlert(VulnType.HSTS_ON_PLAIN_RESP, stsOption.get(0), msg, id);
    }

    if (metaHSTS != null) {
        // HSTS found defined by META tag
        raiseAlert(VulnType.HSTS_META, metaHSTS, msg, id);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("\tScan of record " + id + " took " + (System.currentTimeMillis() - start) + " ms");
    }
}