Search in sources :

Example 1 with ParameterParser

use of org.zaproxy.zap.model.ParameterParser in project zaproxy by zaproxy.

the class ContextStructurePanel method initContextData.

@Override
public void initContextData(Session session, Context context) {
    ParameterParser urlParamParser = context.getUrlParamParser();
    ParameterParser formParamParser = context.getPostParamParser();
    this.ddnTableModel.setStructuralNodeModifiers(context.getDataDrivenNodes());
    if (urlParamParser instanceof StandardParameterParser) {
        StandardParameterParser urlStdParamParser = (StandardParameterParser) urlParamParser;
        this.getUrlKvPairSeparators().setText(urlStdParamParser.getKeyValuePairSeparators());
        this.getUrlKeyValueSeparators().setText(urlStdParamParser.getKeyValueSeparators());
        for (String structParam : urlStdParamParser.getStructuralParameters()) {
            this.ddnTableModel.addStructuralNodeModifier(new StructuralNodeModifier(StructuralNodeModifier.Type.StructuralParameter, null, structParam));
        }
    }
    if (formParamParser instanceof StandardParameterParser) {
        StandardParameterParser formStdParamParser = (StandardParameterParser) formParamParser;
        this.getPostKvPairSeparators().setText(formStdParamParser.getKeyValuePairSeparators());
        this.getPostKeyValueSeparators().setText(formStdParamParser.getKeyValueSeparators());
    }
}
Also used : StandardParameterParser(org.zaproxy.zap.model.StandardParameterParser) ParameterParser(org.zaproxy.zap.model.ParameterParser) StructuralNodeModifier(org.zaproxy.zap.model.StructuralNodeModifier) StandardParameterParser(org.zaproxy.zap.model.StandardParameterParser)

Example 2 with ParameterParser

use of org.zaproxy.zap.model.ParameterParser in project zaproxy by zaproxy.

the class Session method open.

protected void open(String fileName) throws DatabaseException, IOException, Exception {
    // TODO extract into db specific classes??
    if (Database.DB_TYPE_HSQLDB.equals(model.getDb().getType())) {
        configuration = new ZapXmlConfiguration(new File(fileName));
        sessionId = configuration.getLong(SESSION_ID);
        sessionName = configuration.getString(SESSION_NAME, "");
        sessionDesc = configuration.getString(SESSION_DESC, "");
    } else {
        this.setSessionId(Long.parseLong(fileName));
    }
    model.getDb().close(false, isCleanUpRequired());
    model.getDb().open(fileName);
    this.fileName = fileName;
    if (View.isInitialised()) {
        // Detach the siteTree model from the Sites tree, to reduce notification changes to the
        // UI while loading
        View.getSingleton().getSiteTreePanel().getTreeSite().setModel(new SiteMap(null, null));
    }
    if (!Constant.isLowMemoryOptionSet()) {
        SiteNode newRoot = new SiteNode(siteTree, -1, Constant.messages.getString("tab.sites"));
        siteTree.setRoot(newRoot);
    }
    // update history reference
    List<Integer> list = model.getDb().getTableHistory().getHistoryIdsOfHistType(getSessionId(), HistoryReference.TYPE_PROXIED, HistoryReference.TYPE_ZAP_USER);
    HistoryReference historyRef = null;
    discardContexts();
    // Load the session urls
    this.setExcludeFromProxyRegexs(sessionUrlListToStingList(model.getDb().getTableSessionUrl().getUrlsForType(RecordSessionUrl.TYPE_EXCLUDE_FROM_PROXY)));
    this.setExcludeFromScanRegexs(sessionUrlListToStingList(model.getDb().getTableSessionUrl().getUrlsForType(RecordSessionUrl.TYPE_EXCLUDE_FROM_SCAN)));
    this.setExcludeFromSpiderRegexs(sessionUrlListToStingList(model.getDb().getTableSessionUrl().getUrlsForType(RecordSessionUrl.TYPE_EXCLUDE_FROM_SPIDER)));
    for (int i = 0; i < list.size(); i++) {
        // ZAP: Removed unnecessary cast.
        int historyId = list.get(i);
        try {
            historyRef = new HistoryReference(historyId);
            if (View.isInitialised()) {
                final HistoryReference hRef = historyRef;
                final HttpMessage msg = historyRef.getHttpMessage();
                EventQueue.invokeAndWait(new Runnable() {

                    @Override
                    public void run() {
                        SiteNode sn = getSiteTree().addPath(hRef, msg);
                        if (sn != null) {
                            sn.setIncludedInScope(isIncludedInScope(sn), false);
                            sn.setExcludedFromScope(isExcludedFromScope(sn), false);
                        }
                    }
                });
            } else {
                SiteNode sn = getSiteTree().addPath(historyRef);
                if (sn != null) {
                    sn.setIncludedInScope(this.isIncludedInScope(sn), false);
                    sn.setExcludedFromScope(this.isExcludedFromScope(sn), false);
                }
            }
            // ZAP: Load alerts from db
            historyRef.loadAlerts();
            if (i % 100 == 99)
                Thread.yield();
        } catch (Exception e) {
            // ZAP: Log exceptions
            log.warn(e.getMessage(), e);
        }
    }
    // update siteTree reference
    list = model.getDb().getTableHistory().getHistoryIdsOfHistType(getSessionId(), HistoryReference.TYPE_SPIDER, HistoryReference.TYPE_BRUTE_FORCE, HistoryReference.TYPE_SPIDER_AJAX, HistoryReference.TYPE_SCANNER);
    for (int i = 0; i < list.size(); i++) {
        // ZAP: Removed unnecessary cast.
        int historyId = list.get(i);
        try {
            historyRef = new HistoryReference(historyId);
            if (View.isInitialised()) {
                final HistoryReference hRef = historyRef;
                final HttpMessage msg = historyRef.getHttpMessage();
                EventQueue.invokeAndWait(new Runnable() {

                    @Override
                    public void run() {
                        getSiteTree().addPath(hRef, msg);
                    }
                });
            } else {
                getSiteTree().addPath(historyRef);
            }
            historyRef.loadAlerts();
            if (i % 100 == 99)
                Thread.yield();
        } catch (Exception e) {
            // ZAP: Log exceptions
            log.warn(e.getMessage(), e);
        }
    }
    List<RecordContext> contextData = model.getDb().getTableContext().getAllData();
    for (RecordContext data : contextData) {
        Context ctx = this.getContext(data.getContextId());
        if (ctx == null) {
            ctx = new Context(this, data.getContextId());
            this.addContext(ctx);
            if (nextContextId <= data.getContextId()) {
                nextContextId = data.getContextId() + 1;
            }
        }
        switch(data.getType()) {
            case RecordContext.TYPE_NAME:
                ctx.setName(data.getData());
                if (View.isInitialised() && !ctx.getName().equals(String.valueOf(ctx.getId()))) {
                    View.getSingleton().renameContext(ctx);
                }
                break;
            case RecordContext.TYPE_DESCRIPTION:
                ctx.setDescription(data.getData());
                break;
            case RecordContext.TYPE_INCLUDE:
                ctx.addIncludeInContextRegex(data.getData());
                break;
            case RecordContext.TYPE_EXCLUDE:
                ctx.addExcludeFromContextRegex(data.getData());
                break;
            case RecordContext.TYPE_IN_SCOPE:
                ctx.setInScope(Boolean.parseBoolean(data.getData()));
                break;
            case RecordContext.TYPE_INCLUDE_TECH:
                ctx.getTechSet().include(new Tech(data.getData()));
                break;
            case RecordContext.TYPE_EXCLUDE_TECH:
                ctx.getTechSet().exclude(new Tech(data.getData()));
                break;
        }
    }
    for (Context ctx : contexts) {
        try {
            // Set up the URL parameter parser
            List<String> strs = this.getContextDataStrings(ctx.getId(), RecordContext.TYPE_URL_PARSER_CLASSNAME);
            if (strs.size() == 1) {
                Class<?> c = ExtensionFactory.getAddOnLoader().loadClass(strs.get(0));
                if (c == null) {
                    log.error("Failed to load URL parser for context " + ctx.getId() + " : " + strs.get(0));
                } else {
                    ParameterParser parser = (ParameterParser) c.getConstructor().newInstance();
                    strs = this.getContextDataStrings(ctx.getId(), RecordContext.TYPE_URL_PARSER_CONFIG);
                    if (strs.size() == 1) {
                        parser.init(strs.get(0));
                    }
                    parser.setContext(ctx);
                    ctx.setUrlParamParser(parser);
                }
            }
        } catch (Exception e) {
            log.error("Failed to load URL parser for context " + ctx.getId(), e);
        }
        try {
            // Set up the URL parameter parser
            List<String> strs = this.getContextDataStrings(ctx.getId(), RecordContext.TYPE_POST_PARSER_CLASSNAME);
            if (strs.size() == 1) {
                Class<?> c = ExtensionFactory.getAddOnLoader().loadClass(strs.get(0));
                if (c == null) {
                    log.error("Failed to load POST parser for context " + ctx.getId() + " : " + strs.get(0));
                } else {
                    ParameterParser parser = (ParameterParser) c.getConstructor().newInstance();
                    strs = this.getContextDataStrings(ctx.getId(), RecordContext.TYPE_POST_PARSER_CONFIG);
                    if (strs.size() == 1) {
                        parser.init(strs.get(0));
                    }
                    parser.setContext(ctx);
                    ctx.setPostParamParser(parser);
                }
            }
        } catch (Exception e) {
            log.error("Failed to load POST parser for context " + ctx.getId(), e);
        }
        try {
            // Set up the Data Driven Nodes
            List<String> strs = this.getContextDataStrings(ctx.getId(), RecordContext.TYPE_DATA_DRIVEN_NODES);
            for (String str : strs) {
                ctx.addDataDrivenNodes(new StructuralNodeModifier(str));
            }
        } catch (Exception e) {
            log.error("Failed to load data driven nodes for context " + ctx.getId(), e);
        }
        ctx.restructureSiteTree();
    }
    if (View.isInitialised()) {
        View.getSingleton().getSiteTreePanel().getTreeSite().setModel(siteTree);
        View.getSingleton().getSiteTreePanel().expandRoot();
    }
    this.refreshScope();
    Stats.clearAll();
    System.gc();
}
Also used : Context(org.zaproxy.zap.model.Context) RecordContext(org.parosproxy.paros.db.RecordContext) StandardParameterParser(org.zaproxy.zap.model.StandardParameterParser) ParameterParser(org.zaproxy.zap.model.ParameterParser) StructuralNodeModifier(org.zaproxy.zap.model.StructuralNodeModifier) RecordContext(org.parosproxy.paros.db.RecordContext) URIException(org.apache.commons.httpclient.URIException) InvalidParameterException(java.security.InvalidParameterException) IllegalContextNameException(org.zaproxy.zap.model.IllegalContextNameException) IOException(java.io.IOException) DatabaseException(org.parosproxy.paros.db.DatabaseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) Tech(org.zaproxy.zap.model.Tech) ZapXmlConfiguration(org.zaproxy.zap.utils.ZapXmlConfiguration) HttpMessage(org.parosproxy.paros.network.HttpMessage) File(java.io.File)

Example 3 with ParameterParser

use of org.zaproxy.zap.model.ParameterParser in project zaproxy by zaproxy.

the class Session method importContext.

/**
 * Imports a context from the specified (XML) file.
 *
 * @param file the (XML) file that contains the context data
 * @return the imported {@code Context}, already added to the session.
 * @throws ConfigurationException
 * @throws ClassNotFoundException
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 * @throws SecurityException
 * @throws IllegalContextNameException (since 2.6.0) if context's name is not provided or it's
 *     empty or if a context with the same name already exists.
 */
public Context importContext(File file) throws ConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    ZapXmlConfiguration config = new ZapXmlConfiguration(file);
    String name = config.getString(Context.CONTEXT_CONFIG_NAME);
    validateContextName(name);
    Context c = createContext(name);
    c.setDescription(config.getString(Context.CONTEXT_CONFIG_DESC));
    c.setInScope(config.getBoolean(Context.CONTEXT_CONFIG_INSCOPE, false));
    for (Object obj : config.getList(Context.CONTEXT_CONFIG_INC_REGEXES)) {
        c.addIncludeInContextRegex(obj.toString());
    }
    for (Object obj : config.getList(Context.CONTEXT_CONFIG_EXC_REGEXES)) {
        c.addExcludeFromContextRegex(obj.toString());
    }
    TechSet techSet = new TechSet();
    for (Object obj : config.getList(Context.CONTEXT_CONFIG_TECH_INCLUDE)) {
        techSet.include(new Tech(obj.toString()));
    }
    for (Object obj : config.getList(Context.CONTEXT_CONFIG_TECH_EXCLUDE)) {
        techSet.exclude(new Tech(obj.toString()));
    }
    c.setTechSet(techSet);
    String urlParserClass = config.getString(Context.CONTEXT_CONFIG_URLPARSER_CLASS);
    if (urlParserClass == null) {
        // Can happen due to a bug in 2.4.0 where is was saved using the wrong name :(
        urlParserClass = config.getString(Context.CONTEXT_CONFIG_URLPARSER);
    }
    if (urlParserClass == null) {
        urlParserClass = StandardParameterParser.class.getCanonicalName();
    }
    Class<?> cl = ExtensionFactory.getAddOnLoader().loadClass(urlParserClass);
    if (cl == null) {
        throw new ConfigurationException("Failed to load URL parser for context " + urlParserClass);
    } else {
        ParameterParser parser = (ParameterParser) cl.getConstructor().newInstance();
        parser.init(config.getString(Context.CONTEXT_CONFIG_URLPARSER_CONFIG));
        parser.setContext(c);
        c.setUrlParamParser(parser);
    }
    String postParserClass = config.getString(Context.CONTEXT_CONFIG_POSTPARSER_CLASS);
    String postParserConfig = config.getString(Context.CONTEXT_CONFIG_POSTPARSER_CONFIG);
    if (postParserClass == null) {
        // Can happen due to a bug in 2.4.0 where is was saved using the wrong name :(
        postParserClass = config.getString(urlParserClass);
        postParserConfig = config.getString(Context.CONTEXT_CONFIG_URLPARSER_CONFIG);
    }
    if (postParserClass == null) {
        postParserClass = StandardParameterParser.class.getCanonicalName();
    }
    cl = ExtensionFactory.getAddOnLoader().loadClass(postParserClass);
    if (cl == null) {
        throw new ConfigurationException("Failed to load POST parser for context " + postParserClass);
    } else {
        ParameterParser parser = (ParameterParser) cl.getConstructor().newInstance();
        parser.init(postParserConfig);
        parser.setContext(c);
        c.setPostParamParser(parser);
    }
    for (Object obj : config.getList(Context.CONTEXT_CONFIG_DATA_DRIVEN_NODES)) {
        c.addDataDrivenNodes(new StructuralNodeModifier(obj.toString()));
    }
    model.importContext(c, config);
    c.restructureSiteTree();
    addContext(c);
    saveContext(c);
    return c;
}
Also used : Context(org.zaproxy.zap.model.Context) RecordContext(org.parosproxy.paros.db.RecordContext) Tech(org.zaproxy.zap.model.Tech) TechSet(org.zaproxy.zap.model.TechSet) StandardParameterParser(org.zaproxy.zap.model.StandardParameterParser) ParameterParser(org.zaproxy.zap.model.ParameterParser) StructuralNodeModifier(org.zaproxy.zap.model.StructuralNodeModifier) ConfigurationException(org.apache.commons.configuration.ConfigurationException) StandardParameterParser(org.zaproxy.zap.model.StandardParameterParser) ZapXmlConfiguration(org.zaproxy.zap.utils.ZapXmlConfiguration)

Example 4 with ParameterParser

use of org.zaproxy.zap.model.ParameterParser in project zaproxy by zaproxy.

the class ContextStructurePanel method saveToContext.

/**
 * Save the data from this panel to the provided context.
 *
 * @param context the context
 * @param updateSiteStructure {@code true} if the nodes of the context should be restructured,
 *     {@code false} otherwise
 * @see Context#restructureSiteTree()
 */
private void saveToContext(Context context, boolean updateSiteStructure) {
    ParameterParser urlParamParser = context.getUrlParamParser();
    ParameterParser formParamParser = context.getPostParamParser();
    List<String> structParams = new ArrayList<>();
    List<StructuralNodeModifier> ddns = new ArrayList<>();
    for (StructuralNodeModifier snm : this.ddnTableModel.getElements()) {
        if (snm.getType().equals(StructuralNodeModifier.Type.StructuralParameter)) {
            structParams.add(snm.getName());
        } else {
            ddns.add(snm);
        }
    }
    if (urlParamParser instanceof StandardParameterParser) {
        StandardParameterParser urlStdParamParser = (StandardParameterParser) urlParamParser;
        urlStdParamParser.setKeyValuePairSeparators(this.getUrlKvPairSeparators().getText());
        urlStdParamParser.setKeyValueSeparators(this.getUrlKeyValueSeparators().getText());
        urlStdParamParser.setStructuralParameters(structParams);
        context.setUrlParamParser(urlStdParamParser);
        urlStdParamParser.setContext(context);
    }
    if (formParamParser instanceof StandardParameterParser) {
        StandardParameterParser formStdParamParser = (StandardParameterParser) formParamParser;
        formStdParamParser.setKeyValuePairSeparators(this.getPostKvPairSeparators().getText());
        formStdParamParser.setKeyValueSeparators(this.getPostKeyValueSeparators().getText());
        context.setPostParamParser(formStdParamParser);
        formStdParamParser.setContext(context);
    }
    context.setDataDrivenNodes(ddns);
    if (updateSiteStructure) {
        context.restructureSiteTree();
    }
}
Also used : StandardParameterParser(org.zaproxy.zap.model.StandardParameterParser) ParameterParser(org.zaproxy.zap.model.ParameterParser) StructuralNodeModifier(org.zaproxy.zap.model.StructuralNodeModifier) StandardParameterParser(org.zaproxy.zap.model.StandardParameterParser) ArrayList(java.util.ArrayList)

Example 5 with ParameterParser

use of org.zaproxy.zap.model.ParameterParser in project zaproxy by zaproxy.

the class VariantAbstractQuery method setParameter.

private String setParameter(HttpMessage msg, NameValuePair originalPair, String name, String value, boolean escaped) {
    // We need the correct parameter parser to use the right separators
    ParameterParser parser;
    if (originalPair.getType() == NameValuePair.TYPE_POST_DATA) {
        parser = Model.getSingleton().getSession().getFormParamParser(msg.getRequestHeader().getURI().toString());
    } else if (originalPair.getType() == NameValuePair.TYPE_QUERY_STRING) {
        parser = Model.getSingleton().getSession().getUrlParamParser(msg.getRequestHeader().getURI().toString());
    } else {
        parser = new StandardParameterParser();
    }
    StringBuilder sb = new StringBuilder();
    String encodedValue = (escaped) ? value : getEscapedValue(msg, value);
    NameValuePair pair;
    boolean isAppended;
    for (int i = 0; i < getParamList().size(); i++) {
        pair = getParamList().get(i);
        String origName = originalNames.get(i);
        if (i == originalPair.getPosition()) {
            isAppended = paramAppend(sb, getEscapedName(msg, pair.getName() == name ? origName : name), encodedValue, parser);
        } else {
            isAppended = paramAppend(sb, getEscapedName(msg, origName), getEscapedValue(msg, pair.getValue()), parser);
        }
        if (isAppended && i < getParamList().size() - 1) {
            sb.append(parser.getDefaultKeyValuePairSeparator());
        }
    }
    if (sb.length() == 0) {
        // No original query string
        sb.append(encodedValue);
    }
    String query = sb.toString();
    buildMessage(msg, query);
    return query;
}
Also used : StandardParameterParser(org.zaproxy.zap.model.StandardParameterParser) ParameterParser(org.zaproxy.zap.model.ParameterParser) StandardParameterParser(org.zaproxy.zap.model.StandardParameterParser)

Aggregations

ParameterParser (org.zaproxy.zap.model.ParameterParser)5 StandardParameterParser (org.zaproxy.zap.model.StandardParameterParser)5 StructuralNodeModifier (org.zaproxy.zap.model.StructuralNodeModifier)4 ConfigurationException (org.apache.commons.configuration.ConfigurationException)2 RecordContext (org.parosproxy.paros.db.RecordContext)2 Context (org.zaproxy.zap.model.Context)2 Tech (org.zaproxy.zap.model.Tech)2 ZapXmlConfiguration (org.zaproxy.zap.utils.ZapXmlConfiguration)2 File (java.io.File)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 InvalidParameterException (java.security.InvalidParameterException)1 ArrayList (java.util.ArrayList)1 URIException (org.apache.commons.httpclient.URIException)1 DatabaseException (org.parosproxy.paros.db.DatabaseException)1 HttpMessage (org.parosproxy.paros.network.HttpMessage)1 IllegalContextNameException (org.zaproxy.zap.model.IllegalContextNameException)1 TechSet (org.zaproxy.zap.model.TechSet)1