PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

prefer gui firewall on linux

Go down

prefer gui firewall on linux Empty prefer gui firewall on linux

Post by jamied_uk 2nd April 2024, 10:11

prefer gui firewall on linux


Code:
sudo apt install -y gufw



Code:
sudo gufw &


example rules to allow a subnet access to a port with tcp protocol
Code:
sudo ufw allow from 192.168.2.0/24 to any port 5900

sudo ufw allow from 192.168.2.0/24 to any port 5901
jamied_uk
jamied_uk
Admin

Posts : 2951
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

prefer gui firewall on linux Empty Re: prefer gui firewall on linux

Post by jamied_uk 2nd April 2024, 12:33

start.sh (setup and loader all in 1)


Code:
#!/bin/bash
# Linux Network Menu (c) J~2024
# https://jnet.forumotion.com/t2007-prefer-gui-firewall-on-linux#3097
#
#
echo "Linux Network Menu (c) J~Net 2024"
# Check if ever ran before


# Check if setting.txt exists and is executable
if [ -f setting.txt ]; then
    echo ""
else
    echo "Decide Either UFW Or IPTABLES Method"
    echo "You can only use 1 method on a system at a time!"
    echo ""
    echo "Type 1 (default for UFW) or 2 for IPTABLES"
    echo "Remove setting.txt file to reset choice!"
    echo ""
    read -p "Enter your choice: " input
    echo "$input" > setting.txt

    if [ "$input" == "1" ] || [ "$input" == "" ]; then
        sudo apt install -y gufw
    else
        sudo apt install -y iptables-persistent
    fi
fi


# Check if /usr/bin/tcpdump exists
if [ -x /usr/bin/tcpdump ]; then
    echo ""
else
    echo "Seting Up TCP Dump"
    sudo apt install -y tcpdump
fi

if [ -x /usr/bin/nmap ]; then
    echo ""
else
    echo "Seting Up TCP NMAP"
    sudo apt install -y nmap
fi


# Read the contents of the file into a variable
var=$(<setting.txt)

# Check if the variable is empty or contains "1"
if [ -z "$var" ] || [ "$var"="1" ]; then
    # If empty or equal to "1", execute the first set of commands
    sudo python lnm.py
    # Add your commands here
else
    sudo python iptables_lnm.py
fi


lnm.py


Code:
#
# Linux Network Menu (c) J~2024
# https://jnet.forumotion.com/t2007-prefer-gui-firewall-on-linux#3097
import subprocess
import os
import json
import time

print("\033[H\033[J", end="")
print("\033[92mLinux Network Menu (c) J~Net 2024\033[0m")

# Function to enable SSH access from a specific IP
def enable_ssh_access(ip_address, port):
    subprocess.run(["sudo", "ufw", "allow", f"from {ip_address} to any port {port}"])
    log_changes("Enable SSH access", f"Allowed access from IP: {ip_address} on port {port}")

# Function to log changes to settings.log file
def log_changes(action, details):
    timestamp=time.strftime("%Y-%m-%d %H:%M:%S")
    log_entry=f"{timestamp} - {action}: {details}\n"
    with open("settings.log", "a") as log_file:
        log_file.write(log_entry)

# Function to load settings from settings.json
def load_settings():
    if os.path.exists("settings.json"):
        with open("settings.json", "r") as settings_file:
            return json.load(settings_file)
    else:
        return {"port": 22}  # Default port is 22

# Function to save settings to settings.json
def save_settings(settings):
    with open("settings.json", "w") as settings_file:
        json.dump(settings, settings_file, indent=4)

# Function to check SSH server status
def check_ssh_status():
    result=subprocess.run(["systemctl", "status", "ssh"], capture_output=True)
    return result.stdout.decode()

# Function to remove a UFW rule
def remove_ufw_rule(rule_number):
    subprocess.run(["sudo", "ufw", "delete", str(rule_number)])
    log_changes("Remove UFW rule", f"Removed rule number {rule_number}")

