Published on

Cấu hình TCP Wrappers và Xinetd

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. <ul><li>TCP WRAPPERS and XINETD </li></ul>
  2. 2. <ul><li>TCP Wrappers and xinetd </li></ul><ul><li>Controlling access to network services is one of the most important security tasks facing a server administrator: </li></ul><ul><li>An iptables-based firewall filters out unwelcome network packets within the kernel's network stack. </li></ul><ul><li>TCP wrappers add an additional layer of protection by defining which hosts are allowed or not allowed to connect to &quot;wrapped&quot; network services. One such wrapped network service is the xinetd super server. </li></ul>
  3. 3. <ul><li>TCP Wrappers </li></ul><ul><li>When a connection attempt is made to a TCP wrapped service, the service first references the hosts access files (/ etc/hosts.allow and /etc/hosts.deny ) to determine whether or not the client host is allowed to connect. It then uses the syslog daemon ( syslogd ) to write the name of the requesting host and the requested service to / var/log/secure or / var/log/messages . </li></ul><ul><li>If a client host is allowed to connect, TCP wrappers release control of the connection to the requested service and do not interfere further with communication between the client host and the server. </li></ul><ul><li>Because TCP wrappers are a valuable addition to any server administrator's arsenal of security tools, most network services within Red Hat Linux are linked against the libwrap.a library. Some such applications include /usr/sbin/sshd , /usr/sbin/sendmail , and /usr/sbin/xinetd. </li></ul>
  4. 4. <ul><li>TCP Wrappers Configuration Files </li></ul><ul><li>To determine if a client machine is allowed to connect to a service, TCP wrappers reference the following two files, which are commonly referred to as hosts access files : </li></ul><ul><ul><li>/etc/hosts.allow </li></ul></ul><ul><ul><li>/etc/hosts.deny </li></ul></ul><ul><li>When a client request is received by a TCP wrapped service, it takes the following basic steps: </li></ul><ul><li>The service references /etc/hosts.allow . The TCP wrapped service sequentially parses the / etc/hosts.allow file and applies the first rule specified for that service. If it finds a matching rule, it allows the connection. If not, it moves on to step 2. </li></ul><ul><li>The service references /etc/hosts.deny . The TCP wrapped service sequentially parses the / etc/hosts.deny file. If it finds a matching rule is denies the connection. If not, access to the service is granted. </li></ul>
  5. 5. <ul><li>TCP Wrappers Configuration Files </li></ul><ul><li>Because access rules in hosts.allow are applied first, they take precedence over rules specified in hosts.deny . Therefore, if access to a service is allowed in hosts.allow , a rule denying access to that same service in hosts.deny is ignored. </li></ul><ul><li>Since the rules in each file are read from the top down and the first matching rule for a given service is the only one applied, the order of the rules is extremely important . </li></ul><ul><li>If no rules for the service are found in either file, or if neither file exists, access to the service is granted . </li></ul><ul><li>TCP wrapped services do not cache the rules from the hosts access files, so any changes to hosts.allow or hosts.deny take effect immediately without restarting network services . </li></ul>
  6. 6. <ul><li>Formatting Access Rules </li></ul><ul><li>The format for both /etc/hosts.allow and /etc/hosts.deny are identical. Any blank lines or lines that start with a hash mark (#) are ignored, and each rule must be on its own line. </li></ul><ul><li>Each rule uses the following basic format to control access to network services: </li></ul><ul><li>< daemonList > : < clientList > [: < option > : < option > : ... ] </li></ul><ul><li>< daemon list > — A comma separated list of process names ( not service names) or the ALL wildcard . The daemon list also accepts operators to allow greater flexibility. </li></ul><ul><li>< client list > — A comma separated list of hostnames, host IP addresses, special patterns , or special wildcards which identify the hosts effected by the rule. The client list also accepts operators to allow greater flexibility. </li></ul><ul><li>< option > — An optional action or colon separated list of actions performed when the rule is triggered. Option fields support expansions , launch shell commands, allow or deny access, and alter logging behavior. </li></ul>
  7. 7. <ul><li>Formatting Access Rules - Examples </li></ul><ul><li>The following is a basic sample hosts access rule: </li></ul><ul><li>vsftpd : . example . com </li></ul><ul><li>This rule instructs TCP wrappers to watch for connections to the FTP daemon ( vsftpd ) from any host in the domain. If this rule appears in hosts.allow , the connection will be accepted. If this rule appears in hosts.deny , the connection will be rejected. </li></ul><ul><li>The next sample hosts access rule is more complex and uses two option fields: </li></ul><ul><li>sshd : : spawn /bin/echo `/bin/date` access denied >> /var/log/sshd.log : deny </li></ul>
  8. 8. <ul><li>Wildcards and Patterns </li></ul><ul><li>The following wildcards may be used: </li></ul><ul><ul><li>ALL — Matches everything. It can be used for both the daemon list and the client list. </li></ul></ul><ul><ul><li>LOCAL — Matches any host that does not contain a period ( . ), such as localhost. </li></ul></ul><ul><li>The following is a list of the most common accepted patterns for a client list entry: </li></ul><ul><ul><li>Hostname beginning with a period (.) — Placing a period at the beginning of a hostname, matches all hosts sharing the listed components of the name. The following example would apply to any host within the domain: </li></ul></ul><ul><ul><li>ALL : </li></ul></ul><ul><ul><li>IP address ending with a period (.) — Placing a period at the end of an IP address matches all hosts sharing the initial numeric groups of an IP address. The following example would apply to any host within the 192.168.x.x network: </li></ul></ul><ul><ul><li>ALL : 192.168. </li></ul></ul>
  9. 9. <ul><li>Patterns and Operators </li></ul><ul><li>IP address/netmask pair — Netmask expressions can also be used as a pattern to control access to a particular group of IP addresses. The following example would apply to any host with an address of through : </li></ul><ul><li>ALL : </li></ul><ul><li>The asterisk (*) — Asterisks can be used to match entire groups of hostnames or IP addresses, as long as they are not mixed in a client list containing other types of patterns. The following example would apply to any host within the domain: </li></ul><ul><li>ALL : * </li></ul><ul><li>The EXCEPT operator allows specific exceptions to broader matches within the same rule. </li></ul><ul><li>ALL: EXCEPT </li></ul><ul><li>ALL EXCEPT vsftpd: 192.168.0. </li></ul>
  10. 10. <ul><li>Access Control </li></ul><ul><li>Option fields also allow administrators to explicitly allow or deny hosts in a single rule by adding the allow or deny directive as the final option. </li></ul><ul><li>For instance, the following two rules allow SSH connections from , but deny connections from : </li></ul><ul><li>sshd : : allow </li></ul><ul><li>sshd : : deny </li></ul><ul><li>By allowing access control on a per-rule basis, the option field allows administrators to consolidate all access rules into a single file: either hosts.allow or hosts.deny . Some consider this an easier way of organizing access rules. </li></ul>
  11. 11. <ul><li>Shell Commands </li></ul><ul><li>Option fields allow access rules to launch shell commands through the following two directives: </li></ul><ul><ul><li>spawn — This option directive can perform tasks to get more information about the requesting client or create special log files using the echo command. </li></ul></ul><ul><ul><li>In the following example, clients attempting to access Telnet services from the domain are quietly logged to a special file: </li></ul></ul><ul><li>in.telnetd : </li></ul><ul><ul><li>: spawn /bin/echo `/bin/date` from %h >> /var/log/telnet.log : allow </li></ul></ul><ul><ul><li>twist — This directive is often used to set up traps for intruders (also called &quot;honey pots&quot;). It can also be used to send messages to connecting clients. The twist command must occur at the end of the rule line. </li></ul></ul><ul><ul><li>In the following example, clients attempting to access FTP services from the domain are sent a message via the echo command: </li></ul></ul><ul><li>vsftpd : </li></ul><ul><ul><li>: twist /bin/echo &quot;421 Bad hacker, go away!&quot; </li></ul></ul>
  12. 12. <ul><li>Expansions </li></ul><ul><li>Expansions, when used in conjunction with the spawn and twist directives provide information about the client, server, and processes involved. </li></ul><ul><li>Below is a list of supported expansions: </li></ul><ul><ul><li>%a — The client's IP address. </li></ul></ul><ul><ul><li>%A — The server's IP address. </li></ul></ul><ul><ul><li>%d — The daemon process name. </li></ul></ul><ul><ul><li>%h — The client's hostname (or IP address, if the hostname is unavailable). </li></ul></ul><ul><ul><li>%H — The server's hostname (or IP address, if the hostname is unavailable). </li></ul></ul><ul><ul><li>%u — The client's username. If unavailable, unknown is printed. </li></ul></ul>
  13. 13. <ul><li>XINETD </li></ul><ul><li>Xinetd is a program used to start and stop a variety of Linux data communication applications. Some of these applications, such as Telnet , are installed by default in RedHat and Fedora Core Linux, others such as TFTP , need to be installed afterwards. </li></ul><ul><li>xinetd Configuration Files </li></ul><ul><ul><li>/etc/xinetd.conf — The global xinetd configuration file. </li></ul></ul><ul><ul><li>/etc/xinetd.d/ directory — The directory containing all service-specific files. </li></ul></ul>
  14. 14. <ul><li>The /etc/xinetd.d/ Directory </li></ul><ul><li>The files in the /etc/xinetd.d/ directory contains the configuration files for each service managed by xinetd and the names of the files correlate to the service. As with xinetd.conf , this file is read only when the xinetd service is started. In order for any changes to take effect, the administrator must restart the xinetd service . </li></ul><ul><li>To get an idea of how these files are structured, consider the / etc/xinetd.d/telnet file: </li></ul><ul><li>service telnet </li></ul><ul><li>{ </li></ul><ul><li>flags = REUSE </li></ul><ul><li>socket_type = stream </li></ul><ul><li>wait = no </li></ul><ul><li>user = root </li></ul><ul><li>server = /usr/sbin/in.telnetd </li></ul><ul><li>log_on_failure += USERID </li></ul><ul><li>disable = yes </li></ul><ul><li>} </li></ul>
  15. 15. <ul><li>Service Configuration File </li></ul><ul><li>service — Defines the service name, usually to match a service listed in the /etc/services file. </li></ul><ul><li>flags — Sets any of a number of attributes for the connection. REUSE instructs xinetd to reuse the socket for a Telnet connection. </li></ul><ul><li>socket_type — Sets the network socket type to stream. </li></ul><ul><li>wait — Defines whether the service is single-threaded (yes) or multi-threaded (no). </li></ul><ul><li>user — Defines what user ID the process process will run under. </li></ul><ul><li>server — Defines the binary executable to be launched. </li></ul><ul><li>log_on_failure — Defines logging parameters for log_on_failure in addition to those already defined in xinetd.conf. </li></ul><ul><li>disable — Defines whether or not the service is active. </li></ul><ul><li>only_from — Allows only the specified hosts to use the service. </li></ul><ul><li>no_access — Blocks listed hosts from using the service. no_access = </li></ul><ul><li>access_times — Specifies the time range when a particular service may be used. The time range must be stated in 24-hour format notation, HH:MM-HH:MM . </li></ul>
  16. 16. <ul><li>The TELNET server </li></ul><ul><li>Setting Up A Telnet Server To set up a Telnet server use the chkconfig command to activate Telnet. </li></ul><ul><li>root@bigboy tmp# chkconfig telnet on </li></ul><ul><li>Verifying the telnet service by netstat comand </li></ul><ul><li>root@bigboy tmp# netstat -a | grep telnet tcp 0 0 *:telnet *:* LISTEN </li></ul><ul><li>Verifying the telnet service by chkconfig --list command </li></ul><ul><li>[root@bigboy tmp]# chkconfig --list | grep telnet </li></ul><ul><li>telnet: on </li></ul><ul><li>Stopping A Telnet Server Use the chkconfig command to deactivate telnet, even after the next reboot. </li></ul><ul><li>[root@bigboy tmp]# chkconfig telnet off </li></ul>
  17. 17. <ul><li>Basic Telnet Security </li></ul><ul><li>Let Telnet Listen On Another TCP Port </li></ul><ul><li>1) Edit a /etc/services file and add an entry for a new service. Call it stelnet . </li></ul><ul><li># Local services stelnet 7777/tcp # &quot;secure&quot; telnet </li></ul><ul><li>2) Copy the telnet configuration file called /etc/xinetd.d/telnet and call it /etc/xinetd.d/stelnet : </li></ul><ul><li>[root@bigboy tmp]# cp /etc/xinetd.d/telnet /etc/xinetd.d/stelnet </li></ul><ul><li>3) Edit the new /etc/xinetd.d/stelnet file. Make the new service stelnet and add a port statement for TCP port 7777. </li></ul><ul><ul><li>service stelnet </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>flags = REUSE </li></ul></ul><ul><ul><li>socket_type = stream </li></ul></ul><ul><ul><li>wait = no </li></ul></ul><ul><ul><li>user = root </li></ul></ul><ul><ul><li>server = /usr/sbin/in.telnetd </li></ul></ul><ul><ul><li>log_on_failure += USERID </li></ul></ul><ul><ul><li>disable = no </li></ul></ul><ul><ul><li>port = 7777 </li></ul></ul><ul><ul><li>} </li></ul></ul>
  18. 18. <ul><li>Basic Telnet Security </li></ul><ul><li>4) Use chkconfig to activate stelnet. </li></ul><ul><li>[root@bigboy tmp]# chkconfig stelnet on </li></ul><ul><li>5) Check to make sure your server is now listening on port 7777 with the netstat command. </li></ul><ul><li>[root@bigboy tmp]# netstat -an | grep 777 </li></ul><ul><li>tcp 0 0* LISTEN </li></ul><ul><li>Let Telnet Allow Connections From Trusted Addresses </li></ul><ul><li>service telnet </li></ul><ul><li>{ </li></ul><ul><li>flags = REUSE </li></ul><ul><li>socket_type = stream </li></ul><ul><li>wait = no </li></ul><ul><li>user = root </li></ul><ul><li>server = /usr/sbin/in.telnetd </li></ul><ul><li>log_on_failure += USERID </li></ul><ul><li>disable = no </li></ul><ul><li>only_from = </li></ul><ul><li>} </li></ul>
  19. 19. <ul><li>The TFTP Server Software </li></ul><ul><li>Installing the TFTP Server Most RedHat and Fedora Linux software products are available in the RPM format. Downloading and installing RPMs isn't hard( the RPM's filename usually starts with the word &quot;tftp-server&quot; followed by a version number like this: tftp-server-0.33-3.i386.rpm ). </li></ul><ul><li>Configuring The TFTP Server </li></ul><ul><li>The first step is to the configuration file /etc/xinetd.d/tftp and set disable to &quot;no&quot;. service tftp { </li></ul><ul><ul><li>socket_type = dgram protocol = udp wait = yes user = root only_from = server = /usr/sbin/in.tftpd server_args = -s /tftpboot disable = no </li></ul></ul><ul><li>} </li></ul>
  20. 20. <ul><li>The TFTP Server Software </li></ul><ul><li>In this example, xinetd will only allow the TFTP server to accept connections from the router / switch / firewall with an address of You can extend this list with commas in between or just comment it out al together for global access. </li></ul><ul><li>Create a / tftpboot directory with global read write privileges [root@bigboy tmp]# chmod 777 /tftpboot </li></ul><ul><li>Restart xinetd [root@bigboy tmp]# /etc/init.d/xinetd restart Stopping xinetd: [ OK ] Starting xinetd: [ OK ] </li></ul>