如何使用apache禁止请求特定目录的IP地址

问题描述

您好,我想永久禁止任何请求目录“/backups/”的内容,现在我该如何使用 PHP 脚本或 mod_security 执行此操作?

如果可能的话,我想以类似于 iptables 的方式来做到这一点。

解决方法

我自己想出了如何做到这一点。

在 /bin 中创建一个名为“blockip”的脚本,包含以下内容

public class Lab2 {
    static String code;
    static String num;
    int year;
    static int sem;

    public static void main(String[] args) {
        info();
        num();
        sem();
        System.out.println(num + " " + sem + " " + code);
    }

    public static void info() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Course ID Generator\n--------------------------");
        System.out.println("Please Enter Course Information:");
        String message = "Initial";
        String code = "";

        while (!message.isEmpty()) {
            if (!message.equals("Initial")) {
                System.out.println(message);
            }

            System.out.print("Course Code (only capital letters " + "with a length between 2 and 4): ");
            code = sc.nextLine();

            message = checkLength(code);
            if (message.isEmpty()) {
                message = checkUpperCase(code);
            }
        }

    }

    private static String checkLength(String code) {
        String output = "";
        if (code.length() < 2 || code.length() > 4) {
            output = "Course Code length was not between " + "2 and 4,Please try again";
        }
        return output;
    }

    private static String checkUpperCase(String code) {
        String output = "";
        for (int i = 0; i < code.length(); i++) {
            char ch = code.charAt(i);
            if (Character.isAlphabetic(ch) && Character.isUpperCase(ch)) {
            } else {
                output = "Course Code must only have capital " + "letters,please try again";
                break;
            }
        }
        return output;
    }

    public static void num() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("Course Number (consists of only 3 digits): ");
            num = sc.nextLine();

            if (num.length() == 3) {
                break;
            } else {
                System.out.println("Course Number length was not 3,please try again");
            }
            sc.close();
        }

    }

    public static void sem() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("Semester (Fall=01,Sprint=02,Summer=03):");
            sem = sc.nextInt();

            if (sem > 0 && sem < 4) {
                break;
            } else {
                System.out.println("Please enter correct semester code,try again");
            }
        }

        sc.close();
    }

}

然后

if [ -z "$1" ]
  then
    echo "No argument supplied"
    exit 1
fi
echo "Blocking IP address $1"
iptables -A INPUT -s $1 -j DROP

现在运行

chmod +x /bin/blockip

添加:

sudo visudo

确保它是 chmod 755

并将此代码添加到目录中的 index.php 中。

nobody ALL = NOPASSWD: /your/script