Search in sources :

Example 1 with RecordHistory

use of org.parosproxy.paros.db.RecordHistory 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)) {
        final 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)) {
        ExtensionDynSSL extDynSSL = (ExtensionDynSSL) Control.getSingleton().getExtensionLoader().getExtension(ExtensionDynSSL.EXTENSION_ID);
        if (extDynSSL != null) {
            try {
                Certificate rootCA = extDynSSL.getRootCA();
                if (rootCA == null) {
                    throw new ApiException(ApiException.Type.DOES_NOT_EXIST);
                }
                final StringWriter sw = new StringWriter();
                try (final PemWriter pw = new PemWriter(sw)) {
                    pw.writeObject(new JcaMiscPEMGenerator(rootCA));
                    pw.flush();
                }
                String response = sw.toString();
                msg.setResponseHeader(API.getDefaultResponseHeader("application/pkix-cert;", response.length()));
                msg.setResponseBody(response);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new ApiException(ApiException.Type.INTERNAL_ERROR);
            }
        } else {
            throw new ApiException(ApiException.Type.DOES_NOT_EXIST);
        }
        return msg;
    } else if (OTHER_XML_REPORT.equals(name)) {
        try {
            writeReportLastScanTo(msg, ScanReportType.XML);
            return msg;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ApiException(ApiException.Type.INTERNAL_ERROR);
        }
    } else if (OTHER_HTML_REPORT.equals(name)) {
        try {
            writeReportLastScanTo(msg, ScanReportType.HTML);
            return msg;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ApiException(ApiException.Type.INTERNAL_ERROR);
        }
    } else if (OTHER_MD_REPORT.equals(name)) {
        try {
            writeReportLastScanTo(msg, ScanReportType.MD);
            return msg;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ApiException(ApiException.Type.INTERNAL_ERROR);
        }
    } else if (OTHER_MESSAGE_HAR.equals(name)) {
        byte[] responseBody;
        try {
            final HarEntries entries = new HarEntries();
            TableHistory tableHistory = Model.getSingleton().getDb().getTableHistory();
            RecordHistory recordHistory;
            try {
                recordHistory = tableHistory.read(this.getParam(params, PARAM_ID, -1));
            } catch (HttpMalformedHeaderException | DatabaseException e) {
                throw new ApiException(ApiException.Type.INTERNAL_ERROR);
            }
            if (recordHistory == null || recordHistory.getHistoryType() == HistoryReference.TYPE_TEMPORARY) {
                throw new ApiException(ApiException.Type.DOES_NOT_EXIST);
            }
            entries.addEntry(HarUtils.createHarEntry(recordHistory.getHttpMessage()));
            HarLog harLog = HarUtils.createZapHarLog();
            harLog.setEntries(entries);
            responseBody = HarUtils.harLogToByteArray(harLog);
        } 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.equals(name)) {
        byte[] responseBody;
        try {
            final HarEntries entries = new HarEntries();
            processHttpMessages(this.getParam(params, PARAM_BASE_URL, (String) null), this.getParam(params, PARAM_START, -1), this.getParam(params, PARAM_COUNT, -1), new Processor<RecordHistory>() {

                @Override
                public void process(RecordHistory recordHistory) {
                    entries.addEntry(HarUtils.createHarEntry(recordHistory.getHttpMessage()));
                }
            });
            HarLog harLog = HarUtils.createZapHarLog();
            harLog.setEntries(entries);
            responseBody = HarUtils.harLogToByteArray(harLog);
        } 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, new Processor<HttpMessage>() {

                        @Override
                        public void process(HttpMessage msg) {
                            entries.addEntry(HarUtils.createHarEntry(msg));
                        }
                    });
                    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 : ExtensionDynSSL(org.zaproxy.zap.extension.dynssl.ExtensionDynSSL) JcaMiscPEMGenerator(org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator) StringWriter(java.io.StringWriter) ProxyParam(org.parosproxy.paros.core.proxy.ProxyParam) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) RecordHistory(org.parosproxy.paros.db.RecordHistory) HarEntries(edu.umass.cs.benchlab.har.HarEntries) HarLog(edu.umass.cs.benchlab.har.HarLog) PemWriter(org.bouncycastle.util.io.pem.PemWriter) JSONException(net.sf.json.JSONException) IOException(java.io.IOException) URIException(org.apache.commons.httpclient.URIException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) PatternSyntaxException(java.util.regex.PatternSyntaxException) JSONException(net.sf.json.JSONException) IOException(java.io.IOException) DatabaseException(org.parosproxy.paros.db.DatabaseException) JSONObject(net.sf.json.JSONObject) TableHistory(org.parosproxy.paros.db.TableHistory) HttpMessage(org.parosproxy.paros.network.HttpMessage) DatabaseException(org.parosproxy.paros.db.DatabaseException) Certificate(java.security.cert.Certificate)

Example 2 with RecordHistory

use of org.parosproxy.paros.db.RecordHistory in project zaproxy by zaproxy.

