0

I made an app on linux with python to install iptable rules but it gives a run time error when i click the install button 4 Bash line 1 :(command name) command not found

I tried running with sudo, logging as root to execute the code ,shebang header for pyhon environment but all give the same error

import subprocess import tkinter as tk from tkinter import messagebox

import subprocess
import tkinter as tk
from tkinter import messagebox

def ignore_ICMP():
    print("Installing ICMP Ignore")
    subprocess.run("echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts", shell=True)
    subprocess.run("echo '** Kernel: Setting parameter: icmp_echo_ignore_broadcasts -> true'", shell=True)
    subprocess.run("echo 0 > /proc/sys/net/ipv4/conf/all/accept_redirects", shell=True)
    subprocess.run("echo '** Kernel: Setting parameter: accept_redirects -> false'", shell=True)
    subprocess.run("iptables -t mangle -A PREROUTING -p icmp -j DROP", shell=True)
    subprocess.run("echo '** IPTables: Setting rule: -t mangle -A PREROUTING -p icmp -> DROP'", shell=True)

def drop_routed_packets():
    print("Installing Drop source routed packets")
    subprocess.run("echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route", shell=True)
    subprocess.run("echo '** Kernel: Setting parameter: accept_source_route -> false'", shell=True)

def tcp_syn_cookies():
    print("Installing TCP Syn cookies")
    subprocess.run("sysctl -w net.ipv4.tcp_syncookies=1", shell=True)
    subprocess.run("echo '** Kernel: Setting parameter: tcp_syncookies -> true'", shell=True)

def tcp_syn_backlog():
    print("Increasing TCP Syn Backlog")
    subprocess.run("echo 2048 > /proc/sys/net/ipv4/tcp_max_syn_backlog", shell=True)
    subprocess.run("echo '** Kernel: Setting parameter: tcp_max_syn_backlog -> 2048'", shell=True)

def tcp_syn_ack():
    print("Decreasing TCP Syn-Ack Retries")
    subprocess.run("echo 3 > /proc/sys/net/ipv4/tcp_synack_retries", shell=True)
    subprocess.run("echo '** Kernel: Setting parameter: tcp_synack_retries -> 3'", shell=True)

def ip_spoof():
    print("Enabling Address Spoofing Protection")
    subprocess.run("echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter", shell=True)
    subprocess.run("echo '** Kernel: Setting parameter: rp_filter -> true'", shell=True)

def disable_syn_packet_track():
    print("Disabling SYN Packet Track")
    subprocess.run("sysctl -w net.netfilter.nf_conntrack_tcp_loose=0", shell=True)
    subprocess.run("echo '** Kernel: Setting parameter: nf_conntrack_tcp_loose -> false'", shell=True)

def drop_invalid_packets():
    print("Installing invalid packet drop")
    subprocess.run("iptables -A INPUT -m state --state INVALID -j DROP", shell=True)
    subprocess.run("echo '** IPTables: Setting rule: -A INPUT -m state INVALID -j DROP'", shell=True)

def bogus_tcp_flags():
    print("Installing Bogus TCP Flags")
    subprocess.run("iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP", shell=True)
    subprocess.run("echo '** IPTables: Setting rule: -t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -> DROP'", shell=True)


class App:
    def __init__(self, master):
        self.master = master
        master.title("Security Module Installer")

        self.modules = [
            {"name": "ICMP Ignore", "command": "ignore_ICMP"},
            {"name": "Drop Routed Packets", "command": "drop_routed_packets"},
            {"name": "TCP Syn Cookies", "command": "tcp_syn_cookies"},
            {"name": "TCP Syn Backlog", "command": "tcp_syn_backlog"},
            {"name": "TCP Syn-Ack Retries", "command": "tcp_syn_ack"},
            {"name": "Address Spoofing Protection", "command": "ip_spoof"},
            {"name": "Disable SYN Packet Track", "command": "disable_syn_packet_track"},
            {"name": "Invalid Packet Drop", "command": "drop_invalid_packets"},
            {"name": "Bogus TCP Flags", "command": "bogus_tcp_flags"}
        ]

        self.selected_modules = []

        self.module_listbox = tk.Listbox(master, selectmode=tk.MULTIPLE)
        for module in self.modules:
            self.module_listbox.insert(tk.END, module["name"])
        self.module_listbox.pack()

        self.install_button = tk.Button(master, text="Install", command=self.install_modules)
        self.install_button.pack()

    def install_modules(self):
        selected_indices = self.module_listbox.curselection()
        for index in selected_indices:
            module = self.modules[index]
            self.selected_modules.append(module["command"])
            subprocess.run(["sudo", "bash", "-c", f"{module['command']}"])
        if self.selected_modules:
            message = "The following modules have been installed:\n\n" + "\n".join(self.selected_modules)
            tk.messagebox.showinfo("Installation Complete", message)
        else:
            tk.messagebox.showerror("Error", "No modules were selected.")

root = tk.Tk()
app = App(root)
root.mainloop()

  • 1
    Take a look at https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files. You don't need the `echo`s. – Diego Torres Milano Mar 18 '23 at 06:44
  • Oh my god... This is not Python **at all**. I would suggest to read a begginer's book into Python (usually a single evening is enough to start) and make it really Python code. Btw, SO has tons and zillions answers on the topic _"How to achieve smth. in Python"_. – 0andriy Mar 20 '23 at 13:58
  • Might as well write a shell script only for this task. any which ways its calling Linux command to do the job. – Suchandra T Apr 03 '23 at 08:36

0 Answers0