Search in sources :

Example 1 with DomainAlwaysInScopeMatcher

use of org.zaproxy.zap.spider.DomainAlwaysInScopeMatcher in project zaproxy by zaproxy.

the class SpiderAPI method handleApiAction.

@Override
public ApiResponse handleApiAction(String name, JSONObject params) throws ApiException {
    log.debug("Request for handleApiAction: " + name + " (params: " + params.toString() + ")");
    GenericScanner2 scan;
    int maxChildren = -1;
    Context context = null;
    switch(name) {
        case ACTION_START_SCAN:
            // The action is to start a new Scan
            String url = ApiUtils.getOptionalStringParam(params, PARAM_URL);
            if (params.containsKey(PARAM_MAX_CHILDREN)) {
                String maxChildrenStr = params.getString(PARAM_MAX_CHILDREN);
                if (maxChildrenStr != null && maxChildrenStr.length() > 0) {
                    try {
                        maxChildren = Integer.parseInt(maxChildrenStr);
                    } catch (NumberFormatException e) {
                        throw new ApiException(Type.ILLEGAL_PARAMETER, PARAM_MAX_CHILDREN);
                    }
                }
            }
            if (params.containsKey(PARAM_CONTEXT_NAME)) {
                String contextName = params.getString(PARAM_CONTEXT_NAME);
                if (!contextName.isEmpty()) {
                    context = ApiUtils.getContextByName(contextName);
                }
            }
            int scanId = scanURL(url, null, maxChildren, this.getParam(params, PARAM_RECURSE, true), context, getParam(params, PARAM_SUBTREE_ONLY, false));
            return new ApiResponseElement(name, Integer.toString(scanId));
        case ACTION_START_SCAN_AS_USER:
            // The action is to start a new Scan from the perspective of a user
            String urlUserScan = ApiUtils.getOptionalStringParam(params, PARAM_URL);
            int userID = ApiUtils.getIntParam(params, PARAM_USER_ID);
            ExtensionUserManagement usersExtension = (ExtensionUserManagement) Control.getSingleton().getExtensionLoader().getExtension(ExtensionUserManagement.NAME);
            if (usersExtension == null) {
                throw new ApiException(Type.NO_IMPLEMENTOR, ExtensionUserManagement.NAME);
            }
            context = ApiUtils.getContextByParamId(params, PARAM_CONTEXT_ID);
            User user = usersExtension.getContextUserAuthManager(context.getIndex()).getUserById(userID);
            if (user == null) {
                throw new ApiException(Type.USER_NOT_FOUND, PARAM_USER_ID);
            }
            if (params.containsKey(PARAM_MAX_CHILDREN)) {
                String maxChildrenStr = params.getString(PARAM_MAX_CHILDREN);
                if (maxChildrenStr != null && maxChildrenStr.length() > 0) {
                    try {
                        maxChildren = Integer.parseInt(maxChildrenStr);
                    } catch (NumberFormatException e) {
                        throw new ApiException(Type.ILLEGAL_PARAMETER, PARAM_MAX_CHILDREN);
                    }
                }
            }
            scanId = scanURL(urlUserScan, user, maxChildren, this.getParam(params, PARAM_RECURSE, true), context, getParam(params, PARAM_SUBTREE_ONLY, false));
            return new ApiResponseElement(name, Integer.toString(scanId));
        case ACTION_PAUSE_SCAN:
            scan = getSpiderScan(params);
            if (scan == null) {
                throw new ApiException(ApiException.Type.DOES_NOT_EXIST, PARAM_SCAN_ID);
            }
            extension.pauseScan(scan.getScanId());
            break;
        case ACTION_RESUME_SCAN:
            scan = getSpiderScan(params);
            if (scan == null) {
                throw new ApiException(ApiException.Type.DOES_NOT_EXIST, PARAM_SCAN_ID);
            }
            extension.resumeScan(scan.getScanId());
            break;
        case ACTION_STOP_SCAN:
            // The action is to stop a pending scan
            scan = getSpiderScan(params);
            if (scan == null) {
                throw new ApiException(ApiException.Type.DOES_NOT_EXIST, PARAM_SCAN_ID);
            }
            extension.stopScan(scan.getScanId());
            break;
        case ACTION_REMOVE_SCAN:
            // Note that we're removing the scan with this call, not just getting it ;)
            scan = getSpiderScan(params);
            if (scan == null) {
                throw new ApiException(ApiException.Type.DOES_NOT_EXIST, PARAM_SCAN_ID);
            }
            extension.removeScan(scan.getScanId());
            break;
        case ACTION_PAUSE_ALL_SCANS:
            extension.pauseAllScans();
            break;
        case ACTION_RESUME_ALL_SCANS:
            extension.resumeAllScans();
            break;
        case ACTION_STOP_ALL_SCANS:
            extension.stopAllScans();
            break;
        case ACTION_REMOVE_ALL_SCANS:
            extension.removeAllScans();
            break;
        case ACTION_CLEAR_EXCLUDED_FROM_SCAN:
            try {
                Session session = Model.getSingleton().getSession();
                session.setExcludeFromSpiderRegexs(new ArrayList<String>());
            } catch (DatabaseException e) {
                throw new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
            }
            break;
        case ACTION_EXCLUDE_FROM_SCAN:
            String regex = params.getString(PARAM_REGEX);
            try {
                Session session = Model.getSingleton().getSession();
                session.addExcludeFromSpiderRegex(regex);
            } catch (DatabaseException e) {
                throw new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
            } catch (PatternSyntaxException e) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_REGEX);
            }
            break;
        case ACTION_ADD_DOMAIN_ALWAYS_IN_SCOPE:
            try {
                String value = params.getString(PARAM_VALUE);
                DomainAlwaysInScopeMatcher domainAlwaysInScope;
                if (getParam(params, PARAM_IS_REGEX, false)) {
                    domainAlwaysInScope = new DomainAlwaysInScopeMatcher(DomainAlwaysInScopeMatcher.createPattern(value));
                } else {
                    domainAlwaysInScope = new DomainAlwaysInScopeMatcher(value);
                }
                domainAlwaysInScope.setEnabled(getParam(params, PARAM_IS_ENABLED, true));
                List<DomainAlwaysInScopeMatcher> domainsAlwaysInScope = new ArrayList<>(extension.getSpiderParam().getDomainsAlwaysInScope());
                domainsAlwaysInScope.add(domainAlwaysInScope);
                extension.getSpiderParam().setDomainsAlwaysInScope(domainsAlwaysInScope);
            } catch (IllegalArgumentException e) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_VALUE, e);
            }
            break;
        case ACTION_MODIFY_DOMAIN_ALWAYS_IN_SCOPE:
            try {
                int idx = params.getInt(PARAM_IDX);
                if (idx < 0 || idx >= extension.getSpiderParam().getDomainsAlwaysInScope().size()) {
                    throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_IDX);
                }
                DomainAlwaysInScopeMatcher oldDomain = extension.getSpiderParam().getDomainsAlwaysInScope().get(idx);
                String value = getParam(params, PARAM_VALUE, oldDomain.getValue());
                if (value.isEmpty()) {
                    value = oldDomain.getValue();
                }
                DomainAlwaysInScopeMatcher newDomain;
                if (getParam(params, PARAM_IS_REGEX, oldDomain.isRegex())) {
                    newDomain = new DomainAlwaysInScopeMatcher(DomainAlwaysInScopeMatcher.createPattern(value));
                } else {
                    newDomain = new DomainAlwaysInScopeMatcher(value);
                }
                newDomain.setEnabled(getParam(params, PARAM_IS_ENABLED, oldDomain.isEnabled()));
                if (oldDomain.equals(newDomain)) {
                    break;
                }
                List<DomainAlwaysInScopeMatcher> domainsAlwaysInScope = new ArrayList<>(extension.getSpiderParam().getDomainsAlwaysInScope());
                domainsAlwaysInScope.set(idx, newDomain);
                extension.getSpiderParam().setDomainsAlwaysInScope(domainsAlwaysInScope);
            } catch (JSONException e) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_IDX, e);
            } catch (IllegalArgumentException e) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_VALUE, e);
            }
            break;
        case ACTION_REMOVE_DOMAIN_ALWAYS_IN_SCOPE:
            try {
                int idx = params.getInt(PARAM_IDX);
                if (idx < 0 || idx >= extension.getSpiderParam().getDomainsAlwaysInScope().size()) {
                    throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_IDX);
                }
                List<DomainAlwaysInScopeMatcher> domainsAlwaysInScope = new ArrayList<>(extension.getSpiderParam().getDomainsAlwaysInScope());
                domainsAlwaysInScope.remove(idx);
                extension.getSpiderParam().setDomainsAlwaysInScope(domainsAlwaysInScope);
            } catch (JSONException e) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_IDX, e);
            }
            break;
        case ACTION_ENABLE_ALL_DOMAINS_ALWAYS_IN_SCOPE:
            setDomainsAlwaysInScopeEnabled(true);
            break;
        case ACTION_DISABLE_ALL_DOMAINS_ALWAYS_IN_SCOPE:
            setDomainsAlwaysInScopeEnabled(false);
            break;
        default:
            throw new ApiException(ApiException.Type.BAD_ACTION);
    }
    return ApiResponseElement.OK;
}
Also used : Context(org.zaproxy.zap.model.Context) User(org.zaproxy.zap.users.User) ArrayList(java.util.ArrayList) JSONException(net.sf.json.JSONException) DomainAlwaysInScopeMatcher(org.zaproxy.zap.spider.DomainAlwaysInScopeMatcher) ExtensionUserManagement(org.zaproxy.zap.extension.users.ExtensionUserManagement) GenericScanner2(org.zaproxy.zap.model.GenericScanner2) ApiResponseElement(org.zaproxy.zap.extension.api.ApiResponseElement) DatabaseException(org.parosproxy.paros.db.DatabaseException) ApiException(org.zaproxy.zap.extension.api.ApiException) Session(org.parosproxy.paros.model.Session) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 2 with DomainAlwaysInScopeMatcher

use of org.zaproxy.zap.spider.DomainAlwaysInScopeMatcher in project zaproxy by zaproxy.

the class SpiderAPI method domainMatchersToApiResponseList.

private ApiResponse domainMatchersToApiResponseList(String name, List<DomainAlwaysInScopeMatcher> domains, boolean excludeDisabled) {
    ApiResponseList apiResponse = new ApiResponseList(name);
    for (int i = 0; i < domains.size(); i++) {
        DomainAlwaysInScopeMatcher domain = domains.get(i);
        if (!domain.isEnabled() && excludeDisabled) {
            continue;
        }
        Map<String, Object> domainData = new HashMap<>();
        domainData.put("idx", i);
        domainData.put("value", domain.getValue());
        domainData.put("regex", domain.isRegex());
        domainData.put("enabled", domain.isEnabled());
        apiResponse.addItem(new ApiResponseSet<Object>("domain", domainData));
    }
    return apiResponse;
}
Also used : DomainAlwaysInScopeMatcher(org.zaproxy.zap.spider.DomainAlwaysInScopeMatcher) ApiResponseList(org.zaproxy.zap.extension.api.ApiResponseList) HashMap(java.util.HashMap) JSONObject(net.sf.json.JSONObject)

Example 3 with DomainAlwaysInScopeMatcher

use of org.zaproxy.zap.spider.DomainAlwaysInScopeMatcher in project zaproxy by zaproxy.

the class SpiderAPI method setDomainsAlwaysInScopeEnabled.

private void setDomainsAlwaysInScopeEnabled(boolean enabled) {
    List<DomainAlwaysInScopeMatcher> domainsAlwaysInScope = extension.getSpiderParam().getDomainsAlwaysInScope();
    for (DomainAlwaysInScopeMatcher x : extension.getSpiderParam().getDomainsAlwaysInScope()) {
        x.setEnabled(enabled);
    }
    extension.getSpiderParam().setDomainsAlwaysInScope(domainsAlwaysInScope);
}
Also used : DomainAlwaysInScopeMatcher(org.zaproxy.zap.spider.DomainAlwaysInScopeMatcher)

Example 4 with DomainAlwaysInScopeMatcher

use of org.zaproxy.zap.spider.DomainAlwaysInScopeMatcher in project zaproxy by zaproxy.

the class DialogAddDomainAlwaysInScope method performAction.

@Override
protected void performAction() {
    String value = getDomainTextField().getText();
    if (getRegexCheckBox().isSelected()) {
        Pattern pattern = DomainAlwaysInScopeMatcher.createPattern(value);
        domainAlwaysInScope = new DomainAlwaysInScopeMatcher(pattern);
    } else {
        domainAlwaysInScope = new DomainAlwaysInScopeMatcher(value);
    }
    domainAlwaysInScope.setEnabled(getEnabledCheckBox().isSelected());
}
Also used : Pattern(java.util.regex.Pattern) DomainAlwaysInScopeMatcher(org.zaproxy.zap.spider.DomainAlwaysInScopeMatcher)

Example 5 with DomainAlwaysInScopeMatcher

use of org.zaproxy.zap.spider.DomainAlwaysInScopeMatcher in project zaproxy by zaproxy.

the class DomainsAlwaysInScopeTableModel method setDomainsAlwaysInScope.

public void setDomainsAlwaysInScope(List<DomainAlwaysInScopeMatcher> domainsInScope) {
    this.domainsInScope = new ArrayList<>(domainsInScope.size());
    for (DomainAlwaysInScopeMatcher excludedDomain : domainsInScope) {
        this.domainsInScope.add(new DomainAlwaysInScopeMatcher(excludedDomain));
    }
    fireTableDataChanged();
}
Also used : DomainAlwaysInScopeMatcher(org.zaproxy.zap.spider.DomainAlwaysInScopeMatcher)

Aggregations

DomainAlwaysInScopeMatcher (org.zaproxy.zap.spider.DomainAlwaysInScopeMatcher)5 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Pattern (java.util.regex.Pattern)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 JSONException (net.sf.json.JSONException)1 JSONObject (net.sf.json.JSONObject)1 DatabaseException (org.parosproxy.paros.db.DatabaseException)1 Session (org.parosproxy.paros.model.Session)1 ApiException (org.zaproxy.zap.extension.api.ApiException)1 ApiResponseElement (org.zaproxy.zap.extension.api.ApiResponseElement)1 ApiResponseList (org.zaproxy.zap.extension.api.ApiResponseList)1 ExtensionUserManagement (org.zaproxy.zap.extension.users.ExtensionUserManagement)1 Context (org.zaproxy.zap.model.Context)1 GenericScanner2 (org.zaproxy.zap.model.GenericScanner2)1 User (org.zaproxy.zap.users.User)1