the class HistoryReference method getHttpMessage.

/**
	 * Gets the corresponding http message from the database. Try to minimise calls to this method as much as possible.
	 * But also dont cache the HttpMessage either as this can significantly increase ZAP's memory usage.
	 * 
	 * @return the http message
	 * @throws HttpMalformedHeaderException the http malformed header exception
	 * @throws SQLException the sQL exception
	 */
public HttpMessage getHttpMessage() throws HttpMalformedHeaderException, DatabaseException {
    // fetch complete message
    RecordHistory history = staticTableHistory.read(historyId);
    if (history == null) {
        throw new HttpMalformedHeaderException("No history reference for id " + historyId + " type=" + historyType);
    }
    // ZAP: Init HttpMessage HistoryReference field
    history.getHttpMessage().setHistoryRef(this);
    return history.getHttpMessage();
}
Also used : HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) RecordHistory(org.parosproxy.paros.db.RecordHistory)

Example 3 with RecordHistory

use of org.parosproxy.paros.db.RecordHistory in project zaproxy by zaproxy.

the class ProxyThread method isProcessCache.

protected boolean isProcessCache(HttpMessage msg) throws IOException {
    if (!parentServer.isEnableCacheProcessing()) {
        return false;
    }
    if (parentServer.getCacheProcessingList().isEmpty()) {
        return false;
    }
    CacheProcessingItem item = parentServer.getCacheProcessingList().get(0);
    if (msg.equals(item.message)) {
        HttpMessage newMsg = item.message.cloneAll();
        msg.setResponseHeader(newMsg.getResponseHeader());
        msg.setResponseBody(newMsg.getResponseBody());
        writeHttpResponse(msg, httpOut);
        return true;
    } else {
        try {
            RecordHistory history = Model.getSingleton().getDb().getTableHistory().getHistoryCache(item.reference, msg);
            if (history == null) {
                return false;
            }
            msg.setResponseHeader(history.getHttpMessage().getResponseHeader());
            msg.setResponseBody(history.getHttpMessage().getResponseBody());
            writeHttpResponse(msg, httpOut);
            return true;
        } catch (Exception e) {
            return true;
        }
    }
//        return false;
}
Also used : HttpMessage(org.parosproxy.paros.network.HttpMessage) RecordHistory(org.parosproxy.paros.db.RecordHistory) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) HttpException(org.apache.commons.httpclient.HttpException) MissingRootCertificateException(org.parosproxy.paros.security.MissingRootCertificateException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException)

Example 4 with RecordHistory

use of org.parosproxy.paros.db.RecordHistory in project zaproxy by zaproxy.

the class ParosTableHistory method read.

