© David Both 2020
D. BothUsing and Administering Linux: Volume 3https://doi.org/10.1007/978-1-4842-5485-1_8

8. Email Clients

David Both1 
(1)
Raleigh, NC, USA
 

Objectives

In this chapter you will learn
  • More about using the mailx email client

  • To install and configure the IMAP server which allows remote email clients to access email on the server

  • To install and configure Thunderbird for a graphical email client

  • To use OpenSSL to generate a self-signed ID certificate suitable for testing

  • To configure Thunderbird to use the self-signed certificate with the STARTTLS authentication protocol

  • To use network analysis tools like ss and nmap to look for network connections related to email

  • To use those network tools to look for other ports that should not be open

Introduction

In Chapter 7 of this volume, we created an email server for our virtual domain, example.com. We installed SendMail and the tools needed to modify and recompile the SendMail configuration. We also installed and learned some basic usage for the mailx email client. Mailx is a powerful ASCII plain text email client that has two interfaces. It can be used with Standard I/O (STDIO) as part of a command pipeline to send email from shell scripts or command line programs. It can also be used as an interactive text mode user interface that uses keyboard commands to perform tasks like reading and deleting email, composing and sending new emails, and much more.

In this chapter, we will explore mailx in a bit more detail. We will also look at the GUI email client, Thunderbird. We will install and configure the IMAP email access protocol on the server which will enable Thunderbird to access emails on the server from remote clients. We will also create a self-signed certificate1 and use it to enable IMAP2 over Transport Layer Security (TLS).3 Certificates may also be referred to as certs.

More mailx

Although the mailx client and its predecessor, mail, have been around since the early days of Unix, it still has a place in today’s Linux environment. We have already seen in Chapter 7 how useful the mailx utility can be in testing email servers and mail transfer agents (MTAs).

Much of the utility of the mailx program comes from its flexible interface. We have already used it as part of a pipeline to send output from one utility to an email address. This makes it a powerful tool for the SysAdmin when installing or repairing email systems. Now we want to explore the use of mailx as an ASCII text email client from which we can send and retrieve emails.

As an email client, mailx has some major limitations that make it unsuitable for the average user – it is typically run as a text mode tool on the email server itself. It is not intended to run on a local host to access email on a remote server. The user must use SSH to log in to the email server in order to access their email so that the email users must have a login account on the server and not the safer nologin account. This can be a security issue but we won’t consider that here.

That said, mailx does have extensions that will allow it to connect to an IMAP server. We will get to IMAP later in this chapter but will not use mailx with IMAP.

Experiment 8-1

Start this experiment as the student user on StudentVM1. This experiment is an exploration of mailx as a basic email client.

As the student user on StudentVM1, SSH to StudentVM2. Start the interactive user interface (UI) of mailx, and then enter a question mark to get help. Note that the ampersand (&) is the command prompt for mailx.
[student@studentvm2 ~]$ mailx
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/student": 6 messages 3 unread
    1 Student User          Sat Jun 29 12:38  26/1140  "Test email from StudentVM1"
    2 Student User          Sun Jun 30 08:59  26/1130  "Test email for aliases"
    3 Student User          Sun Jun 30 09:06  26/1130  "Test email for aliases"
>U  4 Student User          Sun Jun 30 09:10  26/1132  "Test email for aliases"
 U  5 Student User          Sun Jun 30 12:45  26/1147  "Test email for aliases"
 U  6 [email protected]  Mon Jul  1 03:44  62/2610  "Logwatch for studentvm1.example.com (Lin"
& ?
               mailx commands
type <message list>             type messages
next                            goto and type next message
from <message list>             give head lines of messages
headers                         print out active message headers
delete <message list>           delete messages
undelete <message list>         undelete messages
save <message list> folder      append messages to folder and mark as saved
copy <message list> folder      append messages to folder without marking them
write <message list> file       append message texts to file, save attachments
preserve <message list>         keep incoming messages in mailbox even if saved
Reply <message list>            reply to message senders
reply <message list>            reply to message senders and all recipients
mail addresses                  mail to specific recipients
file folder                     change to another folder
quit                            quit and apply changes to folder
xit                             quit and discard changes made to folder
!                               shell escape
cd <directory>                  chdir to directory or home if none given
list                            list names of all available commands
A <message list> consists of integers, ranges of same, or other criteria
separated by spaces.  If omitted, mailx uses the last message typed.
&

As we have already discovered, many commands can be invoked with just its first letter. Note that “R” and “r” are different commands. Now enter l (lowercase l – el) to list all of the available commands. You can always view the help and list of commands if you get stuck.

In addition to using mailx to send a STDIN data stream as an email, we can also invoke email from the command line. There is no option to create and send an email from within the mailx text interface. Let’s exit from the mailx user interface and then send an email to ourselves.
[student@studentvm2 ~]$ mailx [email protected]
Subject: Testing mailx
This email is a test of using mailx to send email.
EOT
[student@studentvm2 ~]$

After entering the mailx command, it prompts you for the subject. After entering that, you get an empty line and you can simply start typing your message. When you have finished entering the message, use the Ctrl-D (EOT or End Of Text) to tell mailx to send the message.

Now view the message in the interactive interface.

Although mailx is a powerful and flexible tool for the SysAdmin, it leaves a great deal to be desired with respect to the normal email experience we expect today.

IMAP

Once email has been delivered to the server, you must retrieve it from the mail spool file in order to read it. Email can be retrieved from remote servers using the IMAP protocol. IMAP was designed explicitly to allow users to access all of their emails from multiple devices running an email client. This was originally conceived as multiple computers but in this Internet age encompasses, all types of connected devices.

IMAP is a much better tool to use for multiple devices than the old and much simpler post office protocol (POP ). Clients connecting to an email server using POP download all of the messages to the local host and delete them from the server. Thus, those messages that have already been downloaded cannot be viewed by any of the other devices. For this reason, we will cover only IMAP in this course.

Configuring IMAP on the server

It is necessary to install a program that can deal with remote connections using IMAP. There are several, but in this class, we will install the University of Washington IMAP package. UW IMAP provides both POP and IMAP; it is simple to install and there is little configuration required. It is also necessary to install a wrapper program to control the IMAP program. The Xinetd package provides TCP Wrappers around many programs that do not run as daemons, such as Telnet, IMAP, and POP. You should have already installed Xinetd on StudentVM1 in Chapter 17 in Volume 2 of this course but have not done so yet on StudentVM2.

Experiment 8-2

Perform this experiment as the root user on StudentVM2. Install the following programs.
[root@studentvm2 ~]# dnf -y install uw-imap uw-imap-utils xinetd
There is only a bit of configuration to do. You must enable IMAP and start Xinetd. Edit the /etc/xinetd.d/imap file and change the line that says “disable = yes” to “disable = no” to enable IMAP. Then start the Xinetd service and configure it to start upon reboot.
[root@studentvm2 ~]# systemctl start xinetd.service ; systemctl enable xinetd.service
Verify that IMAP is configured and running with the following two commands.
[root@studentvm2 xinetd.d]# systemctl status xinetd
• xinetd.service - Xinetd A Powerful Replacement For Inetd
   Loaded: loaded (/usr/lib/systemd/system/xinetd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-07-01 15:25:43 EDT; 51s ago
     Docs: man:xinetd
           man:xinetd.conf
           man:xinetd.log
 Main PID: 14417 (xinetd)
    Tasks: 1 (limit: 4696)
   Memory: 896.0K
   CGroup: /system.slice/xinetd.service
           └─14417 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid
Jul 01 15:25:43 studentvm2.example.com xinetd[14417]: removing echo
Jul 01 15:25:43 studentvm2.example.com xinetd[14417]: removing imaps
Jul 01 15:25:43 studentvm2.example.com xinetd[14417]: removing pop2
Jul 01 15:25:43 studentvm2.example.com xinetd[14417]: removing pop3
Jul 01 15:25:43 studentvm2.example.com xinetd[14417]: removing pop3s
Jul 01 15:25:43 studentvm2.example.com xinetd[14417]: removing tcpmux
Jul 01 15:25:43 studentvm2.example.com xinetd[14417]: removing time
Jul 01 15:25:43 studentvm2.example.com xinetd[14417]: removing time
Jul 01 15:25:43 studentvm2.example.com xinetd[14417]: xinetd Version 2.3.15 started with loadavg labeled-net>
Jul 01 15:25:43 studentvm2.example.com xinetd[14417]: Started working: 1 available service
[root@studentvm2 xinetd.d]# chkconfig --list
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
livesys           0:off  1:off  2:off  3:on  4:on  5:on  6:off
livesys-late      0:off  1:off  2:off  3:on  4:on  5:on  6:off
xinetd based services:
    chargen-dgram:       off
    chargen-stream:      off
    daytime-dgram:       off
    daytime-stream:      off
    discard-dgram:       off
    discard-stream:      off
    echo-dgram:          off
    echo-stream:         off
    imap:                on
    imaps:               off
    ipop2:               off
    ipop3:               off
    pop3s:               off
    tcpmux-server:       off
    time-dgram:          off
    time-stream:         off
[root@studentvm2 xinetd.d]#
Add the following line to the IPTables filter table in /etc/sysconfig/iptables to allow IMAP on port 143. Be sure to load the revised rule set.
-A INPUT -p tcp -m state --state NEW -m tcp --dport 143 -j ACCEPT
Do a quick test using telnet to see if IMAP is working and accepting connections. You can do this as root or the student user.
[root@studentvm1 ~]# telnet localhost 143
Trying ::1...
Connected to localhost.
Escape character is '^]'.
∗ OK [CAPABILITY IMAP4REV1 I18NLEVEL=1 LITERAL+ SASL-IR LOGIN-REFERRALS STARTTLS] localhost IMAP4rev1 2007f.404 at Mon, 19 Nov 2012 13:18:58 -0500 (EST)
a0001 login student <Student password>
a0001 OK [CAPABILITY IMAP4REV1 I18NLEVEL=1 LITERAL+ IDLE UIDPLUS NAMESPACE CHILDREN MAILBOX-REFERRALS BINARY UNSELECT ESEARCH WITHIN SCAN SORT THREAD=REFERENCES THREAD=ORDEREDSUBJECT MULTIAPPEND] User student authenticated
a0002 logout
∗ BYE studentvm1.example.com IMAP4rev1 server terminating connection
a0002 OK LOGOUT completed
Connection closed by foreign host.

Note that you must use the “a000x” sequence numbers or your commands will not work. Try the same test from StudentVM1 on the virtual network.

Our IMAP server is now working.

Notice our use of Telnet to connect with the IMAP server for testing. This is only possible because the standard Internet protocols such as SMTP, IMAP, and others use ASCII plain text commands and responses. That makes it easy for us to simulate a connection from a client. We could have done the same thing with SMTP to actually send an email because it, too, uses ASCII plain text protocols.

Thunderbird

Thunderbird is only one of many email clients that are available for Linux. It is freely available for all platforms and is becoming very popular. It is my current GUI email client of choice. The Xfce version of Fedora that we installed contains the Claws GUI email client, but we will install and configure Thunderbird because it is so widespread.

Experiment 8-3

Start this experiment as the root user on StudentVM1. Use dnf to install Thunderbird on your student host.
[root@studentvm1 ~]# dnf -y install thunderbird

Perform the rest of this experiment as the student user on StudentVM1.

Launch Thunderbird from the ApplicationsInternet menu. The first time you start Thunderbird, it will take a relatively long time – about 25 seconds on my VM. This is so that it can create the configuration directory and default configuration files. It then displays the “Set Up an Existing Email Account” dialog window as seen in Figure 8-1.
../images/473483_1_En_8_Chapter/473483_1_En_8_Fig1_HTML.jpg
Figure 8-1

This dialog starts the Email account setup the first time Thunderbird is launched

Use the information in Figure 8-2 to set up an IMAP email account using the Thunderbird email client.
../images/473483_1_En_8_Chapter/473483_1_En_8_Fig2_HTML.png
Figure 8-2

Email configuration settings for the first page of Thunderbird email account configuration

After entering the data on this first dialog window, press the Continue button. Thunderbird will query the server by doing an MX lookup of the example.com domain. It then queries the server to determine the connection parameters. If the server responds appropriately, as it does for us, the rest of the configuration is performed automatically and the results are displayed as shown in Figure 8-3.
../images/473483_1_En_8_Chapter/473483_1_En_8_Fig3_HTML.jpg
Figure 8-3

Thunderbird has queried the email server and obtained the required connection data

Now click the Done button to complete the configuration.

Well, things do not always work as one should be able to expect, do they? I received an error message indicating that the username or password is wrong. Having been through this many times, I already know that the configuration for authentication with the server is the most likely cause of this problem.

Click the Manual Config button. The dialog window seen in Figure 8-4 shows the problem. We have not configured Kerberos, an authentication protocol for computer networks, as our authentication method.
../images/473483_1_En_8_Chapter/473483_1_En_8_Fig4_HTML.jpg
Figure 8-4

The Manual Configuration dialog shows the problem because the configuration utility assumes that we are using Kerberos for authentication when we are not

For now, to prevent getting too sidetracked, we will resolve this problem the easy way so that we can get email up and running. In the SSL (Secure Socket Layer) column, select None for both IMAP (inbound) and SMTP (outbound). In the Authentication column, select Normal password for IMAP and No authentication for SMTP. Press Done. You will receive a red warning dialog. Place a check in the I understand the risks box and click the Done button, and then click Done again.

Thunderbird will connect to the server, authenticate for the IMAP connection, and download any emails that already exist in the inbox. Click the Inbox and look at the emails there, as at least a few should be present. Notice that there is no Sent mail folder.

For a bit of additional testing, send a test email to [email protected] – yourself – and also to your external email address. The Send folder is created when the email is sent. Your instance of Thunderbird should look very similar to that shown in Figure 8-5.
../images/473483_1_En_8_Chapter/473483_1_En_8_Fig5_HTML.jpg
Figure 8-5

Thunderbird after initial basic configuration with downloaded email

Adding authentication

Email is insecure – period. No matter what we do on our servers and clients, once an email leaves our network, we must consider it to be readable by everyone on the Internet which amounts to nearly the entire world. Many politicians and white-collar criminals have found this out the hard way. No matter that we have configured TLS to provide some encryption between our SMTP server and other SMTP servers, most SMTP servers have not implemented this security mechanism. As a result, most SMTP servers talk to each other in unencrypted plain text.

Another problem I have encountered when traveling is that I do not have an email account for the local ISPs for outbound SMTP. This is usually an attempt by the ISP to block spam sent directly from infected hosts on their networks. This means that I cannot send email from those networks without some sort of circumvention. I can use my own email server as the outbound SMTP server, but that means it is accessible to spammers as an open relay.

There are a couple things we can do to improve security and help prevent the use of our mail servers as open relays. For example, the use of authentication will allow a laptop to use our own email server as a relay while preventing others from using it the same way.

It is possible to use a number of different forms of authentication so that mobile users can authenticate with their own email server in order to allow relaying. It took me a lot of work to figure this out. Using authentication so that we can connect both SMTP and IMAP to our own email server allows us to send email from anywhere even if we don’t have an email account on the local ISP.

SMTP already uses STARTTLS for authentication and encryption as we found in Chapter 7 of this volume. So we do not need to do anything else for that except to configure it in the Thunderbird client.

Certificates

Certificates are the key, if you will pardon the pun, to enabling authentication and encryption. These certificates provide a verifiable identity for the server and are also used to provide the encryption key for the connections.

ID certificates are sold by a number of recognized certificate authorities (CAs) such as Verisign, Symantec, DigiCert, GeoTrust, RapidSSL, and others. Let’s Encrypt4 provides a free and open certificate authority that is backed by many well-known and respected sponsors and donors. For our purposes, a self-signed certificate is perfect.

IMAP authentication

Adding authentication and encryption to IMAP is easy because we already have most of the necessary parts in place. The IMAP protocol does not include authentication or encryption, but the IMAPS protocol does. TLS is also available just as we used for SMTP in Chapter 7 and is relatively easy to set up so we will use that.

Experiment 8-4

Start this experiment as root on StudentVM2. We will enable IMAPS, restart the Xinetd service, and add a rule to the firewall for IMAPS.

The Xinetd package that we previously installed contains the IMAPS configuration file and placed it in /etc/xinetd.d. In StudentVM2, edit /etc/xinetd.d/imaps. Change the line:
disable                 = yes
to:
disable                 = no

Restart Xinetd.

Generate a self-signed certificate for IMAP. The openssl program can be used to create a certificate that we can use with IMAPS like the one that was created for SMTP TLS. Enter the requested data as shown in bold. Do this as root in root’s home directory.
[root@studentvm2 ~]# openssl req -new -x509 -nodes -out imapd.pem -keyout imapd.pem
Generating a RSA private key
....................+++++
......................................+++++
writing new private key to 'imapd.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:<Your country abbreviation. I used US.>
State or Province Name (full name) []:<Your state abbreviation. I used NC>
Locality Name (eg, city) [Default City]:<Your city, I used Raleigh.>
Organization Name (eg, company) [Default Company Ltd]:<Organization name. I used LinuxGeek46.>
Organizational Unit Name (eg, section) []:<Press Enter>
Common Name (eg, your name or your server's hostname) []:studentvm2.example.com
Email Address []:[email protected]
Verify that the file was created
[root@studentvm2 ~]# ll
total 60
-rw-------. 1 root root  2118 Dec 22  2018 anaconda-ks.cfg
-rw-r--r--. 1 root root   469 Apr 21 21:40 ifcfg-enp0s3
-rw-------. 1 root root  3123 Jul  4 17:58 imapd.pem
-rw-r--r--. 1 root root  2196 Dec 22  2018 initial-setup-ks.cfg
-rw-r--r--. 1 root root    10 Jun 19 07:20 testfile.txt
[root@studentvm2 ~]#

Copy the imapd.pem file to the /etc/ssl/certs directory. You should also view the content of the certificate with a paging tool like less.

For a real-world server, we would use the openSSL tool to generate a certification signing request and send it to a public certification authority (CA) and receive a signed certificate from them that verifies the identity of our server. A self-signed certificate like we just created is fine for a closed environment like we have in our virtual network, used inside an organization where no outsiders will be accessing the server and in other types of testing environments. Self-signed certificates should never be used on a server that allows public access.

A search on “certificate authorities list” will result in links to a good number of public CAs, many of which charge a fee for certificates. There are also some “open” and free CAs that you can find by searching, “free certificate authority.”

Let’s Encrypt5 is a free certificate authority that is a collaborative project with the Linux Foundation. It is sponsored by Mozilla, Akamai, SiteGround, Cisco, Facebook, and many more organizations. It offers free SSL Certificates.

Now let’s configure Thunderbird. As the student user on StudentVM1, use the Menu bar to open EditAccount Settings. Click Server Settings. In the Security Settings section, choose STARTTLS and Normal password. Port 143 is used for the IMAP TLS connection. The result should look like that shown in Figure 8-6.
../images/473483_1_En_8_Chapter/473483_1_En_8_Fig6_HTML.jpg
Figure 8-6

Change the Thunderbird server settings to STARTTLS for connection security and TLS certificate for the authentication method

Click the OK button to complete this configuration.

Testing is easy. Send yourself an email to [email protected]. You should receive the email within a few moments, but if it does not show up fairly, quickly click the Get Messages button on the Thunderbird icon bar. If you get an error, the configuration is not correct. If you have performed the configuration tasks correctly, you should receive no errors.

Be sure to view the log files and the email headers to familiarize yourself with how they might – or might not – differ from emails sent prior to adding IMAP over TLS.

Now we have IMAPS set up for authentication and encryption which enables us to obtain email from remote locations in a reasonably secure manner. The way we have done this is not completely secure because the password is transmitted as ASCII plain text and is not encrypted. However, the data connection itself is encrypted.

More about ports

We have been talking about network ports quite a bit in this course. A TCP or UDP port is not a physical port like the physical connection port on a network interface card (NIC). These are all logical ports that are defined by a number and which we humans usually refer to by the protocol assigned to that port.

We have seen that port 22 is for SSH, port 23 for Telnet, 25 for SMTP, etc. The standard and commonly recognized port assignments are defined in the file /etc/services. If you ever see a port number or name that you don’t recognize, you can start your research with that file.

Although we could have discussed network ports in several places before this, it makes more sense to do so now that we have more ports open in our firewall and more services listening on various ports. This is more meaningful here and it applies to email-related ports as well.

An open port on a computer is one that some service is listening on for connections. However even an open port can be blocked by the host’s firewall. This is why the services we are adding to our server need to have their respective ports added to the firewall for them to work.

Let’s look at the open ports on our server. As with many things in Linux, there are multiple tools we can apply to this task.

Experiment 8-5

Perform this experiment as the root user on StudentVM2. In it we look at four tools that enable us to explore network connections and the ports they use.

Let’s start with netstat which has been around for many years and is now deprecated. It is still useful because many SysAdmins already know it and use it but may soon be gone. Start with a simple look at the open ports on our server.
[root@studentvm2 ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 studentvm2.examp:domain 0.0.0.0:∗               LISTEN
tcp        0      0 localhost:domain        0.0.0.0:∗               LISTEN
tcp        0      0 0.0.0.0:ssh             0.0.0.0:∗               LISTEN
tcp        0      0 0.0.0.0:smtp            0.0.0.0:∗               LISTEN
tcp        0      0 localhost:rndc          0.0.0.0:∗               LISTEN
tcp        0      0 studentvm2.example:imap 192.168.56.21:38372     ESTABLISHED
tcp        0      0 studentvm2.example:imap 192.168.56.21:38310     ESTABLISHED
tcp        0      0 studentvm2.example:imap 192.168.56.21:38334     ESTABLISHED
tcp6       0      0 [::]:imap               [::]:∗                  LISTEN
tcp6       0      0 [::]:domain             [::]:∗                  LISTEN
tcp6       0      0 [::]:ssh                [::]:∗                  LISTEN
tcp6       0      0 localhost:rndc          [::]:∗                  LISTEN
tcp6       0      0 [::]:imaps              [::]:∗                  LISTEN
udp        0      0 0.0.0.0:53826           0.0.0.0:∗
udp        0      0 0.0.0.0:mdns            0.0.0.0:∗
udp        0      0 studentvm2.examp:domain 0.0.0.0:∗
udp        0      0 localhost:domain        0.0.0.0:∗
udp        0      0 0.0.0.0:bootps          0.0.0.0:∗
udp        0      0 localhost:323           0.0.0.0:∗
udp6       0      0 [::]:mdns               [::]:∗
udp6       0      0 [::]:domain             [::]:∗
udp6       0      0 localhost:323           [::]:∗
udp6       0      0 [::]:51664              [::]:∗
raw        0      0 0.0.0.0:icmp            0.0.0.0:∗               7
raw6       0      0 [::]:ipv6-icmp          [::]:∗                  7
raw6       0      0 [::]:ipv6-icmp          [::]:∗                  7
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     29386    @/tmp/.ICE-unix/1466
unix  2      [ ACC ]     STREAM     LISTENING     29828    /tmp/.esd-1000/socket
unix  2      [ ACC ]     STREAM     LISTENING     16648    /run/lvm/lvmetad.socket
unix  2      [ ACC ]     STREAM     LISTENING     29339    /tmp/ssh-MycgW2NgxAFd/agent.1254
unix  2      [ ACC ]     STREAM     LISTENING     29387    /tmp/.ICE-unix/1466
unix  2      [ ACC ]     STREAM     LISTENING     22540    /run/gssproxy.sock
unix  2      [ ACC ]     STREAM     LISTENING     22539    /var/lib/gssproxy/default.sock
unix  2      [ ACC ]     STREAM     LISTENING     22775    /var/lib/sss/pipes/private/sbus-monitor
unix  2      [ ACC ]     STREAM     LISTENING     21865    @irqbalance874.sock
unix  2      [ ACC ]     STREAM     LISTENING     15357    @/org/kernel/linux/storage/multipathd
<snip>

This command with -a shows all network ports that are open and listening, IPV[460], UDP, TCP, as well as all open sockets for applications and services that are communicating with other internal processes and not with other hosts.

Let’s trim this down and to look only at TCP (-t), UDP (-u), and all listening and established connections within those other two filters.
[root@studentvm2 ~]# netstat -tua
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 studentvm2.examp:domain 0.0.0.0:∗               LISTEN
tcp        0      0 localhost:domain        0.0.0.0:∗               LISTEN
tcp        0      0 0.0.0.0:ssh             0.0.0.0:∗               LISTEN
tcp        0      0 0.0.0.0:smtp            0.0.0.0:∗               LISTEN
tcp        0      0 localhost:rndc          0.0.0.0:∗               LISTEN
tcp        0      0 studentvm2.example:imap 192.168.56.21:38372     ESTABLISHED
tcp        0      0 studentvm2.example:imap 192.168.56.21:38310     ESTABLISHED
tcp        0      0 studentvm2.example:imap 192.168.56.21:38334     ESTABLISHED
tcp6       0      0 [::]:imap               [::]:∗                  LISTEN
tcp6       0      0 [::]:domain             [::]:∗                  LISTEN
tcp6       0      0 [::]:ssh                [::]:∗                  LISTEN
tcp6       0      0 localhost:rndc          [::]:∗                  LISTEN
tcp6       0      0 [::]:imaps              [::]:∗                  LISTEN
udp        0      0 0.0.0.0:53826           0.0.0.0:∗
udp        0      0 0.0.0.0:mdns            0.0.0.0:∗
udp        0      0 studentvm2.examp:domain 0.0.0.0:∗
udp        0      0 localhost:domain        0.0.0.0:∗
udp        0      0 0.0.0.0:bootps          0.0.0.0:∗
udp        0      0 localhost:323           0.0.0.0:∗
udp6       0      0 [::]:mdns               [::]:∗
udp6       0      0 [::]:domain             [::]:∗
udp6       0      0 localhost:323           [::]:∗
udp6       0      0 [::]:51664              [::]:∗
Now display only those ports listening on IPV4.
[root@studentvm2 ~]# netstat -tua4
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 studentvm2.examp:domain 0.0.0.0:∗               LISTEN
tcp        0      0 localhost:domain        0.0.0.0:∗               LISTEN
tcp        0      0 0.0.0.0:ssh             0.0.0.0:∗               LISTEN
tcp        0      0 0.0.0.0:smtp            0.0.0.0:∗               LISTEN
tcp        0      0 localhost:rndc          0.0.0.0:∗               LISTEN
tcp        0      0 studentvm2.example:imap 192.168.56.21:38372     ESTABLISHED
tcp        0      0 studentvm2.example:imap 192.168.56.21:38310     ESTABLISHED
tcp        0      0 studentvm2.example:imap 192.168.56.21:38334     ESTABLISHED
udp        0      0 0.0.0.0:53826           0.0.0.0:∗
udp        0      0 0.0.0.0:mdns            0.0.0.0:∗
udp        0      0 studentvm2.examp:domain 0.0.0.0:∗
udp        0      0 localhost:domain        0.0.0.0:∗
udp        0      0 0.0.0.0:bootps          0.0.0.0:∗
udp        0      0 localhost:323           0.0.0.0:∗
The ss utility has replaced the netstat command, but the available options are much the same. Let’s see what results we get from that.
[root@studentvm2 ~]# ss -tua
Netid  State   Recv-Q  Send-Q     Local Address:Port      Peer Address:Port
udp    UNCONN  0       0          0.0.0.0:53826           0.0.0.0:∗
udp    UNCONN  0       0          0.0.0.0:mdns            0.0.0.0:∗
udp    UNCONN  0       0          192.168.56.1:domain     0.0.0.0:∗
udp    UNCONN  0       0          127.0.0.1:domain        0.0.0.0:∗
udp    UNCONN  0       0          0.0.0.0:bootps          0.0.0.0:∗
udp    UNCONN  0       0          127.0.0.1:323           0.0.0.0:∗
tcp    LISTEN  0       10         192.168.56.1:domain     0.0.0.0:∗
tcp    LISTEN  0       10         127.0.0.1:domain        0.0.0.0:∗
tcp    LISTEN  0       128        0.0.0.0:ssh             0.0.0.0:∗
tcp    LISTEN  0       10         0.0.0.0:smtp            0.0.0.0:∗
tcp    LISTEN  0       128        127.0.0.1:rndc          0.0.0.0:∗
tcp    ESTAB   0       0          192.168.56.1:imap       192.168.56.21:38372
tcp    ESTAB   0       0          192.168.56.1:imap       192.168.56.21:38310
tcp    ESTAB   0       0          192.168.56.1:imap       192.168.56.21:38334

It appears from this that the new ss command can be used in place of the netstat command with similar options. The results show the same data although a bit different column and row orders. This might affect existing scripts that parse this data.

Now try this one.
[root@studentvm2 ~]# ss -atu4
Netid  State   Recv-Q  Send-Q  Local Address:Port      Peer Address:Port
udp    UNCONN  0       0       0.0.0.0:53826           0.0.0.0:∗
udp    UNCONN  0       0       0.0.0.0:mdns            0.0.0.0:∗
udp    UNCONN  0       0       192.168.56.1:domain     0.0.0.0:∗
udp    UNCONN  0       0       127.0.0.1:domain        0.0.0.0:∗
udp    UNCONN  0       0       0.0.0.0:bootps          0.0.0.0:∗
udp    UNCONN  0       0       127.0.0.1:323           0.0.0.0:∗
tcp    LISTEN  0       10      192.168.56.1:domain     0.0.0.0:∗
tcp    LISTEN  0       10      127.0.0.1:domain        0.0.0.0:∗
tcp    LISTEN  0       128     0.0.0.0:ssh             0.0.0.0:∗
tcp    LISTEN  0       10      0.0.0.0:smtp            0.0.0.0:∗
tcp    LISTEN  0       128     127.0.0.1:rndc          0.0.0.0:∗
tcp    ESTAB   0       0       192.168.56.1:50590      192.168.56.21:ssh
tcp    ESTAB   0       0       192.168.56.1:imap       192.168.56.21:38372
tcp    ESTAB   0       0       192.168.56.1:imap       192.168.56.21:38310
tcp    ESTAB   0       0       192.168.56.1:imap       192.168.56.21:38334
tcp    ESTAB   0       0       192.168.56.1:ssh        192.168.56.21:40168

Note the peer address port numbers on the established connections which are very high. When any service connects to a remote host, the outbound port number is a high random one. This source port can be different for each connection and usually is. The destination port is the one that is assigned and standardized.

There are three IMAP connections from 192.168.56.21, studentvm1, from three different ports to studentvm2 at 192.168.56.1 on port 143. This also illustrates that multiple connections can be made to the same port. There are two SSH connections in this result, one inbound and one outbound.

What results do you get with the following command?
[root@studentvm2 ~]# ss -antu4

Do you remember the many times I have mentioned that “everything is a file?” Good, because network connections are files, too. The lsof (list open files) utility will show us that. Although intended primarily for listing the files in a specific filesystem that are open and thus allowing us to locate and close them in order to unmount a filesystem, it can also work with network connections.

Let’s take a quick look at lsof and its use with regular files and filesystems. First, try unmounting the /home filesystem.
[root@studentvm2 ~]# umount /home
umount: /home: target is busy.
Now list all of the open files in the /home filesystem.
[root@studentvm2 ~]# lsof /home | less
COMMAND     PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
imapd       936 student  cwd    DIR  253,3     4096   12 /home/student
ssh        1204 student  cwd    DIR  253,3     4096   12 /home/student
pulseaudi  1238 student  mem    REG  253,3    45056   23 /home/student/.config/pulse/b62e5e58cdf74e0e967b39bc94328d81-card-database.tdb
pulseaudi  1238 student  mem    REG  253,3    12288   22 /home/student/.config/pulse/b62e5e58cdf74e0e967b39bc94328d81-stream-volumes.tdb
pulseaudi  1238 student  mem    REG  253,3    12288   21 /home/student/.config/pulse/b62e5e58cdf74e0e967b39bc94328d81-device-volumes.tdb
pulseaudi  1238 student   10u   REG  253,3    12288   21 /home/student/.config/pulse/b62e5e58cdf74e0e967b39bc94328d81-device-volumes.tdb
pulseaudi  1238 student   11u   REG  253,3    12288   22 /home/student/.config/pulse/b62e5e58cdf74e0e967b39bc94328d81-stream-volumes.tdb
pulseaudi  1238 student   12u   REG  253,3    45056   23 /home/student/.config/pulse/b62e5e58cdf74e0e967b39bc94328d81-card-database.tdb
dbus-daem  1242 student  cwd    DIR  253,3     4096   12 /home/student
sh         1254 student  cwd    DIR  253,3     4096   12 /home/student
sh         1254 student    2w   REG  253,3     2200   36 /home/student/.xsession-errors
imsetting  1325 student  cwd    DIR  253,3     4096   12 /home/student
gvfsd      1328 student  cwd    DIR  253,3     4096   12 /home/student
xfconfd    1335 student  cwd    DIR  253,3     4096   12 /home/student
<snip>

This was a very long list on my VM, and you should have a fairly long list, too. Just being logged in opens a number of files.

Now on to network connections. The following command lists network connections. I have reduced the font size of the output to make it a little more organized.
[root@studentvm2 ~]# lsof -i
COMMAND     PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
avahi-dae   858   avahi   15u  IPv4  22281      0t0  UDP ∗:mdns
avahi-dae   858   avahi   16u  IPv6  22282      0t0  UDP ∗:mdns
avahi-dae   858   avahi   17u  IPv4  22283      0t0  UDP ∗:53826
avahi-dae   858   avahi   18u  IPv6  22284      0t0  UDP ∗:51664
chronyd     915  chrony    6u  IPv4  21416      0t0  UDP localhost:323
chronyd     915  chrony    7u  IPv6  21417      0t0  UDP localhost:323
imapd       936 student    0u  IPv4 280506      0t0  TCP studentvm2.example.com:imap->192.168.56.21:38372 (ESTABLISHED)
imapd       936 student    1u  IPv4 280506      0t0  TCP studentvm2.example.com:imap->192.168.56.21:38372 (ESTABLISHED)
sshd        964    root    4u  IPv4  22514      0t0  TCP ∗:ssh (LISTEN)
sshd        964    root    6u  IPv6  22516      0t0  TCP ∗:ssh (LISTEN)
dhcpd      1094   dhcpd   10u  IPv4  25593      0t0  UDP ∗:bootps
sshd       1139    root    5u  IPv4 281551      0t0  TCP studentvm2.example.com:ssh->192.168.56.21:40168 (ESTABLISHED)
sshd       1152    root    5u  IPv4 281551      0t0  TCP studentvm2.example.com:ssh->192.168.56.21:40168 (ESTABLISHED)
ssh        1204 student    5u  IPv4 283915      0t0  TCP studentvm2.example.com:50590->192.168.56.21:ssh (ESTABLISHED)
named      2269   named   21u  IPv6  37221      0t0  TCP ∗:domain (LISTEN)
named      2269   named   22u  IPv4  37225      0t0  TCP localhost:domain (LISTEN)
named      2269   named   23u  IPv4  37227      0t0  TCP studentvm2.example.com:domain (LISTEN)
named      2269   named   24u  IPv4  37230      0t0  TCP localhost:rndc (LISTEN)
named      2269   named   25u  IPv6  37231      0t0  TCP localhost:rndc (LISTEN)
named      2269   named  512u  IPv6  37219      0t0  UDP ∗:domain
named      2269   named  513u  IPv4  37224      0t0  UDP localhost:domain
named      2269   named  514u  IPv4  37226      0t0  UDP studentvm2.example.com:domain
xinetd    21749    root    5u  IPv6 186294      0t0  TCP ∗:imap (LISTEN)
xinetd    21749    root    6u  IPv6 186295      0t0  TCP ∗:imaps (LISTEN)
sendmail  26853    root    6u  IPv4 229788      0t0  TCP ∗:smtp (LISTEN)
imapd     31774 student    0u  IPv4 270554      0t0  TCP studentvm2.example.com:imap->192.168.56.21:38310 (ESTABLISHED)
imapd     31774 student    1u  IPv4 270554      0t0  TCP studentvm2.example.com:imap->192.168.56.21:38310 (ESTABLISHED)
imapd     32384 student    0u  IPv4 274289      0t0  TCP studentvm2.example.com:imap->192.168.56.21:38334 (ESTABLISHED)
imapd     32384 student    1u  IPv4 274289      0t0  TCP studentvm2.example.com:imap->192.168.56.21:38334 (ESTABLISHED)

The nmap utility is used to scan and map open network ports on a host, local or remote, not just those on which a service is actually listening. Because nmap can scan remote hosts, it is used by security specialists to test the vulnerability of hosts that are network connected. It can also be used by crackers to locate those same vulnerabilities with the intent to force access to the systems. I strongly suggest you not use nmap to scan systems that do not belong to you. Many organizations look for such scanning attacks and will react very aggressively to protect their networks.

We start as root on StudentVM2. Install nmap because it is not already installed.
[root@studentvm2 ~]# dnf install nmap

I suggest reading the DESCRIPTION section of the nmap man page before continuing.

Note The nmap utility will take a significant amount of time to perform its task of probing both local and remote hosts for open network ports. From 94 minutes in one instance to 19 hours when scanning StudentVM1 from StudentVM2. Your virtual environment will be different so the times you experience will also be different.

Do not terminate or skip these parts of this experiment because there is much to be learned. However, you may continue on with the rest of this chapter and beyond while you allow these tests to run.

The nmap utility works quite differently from the other tools we have seen. Where the other tools use data from the /proc filesystem, nmap actively tries to connect to all potential network ports on the target computer. Because of this, nmap can take a significant amount of time to scan a host, about 94 minutes on my StudentVM2 host.
[root@studentvm2 ~]# nmap -n -PN -sT -sU -p- localhost
Starting Nmap 7.70 ( https://nmap.org ) at 2019-07-06 09:24 EDT
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00042s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 131059 closed ports
PORT      STATE         SERVICE
22/tcp    open          ssh
25/tcp    open          smtp
53/tcp    open          domain
143/tcp   open          imap
953/tcp   open          rndc
993/tcp   open          imaps
53/udp    open          domain
67/udp    open|filtered dhcps
323/udp   open|filtered unknown
5353/udp  open|filtered zeroconf
53826/udp open|filtered unknown
Nmap done: 1 IP address (1 host up) scanned in 5634.56 seconds

Now let’s run this same command from StudentVM2 against StudentVM1.

But let’s watch nmap as it works. As root on StudentVM1 start tcpdump.
[root@studentvm1 ~]# tcpdump -n
As the root user on StudentVM2, run the following command. This will take a long time – nearly 19 hours in my virtual network.
[root@studentvm2 ~]# nmap -n -PN -sT -sU -p- studentvm1

You will be able to observe a continuous sequence of probes against the network interface of StudentVM1.

Now try these same commands as root on StudentVM1 and compare the results from StudentVM2.

It is fine to let these commands continue to run while you finish this chapter and proceed on with the next chapters in this course.

Experiment 8-5 shows us some diverse tools that we can use to test the network vulnerability of our hosts. Finding open ports that should not be open or that are valid in some situations but which are not needed for a specific environment should trigger some research. Why are these ports open and what happens if I use the firewall to block them and disable the services behind them?

One thing this does tell us is that IMAP and IMAPS are both open. We only need IMAP because we are using TLS so IMAPS could be disabled and the firewall rule that allows external access from IMAPS can be removed from the firewall.

Other considerations

You should be aware that different email clients have some unique configuration requirements that will be different from Thunderbird. For example, some clients do not support certain types of encryption. If you have problems configuring those clients, refer to the web pages and other documentation for those clients and ensure that the server is configured to support their unique requirements.

Chapter summary

This chapter explored the mailx client a bit more. Although it is a powerful tool for the SysAdmin, it leaves a lot to be desired for most users and even for us SysAdmins for daily email use. It does make an excellent tool for testing.

We installed Thunderbird, one of the popular email clients, and used it to test various configurations of our email server. This included with and without authentication using TLS.

We also used some tools that enable us to view open network ports, network connections, and to determine whether some actions should be taken to lock down unused network ports. We used the nmap tool to probe the localhost and a remote host on our virtual network for vulnerabilities.

Exercises

Perform the following exercises to complete this chapter.
  1. 1.

    What unique capability makes mailx an ideal tool for the SysAdmin who works with and supports email systems?

     
  2. 2.

    What limitations does mailx have that make it a poor choice for most of today’s email users?

     
  3. 3.

    Where are the email inboxes for email users located?

     
  4. 4.

    From StudentVM1, use Telnet and SMTP to send an email from StudentVM2.

     
  5. 5.

    Where are email folders other than the Inbox stored?

     
  6. 6.

    What other methods could be used to create an encrypted TLS connection between the local email client such as Thunderbird on StudentVM1 and the remote email server on StudentVM2?

     
  7. 7.

    Disable IMAPS and remove the firewall rule that allows external access from IMAPS.

     
  8. 8.

    Based on nmap scans of both StudentVM1 and StudentVM2, did you find unused but open ports that could be closed down?

     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.225.21