Search in sources :

Example 1 with HarEntries

use of edu.umass.cs.benchlab.har.HarEntries in project zaproxy by zaproxy.

the class CoreAPI method handleApiOther.

@Override
public HttpMessage handleApiOther(HttpMessage msg, String name, JSONObject params) throws ApiException {
    if (OTHER_PROXY_PAC.equals(name)) {
        @SuppressWarnings("deprecation") final org.parosproxy.paros.core.proxy.ProxyParam proxyParam = Model.getSingleton().getOptionsParam().getProxyParam();
        final int port = proxyParam.getProxyPort();
        try {
            String domain = null;
            if (proxyParam.isProxyIpAnyLocalAddress()) {
                String localDomain = msg.getRequestHeader().getHostName();
                if (!API.API_DOMAIN.equals(localDomain)) {
                    domain = localDomain;
                }
            }
            if (domain == null) {
                domain = proxyParam.getProxyIp();
            }
            String response = this.getPacFile(domain, port);
            msg.setResponseHeader(API.getDefaultResponseHeader("text/html", response.length()));
            msg.setResponseBody(response);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return msg;
    } else if (OTHER_SET_PROXY.equals(name)) {
        /* JSON string:
             *  {"type":1,
             *  "http":	{"host":"proxy.corp.com","port":80},
             *  "ssl":	{"host":"proxy.corp.com","port":80},
             *  "ftp":{"host":"proxy.corp.com","port":80},
             *  "socks":{"host":"proxy.corp.com","port":80},
             *  "shareSettings":true,"socksVersion":5,
             *  "proxyExcludes":"localhost, 127.0.0.1"}
             */
        String proxyDetails = params.getString(PARAM_PROXY_DETAILS);
        String response = "OK";
        try {
            try {
                JSONObject json = JSONObject.fromObject(proxyDetails);
                if (json.getInt("type") == 1) {
                    JSONObject httpJson = JSONObject.fromObject(json.get("http"));
                    String proxyHost = httpJson.getString("host");
                    int proxyPort = httpJson.getInt("port");
                    if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
                        Model.getSingleton().getOptionsParam().getConnectionParam().setProxyChainName(proxyHost);
                        Model.getSingleton().getOptionsParam().getConnectionParam().setProxyChainPort(proxyPort);
                    }
                }
            } catch (JSONException e) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_PROXY_DETAILS);
            }
            msg.setResponseHeader(API.getDefaultResponseHeader("text/html", response.length()));
            msg.setResponseBody(response);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return msg;
    } else if (OTHER_ROOT_CERT.equals(name)) {
        return getNetworkImplementor().handleApiOther(msg, "rootCaCert", params);
    } else if (OTHER_XML_REPORT.equals(name)) {
        generateReport(msg, ScanReportType.XML);
        return msg;
    } else if (OTHER_HTML_REPORT.equals(name)) {
        generateReport(msg, ScanReportType.HTML);
        return msg;
    } else if (OTHER_JSON_REPORT.equals(name)) {
        generateReport(msg, ScanReportType.JSON);
        return msg;
    } else if (OTHER_MD_REPORT.equals(name)) {
        generateReport(msg, ScanReportType.MD);
        return msg;
    } else if (OTHER_MESSAGE_HAR.equals(name)) {
        byte[] responseBody;
        try {
            final HarEntries entries = new HarEntries();
            TableHistory tableHistory = Model.getSingleton().getDb().getTableHistory();
            RecordHistory recordHistory = getRecordHistory(tableHistory, getParam(params, PARAM_ID, -1));
            addHarEntry(entries, recordHistory);
            HarLog harLog = HarUtils.createZapHarLog();
            harLog.setEntries(entries);
            responseBody = HarUtils.harLogToByteArray(harLog);
        } catch (ApiException e) {
            responseBody = e.toString(API.Format.JSON, incErrorDetails()).getBytes(StandardCharsets.UTF_8);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ApiException apiException = new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
            responseBody = apiException.toString(API.Format.JSON, incErrorDetails()).getBytes(StandardCharsets.UTF_8);
        }
        try {
            msg.setResponseHeader(API.getDefaultResponseHeader("application/json; charset=UTF-8", responseBody.length));
        } catch (HttpMalformedHeaderException e) {
            logger.error("Failed to create response header: " + e.getMessage(), e);
        }
        msg.setResponseBody(responseBody);
        return msg;
    } else if (OTHER_MESSAGES_HAR_BY_ID.equals(name) || OTHER_MESSAGES_HAR.equals(name)) {
        byte[] responseBody;
        try {
            final HarEntries entries = new HarEntries();
            if (OTHER_MESSAGES_HAR_BY_ID.equals(name)) {
                TableHistory tableHistory = Model.getSingleton().getDb().getTableHistory();
                for (Integer id : getIds(params)) {
                    RecordHistory recordHistory = getRecordHistory(tableHistory, id);
                    addHarEntry(entries, recordHistory);
                }
            } else {
                processHttpMessages(this.getParam(params, PARAM_BASE_URL, (String) null), this.getParam(params, PARAM_START, -1), this.getParam(params, PARAM_COUNT, -1), rh -> addHarEntry(entries, rh));
            }
            HarLog harLog = HarUtils.createZapHarLog();
            harLog.setEntries(entries);
            responseBody = HarUtils.harLogToByteArray(harLog);
        } catch (ApiException e) {
            responseBody = e.toString(API.Format.JSON, incErrorDetails()).getBytes(StandardCharsets.UTF_8);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ApiException apiException = new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
            responseBody = apiException.toString(API.Format.JSON, incErrorDetails()).getBytes(StandardCharsets.UTF_8);
        }
        try {
            msg.setResponseHeader(API.getDefaultResponseHeader("application/json; charset=UTF-8", responseBody.length));
        } catch (HttpMalformedHeaderException e) {
            logger.error("Failed to create response header: " + e.getMessage(), e);
        }
        msg.setResponseBody(responseBody);
        return msg;
    } else if (OTHER_SEND_HAR_REQUEST.equals(name)) {
        byte[] responseBody = {};
        HttpMessage request = null;
        try {
            request = HarUtils.createHttpMessage(params.getString(PARAM_REQUEST));
        } catch (IOException e) {
            ApiException apiException = new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_REQUEST, e);
            responseBody = apiException.toString(API.Format.JSON, incErrorDetails()).getBytes(StandardCharsets.UTF_8);
        }
        if (request != null) {
            if (!isValidForCurrentMode(request.getRequestHeader().getURI())) {
                ApiException apiException = new ApiException(ApiException.Type.MODE_VIOLATION);
                responseBody = apiException.toString(API.Format.JSON, incErrorDetails()).getBytes(StandardCharsets.UTF_8);
            } else {
                boolean followRedirects = getParam(params, PARAM_FOLLOW_REDIRECTS, false);
                try {
                    final HarEntries entries = new HarEntries();
                    sendRequest(request, followRedirects, httpMessage -> {
                        HistoryReference hRef = httpMessage.getHistoryRef();
                        entries.addEntry(HarUtils.createHarEntry(hRef.getHistoryId(), hRef.getHistoryType(), httpMessage));
                    });
                    HarLog harLog = HarUtils.createZapHarLog();
                    harLog.setEntries(entries);
                    responseBody = HarUtils.harLogToByteArray(harLog);
                } catch (ApiException e) {
                    responseBody = e.toString(API.Format.JSON, incErrorDetails()).getBytes(StandardCharsets.UTF_8);
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                    ApiException apiException = new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
                    responseBody = apiException.toString(API.Format.JSON, incErrorDetails()).getBytes(StandardCharsets.UTF_8);
                }
            }
        }
        try {
            msg.setResponseHeader(API.getDefaultResponseHeader("application/json; charset=UTF-8", responseBody.length));
        } catch (HttpMalformedHeaderException e) {
            logger.error("Failed to create response header: " + e.getMessage(), e);
        }
        msg.setResponseBody(responseBody);
        return msg;
    } else if (OTHER_SCRIPT_JS.equals(name)) {
        try {
            msg.setResponseBody(API_SCRIPT);
            // Allow caching
            msg.setResponseHeader(API.getDefaultResponseHeader("text/javascript", API_SCRIPT.length(), true));
            msg.getResponseHeader().addHeader(HttpResponseHeader.CACHE_CONTROL, API_SCRIPT_CACHE_CONTROL);
        } catch (HttpMalformedHeaderException e) {
            logger.error("Failed to create response header: " + e.getMessage(), e);
        }
        return msg;
    } else {
        throw new ApiException(ApiException.Type.BAD_OTHER);
    }
}
Also used : Enumeration(java.util.Enumeration) HistoryReference(org.parosproxy.paros.model.HistoryReference) Model(org.parosproxy.paros.model.Model) Date(java.util.Date) OptionsParamCertificate(org.parosproxy.paros.extension.option.OptionsParamCertificate) KeyStoreException(java.security.KeyStoreException) URIException(org.apache.commons.httpclient.URIException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) AlertParam(org.zaproxy.zap.extension.alert.AlertParam) Map(java.util.Map) Constant(org.parosproxy.paros.Constant) HttpMessage(org.parosproxy.paros.network.HttpMessage) Path(java.nio.file.Path) ConnectionParam(org.parosproxy.paros.network.ConnectionParam) PatternSyntaxException(java.util.regex.PatternSyntaxException) StructuralNode(org.zaproxy.zap.model.StructuralNode) HarUtils(org.zaproxy.zap.utils.HarUtils) SSLContextManager(ch.csnc.extension.httpclient.SSLContextManager) HarEntries(edu.umass.cs.benchlab.har.HarEntries) Session(org.parosproxy.paros.model.Session) Set(java.util.Set) KeyManagementException(java.security.KeyManagementException) DomainMatcher(org.zaproxy.zap.network.DomainMatcher) Control(org.parosproxy.paros.control.Control) StandardCharsets(java.nio.charset.StandardCharsets) SiteNode(org.parosproxy.paros.model.SiteNode) RecordHistory(org.parosproxy.paros.db.RecordHistory) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JSONObject(net.sf.json.JSONObject) Pattern(java.util.regex.Pattern) URI(org.apache.commons.httpclient.URI) SessionUtils(org.zaproxy.zap.model.SessionUtils) Mode(org.parosproxy.paros.control.Control.Mode) HttpRequestHeader(org.parosproxy.paros.network.HttpRequestHeader) TreeNode(javax.swing.tree.TreeNode) ApiUtils(org.zaproxy.zap.utils.ApiUtils) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) HttpSender(org.parosproxy.paros.network.HttpSender) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HttpRequestConfig(org.zaproxy.zap.network.HttpRequestConfig) TableHistory(org.parosproxy.paros.db.TableHistory) ExtensionAlert(org.zaproxy.zap.extension.alert.ExtensionAlert) HttpHeader(org.parosproxy.paros.network.HttpHeader) HttpResponseHeader(org.parosproxy.paros.network.HttpResponseHeader) HttpRedirectionValidator(org.zaproxy.zap.network.HttpRedirectionValidator) JSONException(net.sf.json.JSONException) EventQueue(java.awt.EventQueue) Iterator(java.util.Iterator) Files(java.nio.file.Files) ExtensionHistory(org.parosproxy.paros.extension.history.ExtensionHistory) SessionStructure(org.zaproxy.zap.model.SessionStructure) SessionListener(org.parosproxy.paros.model.SessionListener) IOException(java.io.IOException) DatabaseException(org.parosproxy.paros.db.DatabaseException) CertificateException(java.security.cert.CertificateException) File(java.io.File) SiteMap(org.parosproxy.paros.model.SiteMap) HarLog(edu.umass.cs.benchlab.har.HarLog) Paths(java.nio.file.Paths) AlertAPI(org.zaproxy.zap.extension.alert.AlertAPI) LogManager(org.apache.logging.log4j.LogManager) View(org.parosproxy.paros.view.View) HarEntries(edu.umass.cs.benchlab.har.HarEntries) HarLog(edu.umass.cs.benchlab.har.HarLog) JSONException(net.sf.json.JSONException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) URIException(org.apache.commons.httpclient.URIException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) PatternSyntaxException(java.util.regex.PatternSyntaxException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JSONException(net.sf.json.JSONException) IOException(java.io.IOException) DatabaseException(org.parosproxy.paros.db.DatabaseException) CertificateException(java.security.cert.CertificateException) HistoryReference(org.parosproxy.paros.model.HistoryReference) JSONObject(net.sf.json.JSONObject) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) TableHistory(org.parosproxy.paros.db.TableHistory) HttpMessage(org.parosproxy.paros.network.HttpMessage) RecordHistory(org.parosproxy.paros.db.RecordHistory)