@Override
public synchronized RecordHistory read(int historyId) throws HttpMalformedHeaderException, DatabaseException {
    try {
        psRead.setInt(1, historyId);
        psRead.execute();
        RecordHistory result = null;
        try (ResultSet rs = psRead.getResultSet()) {
            result = build(rs);
        }
        return result;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) RecordHistory(org.parosproxy.paros.db.RecordHistory) DatabaseException(org.parosproxy.paros.db.DatabaseException)

Example 5 with RecordHistory

use of org.parosproxy.paros.db.RecordHistory in project zaproxy by zaproxy.

the class SpiderAPI method handleApiView.

@Override
public ApiResponse handleApiView(String name, JSONObject params) throws ApiException {
    ApiResponse result;
    if (VIEW_STATUS.equals(name)) {
        SpiderScan scan = (SpiderScan) this.getSpiderScan(params);
        int progress = 0;
        if (scan != null) {
            if (scan.isStopped()) {
                progress = 100;
            } else {
                progress = scan.getProgress();
            }
        }
        result = new ApiResponseElement(name, Integer.toString(progress));
    } else if (VIEW_RESULTS.equals(name)) {
        result = new ApiResponseList(name);
        SpiderScan scan = (SpiderScan) this.getSpiderScan(params);
        if (scan != null) {
            synchronized (scan.getResults()) {
                for (String s : scan.getResults()) {
                    ((ApiResponseList) result).addItem(new ApiResponseElement("url", s));
                }
            }
        }
    } else if (VIEW_FULL_RESULTS.equals(name)) {
        ApiResponseList resultUrls = new ApiResponseList(name);
        SpiderScan scan = (SpiderScan) this.getSpiderScan(params);
        ApiResponseList resultList = new ApiResponseList("urlsInScope");
        synchronized (scan.getResourcesFound()) {
            for (SpiderResource sr : scan.getResourcesFound()) {
                Map<String, String> map = new HashMap<>();
                map.put("messageId", Integer.toString(sr.getHistoryId()));
                map.put("method", sr.getMethod());
                map.put("url", sr.getUri());
                map.put("statusCode", Integer.toString(sr.getStatusCode()));
                map.put("statusReason", sr.getStatusReason());
                resultList.addItem(new ApiResponseSet<String>("resource", map));
            }
        }
        resultUrls.addItem(resultList);
        resultList = new ApiResponseList("urlsOutOfScope");
        synchronized (scan.getResultsOutOfScope()) {
            for (String url : scan.getResultsOutOfScope()) {
                resultList.addItem(new ApiResponseElement("url", url));
            }
        }
        resultUrls.addItem(resultList);
        result = resultUrls;
    } else if (VIEW_EXCLUDED_FROM_SCAN.equals(name)) {
        result = new ApiResponseList(name);
        Session session = Model.getSingleton().getSession();
        List<String> regexs = session.getExcludeFromSpiderRegexs();
        for (String regex : regexs) {
            ((ApiResponseList) result).addItem(new ApiResponseElement("regex", regex));
        }
    } else if (VIEW_SCANS.equals(name)) {
        ApiResponseList resultList = new ApiResponseList(name);
        for (GenericScanner2 scan : extension.getAllScans()) {
            SpiderScan spiderScan = (SpiderScan) scan;
            Map<String, String> map = new HashMap<>();
            map.put("id", Integer.toString(spiderScan.getScanId()));
            map.put("progress", Integer.toString(spiderScan.getProgress()));
            map.put("state", spiderScan.getState());
            resultList.addItem(new ApiResponseSet<String>("scan", map));
        }
        result = resultList;
    } else if (VIEW_ALL_URLS.equals(name)) {
        ApiResponseList resultUrls = new ApiResponseList(name);
        Set<String> urlSet = new HashSet<String>();
        TableHistory tableHistory = extension.getModel().getDb().getTableHistory();
        List<Integer> ids = Collections.emptyList();
        try {
            ids = tableHistory.getHistoryIdsOfHistType(extension.getModel().getSession().getSessionId(), HistoryReference.TYPE_SPIDER, HistoryReference.TYPE_SPIDER_TASK);
        } catch (DatabaseException e) {
            throw new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
        }
        String url;
        for (Integer id : ids) {
            try {
                RecordHistory rh = tableHistory.read(id.intValue());
                if (rh != null) {
                    url = rh.getHttpMessage().getRequestHeader().getURI().toString();
                    if (urlSet.add(url)) {
                        resultUrls.addItem(new ApiResponseElement("url", url));
                    }
                }
            } catch (HttpMalformedHeaderException | DatabaseException e) {
                throw new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
            }
        }
        result = resultUrls;
    } else if (VIEW_DOMAINS_ALWAYS_IN_SCOPE.equals(name) || VIEW_OPTION_DOMAINS_ALWAYS_IN_SCOPE.equals(name)) {
        result = domainMatchersToApiResponseList(name, extension.getSpiderParam().getDomainsAlwaysInScope(), false);
    } else if (VIEW_OPTION_DOMAINS_ALWAYS_IN_SCOPE_ENABLED.equals(name)) {
        result = domainMatchersToApiResponseList(name, extension.getSpiderParam().getDomainsAlwaysInScope(), true);
    } else {
        throw new ApiException(ApiException.Type.BAD_VIEW);
    }
    return result;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ApiResponseSet(org.zaproxy.zap.extension.api.ApiResponseSet) HashMap(java.util.HashMap) ApiResponse(org.zaproxy.zap.extension.api.ApiResponse) ApiResponseElement(org.zaproxy.zap.extension.api.ApiResponseElement) ApiResponseList(org.zaproxy.zap.extension.api.ApiResponseList) GenericScanner2(org.zaproxy.zap.model.GenericScanner2) ApiResponseList(org.zaproxy.zap.extension.api.ApiResponseList) ArrayList(java.util.ArrayList) List(java.util.List) TableHistory(org.parosproxy.paros.db.TableHistory) DatabaseException(org.parosproxy.paros.db.DatabaseException) RecordHistory(org.parosproxy.paros.db.RecordHistory) Session(org.parosproxy.paros.model.Session) ApiException(org.zaproxy.zap.extension.api.ApiException)

Aggregations

RecordHistory (org.parosproxy.paros.db.RecordHistory)15 DatabaseException (org.parosproxy.paros.db.DatabaseException)11 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)9 SQLException (java.sql.SQLException)5 ResultSet (java.sql.ResultSet)4 TableHistory (org.parosproxy.paros.db.TableHistory)4 HttpMessage (org.parosproxy.paros.network.HttpMessage)4 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 ApiException (org.zaproxy.zap.extension.api.ApiException)3 HarEntries (edu.umass.cs.benchlab.har.HarEntries)2 HarLog (edu.umass.cs.benchlab.har.HarLog)2 IOException (java.io.IOException)2 PreparedStatement (java.sql.PreparedStatement)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Session (org.parosproxy.paros.model.Session)2 ApiResponseList (org.zaproxy.zap.extension.api.ApiResponseList)2 ApiResponseSet (org.zaproxy.zap.extension.api.ApiResponseSet)2 StringWriter (java.io.StringWriter)1