Nefaria.com

I.T.

How to make your GNU/Linux servers just a bit more secure (using iptables)

by musashi on Feb.26, 2010, under I.T.

First, the code:

#!/bin/bash
shopt -s -o nounset
shopt -s extglob

declare APNIC="ftp://ftp.apnic.net/public/stats/apnic/delegated-apnic-latest"
declare AFRINIC="ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest"
declare LACNIC="ftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest"
declare RIPENCC="ftp://ftp.ripe.net/pub/stats/ripencc/delegated-ripencc-latest"
declare IP=unset
declare CACHE_MAX_AGE=30 #Number of days to rely on the cached list of networks
declare USE_CACHE=false  #Do not use cache by default

#Wipe out any existing iptables rules before proceeding
iptables -F

checkcache() {
if [ `find /tmp/ -maxdepth 1 -iname "cached_IP_addresses" -mtime +$CACHE_MAX_AGE` ]; then
        rm -f /tmp/cached_IP_addresses
elif [ -f /tmp/cached_IP_addresses ]; then
        USE_CACHE=true
fi
}

ipfeeder() {
if [ "$USE_CACHE" = "true" ]; then
        cat /tmp/cached_IP_addresses
else
        curl -s $APNIC $AFRINIC $LACNIC $RIPENCC |\
                awk -F'|' '{print $4}'|\
                fgrep ".0.0.0" |\
                sed -e 's:$:/8:g'
fi
}

checkcache

ipfeeder | tee /tmp/cached_IP_addresses | {
        while read IP; do
                #Drop inbound packets from $IP
                iptables -A INPUT -s $IP -j DROP 
                #Drop outbound packets to $IP
                iptables -A OUTPUT -d $IP -j DROP
        done
}       

#Rate limit some protocols

iptables -A INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --set
iptables -A INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --update --seconds 1800 --hitcount 10 -j DROP

This is fairly self-explanatory. Basically, you’re telling iptables to drop any inbound and outbound packets to any of the networks listed in the ipfeeder() function, and just for good measure, we rate limit new incoming ssh connections to 10 every 30 minutes. (If you have port 22 opened to the outside world, have a look in /var/log/secure or /var/log/auth — there’s probably a shitload of break-in attempts that have been logged; grep for sshd and you’ll see what I mean.)

Remember, iptables rules don’t survive reboots! So make sure that this script runs on system startup (e.g., insert a call to the script in /etc/rc.local.)

For detailed lists of all networks in the apnic, afrinic, lacnic, and ripe-ncc registries, visit the following urls:

ftp://ftp.apnic.net/public/stats/apnic/delegated-apnic-latest
ftp://ftp.apnic.net/public/stats/afrinic/delegated-afrinic-latest
ftp://ftp.apnic.net/public/stats/lacnic/delegated-lacnic-latest
ftp://ftp.apnic.net/public/stats/ripe-ncc/delegated-ripencc-latest

1 Comment :, , more...

Configuring “Per User” licensing in Terminal Services, remotely *without* Remote Desktop access

by musashi on Jan.07, 2010, under I.T.

So the other day I was trying to connect to one of the terminal servers that I manage (for the purpose of this post, we’ll call the server ‘TERMSVR01′) and I got the following error message and was promptly disconnected:

The remote session was disconnected because there are no Terminal Server client access licenses available for this computer

At first glance, this seems as though the server ran out of TS CALS (Terminal Server Client Access Licenses). I was pretty sure that the server was configured to use the “Per User” licensing mode. However, a Windows Server 2003 Terminal Server operating in the “Per User” licensing mode can’t run out of licenses to the extent that it prevents the user from connecting (and instead, giving them the aforementioned error message). To the best of my knowledge, it can only do this when it is operating in “Per Device” mode. So this was the assumption that I ran with — that somehow, this server was never configured for “Per User” -or- it was, but the setting was either changed, reset, or corrupted somehow.

So, even though I wasn’t able to connect to TERMSVR01 via Remote Desktop, I was able to “Manage” it remotely by doing the following:

  1. Open “Active Directory Users and Computers” on any Domain Controller
  2. Expand the “Computers” node
  3. Right-click TERMSVR01 and select ‘Manage’

Now we can do a few things (not many) on the server. One thing I wanted was to have a look at the Event Viewer. There were a few error messages like the following:

Event Type: Information
Event Source: TermService
Event Category: None
Event ID: 1004
Date: 1/5/2010
Time: 6:18:23 PM
User: N/A
Computer: TERMSVR01
Description:
The terminal server cannot issue a client license. It was unable to issue the license due to a changed (mismatched) client license, insufficient memory, or an internal error. Further details for this problem may have been reported at the client’s computer.

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

The more of these I saw, the more confident I was that my assumption was correct — the server was operating in “Per Device” mode and it had finally run out of licenses. I had the following options:

  1. Wait for someone to go onsite and reconfigure the licensing mode (easy, but it would have to wait until tomorrow) or…
  2. Attempt to reconfigure this setting and restart the service remotely (so that the setting takes takes effect) … all without having “Remote Desktop” access to the server.

Care to guess which option I chose? :-)

Step #1: Override the licensing mode setting using group policy

  1. Click ‘Start’
  2. Click ‘Run’
  3. Type the following command:
    gpedit.msc /gpcomputer:TERMSVR01
  4. Click ‘OK’

Those four steps open the group policy (remotely) for TERMSVR01. Next we need to actually change the setting:

  1. In the left-hand panel, expand “Administrative Templates”
  2. Expand “Windows Components”
  3. Click on “Terminal Services”
  4. Locate the following setting in the right-hand panel:
    Set the Terminal Server licensing mode
  5. Double-click the aforementioned setting
  6. Change the option (directly below the heading) to “Enabled”
  7. Select “Per User” from the drop-down box (below the heading: “Specify the licensing mode for the terminal server”.)
  8. Click ‘OK’
  9. Close the “Group Policy Object Editor” window

Great. The licensing mode has been changed but the setting won’t take effect until the service is restarted. We could open ’services.msc’ and connect to ‘TERMSVR01′ by using the ‘Connect to another computer …’ option in the ‘Action’ menu. This will allow us to administer almost all running services on TERMSVR01 … almost all. You’ll notice immediately that you cannot start/stop the ‘Terminal Services’ service from this management console, so we need to find another way to do it.

The easiest way I know to accomplish this task is to use the WMIC command from the command prompt.

Step #2: Restart a remote service using WMIC

  1. Open a command prompt
  2. Type the following command (then hit enter) to stop the service:
    wmic /node:TERMSVR01 service where “caption=’Terminal Services’” call StopService
  3. Then, type the following command to start the service:
    wmic /node:TERMSVR01 service where “caption=’Terminal Services’” call StartService
  4. Close the command prompt

If everything was successful (and my assumption about the nature of the problem was correct), then I should be able to connect to the server using the Remote Desktop client. I fired up the client and voilà! It worked perfectly.

1 Comment :, , more...

How to upgrade ClamAV on Ubuntu (Intrepid)

by musashi on Oct.08, 2009, under I.T.

On October 5th, the Clam Antivirus team announced that ClamAV 0.94.x is now entering its end-of-life phase. What’s worse, versions of ClamAV earlier than 0.95 will no longer be able to receive CVD updates; basically rendering any older versions of ClamAV nearly worthless. This is all supposed to happen by April 2010—soon. You can read more about it here.

Good news though, the upgrade process on Ubuntu is pretty easy:

If you haven’t already done so, enable the ‘backports‘ repo by editing your /etc/apt/sources.list file and uncommenting (or, inserting) the following two lines:

deb http://us.archive.ubuntu.com/ubuntu/ intrepid-backports main restricted universe multiverse
deb-src http://us.archive.ubuntu.com/ubuntu/ intrepid-backports main restricted universe multiverse

Then, resynchronize the package index files with the following command:

sudo apt-get update

Next, upgrade ClamAV:

sudo apt-get install clamav-daemon

This command will [sometimes] install apparmor as well; I don’t use apparmor so I uninstall it afterwards:

/etc/init.d/apparmor stop
update-rc.d -f apparmor remove
apt-get remove apparmor apparmor-utils

That’s all there is to it!

root@localhost:~# clamd -V
ClamAV 0.95.2/9874/Thu Oct  8 06:24:12 2009
Leave a Comment :, , , more...

Adding “Trusted Sites” to Internet Explorer, via the registry

by musashi on Oct.01, 2009, under I.T.

A while ago I needed to add a list of websites to the Internet Explorer’s “Trusted Sites” zone for multiple users, scattered across multiple terminal servers. IE’s “Enhanced Security Configuration” (ESC) is configured by default on windows terminal services and it’s normally a good idea to leave it intact.

However, this can have unintended consequences for users who require the use of websites that employ ActiveX, javascript, etc. because, by default, ESC does not allow those items to run. Sometimes, this means that the site in question will only be partially non-functioning. Other times, the entire site will be completely unusable. Furthermore, most users on terminal services have only a limited ability to actually modify the settings for an entire zone. Normally the best thing they can do is add the site to their trusted sites zone, if in fact the site is legitimate (i.e., “trusted”).

Originally, I explained to the users the steps involved in adding a site to their trusted sites, however many of the users used many of the same websites that other users were using. Also, new users needed to be trained on how to do this as well. Needless to say, it got very repetitive, very fast; so I came up with a “global” list of sites that can be trusted, and imported them to the registry on each terminal server. The list consisted of about 40+ sites, and I was able to generate the list mostly by exporting the following registry key:

HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\EscDomains

…from a few user accounts who had already added most of the sites to their trusted sites zone. After grepping out the duplicates (among other things), I had my list.

Now, I’m going to cover two ways of making this list of domains “globally trusted”—both of them involve writing to the following registry key:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\EscDomains

Pay attention! This is not the same key as previously mentioned. This key resides in the ‘HKEY_LOCAL_MACHINE’ hive, whereas the previous key resides in the ‘HKEY_CURRENT_USER’ hive.

The first way is via the following visual basic script:

Option Explicit

Dim DomainArray(5), strComputer, strHTTP, strHTTPS
Dim dwordZone, regPath, objReg, counter, subkeyPath
Dim subkeyValue
Const HKEY_LOCAL_MACHINE = &H80000002

DomainArray(0) = "testdomain0.com"
DomainArray(1) = "testdomain1.com"
DomainArray(2) = "testdomain2.com"
DomainArray(3) = "testdomain3.com"
DomainArray(4) = "testdomain4.com"

strComputer = "."
strHTTP = "http"
strHTTPS = "https"
dwordZone = "2"
regPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings" &_
        "\ZoneMap\EscDomains\"
Set objReg = GetObject("winmgmts:{impersonationLevel = impersonate}!\\" & _
        strComputer & "\root\default:StdRegProv")

For counter = 0 to 4
        subkeyPath = regPath & DomainArray(counter)
        objReg.CreateKey HKEY_LOCAL_MACHINE,subkeyPath
        objReg.SetDWORDValue HKEY_LOCAL_MACHINE,subkeyPath,strHTTP,dwordZone
        objReg.SetDWORDValue HKEY_LOCAL_MACHINE,subkeyPath,strHTTPS,dwordZone
Next

This script will insert ‘testdomain0.com’, ‘testdomain1.com’, [...] into IE’s trusted sites zone when run on any machine. It must be run by an Administrator (or another user who has access to write to the HKEY_LOCAL_MACHINE registry hive), and the changes are global (to the machine).

The next way involves creating a “registry entries” (.reg) file:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\EscDomains]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\EscDomains\testdomain0.com]
"http"=dword:00000002
"https"=dword:00000002

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\EscDomains\testdomain1.com]
"http"=dword:00000002
"https"=dword:00000002

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\EscDomains\testdomain2.com]
"http"=dword:00000002
"https"=dword:00000002

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\EscDomains\testdomain3.com]
"http"=dword:00000002
"https"=dword:00000002

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\EscDomains\testdomain4.com]
"http"=dword:00000002
"https"=dword:00000002

Just like the previous script, this must also be run by a user with Administrator privileges and any changes will be global to all users on the machine.

(Of course, you would want to customize these snippets of code to suit your needs.)

For more information, please visit the following sites:

Internet Explorer Enhanced Security Configuration changes the browsing experience
Enhanced Security Configuration for Internet Explorer
Internet Explorer security zones registry entries for advanced users

Leave a Comment :, , more...

Alternate SMTP port with iptables

by musashi on Sep.29, 2009, under I.T.

Nowadays, more and more ISPs are blocking outbound port 25 (SMTP) for spam prevention or reduction purposes. This should be of concern to sysadmins who have users scattered across multiple ISPs (such as webhosting services) or corporate sysadmins who maintain e-mail for mobile users, for example. The workaround is to use the mail submission agent (MSA) port, 587. Most ISPs do not block outbound traffic for this port. On GNU/Linux, we can use iptables for this task. With a single command, we can configure any inbound traffic, destined for port 587 traffic to be redirected to port 25:

iptables -t nat -I PREROUTING -p tcp --dport 587 -j REDIRECT --to-port 25
Leave a Comment :, , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...