Example 2 with HarEntries

use of edu.umass.cs.benchlab.har.HarEntries in project zaproxy by zaproxy.

the class SearchAPI method handleApiOther.

@Override
public HttpMessage handleApiOther(HttpMessage msg, String name, JSONObject params) throws ApiException {
    byte[] responseBody = {};
    ExtensionSearch.Type searchType;
    switch(name) {
        case OTHER_HAR_BY_URL_REGEX:
            searchType = ExtensionSearch.Type.URL;
            break;
        case OTHER_HAR_BY_REQUEST_REGEX:
            searchType = ExtensionSearch.Type.Request;
            break;
        case OTHER_HAR_BY_RESPONSE_REGEX:
            searchType = ExtensionSearch.Type.Response;
            break;
        case OTHER_HAR_BY_HEADER_REGEX:
            searchType = ExtensionSearch.Type.Header;
            break;
        default:
            throw new ApiException(ApiException.Type.BAD_OTHER);
    }
    validateRegex(params);
    try {
        final HarEntries entries = new HarEntries();
        search(params, searchType, rh -> {
            HarEntry entry = HarUtils.createHarEntry(rh.getHistoryId(), rh.getHistoryType(), rh.getHttpMessage());
            entries.addEntry(entry);
        });
        HarLog harLog = HarUtils.createZapHarLog();
        harLog.setEntries(entries);
        responseBody = HarUtils.harLogToByteArray(harLog);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        ApiException apiException = new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
        responseBody = apiException.toString(API.Format.JSON, incErrorDetails()).getBytes(StandardCharsets.UTF_8);
    }
    try {
        msg.setResponseHeader(API.getDefaultResponseHeader("application/json; charset=UTF-8", responseBody.length));
    } catch (HttpMalformedHeaderException e) {
        log.error("Failed to create response header: " + e.getMessage(), e);
    }
    msg.setResponseBody(responseBody);
    return msg;
}
Also used : HarEntry(edu.umass.cs.benchlab.har.HarEntry) HarEntries(edu.umass.cs.benchlab.har.HarEntries) HarLog(edu.umass.cs.benchlab.har.HarLog) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) PatternSyntaxException(java.util.regex.PatternSyntaxException) ApiException(org.zaproxy.zap.extension.api.ApiException) DatabaseException(org.parosproxy.paros.db.DatabaseException) ApiException(org.zaproxy.zap.extension.api.ApiException)

Aggregations

HarEntries (edu.umass.cs.benchlab.har.HarEntries)2 HarLog (edu.umass.cs.benchlab.har.HarLog)2 PatternSyntaxException (java.util.regex.PatternSyntaxException)2 DatabaseException (org.parosproxy.paros.db.DatabaseException)2 SSLContextManager (ch.csnc.extension.httpclient.SSLContextManager)1 HarEntry (edu.umass.cs.benchlab.har.HarEntry)1 EventQueue (java.awt.EventQueue)1 File (java.io.File)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateException (java.security.cert.CertificateException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1