Home » , , » Using Secure Shell (SSH) for Remote Logins

Using Secure Shell (SSH) for Remote Logins

Written By Sajib Barua on Monday, August 20, 2012 | 10:02 AM

Linux comes with the Open Secure Shell (OpenSSH) software, a suite of programs that provides a secure replacement for the Berkeley r commands: rlogin (remote login), rsh (remote shell), and rcp (remote copy). OpenSSH uses public key cryptography to authenticate users and to encrypt the communication between two hosts, so users can securely log in from remote systems and copy files securely.
This section briefly describes how to use the OpenSSH software in Linux. To find out more about OpenSSH and read the latest news about it, visit www. openssh.com or www.openssh.org.
The OpenSSH software is installed during Linux installation. Table 2-2 lists the main components of the OpenSSH software.
Table 2-2
Components of the OpenSSH Software
Component
Description
/usr/sbin/sshd
This Secure Shell daemon must run on a host if you want users on remote systems to use the ssh client to log in securely. When a connection from the ssh client arrives, sshd performs authentication using public key cryptography and establishes an encrypted communication link with the ssh client.
/usr/bin/ssh
Users can run this Secure Shell client to log in to a host that is running sshd. Users can also use ssh to execute a command on another host.
/usr/bin/slogin
This component is a symbolic link to /usr/bin/ssh
/usr/bin/scp
This secure-copy program works like rcp but securely. The scp program uses ssh for data transfer and provides the same authentication and security as ssh.
/usr/bin/ssh-keygen
You use this program to generate the public and private key pairs you need for the public key cryptography used in OpenSSH. The ssh-keygen program can generate key pairs for both RSA and DSA (Digital Signature Algorithm) authentication. (RSA comes from the initial of the last Book VI name of Ron Rivest, Adi Shamir, and Leonard Adleman Chapter 2 the developers of the RSA algorithm.)
/etc/ssh/sshd_config
This configuration file for the sshd server specifies many parameters for sshd, including the port to listen to, the protocol to use, and the location of other files. (There are two versions of SSH protocols: SSH1 and SSH2, both supported by OpenSSH.)
/etc/ssh/ ssh_config
This configuration file is for the ssh client. Each user can also have an ssh configuration file named config in the .ssh subdirectory of the users home directory.
OpenSSH uses public key encryption, in which the sender and receiver both have a pair of keys — a public key and a private key. The public keys are freely distributed, and each party knows the other’s public key. The sender encrypts data by using the recipient’s public key. Only the recipient’s private key can then decrypt the data.
To use OpenSSH, you first need to start the sshd server and then generate the host keys. Here’s how:
  • If you want to support SSH-based remote logins on a host, start the sshd server on your system. Type ps ax | grep sshd to see if the server is already running. If not, log in as root and turn on the SSH service.
    In Fedora and SUSE, type chkconfig --level 35 sshd on. In Debian and Xandros, type update-rc.d ssh defaults. To start the sshd server immediately, type /etc/init.d/ssh start in Debian and Xandros, or type /etc/init.d/sshd start in Fedora and SUSE.
  • Generate the host keys with the following command:
    ssh-keygen -d -f /etc/ssh/ssh_host_key -N ‘’
    The -d flag causes the ssh-keygen program to generate DSA keys, which the SSH2 protocol uses. If you see a message saying that the file /etc/ssh/ssh_host_key already exists, that means that the key pairs were generated during Linux installation. You can use the existing file without having to regenerate the keys.

