Search in sources :

Example 1 with TableAlertTag

use of org.zaproxy.zap.db.TableAlertTag in project zaproxy by zaproxy.

the class AlertAPI method processAlerts.

private void processAlerts(String baseUrl, int start, int count, int riskId, Processor<Alert> processor) throws ApiException {
    List<Alert> alerts = new ArrayList<>();
    try {
        TableAlert tableAlert = Model.getSingleton().getDb().getTableAlert();
        TableAlertTag tableAlertTag = Model.getSingleton().getDb().getTableAlertTag();
        // TODO this doesn't 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 alertId : v) {
            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;
                }
                if (riskId != NO_RISK_ID && alert.getRisk() != riskId) {
                    continue;
                }
                pcc.recordProcessed();
                alerts.add(alert);
                if (!pcc.hasPageStarted()) {
                    continue;
                }
                alert.setTags(tableAlertTag.getTagsByAlertId(alertId));
                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) TableAlertTag(org.zaproxy.zap.db.TableAlertTag) 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) ApiException(org.zaproxy.zap.extension.api.ApiException)

Example 2 with TableAlertTag

use of org.zaproxy.zap.db.TableAlertTag in project zaproxy by zaproxy.

the class ExtensionAlert method getAllAlerts.

public List<Alert> getAllAlerts() {
    List<Alert> allAlerts = new ArrayList<>();
    TableAlert tableAlert = getModel().getDb().getTableAlert();
    TableAlertTag tableAlertTag = getModel().getDb().getTableAlertTag();
    Vector<Integer> v;
    try {
        // TODO this doesn't 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);
            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)) {
                    alert.setTags(tableAlertTag.getTagsByAlertId(alertId));
                    allAlerts.add(alert);
                }
            }
        }
    } catch (DatabaseException e) {
        logger.error(e.getMessage(), e);
    }
    return allAlerts;
}
Also used : TableAlert(org.parosproxy.paros.db.TableAlert) TableAlertTag(org.zaproxy.zap.db.TableAlertTag) 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 TableAlertTag

use of org.zaproxy.zap.db.TableAlertTag in project zaproxy by zaproxy.

the class ExtensionAlert method writeAlertToDB.

private void writeAlertToDB(Alert alert, HistoryReference ref) throws HttpMalformedHeaderException, DatabaseException {
    TableAlert tableAlert = getModel().getDb().getTableAlert();
    int scanId = 0;
    if (recordScan != null) {
        scanId = recordScan.getScanId();
    }
    RecordAlert recordAlert = tableAlert.write(scanId, alert.getPluginId(), alert.getName(), alert.getRisk(), alert.getConfidence(), alert.getDescription(), alert.getUri(), alert.getParam(), alert.getAttack(), alert.getOtherInfo(), alert.getSolution(), alert.getReference(), alert.getEvidence(), alert.getCweId(), alert.getWascId(), ref.getHistoryId(), alert.getSourceHistoryId(), alert.getSource().getId(), alert.getAlertRef());
    int alertId = recordAlert.getAlertId();
    alert.setAlertId(alertId);
    TableAlertTag tableAlertTag = getModel().getDb().getTableAlertTag();
    for (Map.Entry<String, String> e : alert.getTags().entrySet()) {
        tableAlertTag.insertOrUpdate(alertId, e.getKey(), e.getValue());
    }
}
Also used : TableAlert(org.parosproxy.paros.db.TableAlert) TableAlertTag(org.zaproxy.zap.db.TableAlertTag) Map(java.util.Map) HashMap(java.util.HashMap) SiteMap(org.parosproxy.paros.model.SiteMap) RecordAlert(org.parosproxy.paros.db.RecordAlert)

Example 4 with TableAlertTag

use of org.zaproxy.zap.db.TableAlertTag in project zaproxy by zaproxy.

the class ExtensionAlert method updateAlertInDB.

private void updateAlertInDB(Alert alert) throws HttpMalformedHeaderException, DatabaseException {
    TableAlert tableAlert = getModel().getDb().getTableAlert();
    tableAlert.update(alert.getAlertId(), alert.getName(), alert.getRisk(), alert.getConfidence(), alert.getDescription(), alert.getUri(), alert.getParam(), alert.getAttack(), alert.getOtherInfo(), alert.getSolution(), alert.getReference(), alert.getEvidence(), alert.getCweId(), alert.getWascId(), alert.getSourceHistoryId());
    int alertId = alert.getAlertId();
    TableAlertTag tableAlertTag = getModel().getDb().getTableAlertTag();
    Map<String, String> existingTags = tableAlertTag.getTagsByAlertId(alertId);
    Map<String, String> newTags = alert.getTags();
    for (Map.Entry<String, String> e : existingTags.entrySet()) {
        if (!newTags.containsKey(e.getKey())) {
            tableAlertTag.delete(alertId, e.getKey());
        }
    }
    for (Map.Entry<String, String> e : newTags.entrySet()) {
        tableAlertTag.insertOrUpdate(alertId, e.getKey(), e.getValue());
    }
}
Also used : TableAlert(org.parosproxy.paros.db.TableAlert) TableAlertTag(org.zaproxy.zap.db.TableAlertTag) Map(java.util.Map) HashMap(java.util.HashMap) SiteMap(org.parosproxy.paros.model.SiteMap)

Example 5 with TableAlertTag

use of org.zaproxy.zap.db.TableAlertTag 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();
    TableAlertTag tableAlertTag = getModel().getDb().getTableAlertTag();
    // TODO this doesn't 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 = Control.getSingleton().getExtensionLoader().getExtension(ExtensionHistory.class);
    for (int i = 0; i < v.size(); i++) {
        int alertId = v.get(i);
        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(historyId);
        }
        Alert alert;
        if (historyReference != null) {
            alert = new Alert(recAlert, historyReference);
        } else {
            alert = new Alert(recAlert);
        }
        alert.setTags(tableAlertTag.getTagsByAlertId(alertId));
        historyReference = alert.getHistoryRef();
        if (historyReference != null) {
            // The ref can be null if hrefs are purged
            addAlertToTree(alert);
            Integer key = historyId;
            if (!hrefs.containsKey(key)) {
                this.hrefs.put(key, alert.getHistoryRef());
            }
        }
    }
    siteTree.nodeStructureChanged(siteTree.getRoot());
}
Also used : HistoryReference(org.parosproxy.paros.model.HistoryReference) TableAlert(org.parosproxy.paros.db.TableAlert) TableAlertTag(org.zaproxy.zap.db.TableAlertTag) 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

TableAlert (org.parosproxy.paros.db.TableAlert)6 TableAlertTag (org.zaproxy.zap.db.TableAlertTag)6 RecordAlert (org.parosproxy.paros.db.RecordAlert)5 Alert (org.parosproxy.paros.core.scanner.Alert)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 DatabaseException (org.parosproxy.paros.db.DatabaseException)3 SiteMap (org.parosproxy.paros.model.SiteMap)3 ArrayList (java.util.ArrayList)2 ApiException (org.zaproxy.zap.extension.api.ApiException)2 Enumeration (java.util.Enumeration)1 JSONObject (net.sf.json.JSONObject)1 ExtensionHistory (org.parosproxy.paros.extension.history.ExtensionHistory)1 HistoryReference (org.parosproxy.paros.model.HistoryReference)1 ApiResponse (org.zaproxy.zap.extension.api.ApiResponse)1 ApiResponseElement (org.zaproxy.zap.extension.api.ApiResponseElement)1 ApiResponseList (org.zaproxy.zap.extension.api.ApiResponseList)1 ApiResponseSet (org.zaproxy.zap.extension.api.ApiResponseSet)1