# Function to display submenu for additional network-related options
def display_more_menu():
    print("\n==== More Network Options ====")
    print("1. Run nmap")
    print("2. Run network sniffer")
    print("3. Show UFW status")
    print("4. Show UFW SSH rules")
    print("5. Allow SSH access from specific IP")
    print("6. Return to main menu")
    print("==============================")

# Main function
def main():
    # Create settings.log file if it doesn't exist
    if not os.path.exists("settings.log"):
        with open("settings.log", "w") as log_file:
            log_file.write("=== Settings Log ===\n")

    # Load settings
    settings=load_settings()

    while True:
        print("\n==== Main Menu ====")
        print("1. Check SSH server status")
        print("2. Enable SSH access for a specific IP")
        print("3. View allowed ports")
        print("4. Edit ufw rules")
        print("5. Change Port")
        print("6. More Network Options")
        print("7. Exit")
        print(f"Current SSH port: {settings['port']}")
        print("====================")
        choice=input("Enter your choice: ")

        if choice == "1":
            status=check_ssh_status()
            print(status)

        elif choice == "2":
            ip_address=input("Enter the IP address to allow SSH access: ")
            enable_ssh_access(ip_address, settings['port'])

        elif choice == "3":
            subprocess.run(["sudo", "ufw", "show", "added", "|", "grep", "ssh"])

        elif choice == "4":
            while True:
                print("\n==== Edit ufw Rules ====")
                print("1. Remove a rule")
                print("2. Return to main menu")
                print("==============================")
                ufw_choice=input("Enter your choice: ")
                if ufw_choice == "1":
                    rule_number=input("Enter the rule number to remove: ")
                    remove_ufw_rule(rule_number)
                elif ufw_choice == "2":
                    break
                else:
                    print("Invalid choice. Please enter a valid option.")

        elif choice == "5":
            new_port=input(f"Enter the new SSH port number (current port is {settings['port']}): ")
            settings['port']=int(new_port) if new_port else 22
            save_settings(settings)
            print(f"SSH port changed to {settings['port']}")
            log_changes("Change SSH port", f"Port changed to {settings['port']}")

        elif choice == "6":
            while True:
                display_more_menu()
                more_choice=input("Enter your choice: ")
                if more_choice == "1":
                    subprocess.run(["nmap", "-sn", "192.168.2.1/24"])
                elif more_choice == "2":
                    subprocess.run(["tcpdump", "-i", "eth0"])
                elif more_choice == "3":
                    subprocess.run(["sudo", "ufw", "status"])
                elif more_choice == "4":
                    subprocess.run(["sudo", "ufw", "show", "added", "|", "grep", "ssh"])
                elif more_choice == "5":
                    ip_address=input("Enter the IP address to allow SSH access: ")
                    enable_ssh_access(ip_address, settings['port'])
                elif more_choice == "6":
                    break
                else:
                    print("Invalid choice. Please enter a valid option.")

        elif choice == "7":
            print("Exiting program.")
            break

        else:
            print("Invalid choice. Please enter a valid option.")

if __name__ == "__main__":
    main()




iptables_lnm.py


Code:
#
# Linux Network Menu (c) J~2024
# https://jnet.forumotion.com/t2007-prefer-gui-firewall-on-linux#3097
import os
import subprocess
import json
import time

print("\033[H\033[J", end="")
print("\033[92mLinux Network Menu (c) J~Net 2024\033[0m")

print("")

# Function to save iptables rules to a file
def save_iptables_rules(file_path):
    with open(file_path, "w") as rules_file:
        subprocess.run(["iptables-save"], stdout=rules_file)
    log_changes("Save iptables rules", f"Saved iptables rules to {file_path}")



# Function to log changes to settings.log file
def log_changes(action, details):
    timestamp=time.strftime("%Y-%m-%d %H:%M:%S")
    log_entry=f"{timestamp} - {action}: {details}\n"
    with open("settings.log", "a") as log_file:
        log_file.write(log_entry)

# Function to load settings from settings.json
def load_settings():
    if os.path.exists("settings.json"):
        with open("settings.json", "r") as settings_file:
            return json.load(settings_file)
    else:
        return {"port": 22}  # Default port is 22