A user who wants to log in using SSH can simply use the ssh command. For example:
ssh 192.168.0.4 -l kdulaney
where 192.168.0.4 is the IP address of the other Linux system. SSH then displays a message:
The authenticity of host ‘192.168.0.4 (192.168.0.4)’ can’t be established.
RSA key fingerprint is 7b:79:f2:dd:8c:54:00:a6:94:ec:fa:8e:7f:c9:ad:66.
Are you sure you want to continue connecting (yes/no)?
Type yes and press Enter. SSH then adds the host to its list of known hosts and prompts you for a password on the other Linux system:
kdulaney@192.168.0.4’s password:
After entering the password, you have a secure login session with that system. You can also log in to this account with the following equivalent command:
ssh kdulaney@192.168.0.4
If you simply want to copy a file securely from another system on the LAN (identified by its IP address, 192.168.0.4), you can use scp like this:
scp 192.168.0.4:/etc/X11/xorg.conf
This command prompts for a password and securely copies the /etc/X11/ xorg.conf file from the 192.168.0.4 host to the system from which the scp command was typed, as follows:
kdulaney@192.168.0.4’s password: (type the password.)
xorg.conf 100% 2814 2.8KB/s 00:00

Setting Up Simple Firewalls
A firewall is a network device or host with two or more network interfaces — one connected to the protected internal network and the other connected to unprotected networks, such as the Internet. The firewall controls access to and from the protected internal network.
If you connect an internal network directly to the Internet, you have to make sure that every system on the internal network is properly secured — which can be nearly impossible because a single careless user can render the entire internal network vulnerable. A firewall is a single point of connection to the Internet: You can direct all your efforts toward making that firewall system a daunting barrier to unauthorized external users. Essentially, a firewall is like a protective fence that keeps unwanted external data and software out and sensitive internal data and software in. (See Figure 2-3.)
A firewall protects hosts on a private network from the Internet
Figure 2-3: A firewall protects hosts on a private network from the Internet.
The firewall runs software that examines the network packets arriving at its network interfaces and takes appropriate action based on a set of rules. The idea is to define these rules so that they allow only authorized network traffic to flow between the two interfaces. Configuring the firewall involves setting up the rules properly. A configuration strategy is to reject all network traffic and then enable only a limited set of network packets to go through the firewall. The authorized network traffic would include the connections necessary to enable internal users to do things such as visit Web sites and receive electronic mail.
To be useful, a firewall has the following general characteristics:
  • It must control the flow of packets between the Internet and the internal network.
  • It must not provide dynamic routing because dynamic routing tables are subject to route spoofing — the use of fake routes by intruders. Instead, the firewall uses static routing tables (which you can set up with the route command on Linux systems).
  • It must not allow any external user to log in as root. That way, even if the firewall system is compromised, the intruder is blocked from using root privileges from a remote login.
  • It must be kept in a physically secure location.
  • It must distinguish between packets that come from the Internet and packets that come from the internal protected network. This feature allows the firewall to reject packets that come from the Internet but have the IP address of a trusted system on the internal network.
  • It acts as the SMTP mail gateway for the internal network. Set up the sendmail software so that all outgoing mail appears to come from the firewall system.
  • Its user accounts are limited to a few user accounts for those internal users who need access to external systems. External users who need access to the internal network should use SSH for remote login (see “Using Secure Shell (SSH) for Remote Logins,” earlier in this chapter).
  • It keeps a log of all system activities, such as successful and unsuccessful login attempts.
  • It provides DNS name-lookup service to the outside world to resolve any hostnames that are known to the outside world.
  • It provides good performance so that it doesn’t hinder the internal users’ access to specific Internet services (such as HTTP and FTP).
