| 1 | |
|---|
| 2 | Host Security Exercises |
|---|
| 3 | ----------------------- |
|---|
| 4 | |
|---|
| 5 | I. System Services |
|---|
| 6 | |
|---|
| 7 | In this section we'll be using some of the commands that are used to |
|---|
| 8 | monitor a running system. Unix Systems Administrators use these |
|---|
| 9 | commands every day. Remember to use the manual pages if you need |
|---|
| 10 | to check the options and syntax of commands: |
|---|
| 11 | |
|---|
| 12 | % man ps |
|---|
| 13 | % man netstat |
|---|
| 14 | |
|---|
| 15 | ( the manpages below will not be there until you install the utilities ) |
|---|
| 16 | |
|---|
| 17 | % man lsof |
|---|
| 18 | % man nmap |
|---|
| 19 | % man last |
|---|
| 20 | % man acct |
|---|
| 21 | |
|---|
| 22 | You need to know how to stop and start services. |
|---|
| 23 | |
|---|
| 24 | a) service |
|---|
| 25 | |
|---|
| 26 | 1. Start a webserver on your system. Verify that the |
|---|
| 27 | system is running using: 1) ps 2) lsof 3) nmap |
|---|
| 28 | |
|---|
| 29 | % sudo apt-get install apache2 |
|---|
| 30 | |
|---|
| 31 | Did the webserver start up after install? |
|---|
| 32 | If you open a browser, can you connect to: http://pcXX.ws.nsrc.org |
|---|
| 33 | |
|---|
| 34 | 2. Stop the service |
|---|
| 35 | |
|---|
| 36 | % sudo service apache2 stop |
|---|
| 37 | |
|---|
| 38 | Try the browser test again. Can you connect? |
|---|
| 39 | |
|---|
| 40 | 3. Start the service again |
|---|
| 41 | |
|---|
| 42 | % sudo service apache2 start |
|---|
| 43 | |
|---|
| 44 | Now, instead of using a browser, let's verify that the service |
|---|
| 45 | is running using all of our tools: %ps, %lsof, %netstat, and %nmap |
|---|
| 46 | |
|---|
| 47 | * INSTALL THE nmap and lsof PACKAGES! * |
|---|
| 48 | |
|---|
| 49 | % sudo apt-get install nmap |
|---|
| 50 | % sudo apt-get install lsof |
|---|
| 51 | |
|---|
| 52 | Now let's see what is running on the system: |
|---|
| 53 | |
|---|
| 54 | % ps -af | grep apache2 |
|---|
| 55 | |
|---|
| 56 | % netstat -apt |
|---|
| 57 | |
|---|
| 58 | % netstat -lpt # is the webserver running??? |
|---|
| 59 | |
|---|
| 60 | % netstat -lnpt ( what is different on this command ) |
|---|
| 61 | |
|---|
| 62 | % nmap localhost # is the webserver running??? |
|---|
| 63 | |
|---|
| 64 | % sudo lsof | grep apache2 |
|---|
| 65 | |
|---|
| 66 | % sudo lsof | grep apache2 | grep TCP # is the webserver running??? |
|---|
| 67 | |
|---|
| 68 | Now, stop the service again. |
|---|
| 69 | |
|---|
| 70 | 4. Stop the service again |
|---|
| 71 | |
|---|
| 72 | % sudo service apache2 stop |
|---|
| 73 | |
|---|
| 74 | Now run your different commands for looking at the system again. |
|---|
| 75 | |
|---|
| 76 | % ps -af | grep apache2 |
|---|
| 77 | % netstat -lpt |
|---|
| 78 | % nmap localhost |
|---|
| 79 | % sudo lsof | grep apache2 |
|---|
| 80 | |
|---|
| 81 | Did you see anything running? |
|---|
| 82 | |
|---|
| 83 | b) update-rc.d |
|---|
| 84 | |
|---|
| 85 | Now, let's make sure that we have all of the systems in place |
|---|
| 86 | so that if the machine is rebooted, we know whether or not the |
|---|
| 87 | apache2 service is going to be started. |
|---|
| 88 | |
|---|
| 89 | 1. see what is there now |
|---|
| 90 | |
|---|
| 91 | % ls /etc/init.d |
|---|
| 92 | |
|---|
| 93 | % ls /etc/rc3.d |
|---|
| 94 | |
|---|
| 95 | % ls /etc/rc5.d |
|---|
| 96 | |
|---|
| 97 | Are the apache startup files in the system? |
|---|
| 98 | That is, do you see files named: /etc/rc3.d/SXXapache2 |
|---|
| 99 | or named /etc/rc5.d/SXXapache2 ??? |
|---|
| 100 | |
|---|
| 101 | 2. let's say we do *NOT* want apache2 to run at startup. |
|---|
| 102 | Let's disable the service using the "update-rc.d" command: |
|---|
| 103 | |
|---|
| 104 | % sudo service apache2 stop |
|---|
| 105 | |
|---|
| 106 | % sudo update-rc.d apache disable |
|---|
| 107 | |
|---|
| 108 | Now let's look at those directories again. |
|---|
| 109 | Do we have any startup files in /etc/rc?.d/S*apache* ??? |
|---|
| 110 | |
|---|
| 111 | % ls /etc/init.d |
|---|
| 112 | % ls /etc/rc3.d |
|---|
| 113 | % ls /etc/rc5.d |
|---|
| 114 | |
|---|
| 115 | Take a look at rc3.d and rc5.d directories. |
|---|
| 116 | What other scripts run in rc3.d ??? |
|---|
| 117 | |
|---|
| 118 | c) initctl |
|---|
| 119 | |
|---|
| 120 | List the running services? What is the command option |
|---|
| 121 | you use with initctl to show all services? |
|---|
| 122 | |
|---|
| 123 | % man initctl |
|---|
| 124 | |
|---|
| 125 | % sudo initctl ??? |
|---|
| 126 | |
|---|
| 127 | ------------------------------------ |
|---|
| 128 | |
|---|
| 129 | II. System Updates |
|---|
| 130 | |
|---|
| 131 | Let's make sure the system is up-to-date. |
|---|
| 132 | When ever we install a system, the first thing |
|---|
| 133 | we do is apply updates. |
|---|
| 134 | |
|---|
| 135 | a) system updates |
|---|
| 136 | |
|---|
| 137 | % sudo apt-get update # this updates the package cache |
|---|
| 138 | |
|---|
| 139 | % sudo apt-get upgrade # this performs the upgrade |
|---|
| 140 | |
|---|
| 141 | Now let's make sure that we have Security updates automatically. |
|---|
| 142 | To do this we need the "unattended-upgrades" package? |
|---|
| 143 | |
|---|
| 144 | b) security updates |
|---|
| 145 | |
|---|
| 146 | Do you already have the packages? |
|---|
| 147 | |
|---|
| 148 | % sudo ls /etc/apt/apt.conf.d |
|---|
| 149 | |
|---|
| 150 | If not, install it: |
|---|
| 151 | |
|---|
| 152 | % sudo apt-get install unattended-upgrades |
|---|
| 153 | |
|---|
| 154 | Now let's check again: |
|---|
| 155 | |
|---|
| 156 | % sudo ls /etc/apt/apt.conf.d |
|---|
| 157 | |
|---|
| 158 | ------------------------------------ |
|---|
| 159 | |
|---|
| 160 | III. Filesystem Integrity |
|---|
| 161 | |
|---|
| 162 | In this section, we'll add the programs necessary for monitoring |
|---|
| 163 | filesystem integrity. We'll do this at multiple levels, using the |
|---|
| 164 | debsums, the fcheck, and the incron packages. |
|---|
| 165 | |
|---|
| 166 | a) debsums |
|---|
| 167 | |
|---|
| 168 | You keep the checksums of the files up to date. You must remember to |
|---|
| 169 | update the checksums after you make major changes to the system. |
|---|
| 170 | |
|---|
| 171 | % sudo apt-get install debsums |
|---|
| 172 | |
|---|
| 173 | Initialize the debsums database: |
|---|
| 174 | |
|---|
| 175 | % sudo debsums_init |
|---|
| 176 | |
|---|
| 177 | Now let's change something in the filesystem and see if |
|---|
| 178 | debsums can detect it: |
|---|
| 179 | |
|---|
| 180 | % sudo mv /sbin/ss /sbin/st |
|---|
| 181 | |
|---|
| 182 | % sudo debsums -c |
|---|
| 183 | |
|---|
| 184 | Did debsums detect the change??? |
|---|
| 185 | |
|---|
| 186 | % let's move the file back in place |
|---|
| 187 | |
|---|
| 188 | % sudo mv /sbin/st /sbin/ss |
|---|
| 189 | |
|---|
| 190 | b) incrond |
|---|
| 191 | |
|---|
| 192 | Inotify in the kernel can provide real-time notification of filesystem |
|---|
| 193 | changes. Install the incron package and configure incrond to monitor important |
|---|
| 194 | filesystems. |
|---|
| 195 | |
|---|
| 196 | % sudo apt-get install incron |
|---|
| 197 | |
|---|
| 198 | % tail /var/log/sys |
|---|
| 199 | |
|---|
| 200 | % cd /etc/incron.d |
|---|
| 201 | |
|---|
| 202 | % vi globals # add the following line to the globals file: |
|---|
| 203 | |
|---|
| 204 | /etc IN_MODIFY,IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/bin/logger -p news.warn "$% $@/$#" |
|---|
| 205 | |
|---|
| 206 | That's it. The changes you make to incron are updated automatically. |
|---|
| 207 | Because incron can recognize changes, it even recognizes when you change |
|---|
| 208 | the configuration for incron, and it updates. |
|---|
| 209 | |
|---|
| 210 | Now add a file to the /etc directory: |
|---|
| 211 | |
|---|
| 212 | % sudo touch /etc/dog |
|---|
| 213 | |
|---|
| 214 | Take a look at /var/log/syslog. What does it say??? |
|---|
| 215 | |
|---|
| 216 | % sudo tail /var/log/syslog |
|---|
| 217 | |
|---|
| 218 | From now on, any changes you make in the /etc directory will |
|---|
| 219 | generate syslog messages. |
|---|
| 220 | |
|---|
| 221 | ------------------------------------ |
|---|
| 222 | |
|---|
| 223 | IV. Enable System Accounting |
|---|
| 224 | |
|---|
| 225 | System accounting gives us logs of all the commands that |
|---|
| 226 | have run and terminated on the system. Let's see if we |
|---|
| 227 | have the acct package: |
|---|
| 228 | |
|---|
| 229 | % which sa |
|---|
| 230 | |
|---|
| 231 | Did "which" find the command? If not install the package: |
|---|
| 232 | |
|---|
| 233 | % sudo apt-get install acct |
|---|
| 234 | |
|---|
| 235 | % which sa |
|---|
| 236 | |
|---|
| 237 | Let's run a command and see if acct records it. |
|---|
| 238 | |
|---|
| 239 | % whoami |
|---|
| 240 | |
|---|
| 241 | % sudo sa -u |
|---|
| 242 | |
|---|
| 243 | Did "sa" show a record for the command? |
|---|
| 244 | |
|---|
| 245 | Let's try the "lastcomm" command as well: |
|---|
| 246 | |
|---|
| 247 | % lastcomm sysadm |
|---|
| 248 | |
|---|
| 249 | --- |
|---|
| 250 | |
|---|
| 251 | Now we have a system that is up-to-date, and it |
|---|
| 252 | gets security updates automatically. We are monitoring |
|---|
| 253 | the system files with debsums, and we are logging changes |
|---|
| 254 | immediately as well with the incron/inotify. We have |
|---|
| 255 | disabled services that are not necessary. And we have |
|---|
| 256 | accounting records to log commands. |
|---|
| 257 | |
|---|
| 258 | This is basic host security that system administrators |
|---|
| 259 | will do on every host they deploy. |
|---|
| 260 | |
|---|
| 261 | --- End |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|