# Function to save settings to settings.json
def save_settings(settings):
    with open("settings.json", "w") as settings_file:
        json.dump(settings, settings_file, indent=4)

# Function to check SSH server status
def check_ssh_status():
    result=subprocess.run(["systemctl", "status", "ssh"], capture_output=True)
    return result.stdout.decode()

# Function to enable SSH access from a specific IP
def enable_ssh_access(ip_address, port):
    subprocess.run(["iptables", "-A", "INPUT", "-p", "tcp", "-s", ip_address, "--dport", str(port), "-j", "ACCEPT"])
    log_changes("Enable SSH access", f"Allowed access from IP: {ip_address} on port {port}")
    save_iptables_rules("/etc/iptables/rules.v4")

# Function to remove an iptables rule
def remove_iptables_rule(rule_number):
    subprocess.run(["iptables", "-D", "INPUT", str(rule_number)])
    log_changes("Remove iptables rule", f"Removed rule number {rule_number}")
    save_iptables_rules("/etc/iptables/rules.v4")

# Function to display submenu for additional network-related options
def display_more_menu():
    print("\n==== More Network Options ====")
    print("1. Run nmap")
    print("2. Run network sniffer")
    print("3. Show iptables rules")
    print("4. Show iptables SSH rules")
    print("5. Allow SSH access from specific IP")
    print("6. Return to main menu")
    print("==============================")

# Main function
def main():
    # Create settings.log file if it doesn't exist
    if not os.path.exists("settings.log"):
        with open("settings.log", "w") as log_file:
            log_file.write("=== Settings Log ===\n")

    # Load settings
    settings=load_settings()

    while True:
        print("\n==== Main Menu ====")
        print("1. Check SSH server status")
        print("2. Enable SSH access for a specific IP")
        print("3. View allowed ports")
        print("4. Edit iptables rules")
        print("5. Change Port")
        print("6. More Network Options")
        print("7. Exit")
        print(f"Current SSH port: {settings['port']}")
        print("====================")
        choice=input("Enter your choice: ")

        if choice == "1":
            status=check_ssh_status()
            print(status)

        elif choice == "2":
            ip_address=input("Enter the IP address to allow SSH access: ")
            enable_ssh_access(ip_address, settings['port'])

        elif choice == "3":
            subprocess.run(["sudo", "iptables", "-L"])

        elif choice == "4":
            while True:
                print("\n==== Edit iptables Rules ====")
                print("1. Remove a rule")
                print("2. Return to main menu")
                print("==============================")
                iptables_choice=input("Enter your choice: ")
                if iptables_choice == "1":
                    rule_number=input("Enter the rule number to remove: ")
                    remove_iptables_rule(rule_number)
                elif iptables_choice == "2":
                    break
                else:
                    print("Invalid choice. Please enter a valid option.")

        elif choice == "5":
            new_port=input(f"Enter the new SSH port number (current port is {settings['port']}): ")
            settings['port']=int(new_port) if new_port else 22
            save_settings(settings)
            print(f"SSH port changed to {settings['port']}")
            log_changes("Change SSH port", f"Port changed to {settings['port']}")

        elif choice == "6":
            while True:
                display_more_menu()
                more_choice=input("Enter your choice: ")
                if more_choice == "1":
                    subprocess.run(["nmap", "-sn", "192.168.2.1/24"])
                elif more_choice == "2":
                    subprocess.run(["tcpdump", "-i", "eth0"])
                elif more_choice == "3":
                    subprocess.run(["sudo", "iptables", "-L"])
                elif more_choice == "4":
                    subprocess.run(["sudo", "iptables", "-L", "|", "grep", "tcp", "|", "grep", "dpt:ssh"])
                elif more_choice == "5":
                    ip_address=input("Enter the IP address to allow SSH access: ")
                    enable_ssh_access(ip_address, settings['port'])
                elif more_choice == "6":
                    break
                else:
                    print("Invalid choice. Please enter a valid option.")

        elif choice == "7":
            print("Exiting program.")
            break

        else:
            print("Invalid choice. Please enter a valid option.")

if __name__ == "__main__":
    main()
jamied_uk
jamied_uk
Admin

Posts : 2951
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum