Search in sources :

Example 1 with RecordAlert

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

the class CoreAPI method processAlerts.

private void processAlerts(String baseUrl, int start, int count, Processor<Alert> processor) throws ApiException {
    List<Alert> alerts = new ArrayList<>();
    try {
        TableAlert tableAlert = Model.getSingleton().getDb().getTableAlert();
        // TODO this doesnt work, but should be used when its fixed :/
        //Vector<Integer> v = tableAlert.getAlertListBySession(Model.getSingleton().getSession().getSessionId());
        Vector<Integer> v = tableAlert.getAlertList();
        PaginationConstraintsChecker pcc = new PaginationConstraintsChecker(start, count);
        for (int i = 0; i < v.size(); i++) {
            int alertId = v.get(i).intValue();
            RecordAlert recAlert = tableAlert.read(alertId);
            Alert alert = new Alert(recAlert);
            if (alert.getConfidence() != Alert.CONFIDENCE_FALSE_POSITIVE && !alerts.contains(alert)) {
                if (baseUrl != null && !alert.getUri().startsWith(baseUrl)) {
                    // Not subordinate to the specified URL
                    continue;
                }
                pcc.recordProcessed();
                alerts.add(alert);
                if (!pcc.hasPageStarted()) {
                    continue;
                }
                processor.process(alert);
                if (pcc.hasPageEnded()) {
                    break;
                }
            }
        }
    } catch (DatabaseException e) {
        logger.error(e.getMessage(), e);
        throw new ApiException(ApiException.Type.INTERNAL_ERROR);
    }
}
Also used : TableAlert(org.parosproxy.paros.db.TableAlert) ArrayList(java.util.ArrayList) Alert(org.parosproxy.paros.core.scanner.Alert) RecordAlert(org.parosproxy.paros.db.RecordAlert) ExtensionAlert(org.zaproxy.zap.extension.alert.ExtensionAlert) TableAlert(org.parosproxy.paros.db.TableAlert) DatabaseException(org.parosproxy.paros.db.DatabaseException) RecordAlert(org.parosproxy.paros.db.RecordAlert)

Example 2 with RecordAlert

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

the class ExtensionAlert method getAllAlerts.

public List<Alert> getAllAlerts() {
    List<Alert> allAlerts = new ArrayList<>();
    TableAlert tableAlert = getModel().getDb().getTableAlert();
    Vector<Integer> v;
    try {
        // TODO this doesnt work, but should be used when its fixed :/
        //v = tableAlert.getAlertListBySession(Model.getSingleton().getSession().getSessionId());
        v = tableAlert.getAlertList();
        for (int i = 0; i < v.size(); i++) {
            int alertId = v.get(i).intValue();
            RecordAlert recAlert = tableAlert.read(alertId);
            Alert alert = new Alert(recAlert);
            if (alert.getHistoryRef() != null) {
                // Only use the alert if it has a history reference.
                if (!allAlerts.contains(alert)) {
                    allAlerts.add(alert);
                }
            }
        }
    } catch (DatabaseException e) {
        logger.error(e.getMessage(), e);
    }
    return allAlerts;
}
Also used : TableAlert(org.parosproxy.paros.db.TableAlert) ArrayList(java.util.ArrayList) Alert(org.parosproxy.paros.core.scanner.Alert) RecordAlert(org.parosproxy.paros.db.RecordAlert) TableAlert(org.parosproxy.paros.db.TableAlert) DatabaseException(org.parosproxy.paros.db.DatabaseException) RecordAlert(org.parosproxy.paros.db.RecordAlert)

Example 3 with RecordAlert

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

the class ParosTableAlert method getAlertsBySourceHistoryId.

/* (non-Javadoc)
	 * @see org.parosproxy.paros.db.paros.TableAlert#getAlertsBySourceHistoryId(int)
	 */
@Override
public synchronized List<RecordAlert> getAlertsBySourceHistoryId(int historyId) throws DatabaseException {
    try {
        List<RecordAlert> result = new ArrayList<>();
        psGetAlertsForHistoryId.setLong(1, historyId);
        try (ResultSet rs = psGetAlertsForHistoryId.executeQuery()) {
            RecordAlert ra = build(rs);
            while (ra != null) {
                result.add(ra);
                ra = build(rs);
            }
        }
        return result;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) DatabaseException(org.parosproxy.paros.db.DatabaseException) RecordAlert(org.parosproxy.paros.db.RecordAlert)

Example 4 with RecordAlert

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

the class CoreAPI method handleApiView.

@Override
public ApiResponse handleApiView(String name, JSONObject params) throws ApiException {
    ApiResponse result = null;
    Session session = Model.getSingleton().getSession();
    if (VIEW_HOSTS.equals(name)) {
        result = new ApiResponseList(name);
        SiteNode root = (SiteNode) session.getSiteTree().getRoot();
        @SuppressWarnings("unchecked") Enumeration<SiteNode> en = root.children();
        while (en.hasMoreElements()) {
            String site = en.nextElement().getNodeName();
            if (site.indexOf("//") >= 0) {
                site = site.substring(site.indexOf("//") + 2);
            }
            if (site.indexOf(":") >= 0) {
                site = site.substring(0, site.indexOf(":"));
            }
            ((ApiResponseList) result).addItem(new ApiResponseElement("host", site));
        }
    } else if (VIEW_SITES.equals(name)) {
        result = new ApiResponseList(name);
        SiteNode root = (SiteNode) session.getSiteTree().getRoot();
        @SuppressWarnings("unchecked") Enumeration<SiteNode> en = root.children();
        while (en.hasMoreElements()) {
            ((ApiResponseList) result).addItem(new ApiResponseElement("site", en.nextElement().getNodeName()));
        }
    } else if (VIEW_URLS.equals(name)) {
        result = new ApiResponseList(name);
        SiteNode root = (SiteNode) session.getSiteTree().getRoot();
        this.getURLs(root, (ApiResponseList) result);
    } else if (VIEW_ALERT.equals(name)) {
        TableAlert tableAlert = Model.getSingleton().getDb().getTableAlert();
        RecordAlert recordAlert;
        try {
            recordAlert = tableAlert.read(this.getParam(params, PARAM_ID, -1));
        } catch (DatabaseException e) {
            throw new ApiException(ApiException.Type.INTERNAL_ERROR);
        }
        if (recordAlert == null) {
            throw new ApiException(ApiException.Type.DOES_NOT_EXIST);
        }
        result = new ApiResponseElement(alertToSet(new Alert(recordAlert)));
    } else if (VIEW_ALERTS.equals(name)) {
        final ApiResponseList resultList = new ApiResponseList(name);
        processAlerts(this.getParam(params, PARAM_BASE_URL, (String) null), this.getParam(params, PARAM_START, -1), this.getParam(params, PARAM_COUNT, -1), new Processor<Alert>() {

            @Override
            public void process(Alert alert) {
                resultList.addItem(alertToSet(alert));
            }
        });
        result = resultList;
    } else if (VIEW_NUMBER_OF_ALERTS.equals(name)) {
        CounterProcessor<Alert> counter = new CounterProcessor<>();
        processAlerts(this.getParam(params, PARAM_BASE_URL, (String) null), this.getParam(params, PARAM_START, -1), this.getParam(params, PARAM_COUNT, -1), counter);
        result = new ApiResponseElement(name, Integer.toString(counter.getCount()));
    } else if (VIEW_MESSAGE.equals(name)) {
        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);
        }
        result = new ApiResponseElement(ApiResponseConversionUtils.httpMessageToSet(recordHistory.getHistoryId(), recordHistory.getHistoryType(), recordHistory.getHttpMessage()));
    } else if (VIEW_MESSAGES.equals(name)) {
        final ApiResponseList resultList = new ApiResponseList(name);
        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) {
                resultList.addItem(ApiResponseConversionUtils.httpMessageToSet(recordHistory.getHistoryId(), recordHistory.getHistoryType(), recordHistory.getHttpMessage()));
            }
        });
        result = resultList;
    } else if (VIEW_NUMBER_OF_MESSAGES.equals(name)) {
        CounterProcessor<RecordHistory> counter = new CounterProcessor<>();
        processHttpMessages(this.getParam(params, PARAM_BASE_URL, (String) null), this.getParam(params, PARAM_START, -1), this.getParam(params, PARAM_COUNT, -1), counter);
        result = new ApiResponseElement(name, Integer.toString(counter.getCount()));
    } else if (VIEW_MODE.equals(name)) {
        result = new ApiResponseElement(name, Control.getSingleton().getMode().name());
    } else if (VIEW_VERSION.equals(name)) {
        result = new ApiResponseElement(name, Constant.PROGRAM_VERSION);
    } else if (VIEW_EXCLUDED_FROM_PROXY.equals(name)) {
        result = new ApiResponseList(name);
        List<String> regexs = session.getExcludeFromProxyRegexs();
        for (String regex : regexs) {
            ((ApiResponseList) result).addItem(new ApiResponseElement("regex", regex));
        }
    } else if (VIEW_HOME_DIRECTORY.equals(name)) {
        result = new ApiResponseElement(name, Model.getSingleton().getOptionsParam().getUserDirectory().getAbsolutePath());
    } else if (VIEW_SESSION_LOCATION.equals(name)) {
        result = new ApiResponseElement(name, session.getFileName());
    } else if (VIEW_PROXY_CHAIN_EXCLUDED_DOMAINS.equals(name) || VIEW_OPTION_PROXY_EXCLUDED_DOMAINS.equals(name) || VIEW_OPTION_PROXY_CHAIN_SKIP_NAME.equals(name)) {
        result = proxyChainExcludedDomainsToApiResponseList(name, Model.getSingleton().getOptionsParam().getConnectionParam().getProxyExcludedDomains(), false);
    } else if (VIEW_OPTION_PROXY_EXCLUDED_DOMAINS_ENABLED.equals(name)) {
        result = proxyChainExcludedDomainsToApiResponseList(name, Model.getSingleton().getOptionsParam().getConnectionParam().getProxyExcludedDomains(), true);
    } else {
        throw new ApiException(ApiException.Type.BAD_VIEW);
    }
    return result;
}
Also used : HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) List(java.util.List) ArrayList(java.util.ArrayList) RecordHistory(org.parosproxy.paros.db.RecordHistory) SiteNode(org.parosproxy.paros.model.SiteNode) Enumeration(java.util.Enumeration) RecordAlert(org.parosproxy.paros.db.RecordAlert) TableAlert(org.parosproxy.paros.db.TableAlert) Alert(org.parosproxy.paros.core.scanner.Alert) RecordAlert(org.parosproxy.paros.db.RecordAlert) ExtensionAlert(org.zaproxy.zap.extension.alert.ExtensionAlert) TableAlert(org.parosproxy.paros.db.TableAlert) TableHistory(org.parosproxy.paros.db.TableHistory) DatabaseException(org.parosproxy.paros.db.DatabaseException) Session(org.parosproxy.paros.model.Session)