A firewall can take many different forms. Here are three common forms of a firewall:
  • Packet filter firewall: This simple firewall uses a router capable of filtering (blocking or allowing) packets according to a number of their characteristics, including the source and destination IP addresses, the network protocol (TCP or UDP), and the source and destination port numbers. Packet filter firewalls are usually placed at the outermost boundary with an untrusted network, and they form the first line of defense. An example of a packet filter firewall is a network router that employs filter rules to screen network traffic.
    Packet filter firewalls are fast and flexible, but they can’t prevent attacks that exploit application-specific vulnerabilities or functions. They can log only a minimal amount of information, such as source IP address, destination IP address, and traffic type. Also, they’re vulnerable to attacks and exploits that take advantage of flaws within the TCP/IP protocol, such as IP address spoofing, which involves altering the address information in network packets to make them appear to come from a trusted IP address.
  • Stateful inspection firewall: This type of firewall keeps track of the network connections that network applications are using. When an application on an internal system uses a network connection to create a session with a remote system, a port is also opened on the internal system. This port receives network traffic from the remote system. For successful connections, packet filter firewalls must permit incoming packets from the remote system. Opening up many ports to incoming traffic creates a risk of intrusion by unauthorized users who abuse the expected conventions of network protocols such as TCP. Stateful inspection firewalls solve this problem by creating a table of outbound network connections, along with each session’s corresponding internal port. This “state table” is then used to validate any inbound packets. This stateful inspection is more secure than a packet filter because it tracks internal ports individually rather than opening all internal ports for external access.
  • Application-proxy gateway firewall: This firewall acts as an intermediary between internal applications that attempt to communicate with external servers such as a Web server. For example, a Web proxy receives requests for external Web pages from Web browser clients running inside the firewall and relays them to the exterior Web server as though the firewall was the requesting Web client. The external Web server responds to the firewall, and the firewall forwards the response to the inside client as though the firewall was the Web server. No direct network connection is ever made from the inside client host to the external Web server.
    Application-proxy gateway firewalls have some advantages over packet filter firewalls and stateful inspection firewalls. First, application-proxy gateway firewalls examine the entire network packet rather than only the network addresses and ports. This enables these firewalls to provide more extensive logging capabilities than packet filters or stateful inspection firewalls. Another advantage is that application-proxy gateway firewalls can authenticate users directly whereas packet filter firewalls and stateful inspection firewalls normally authenticate users based on the IP address of the system (that is, source, destination, and protocol type). Given that network addresses can be easily spoofed, the authentication capabilities of application-proxy gateway firewalls are superior to those found in packet filter and stateful inspection firewalls.The advanced functionality of application-proxy gateway firewalls, however, results in some disadvantages when compared with packet filter or stateful inspection firewalls. First, because of the full packet awareness found in application-proxy gateways, the firewall is forced to spend significant time reading and interpreting each packet. Therefore, application-proxy gateway firewalls are generally not well suited to highbandwidth or real-time applications. To reduce the load on the firewall, a dedicated proxy server can be used to secure less time-sensitive services, such as e-mail and most Web traffic. Another disadvantage is that application-proxy gateway firewalls are often limited in terms of support for new network applications and protocols. An individual applicationspecific proxy agent is required for each type of network traffic that needs to go through the firewall. Most vendors of application-proxy gateways provide generic proxy agents to support undefined network protocols or applications. However, those generic agents tend to negate many of the strengths of the application-proxy gateway architecture, and they simply allow traffic to tunnel through the firewall.
Most firewalls implement a combination of these firewall functionalities. For example, many vendors of packet filter firewalls or stateful inspection firewalls have also implemented basic application-proxy functionality to offset some of the weaknesses associated with their firewalls. In most cases, these vendors implement application proxies to provide better logging of network traffic and stronger user authentication. Nearly all major firewall vendors have introduced multiple firewall functions into their products in some manner.
In a large organization, you may also have to isolate smaller internal networks from the corporate network. You can set up such internal firewalls the same way that you set up Internet firewalls.
Using NAT
Network Address Translation (NAT) is an effective tool that enables you to hide the network addresses of an internal network behind a firewall. In essence, NAT allows an organization to use private network addresses behind a firewall while maintaining the ability to connect to external systems through the firewall.
Here are the three methods for implementing NAT:
  • Static: In static NAT, each internal system on the private network has a corresponding external, routable IP address associated with it. This particular technique is seldom used because unique IP addresses are in short supply.
  • Hiding: With hiding NAT, all systems behind a firewall share the same external, routable IP address, while the internal systems use private IP addresses. Thus, with a hiding NAT, a number of systems behind a firewall still appear to be a single system.
  • Port address translation: With port address translation, you can place hosts behind a firewall system and still make them selectively accessible to external users.
In terms of strengths and weaknesses, each type of NAT — static, hiding, or port address translation — is applicable in certain situations; the variable is the amount of design flexibility offered by each type. Static NAT offers the most flexibility, but it’s not always practical because of the shortage of IP addresses. Hiding NAT technology is seldom used because port address translation offers additional features. Port address translation is often the most convenient and secure solution.
Enabling packet filtering on your Linux system
The Linux kernel has built-in packet filtering software in the form of something called netfilter. You use the iptables command to set up the rules for what happens to the packets based on the IP addresses in their header and the network connection type.
To find out more about netfilter and iptables, visit the documentation section of the netfilter Web site at www.netfilter.org/documentation.
The built-in packet filtering capability is handy when you don’t have a dedicated firewall between your Linux system and the Internet. This is the case, for example, when you connect your Linux system to the Internet through a DSL or cable modem. Essentially, you can have a packet filtering firewall inside your Linux system, sitting between the kernel and the applications.
Using the security level configuration tool
Some Linux distributions, such as Fedora and SUSE, include GUI tools to turn on a packet filtering firewall.
In Fedora, you can turn on different levels of packet filtering through the graphical Firewall Configuration tool. To run the tool, log in as root and choose System»Administration»Firewall. The Firewall Configuration window appears (see Figure 2-4) along with an authentication window.
In Fedora, you can configure the firewall with this toolFigure 2-4: In Fedora, you can configure the firewall with this tool.
From the Firewall Configuration dialog box, you can select two predefined levels of simple firewalling (more precisely, packet filtering):
  • Disabled: This option doesn’t perform any filtering and allows all connections. (You can still turn off Internet services by not running the servers or disabling them in the xinetd configuration files.) This security level is fine if your Linux system is inside a protected local area network or if you have a separate firewall device.
  • Enabled: This option turns on packet filtering. You can then select the services that you want to allow and the network devices that you trust.
You can allow incoming packets meant for specific Internet services such as SSH, TELNET, and FTP. If you select a network interface such as eth0 (the first Ethernet card) as trusted, all network traffic over that interface is allowed without any filtering.
In SUSE, to set up a firewall, choose Main Menu»System»YaST. In the YaST Control Center window that appears, click Security and Users on the left side of the window and then click Firewall on the right side. YaST opens a window that you can use to configure the firewall.
You can designate network interfaces (by device name, such as eth0, ppp0, and so on) to one of three zones: internal, external, or demilitarized zone. Then for that zone, you can specify what services (such as HTTP, FTP, and SSH) are allowed. If you have two or more network interfaces and you use the Linux system as a gateway (a router), you can enable forwarding packets between network interfaces (a feature called masquerading). You can also turn on different levels of logging (for example, logging all dropped packets that attempt connection at specific ports). If you make changes to firewall settings, click the Startup category and click Save Settings and Restart Firewall Now.
Using the iptables command
The GUI firewall configuration tools use the iptables command to implement the firewall. If your Linux system doesn’t have a GUI tool, you can use iptables directly to configure firewalling on your Linux system.
Using the iptables command is somewhat complex. The iptables command uses the concept of a chain, which is a sequence of rules. Each rule says what to do with a packet if the header contains certain information (such as the source or destination IP address). If a rule doesn’t apply, iptables consults the next rule in the chain. By default, there are three chains:
  • INPUT chain: The first set of rules against which packets are tested. The packets continue to the next chain only if the INPUT chain doesn’t specify DROP or REJECT.
  • FORWARD chain: Contains the rules that apply to packets attempting to pass through this system to another system (when you use your Linux system as a router between your LAN and the Internet, for example).
  • OUTPUT chain: Includes the rules applied to packets before they are sent out (either to another network or to an application).
