/*
 * Copyright 2022 Google LLC
 *
 * 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 com.google.tsunami.plugins.detectors.directorytraversal.genericpathtraversaldetector;

import com.google.common.collect.ImmutableSet;
import com.google.inject.Provides;
import com.google.tsunami.plugin.PluginBootstrapModule;

/** A Guice module that bootstraps the {@link GenericPathTraversalDetector}. */
public final class GenericPathTraversalDetectorBootstrapModule extends PluginBootstrapModule {
  private static final String PERCENT_ENCODED_RELATIVE_PAYLOAD =
      "..%2F".repeat(30) + "etc%2Fpasswd";
  private static final String PERCENT_ENCODED_ABSOLUTE_PAYLOAD = "%2Fetc%2Fpasswd";

  @Override
  protected void configurePlugin() {
    // registerPlugin method is required in order for the Tsunami scanner to identify the plugin.
    registerPlugin(GenericPathTraversalDetector.class);
  }

  @Provides
  GenericPathTraversalDetectorConfig provideGenericPathTraversalDetectorConfig() {
    return GenericPathTraversalDetectorConfig.create(
        ImmutableSet.of(new GetParameterInjection(), new PathParameterInjection()),
        /* maxCrawledUrlsToFuzz= */ 50,
        /* maxExploitsToTest= */ 200,
        /**
         * TODO(b/202565385) Add a command line parameter to configure the scanning mode. Ideally,
         * it should be possible to pass the scanning mode (QUICK/EXTENSIVE) via a command line
         * parameter to configure Tsunami to do either QUICK or EXTENSIVE scanning.
         */
        getPayloadsForScanningMode(Mode.QUICK));
  }

  private static ImmutableSet<String> getPayloadsForScanningMode(Mode scanningMode) {
    switch (scanningMode) {
      case QUICK:
        return ImmutableSet.of(PERCENT_ENCODED_RELATIVE_PAYLOAD, PERCENT_ENCODED_ABSOLUTE_PAYLOAD);
      case EXTENSIVE:
        return ImmutableSet.of(PERCENT_ENCODED_RELATIVE_PAYLOAD, PERCENT_ENCODED_ABSOLUTE_PAYLOAD);
    }
    return ImmutableSet.of();
  }

  enum Mode {
    QUICK,
    EXTENSIVE;
  }
}

@@ -1183,16 +1183,230 @@
 passwd%22;
+%0A  private static final String DOUBLE_PERCENT_ENCODED_RELATIVE_PAYLOAD =%0A      %22..%25252F%22.repeat(30) + %22etc%25252Fpasswd%22;%0A  private static final String DOUBLE_PERCENT_ENCODED_ABSOLUTE_PAYLOAD = %22%25252Fetc%25252Fpasswd%22;
 %0A%0A  @Ove
@@ -2552,32 +2552,45 @@
 ImmutableSet.of(
+%0A            
 PERCENT_ENCODED_
@@ -2598,33 +2598,151 @@
 ELATIVE_PAYLOAD,
- 
+%0A            PERCENT_ENCODED_ABSOLUTE_PAYLOAD,%0A            DOUBLE_PERCENT_ENCODED_RELATIVE_PAYLOAD,%0A            DOUBLE_
 PERCENT_ENCODED_

Add double-percent-encoded payloads to GenericPathTraversalDetector.