Implement dynamic firewall using iptable
Author: Daniel Robbins (drobbins@gentoo.org) Compile: ideal
And by this script can also prevent errors that are easily occurring when manually adding rules. Therefore, blocking BOB attack will become determined to determine its attack source address. Then pass the following command: # ipdrop 129.24.8.1 on
IP 129.24.8.1 Drop ON.
IPDROP scripts will immediately block 129.24.8.1. By using this script, you can significantly improve your defense capabilities. Below is the implementation of IPDROP scripts: The ipdrop Bash Script
#! / bin / bash
Source /usr/local/share/dynfw.sh
Args 2 $ # "$ {0} ipaddr {on / off}" "Drops Packets to / from ipaddr. Good for Obnoxious Networks / Hosts / DOS"
IF ["$ 2" == "on"]
THEN
#rules will be appended or inserted as Normal
Append = "- a"
INSERT = "- i"
REC_CHECK IPDROP $ 1 "$ 1 already blocked" on
Record IPDROP $ 1
Elif ["$ 2" == "OFF"]
THEN
#rules will be deleted inStead
Append = "- d"
INSERT = "- D"
REC_CHECK IPDROP $ 1 "$ 1 not currently blocked" off "OFF
Unrecord IPDROP $ 1
Else
Echo "error: /" OFF / "OR /" ON / "Expected as Second Argument"
EXIT 1
Fi
#Block Outside IP Address That's Causeing Problems
# Attacker's incoming TCP Connections Will Take a Minute or So Time OUT,
#reducing dos effect.
Iptables $ INSERT INPUT -S $ 1 -J DROP
iptables $ INSERT OUTPUT -D $ 1 -J DROP
Iptables $ INSERT Forward -D $ 1 -J Drop
Iptables $ INSERT Forward-$ 1 -J Drop
Echo "ip $ {1} Drop $ {2}."
IPDROP: Interpretation of the last four lines of content from the above script source code can see the actual command is inserted into the appropriate rule in the firewall table. It can be seen that the value of the $ INSERT variable depends on the "ON" or "OFF" mode in the command line parameter. The specific rule will be properly inserted or deleted when the IPTables line is executed. Now let's take a look at these rules itself, they can play with any type of firewall, even on systems that do not deploy firewalls. The required conditions are merely the kernel that supports the Linux2.4 version of iptables. We blocked the attack datagram from malicious IP (first iptables statement), blocking the datagram to malicious attack IP (second iptables statement), and turn off the data forwarding of the IP (last two iptables tools ). Once these rules play a role system, any datagram that meets these conditions will be discarded. Another thing to note is that "REC_CHECK", "Unrecord", "Record", and "Args" are called in the script. These are special Bash functions defined in "DynfW.sh". "Record" function implements the IP record that will be blocked in the file /root/.dynfw-ipdrop file, and "unrecord" is removed from the file / ROOT /.DYNFW-IPDROP. The "REC_CHECK" function is to output an error message when it is discovered to re-block a blocked IP address or cancel a certain IP address that has not been blocked and stops the script execution. "ARGS" function implements the correctness of the command line parameter and implements the print script help command. Document DYNFW-1.0.tar.gz contains all of these tools, see the final resource section of the article. TCPLIMIT If you need to limit a particular TCP-based network service (for example, when a serious load is generated on the end system), the TCPLIMIT script can help you achieve this, this script uses TCP ports, one rate value And "on" or "OFF" as parameters: # tcplimit 873 5 minute on
Port 873 New Connection Limit (5 / minute, Burst = 5).
TCPLIMIT uses iptables' "State" modules (which should be made to open this option or load module in the kernel) to implement only specific number of connection requests in a certain period of time. In this example, the firewall will limit only 5 newly connected to my RSYNC server per minute (Port 873). Of course, you can choose timeout / hour / hour as needed. TCPLimit provides a very good way to limit the use of non-critical services - such a large number of non-critical data does not destroy the server. TCPLimit is used in the example above to set the limitations of RSYNC to prevent Tsync data from occupying all bandwidths of the Internet connection. Where connection service restriction information is recorded in file / ROOT /.DYNFW-TCPLIMIT. If you want to close this limit, you only need to type the following command:
# TCPLIMIT 873 5 minute off
Port 873 New Connection Limit OFF.
TCPLIMIT is achieved by creating a new rule chain in the Filter table. This new rule chain will reject all datagrams that exceed the specified restriction, and insert a rule into the INPUT rule chain, which will all go to the target port (873 port in this case) to this New rule chain. The new rules chain only affects new over-limit connectors without affecting established connections. When TCPLIMIT definitions are turned off, the Input rules and new rules will be deleted. As IPDROP, TCPLIMIT can work with any type of firewall. Host-tcplimit host-tcplimit and tcplimit are very similar, but it is limited to the number of TCP connections from a particular port on a server from a specific IP. Host-Tcplimit is very useful when preventing a particular person from abuse your network resources. For example, you maintain a CVS server, one day, suddenly found a special new developer appeared, he seems to have built a script to update its resources every ten minutes. Take a large number of network resources. Then you send him a letter to indicate the mistake of his behavior. But you receive the following reply: hi guys!
I'm real excited to be part of your development project. I Just Set Up A
Script to Update My Local Copy of The Code Every Ten Minutes. I'm About To
Leave ON A Two-Week Cruise, But When I Get Back, My Sources Will Be Totally
Up-to-date and i'll be ready to help out! I'm heading out the door now ... See
You in Two weeks!
Sincerely,
Mr. Newbie
For this case, you can solve the problem with Host-TCPLimit:
# host-tcplimit 1.1.1.1 2401 1 day on
Mr. NEWBIE (IP address is 1.1.1.1) is limited to only a CVS connection per day to save network bandwidth. User-Outblock last, is also the most interesting in these firewall scripts is User-Outblock. This script provides an ideal way to implement a user to log in to the system through SSH or Telnet but not allowing it to create an outward connection through the command line command. Below is an example occasion of app USER-OUTBLOCK. Suppose a special family has an account in our ISP. Mom and Dad use the graphical Email client program to read their own letters, occasionally surf the Internet, but their son is a keen Hacker molecule, and he often uses its shell access to make some naughty for other machines. thing. One day you found him with a number of systems to establish an SSH connection, and found that the target address belongs to the Pakistani military website. You want to help this child to go forward, so you have taken the following action: First, you check your own system and make sure you remove the Suid bit of all and network-related programs, such as SSH:
# chmod u-s / usr / bin / ssh
Now he attempts to use any and network-related processes will have its own UID. You can now use user-outblock to block outward TCP connections issued by this UID (assuming its UID 2049):
# User-outblock 2049 on
Uid 2049 block on.
Now he can only log in to the system to read his own letters, but he can't use your server to establish an SSH connection. Resource
Since dynamic these firewall scripts are found to be very useful, they package them (DYNFW-1.0.Tar.gz) for download installation. To install, you only need to decompress the package and run the install.sh file. This script will install a shared Bash script as /usr/local/share/Dynfw.sh, and install the dynamic firewall script to / usr / local / sbin directory. If you want to install in other scripts, you only need to perform the latest version of DynfW before performing install.sh: # export prefix = / usr.
TCPDUMP is a very important detection tool for the underlying IP report, using it to verify that the firewall is working properly.