Open main menu

CDOT Wiki β

Changes

OPS235 Lab 7

1,752 bytes added, 17:52, 25 November 2020
Part 1: Confirming sshd service is Running on VMs.
[http://man7.org/linux/man-pages/man8/netstat.8.html netstat]<br>
[http://man7.org/linux/man-pages/man8/ifconfig.8.html ifconfig]<br>
[http://man7.org/linux/man-pages/man8/ip.8.html ip]<br>
[http://man7.org/linux/man-pages/man8/ping.8.html ping]<br>
[http://man7.org/linux/man-pages/man8/arp.8.html arp]<br>
|style="padding-left:20px;"|Additional Utilities<br>
[http://man7.org/linux/man-pages/man7/hostname.7.html hostname]<br>
[http://linux.die.net/man/8/restorecon restorecon]<br><br>Managing Services<br>[http://linuxwww.diedsm.netfordham.edu/cgi-bin/man/8/chkconfig chkconfig-cgi.pl?topic=systemctl systemctl]<br><br>
Configuration Files<br>
[httphttps://linuxwww.aboutfreebsd.comorg/librarycgi/cmd/blcmdl5_ssh_configman.htm cgi?query=ssh_config&sektion=5 ssh_config]<br>[httphttps://linuxwww.aboutfreebsd.com/od/commandsorg/lcgi/blcmdl5_sshdconman.htm cgi?sshd_config(5) sshd_config]<br>
|style="padding-left:20px;"|SSH Reference<br>
[http://support.suso.com/supki/SSH_Tutorial_for_Linux A good ssh tutorial]<br>
# Switch to your '''c7host''' VM.
# Create a file in your current directory of your c7host machine with some text in it called: '''myfile.txt'''
# Issue Ensure you've successfully connected to the VPN required for Matrix (https://inside.senecacollege.ca/its/services/vpn/studentvpn.html). Then issue the following command (using your Matrix login id):<br><b><code><span style="color:#3366CC;font-size:1.2em;">scp &nbsp; myfile.txt &nbsp; yourmatrixid@matrix.senecac.on.ca:/home/yourmatrixid</span></code></b><br>(followed by your Matrix password)<br>What did this command do?
# Issue the following single command (arguments are separated by a space - use your Matrix login id):<br><b><code><span style="color:#3366CC;font-size:1.2em;">ssh &nbsp; yourmatrixid@matrix.senecac.on.ca &nbsp; ls /home/yourmatrixid/myfile.txt</span></code></b><br>(followed by your Matrix password)<br>What did this command do?<br>Issue the following Linux command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">ssh &nbsp; yourmatrixid@matrix.senecac.on.ca &nbsp; cat /home/yourmatrixid/myfile.txt</span></code></b><br>How do these commands differ from using issuing the ssh command without the ls or cat command? How is this useful?<br><br>The client ssh application contains the utlities: '''ssh''', '''scp''' and '''sftp''' (learned in ULI101) to connect to remote Linux servers in order to issue commands or transfer files between Linux servers. You can install the SSH service on your Linux server, although this has already been performed upon installation. We will now confirm that the ssh service is running on all of your VMs.<br><br>
# OpenSSH should have been installed by default. Let's confirm this by issuing the command:<br /> <b><code><span style="color:#3366CC;font-size:1.2em;">rpm -qa | grep ssh</span></code></b>
<li>Make sure the '''sshd''' service is running on '''all 3 of your VM's'''</li>
</ol>
 
===Part 2: SSH Server Security Configuration===
# Try SSHing from your c7host VM to your centos1 VM as your regular user accountname. Did it work?
# Create another regular user called: '''other'''
# Set the password for the newly-created called called '''other'''
# Try SSHing from your c7host VM to your centos1 VM for the account called '''other'''. Why didn't it work?
# Edit the file '''/etc/ssh/sshd_config''' to add the account '''other''' for the '''AllowUsers''' option (use a space to separate usernames instead of a comma).
'''Answer INVESTIGATION 1 observations / questions in your lab log book.'''
 
=INVESTIGATION 2: ADDITIONAL METHODS TO SECURE YOUR SSH SERVER =
As a system administrator, you have the ability to generate or create '''public''' and '''private''' keys to ensure safe and secure ssh connections. This will require a user to prove who they say they are in order to access a Linux server via SSH (i.e. '''authentication'''). The system administer can generate these keys for the first time, or if the system administrator suspects that a hacker has compromised or trying to penetrate the server, they can remove the existing keys and generate new keys.
 
A common type of attack, '''Arp Poisoning (Man in the Middle Attack)''', can be used to <u>redirect</u> packets to a third party while maintaining the illusion that the connection is secure. Therefore, understanding about the generation and management of public/private keys are important to the security of servers.
 
:'''Perform the following steps:'''
# Switch to your '''centos3''' VM.<br><br>
:We can use the '''netstat''' utility as a trouble-shooting / confirmation tool to view the SSH service and determine which STATE the SSH service is performing: <br> '''LISTENING''', '''ESTABLISHED''', '''CLOSED''' , or '''WAITING''' <br><br>
<ol><li value="2">Run the '''netstat -atunp''' command (pipe to "grep sshd") to check the state of a possible ''ssh connection''. What is the state (i.e. LISTENING or ESTABLISHED)?</li>
<li>While in your '''centos3''' VM, issue the following command to connect to the '''your same VM ''' via ssh: <b><code><span style="color:#3366CC;font-size:1.2em;">ssh ops235@centos3</span></code></b></li>
<li>Enter yes at the prompt, and enter your OPS235 password.<br>The output should appear similar as what is shown below:<br><br>
:<span style="font-family:courier">The authenticity of host 'centos3 (192.168.235.13)' can't be established.</span><br>
:<span style="font-family:courier">Warning: Permanently added 'centos3' (RSA) to the list of known hosts.</span><br><br></li>
<li>Issue the following command to confirm that you connected to your centos3 VM: <b><code><span style="color:#3366CC;font-size:1.2em;">hostname</span></code></b></li></ol>[[Image:spoof.png|thumb|right|485px|If you receive a message like the one displayed above, you should investigate why it is happening as it could indicate a '''serious security issue''', or it could just mean that something on '''the host has changed'''(i.e. the OS was <u>reinstalled</u>)]]<ol><li value="7">Re-run that same '''netstat pipeline command'''. Any change to the connection status?</li>
<li>Log-out of your ssh connection by typing <b><code><span style="color:#3366CC;font-size:1.2em;">exit</span></code></b>.
<li>Run that same '''netstat''' command again. Wait a few minutes and then check again. Record your observations.</li>
[[Image:spoof.png|thumb|right|485px|If you ever receive a message like the one displayed above, you should investigate why it is happening as it could indicate a '''serious security issue''', or it could just mean that something on '''the host has changed'''(i.e. the OS was <u>reinstalled</u>)]]
So far, we have learned to establish an ssh connection to another host using a password to establish your identity. But '''passwords are not the only or even the best way of authenticating your identit'''y. We can also use '''Public/Private key encryption'''.
<li value="10">Switch to your '''centos2''' VM.</li>
<li>Confirm you are in your centos2 VM by entering the command: <b><code><span style="color:#3366CC;font-size:1.2em;">hostname</span></code></b></li>
<li>Make <u>certain</u> that you are in your centos centos2 VM and that you are NOT logged in as root!a '''regular user''' (i.e. NOT root!) (you have been warned!)</li>
<li>To generate a keypair (public/private keys), issue the following command: <b><code><span style="color:#3366CC;font-size:1.2em;">ssh-keygen</span></code></b></li>
<livalue="14">After generating the keys it prompts you for the location to save the keys. The default is '''~/.ssh''' Your private key will be saved as <b>id_rsa</b> and your public key will be saved as '''id_rsa.pub'''. Press ENTER to accept the default, .</li><li>You will then enter be prompted for a '''pass-phrase used '''. The pass-phrase must be entered in order to establish use your identityprivate key. Pass-phrases are more secure than passwords and should be lengthy, hard to guess and reeasy to remember. For example one pass-enter phrase that meets this criteria might be ''"seneca students like to dance at 4:00am"''. Avoid famous phrases such as ''"to be or not to be"'' as they are easy to guess. It is possible to leave the pass-phrase blank but this is dangerous. It means that if a hacker were able to get into your account they could then use your private key to verifyaccess other systems you use.<br><br>The output should appear similar as what is shown below:</li></ol>
<pre style="font-family:monospace;background-color:white;border-style:none;padding-left:50px;">
</pre>
<ol><li value="15"> After generating the keys it prompts you for the location to save the keys. The default is '''~/.ssh''' Your private key will be saved as <b>id_rsa</b> and your public key will be saved as '''id_rsa.pub'''</li><li>You will then be prompted for a pass-phrase. The pass-phrase must be entered in order to use your private key. Pass-phrases are more secure than passwords and should be lengthy, hard to guess and easy to remember. For example one pass-phrase that meets this criteria might be "seneca students like fish at 4:00am". Avoid famous phrases such as "to be or not to be" as they are easy to guess. It is possible to leave the pass-phrase blank but this is dangerous. It means that if a hacker were able to get into your account they could then use your private key to access other systems you use.<br><br></li><li16>Now issue the command <b><code><span style="color:#3366CC;font-size:1.2em;">ssh-copy-id -i ~/.ssh/id_rsa.pub ops235@centos3</span></code></b></li>
<li>When prompted for password, enter OPS235's root password</li>
<li>Try using ssh to now log into your '''centos3''' VM <u>from</u> your '''centos2''' VM. What happens? Were you required to use your pass-phrase?</li>
# Exit the '''gedit''' application.
# In which VM was the file saved? What does that tell you about the use of tunneling for this section?
# Run the graphical program remotely by issuing only one Linux command:<br> <b><code><span style="color:#3366CC;font-size:1.2em;">ssh -X -C yourUserID@centos1 &nbsp; gedit</span></code></b> (Note: ignore warning messages).# Exit the gedit application.# Experiment with running other GUI applications through (in the /bin directory with applications starting with the letter "x" via '''ssh'''(for example: xev or xchat).
'''Answer INVESTIGATION 2 observations / questions in your lab log book.'''
 
=INVESTIGATION 3: MANAGING FIREWALLS FOR PROTECTION &amp; TROUBLESHOOTING =
==== Linux Firewall (iptables) Concepts====
Since Linux servers can be connected to the Internet, it is very important to run a '''firewall ''' to control what comes packets might come into the computer system, what goes packets might go out of the computer system, and what may packets might be forwarded to another computer. A We are currently using the utility called '''iptables''' can be used to set the firewall rules on a Linux server.
=== Part 1: Listing &amp; Clearing Existing iptables Rules ===
Let's get some practice using the iptables command as we introduce concepts such as listing CHAIN rules, and clearing the CHAIN rules:  
:'''Perform the following steps:'''
# For the remainder of this section, use your '''c7host''' machine.
# Issue the following command to list the existing iptables policy rules: <b><code><span style="pointer-events: none;cursor: default;color:#3366CC;font-size:1.2em;">iptables -L</span></code></b>.
# Were there already iptables policy rules that already existed by default?
# Before we proceed, we need to understand various methods to list iptables rules:<br><br>'''Listing iptables Rules:'''<br><table width="100%" cellpadding="10" cellspacing="0" border="1"><tr><td width="30%">'''iptables -L'''</td><td>List all iptables rules (eg. INPUT, OUTPUT. FORWARD, and any customized chains (if any)</td></tr><tr><td>'''iptables -L -v'''</td><td>Verbosely List all iptables rules including information such as total size of packets affected by rules</td></tr><tr><td>'''iptables -L CHAIN-NAME'''</td><td>List all iptables rules for that particular chain-name for less clutter (eg. INPUT or OUTPUT, etc)</td></tr></table><br>
# Issue the following Linux command: <b><code><span style="pointer-events: none;cursor: default;color:#3366CC;font-size:1.2em;">iptables -L INPUT</span></code></b><br>What do you notice is different with this command compared to the previous iptables command?
# Issue the iptables command separately to display the rules for the '''OUTPUT''' chain and for the '''FORWARD''' chain.
# Issue the following command: <b><code><span style="pointer-events: none;cursor: default;color:#3366CC;font-size:1.2em;">iptables -L -v </span></code></b><br>What do you notice about this command as opposed to the first iptables command you issued? <br>What sort of additional information does this command provide regarding affected packets?<br><br>
# Sometimes it may be useful to completely clear the rules for all or a particular chain. Note the options that can be used to clear (or flush) the iptables rules,<br><br>'''Clearing (Flushing) iptables Rules:'''<br><table width="100%" cellpadding="10" cellspacing="0" border="1"><tr><td width="30%">'''iptables -F'''</td><td>Clears the rules for ALL of the chains</td></tr><tr><td>'''iptables -F CHAIN-NAME'''</td><td>Clears the rules for only the specified CHAIN-NAME (eg. INPUT or OUTPUT)</td></tr></table><br>
# Issue the following command to reset the iptables rules for the INPUT chain: <b><code><span style="pointer-events: none;cursor: default;color:#3366CC;font-size:1.2em;">iptables -F INPUT</span></code></b>
# Issue the '''iptables -L INPUT''' command to verify that the iptables rules for the INPUT chain have been cleared.
# Now, issue the command: <b><code><span style="pointer-events: none;cursor: default;color:#3366CC;font-size:1.2em;">iptables -F</span></code></b><br>and then issue the command: <b><code><span style="pointer-events: none;cursor: default;color:#3366CC;font-size:1.2em;">iptables -L</span></code></b> . <br>What do you notice?
=== Part 2: Setting a Default Policy / Setting Policy Exceptions (iptables) ===
Usually when setting policy rules with iptables, a general "overall" policy is set (default policy chain). A good way to think about setting policies is to have a '''"safety -net "''' to take some sort of action to prevent un-handled packets from passing through the firewall by mistake.After the default policy is set-up, then specific exceptions to the default policy can be added to control specific network traffic.
An example would be to set a default policy for incoming network traffic (INPUT chain) to DROP everything, and then set an exception certain exceptions (like ssh connections). Note the following table below for policy setting examples.<br><br>
:'''Perform the following steps:'''
# Make certain you are in your '''c7host''' machine.
# Issue the following Linux command: <b><code><span style="pointer-events: none;cursor: default;color:#3366CC;font-size:1.2em;">iptables -P INPUT DROP</span></code></b>
# Issue the '''iptables -L''' command verify . Can you see the INPUT policy has been to DROP all incoming connections?# Although you have set (look for a default policy to DROP all incoming connections, there is a problem: now, you cannot browse the Policy section, NOT INPUT)Internet. You can confirm that by opening a SEPARATE web-browser and perform a Net-search.<br><br>After the overall default policy is setIn order to fix that problem, then you can create policy rules that are "exceptions" make an exception to the default policy rulesallow incoming web-based traffic (via port 80). These Those iptables commands to create exceptions are more complex since you need to determine: <ul><li>'''Where each rules appears in the chain'''? (order can be important)</li><li>'''Which protocol(s)''' are affected (eg. tcp, udp, icmp)</li><li>'''What source or destination IP Addresses''' are affected?</li><li>'''What port numbers''' are affected?</li><li>'''What action to take''' if all of the above conditions are met? (eg. ACCEPT, REJECT, DROP, or LOG)</li></ul><br><br>'''iptables Command Structure (for setting exceptions):<br>(NOTE: If element in column is not specified in the iptables command, then rule relates to ALL elements)'''<br><table width="100%" cellpadding="10" cellspacing="0" border="1"><tr valign="top><td>Place Rule in Chain</td><td>Chain Name</td><td>Specify Protocol</td><td>Source/Destination IPADDR</td><td>Port Number</td><td>Action<br> -&gt;</td><td>Target</td></tr><tr valign="top"><td>'''-A''' (add / Append to bottom of chain)<br>'''-I''' (insert at top of chain)<br>'''-i I CHAIN-NAME 5''' (insert before line 5) </td><td>'''INPUT'''<br>'''OUTPUT'''<br>'''FORWARD'''<br>'''CHAIN-NAME'''</td><td>'''-p tcp''' (tcp packets)<br>'''-p udp''' (datagram packets)<br>'''-p tcp,udp,icmp''' (combined)<br><br>(refer to '''/etc/protocols''' )</td><td>'''-s IPADDR''' (originating IPADDR)<br>'''-d IPADDR''' (destination IPADDR)</td><td>'''<span style="font-family:courier">--</span>sport 22''' (originating port 22 - SSH)<br>'''<span style="font-family:courier">--</span>sport dport 80''' (originating destined port 80 - http)<br><br>(refer to '''/etc/services''')</td><td>'''-j''' </td><td>'''ACCEPT'''<br>'''REJECT'''<br>'''DROP'''<br>'''LOG'''</td></tr></table><br># Issue the following Linux commands to ensure the loopback interface is not affected by these rules. The computer should be able to communicate with itself with any state and protocol:<br><b><code><span style="pointer-events: none;cursor: default;color:# Make certain you are in your '''c7host''' machine3366CC;font-size:1.2em;">iptables -A INPUT -i lo -p all -j ACCEPT</span></code><br><code><span style="pointer-events: none;cursor: default;color:# Determine the '''external facing address''' of your c7host machine (ip address should start with 103366CC;font-size:1. 2em;">iptables -A INPUT -m state -- otherwisestate RELATED, type ip address in webESTABLISHED -browser)# Have a lab neighbour try to ping that external facing address. Were they successful?j ACCEPT</span></code></b># Issue the following iptables policy Linux command to ADD an exception to the INPUT chain to DROP all allow web-based incoming connectionstraffic (ie. port 80):<br><b><code><span style="pointer-events: none;cursor: default;color:#3366CC;font-size:1.2em;">iptables -P A INPUT DROP-p tcp --dport 80 -j ACCEPT</span></code></b># Have Issue an iptables command to confirm that their is an exception rule to handle incoming tcp packets over port 80.# Use your neighbour try other web-browser to ping confirm that you can now browse the Internet. If you cannot, contact your lab assistant or professor for help.# Determine the '''external facing address''' of your c7host machine.<br>('''Tip:''' in a web-browser, enter the term: '''"ip address"'''. The external facing IP Addressshould start with '''"10."'''). What happened? Why# Provide your external facing address, and provide another lab-mate to ping that external facing address. Were they successful?# Have your neighbour obtain the lab-mate determine THEIR external facing address and obtain that IP Address on THEIR c7host machine.# Issue the following iptables command to ADD ping allow an exception for pings from your neighbour's IPAddresslab-mate:<br><b><code><span style="pointer-events: none;cursor: default;color:#3366CC;font-size:1.2em;">iptables -A INPUT -p icmp -s {neighbour's exeternal external facing address} -j ACCEPT</span></code></b><br>Is # Have your neighbour able to ping YOUR repeat pinging your external facing IP Address. What happened? Why?
# Have your neighbour try to SSH into YOUR c7host. Were they Successful?
# Issue an iptables rule (in a similar way as with the following previous iptables command:<br><b><code><span style="pointer-events: none;cursor: default;color:) to allow an exception for incoming ssh traffic (eg. port #3366CC;font-size:1.2em;">iptables -A INPUT -p tcp -s {22) from your neighbour's exeternal external facing IP address} --sport 22 -j ACCEPT</span></code></b><br>.# Have your neighbour try to SSH into YOUR c7host(at least to get a password prompt). Were they Successful? If so, why?# Issue the following iptables command to add a rule at bottom of OUTPUT chain to DROP http (port 80 connections). Try to think of the command yourself.# Open another web-browser. Can you connect to a webpage?# Issue iptables rule to '''flush''' the OUTPUT chain. Does your web-browser now work?
# Shutdown all VMs and restart your c7host Linux machine.
# List the iptables rules for the INPUT chain. What happened to your iptables rules for the INPUT chain?
# Proceed to the next part to learn how to learn how to make your iptables rules persistent.
 
=== Part 3: Making iptables Policies Persistent ===
# Flush all of your iptables rules by issuing the following command: <b><code><span style="color:#3366CC;font-size:1.2em;">iptables -F</span></code></b>
# Set the default INPUT policy to ACCEPT by issuing the following command: <b><code><span style="color:#3366CC;font-size:1.2em;">iptables -P INPUT ACCEPT</span></code></b>
# Verify there are no iptables rules by issuing the command: <b><code><span style="color:#3366CC;font-size:1.2em;">iptables -L</span></code></b>
# Make a backup of the file '''/etc/sysconfig/iptables''' by issuing the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">cp /etc/sysconfig/iptables-save > /etc/sysconfig/iptables.bk</span></code></b>
#To make the iptables rules '''persistent''' (i.e. keeps rules when system restarts), you issue the command: <br><b><code><span style="color:#3366CC;font-size:1.2em;">iptables-save > /etc/sysconfig/iptables</span></code></b>
# Verify that the file '''/etc/sysconfig/iptables''' exists.
= LAB 7 SIGN-OFF (SHOW INSTRUCTOR) =
===Exclusively for Summer 2020 term, submissions are accepted only online!===
Follow the submission instructions for lab 7 on Blackboard.
{{Admon/important|Time for a new backup!|If you have successfully completed this lab, make a new backup of your virtual machines as well as your host machine.}}
# Switch to your '''c7host''' VM and '''su -''' into root.
# Change to the '''/root/bin''' directory.
# Issue the Linux command: <b><code><span style="color:#3366CC;font-size:1.2em;">wget httphttps://matrixict.senecac.onsenecacollege.ca/~murray.saulops235/ops235labs/lab7-check.bash</span></code></b>
# Give the '''lab7-check.bash''' file execute permissions (for the file owner).
# Run the shell script and if any warnings, make fixes and re-run shell script until you receive "congratulations" message.
#Arrange proof of the following on the screen:<br><blockquote><span style="color:green;font-size:1.5em;">&#x2713;</span> '''centos2''' VM:<blockquote><ul><li>have logged into centos3 VM using '''public key authentication''' (with a pass-phrase)</li></ul></blockquote><span style="color:green;font-size:1.5em;">&#x2713;</span> '''c7host''' Machine:<blockquote><ul><li>have tunneled Xwindows application from '''centos1''' via ssh</li><li>Run the '''lab7-check.bash''' script in front of your instructor (must have all <b><code><span style="color:#66cc00;border:thin solid black;font-size:1.2em;">&nbsp;OK&nbsp;</span></code></b> messages)</li></ul></blockquote><span style="color:green;font-size:1.5em;">&#x2713;</span> '''Lab7''' log-book filled out.
 
= Practice For Quizzes, Tests, Midterm &amp; Final Exam =
[[Category:OPS235]]
[[Category:OPS235 Labs]]
[[Category:CentOSS 7]]
[[Category:SSD2]]
[[Category:Digital Classroom]]
572
edits