How to make a firewall in Python?
Firewall:
The firewall is a network which controls the incoming and outgoing network traffics of a monitor. It blocks the dataset based on the set of rules written in the security rules of the monitor. The firewall acts as a barrier between the internal components or datasets from the external sources that harm the monitor, such as viruses and hackers. The firewalls act in the same way as the filters. We know that the filters allow only the required frequencies and block unwanted frequencies in the same way the firewall analyzes all the incoming traffic signals based on the set of predefined rules and allows only particular signals and blocks all the harmful signals.
For example:
Let us consider all the IP addresses as the school and all the classrooms as the port numbers. All the students of the particular school are allowed into the school based on their uniforms, and further, the students are filtered based on their standards. They are sent into their respective classrooms the same way the IP addresses and the port numbers act as the school and the classrooms.
Firewalls are divided into mainly three types:
- Packet-filtering firewalls
- Next-generation firewalls
- Proxy firewalls
Packet-filtering firewalls:
The packet-filtering firewalls receive the port numbers in the form of packets; if these packets pass a set of security instructions, then it is passed into the destination IP address, and if the packet does not pass the security rules, then it is not allowed to pass through.
Next-generation firewalls:
These firewalls will add additional technology to the existing firewall technology, such as encrypted and anti-virus. The next-generation firewalls have a technology of deep packet tracking; it analyses the data inside the packet itself and allows the packets if it passes all the set of instructions.
Proxy firewalls:
This firewall acts as a filter at the application level. The proxy acts as an intermediate between the system and external software. The client sends the request to the firewall and allows the packet if it passes all the set of rules and blocks if it does not pass the instructions.
Creating firewall using python:
The python allows the creation of the firewall with the help of nfqueue to perform the firewall operations easily. The firewall algorithm can be written with the help of the python programming language.
Creating Our Firewall:
First, we need to secure our firewall, and then we need to assign the IP addresses and the firewall zones to our network. Now we can configure the access controls; then, we need to configure our other firewalls and then we need to log in to them. Finally, we need to test our firewall configurations.
Enabling python in our firewall:
- Go to windows settings
- Then open the VPN option and then to network protection and firewall.
- Search for the python and mark the URLs as public and private.
Creating a simple Firewall:
- First, we need to create a new rule to create a firewall click on the toolbar and open the New Firewall Rule.
- Now with the help of the file explorer, import the new rule using the XML from the left side of the window.
- We can also edit the rules by clicking the property next to the rule, and we can edit it.
Writing Firewall in python
- Open the sudo system and type iptables –I INPUT –d -192.165.0.0/23 to get our system data
Example:
desktop: iptables -L
Output:
Chain INPUT (policy ACCEPT)
target propt opt source destination
Chain FORWARD (policy ACCEPT)
target propt opt source destination
Chain OUTPUT (policy ACCEPT)
target propt opt source destination
Command to create the firewall configuration
desktop: mkdir /pyp/fw
Command to save the existing iptables
iptables-save > fw-stop
Now input the values to the existing firewall
Commands:
iptables -A INPUT -p TCP - - dport 40 -j ACCEPT
iptables -A INPUT -p TCP - - dport 62 -j ACCEPT
iptables -A INPUT -p TCP - - dport 72 -j ACCEPT
Now we need to save the values in the firewall
iptables-save > fw-stop
Command to start the firewall:
./fw start
Starting the firewall
- The JSON file consists of the rules, and we can store the IP address
- Finally, we can execute the firewall.py using python3.
We have successfully created the firewall in our monitor using python; the firewall helps in accepting the wanted IP address and rejects the harmful IP address or protects our computer from viruses.