CentOS5.5 system uses iptables to filter access domain names
Source: Internet
Author: User
CentOS through string matching domain name to filter, example: iptables-IOUTPUT-ptcp-mstring--stringqq.com--algobm-jDROPiptables-IOUTPUT-pudp-mstring--stringqq.com -- algobm-jDROP so that it can not access the QQ-related business, but CentOS through string matching domain name to filter, example:
Iptables-I OUTPUT-p tcp-m string -- string "qq.com" -- algo bm-j DROP
Iptables-I OUTPUT-p udp-m string -- string "qq.com" -- algo bm-j DROP
In this way, you will not be able to access QQ-related services, but the proxy seems to be okay.
For details, refer:
Iptables String Matching: A way to Enhanced ServerSecurity and Advanced Firewalling Introduction.When it comes to any server or network connected to theinternet, security from malicious files and hack attempts is a bigconcern for any administrator. linux provides its own firewall fromthe early releases itself. the current iptables firewall maintainedby the netfilter team is Cing to more powerful security andnetwork management tool with the recent releases. it will be a topicof interest Any linux based server/network administrator. thisarticle covers valid tive configuration and optimization of theiptables firewall system in 2.6.x kernels in order to define defend against TCP attacks and to drop unwanted packetswithout messing them up with your business critical services !!.
Kernels from 2.6 include support for matching strings present inIP packets, inspecting the entire packet data. earlier kernelssupported matching at the IP header level only, which was alimitation as the rules can only be formed based on header valueslike IP addresses, ports, packet state etc. the netfilter iptablesfirewall system has undergone great advancements in the latestkernels, with the modified string matching option being moreinteresting among them for server administrators. the rules, basedon string matching functions, are very easy to implement. thisguide intends to introduce the method to users with a basicunderstanding of networking and iptables.
System Requirements.Preferred kernel version: 2.6.18 or later.
The iptables program (1.3.5 or later) installed on yourmachine.
The kernel shoshould be compiled with string matching support. Todo this, the following line shoshould be added to . ConfigFile prior to compiling the kernel: CONFIG_NETFILTER_XT_MATCH_STRING = m
If you use a pre-complied kernel, check for this option in ConfigFile with the appropriate version of the kernel inyour /BootDirectory. If present, it means that netfilterstring matching is compiled as a module. Make sure that the moduleis loaded (using LsmodCommand or by looking for theappropriate entry in /Proc/modulesFile ). the minimumrequirement is a 2.6.14 kernel, however it is a bit difficult toget it working on kernels before 2.6.18. customising the kernel andiptables is required in that case. suggested configuration optionsfor older kernels can be detailed in another article, if there isdemand.
Formating the Rules.If iptables is installed with string matching support, its manpage can be found with the following command:
Iptables-m string-help
Here is the relevant section from the iptables (8) man page:
String
This modules matches a given string by using some
Pattern matching strategy. It requires a linux kernel> = 2.6.14.
-- Algo bm | kmp
Select the pattern matching strategy. (bm = Boyer-
Moore, kmp = Knuth-Pratt-Morris)
-- From offset
Set the offset from which it starts looking for any
Matching. If not passed, default is 0.
-- To offset
Set the offset to which it starts looking for any
Matching. If not passed, default is
Packet size.
-- String pattern
Matches the given pattern. -- hex-string pattern
Matches the given pattern in hex notation.
In iptables 1.3.5, you need to specify the algorithm to use forstring matching using? Algo option. we can limit the search byspecifying the offset values as well. there are two algorithms thatcan be used, Boyer-Moore and Knuth-Morris-Pratt. more informationregarding these algorithms can be found at Wikipedia? ForBoyer-Moore the URL is:
Http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm
And for Knuth-Morris-Pratt the URL is:
Http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
Boyer-Moore is efficient and fast and is preferable in mostcases.
Common Applications and Useful Example Rules:1) Toprevent an intrusion attempt.In case, when a suspecious URL upload using the webserver isdetected, You cocould frame similar rules as follows.
Iptables-I INPUT 1-p tcp -- dport 80-m string -- string "cmd.exe" -- algo bm-j DROP
The rule blocks all packets to port 80 containing the stringcmd.exe. Mod_security is an option for the same, but it can be anoverload to your busy webservers.
2) To defend DDOS to a service.It is a common case where we need to drop requests to a domainURL when it is under DDOS. mod_dosevasive is an option, but itreally overloads the webserver. string matching option can beutilized here without overloading the webserver.
The rule, blocks all web requests to domain.com. These rules canalso be used in conjunction with other iptables matches and optionsdepending on what is required.
3) To Defend against E-mail Spoofing.We can make use of the string matching option in numerous casesto drop intruder and spam packets before they enter the server. another instance for example is, if the mail server is wrongly inged spoofed e-mails with a common 'subobject '. if the spammer isusing a unique IP address, it is very easy to block him using ring, conventional IptablesRules etc. but when the spammer isusing different IP addresses, it makes things difficult for theadministrator. in such a case, the following string based rule canbe added to the firewall so that the mail server will not getoverloaded by the spoofed mails.
Iptables-I INPUT-p tcp -- dport 25-m string -- string "Subject" -- algo bm-j DROP
** Do it now with an optimised rule!The same rule might be modified to one with less overhead (thatis, it uses less resources) by limiting the search specifyingoffset values, and by assuming that the SMTP subject header will bewithin an offset limit of 15000 in the packet.
Iptables-I INPUT-p tcp -- dport 25-m string -- string "Subject" -- algo bm -- to 15000-j DROP
4) Other general cases.Apart from the instances discussed above, you can make use ofthe string matching options, wherever you need to manage thepackets entering a server or network, based on strings like URLs, file names, file contents etc.
Conclusion.The string matching option can be specified tively utilized wherevera network needs to be filtered using strings. we can block thepackets right at the kernel level itself without overloading yourserver applications. however, there is a higher overhead involvedfor the kernel with string matching, compared to other ordinaryiptables matchings. offset limits shocould be specified for searchingwherever possible in order to reduce this overhead.
About me:I am Krishnakumar, Freelance Linux administrator and securityexpert for DC's and webhosts. You can reach me atkrishnakumarmec99 [at] gmail [dot] com
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service