Example 5 with RecordAlert

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

the class ExtensionAlert method refreshAlert.

private void refreshAlert(Session session) throws DatabaseException {
    if (Constant.isLowMemoryOptionSet()) {
        return;
    }
    SiteMap siteTree = this.getModel().getSession().getSiteTree();
    TableAlert tableAlert = getModel().getDb().getTableAlert();
    // TODO this doesnt work, but should be used when its fixed :/
    //Vector<Integer> v = tableAlert.getAlertListBySession(Model.getSingleton().getSession().getSessionId());
    Vector<Integer> v = tableAlert.getAlertList();
    final ExtensionHistory extensionHistory = (ExtensionHistory) Control.getSingleton().getExtensionLoader().getExtension(ExtensionHistory.NAME);
    for (int i = 0; i < v.size(); i++) {
        int alertId = v.get(i).intValue();
        RecordAlert recAlert = tableAlert.read(alertId);
        int historyId = recAlert.getHistoryId();
        HistoryReference historyReference = null;
        if (extensionHistory != null) {
            historyReference = extensionHistory.getHistoryReference(historyId);
        }
        if (historyReference == null) {
            historyReference = this.hrefs.get(Integer.valueOf(historyId));
        }
        Alert alert;
        if (historyReference != null) {
            alert = new Alert(recAlert, historyReference);
        } else {
            alert = new Alert(recAlert);
        }
        historyReference = alert.getHistoryRef();
        if (historyReference != null) {
            // The ref can be null if hrefs are purged
            addAlertToTree(alert);
            Integer key = Integer.valueOf(historyId);
            if (!hrefs.containsKey(key)) {
                this.hrefs.put(key, alert.getHistoryRef());
            }
        }
    }
    siteTree.nodeStructureChanged((SiteNode) siteTree.getRoot());
}
Also used : HistoryReference(org.parosproxy.paros.model.HistoryReference) TableAlert(org.parosproxy.paros.db.TableAlert) SiteMap(org.parosproxy.paros.model.SiteMap) ExtensionHistory(org.parosproxy.paros.extension.history.ExtensionHistory) Alert(org.parosproxy.paros.core.scanner.Alert) RecordAlert(org.parosproxy.paros.db.RecordAlert) TableAlert(org.parosproxy.paros.db.TableAlert) RecordAlert(org.parosproxy.paros.db.RecordAlert)

Aggregations

RecordAlert (org.parosproxy.paros.db.RecordAlert)8 DatabaseException (org.parosproxy.paros.db.DatabaseException)6 ArrayList (java.util.ArrayList)5 TableAlert (org.parosproxy.paros.db.TableAlert)5 Alert (org.parosproxy.paros.core.scanner.Alert)4 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 ExtensionAlert (org.zaproxy.zap.extension.alert.ExtensionAlert)2 Enumeration (java.util.Enumeration)1 List (java.util.List)1 RecordHistory (org.parosproxy.paros.db.RecordHistory)1 TableHistory (org.parosproxy.paros.db.TableHistory)1 ExtensionHistory (org.parosproxy.paros.extension.history.ExtensionHistory)1 HistoryReference (org.parosproxy.paros.model.HistoryReference)1 Session (org.parosproxy.paros.model.Session)1 SiteMap (org.parosproxy.paros.model.SiteMap)1 SiteNode (org.parosproxy.paros.model.SiteNode)1 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)1