When an incoming packet arrives, the kernel uses iptables to make a routing decision based on the destination IP address of the packet. If the packet is for this server, the kernel passes the packet to the INPUT chain. If the packet satisfies all the rules in the INPUT chain, the packet is processed by local processes such as an Internet server that is listening for packets of this type.
If the kernel has IP forwarding enabled and the packet has a destination IP address of a different network, the kernel passes the packet to the FORWARD chain. If the packet satisfies the rules in the FORWARD chain, it’s sent out to the other network. If the kernel doesn’t have IP forwarding enabled and the packet’s destination address isn’t for this server, the packet is dropped.
If the local processing programs that receive the input packets want to send network packets out, those packets pass through the OUTPUT chain. If the OUTPUT chain accepts those packets, they’re sent out to the specified destination network.
You can view the current chains, add rules to the existing chains, or create new chains of rules by using the iptables command. When you view the current chains, you can also save them to a file. For example, if you had configured nothing else and your system has no firewall configured, typing iptables -L should show the following:
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination

In this case, all three chains — INPUT, FORWARD, and OUTPUT — show the same ACCEPT policy, which means everything is wide open.
If you’re setting up a packet filter, the first thing you do is specify the packets that you want to accept. For example, to accept packets from the 192.168.0.0 network address, add the following rule to the INPUT chain:
iptables -A INPUT -s 192.168.0.0/24 -j ACCEPT
Now add a rule to drop everything except local loopback (the lo network interface) traffic and stop all forwarding with the following commands:
iptables -A INPUT -i ! lo -j REJECT
iptables -A FORWARD -j REJECT

The first iptables command, for example, appends to the INPUT chain (-A INPUT) the rule that if the packet does not come from the lo interface (-i ! lo), iptables rejects the packet (-j REJECT).
Before rejecting all other packets, you may also add more rules to each INPUT chain to allow specific packets in. You can select packets to accept or reject based on many parameters, such as IP addresses, protocol types (TCP, UDP), network interface, and port numbers.
You can do all sorts of specialized packet filtering with iptables. For example, suppose you set up a Web server and want to accept packets meant for only HTTP (port 80) and secure shell (SSH) services. The secure shell service (port 22) is for you to securely log in and administer the server. Suppose the server’s IP address is 192.168.0.10. Here is how you might set up the rules for this server:
iptables -P INPUT DROP
iptables -A INPUT -s 0/0 -d 192.168.0.10 -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -s 0/0 -d 192.168.0.10 -p tcp --dport 22 -j ACCEPT

In this case, the first rule sets up the default policy of the INPUT chain to DROP, which means that if none of the specific rules match, the packet will be dropped. The next two rules say that packets addressed to 192.168.0.10 and meant for ports 80 and 22 are accepted.
Don’t type iptables commands from a remote login session. A rule that begins denying packets from all addresses can also stop what you type from reaching the system; if that happens, you may have no way of accessing the system over the network. To avoid unpleasant surprises, always type iptables rules at the console — the keyboard and monitor connected directly to your Linux PC that is running the packet filter. If you want to delete all filtering rules in a hurry, type iptables -F to flush them. To change the default policy for the INPUT chain to ACCEPT, type iptables -t filter -P INPUT ACCEPT. This causes iptables to accept all incoming packets by default.
Not every iptables command is discussed in this section. You can type man iptables to read a summary of the commands. You can also read about netfilter and iptables at www.iptables.org.
After you define the rules by using the iptables command, they’re in memory and are gone when you reboot the system. Use the iptablessave command to store the rules in a file. For example, you can save the rules in a file named iptables.rules by using the following command:
# Generated by iptables-save v1.3.0 on Sun Aug 28 16:10:12 2010
*filter
:FORWARD ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [6:636]
-A FORWARD -j REJECT --reject-with icmp-port-unreachable
-A INPUT -s 192.168.0.0/255.255.255.0 -j ACCEPT
-A INPUT -i ! lo -j REJECT --reject-with icmp-port-unreachable
COMMIT
# Completed on Sun Aug 28 16:10:12 2010

These rules correspond to the following iptables commands used to configure the filter:
iptables -A INPUT -s 192.168.0.0/24 -j ACCEPT
iptables -A INPUT -i ! lo -j REJECT
iptables -A FORWARD -j REJECT

If you want to load these saved rules into iptables, use the following command:
iptables-restore < iptables.rules
next Performing Computer Security Audits






























































Share this article :

0 comments:

Post a Comment

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Linux - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger