All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.parosproxy.paros.core.scanner.VariantCookie Maven / Gradle / Ivy

Go to download

The Zed Attack Proxy (ZAP) is an easy to use integrated penetration testing tool for finding vulnerabilities in web applications. It is designed to be used by people with a wide range of security experience and as such is ideal for developers and functional testers who are new to penetration testing. ZAP provides automated scanners as well as a set of tools that allow you to find security vulnerabilities manually.

There is a newer version: 2.15.0
Show newest version
/*
 * Zed Attack Proxy (ZAP) and its related class files.
 *
 * ZAP is an HTTP/HTTPS proxy for assessing web application security.
 *
 * Copyright 2013 The ZAP Development Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.parosproxy.paros.core.scanner;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.parosproxy.paros.network.HttpHeader;
import org.parosproxy.paros.network.HttpMessage;

/**
 * A {@code Variant} for Cookie headers, allowing to attack the names and values of the cookies.
 *
 * @author andy
 * @see Variant
 */
public class VariantCookie implements Variant {

    private List params = Collections.emptyList();

    /** @throws IllegalArgumentException if {@code message} is {@code null}. */
    @Override
    public void setMessage(HttpMessage message) {
        if (message == null) {
            throw new IllegalArgumentException("Parameter message must not be null.");
        }

        List cookieLines = message.getRequestHeader().getHeaderValues(HttpHeader.COOKIE);
        if (cookieLines.isEmpty()) {
            params = Collections.emptyList();
            return;
        }

        ArrayList extractedParameters = new ArrayList<>();
        for (String cookieLine : cookieLines) {
            if (cookieLine.trim().isEmpty()) {
                continue;
            }

            String[] cookieArray = cookieLine.split("; ?");
            for (String cookie : cookieArray) {
                String[] nameValuePair = cookie.split("=", 2);
                boolean hasNameValuePair = nameValuePair.length == 2;
                String name = hasNameValuePair ? nameValuePair[0] : null;
                String value =
                        getUnescapedValue(!hasNameValuePair ? nameValuePair[0] : nameValuePair[1]);
                extractedParameters.add(
                        new NameValuePair(
                                NameValuePair.TYPE_COOKIE,
                                name,
                                value,
                                extractedParameters.size()));
            }
        }

        if (extractedParameters.isEmpty()) {
            params = Collections.emptyList();
        } else {
            extractedParameters.trimToSize();
            params = Collections.unmodifiableList(extractedParameters);
        }
    }

    /**
     * Encodes the given {@code value}.
     *
     * @param value the value that needs to be encoded, must not be {@code null}.
     * @return the encoded value
     */
    private static String getEscapedValue(String value) {
        return AbstractPlugin.getURLEncode(value);
    }

    /**
     * Decodes the given {@code value}.
     *
     * @param value the value that needs to be decoded, must not be {@code null}.
     * @return the decoded value
     */
    private String getUnescapedValue(String value) {
        return AbstractPlugin.getURLDecode(value);
    }

    /**
     * Gets the list of parameters (that is, cookies) extracted from the request header of the
     * message.
     *
     * @return an unmodifiable {@code List} containing the extracted parameters, never {@code null}.
     */
    @Override
    public List getParamList() {
        return params;
    }

    /**
     * @param msg
     * @param originalPair
     * @param name
     * @param value
     * @return
     */
    @Override
    public String setParameter(
            HttpMessage msg, NameValuePair originalPair, String name, String value) {
        return setParameter(msg, originalPair, name, value, false);
    }

    /**
     * @param msg
     * @param originalPair
     * @param name
     * @param value
     * @return
     */
    @Override
    public String setEscapedParameter(
            HttpMessage msg, NameValuePair originalPair, String name, String value) {
        return setParameter(msg, originalPair, name, value, true);
    }

    /**
     * @param msg
     * @param originalPair
     * @param name
     * @param value
     * @param escaped
     * @return
     */
    private String setParameter(
            HttpMessage msg,
            NameValuePair originalPair,
            String name,
            String value,
            boolean escaped) {
        String escapedValue = value == null ? null : escaped ? value : getEscapedValue(value);
        StringBuilder cookieString = new StringBuilder();
        for (int idx = 0; idx < params.size(); idx++) {
            String cookieName = null;
            String cookieValue = null;
            if (idx == originalPair.getPosition()) {
                if (!(name == null && escapedValue == null)) {
                    cookieName = name;
                    if (escapedValue != null) {
                        cookieValue = escapedValue;
                    }
                }
            } else {
                NameValuePair param = params.get(idx);
                cookieName = param.getName();
                cookieValue = param.getValue();
                if (cookieValue != null) {
                    cookieValue = getEscapedValue(cookieValue);
                }
            }

            if (cookieString.length() != 0
                    && !((cookieName == null || cookieName.isEmpty()) && cookieValue == null)) {
                cookieString.append("; ");
            }

            if (cookieName != null && !cookieName.isEmpty()) {
                cookieString.append(cookieName);
                cookieString.append('=');
            }

            if (cookieValue != null) {
                cookieString.append(cookieValue);
            }
        }

        msg.getRequestHeader().setHeader(HttpHeader.COOKIE, null);
        if (cookieString.length() != 0) {
            msg.getRequestHeader().setHeader(HttpHeader.COOKIE, cookieString.toString());
        }

        if (escapedValue == null) {
            if (name == null || name.isEmpty()) {
                return null;
            }
            return name + "=";
        }

        if (name == null) {
            return escapedValue;
        }

        return name + "=" + escapedValue;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy