Saturday, 28 December 2013

How to Find Which Accounts Are Potential Spammers in cPanel/Exim

So you’ve discovered that all of a sudden your server load has shot and your email inbox is getting filled up with hundreds of bounce backs. You sir may be spamming! Now if you are a spammer, this isnt really much of a shock.


But if you’re not a spammer you may be wondering what happened. Well odds are your website got hacked or your personal machine has a virus/malware. Now usually if your website gets hacked its because you are using an outdated version of your CMS software. Because wordpress doesn’t auto update and we don’t always login every day (especially if your wordpress just hosts a static website) it can be hard to keep up with the constant updates. And of course in doing so all the little hackers out there are now able to exploit whatever security holes you didn’t patch. On top of this it isn’t just the wordpress core we have to worry about, but also the plugins and the themes. Joomla and drupal and practically all other CMS’s follow the same logic. Keep your apps up to date and your chances of being compromised slim down quickly. What happens though when you are compromised? Usually the attacker places a php file on the server that acts as part of a ddos or a script that sends out a ton of spam. If your personal machine was compromised then whatever application you are running to connect to your email (such as outlook, thunderbird, etc) is usually used (or they just grab any IMAP/SMTP connection info) and use that to start spamming.


Ok, so we know that someone on the server is spamming. We don’t know if its a script or if its because someone’s personal machine got attacked. Lets take a look a couple one liners to help out with this. First, lets look at a command which searches for all external logins (meaning the personal local machine was compromised)



$ exigrep @ /var/log/exim_mainlog | grep _login | sed -n 's/.*_login:(.*)S=.*/1/p' | sort | uniq -c
1 test@example.com
3 info@example.com
59 notaspamer@example.com

So this will exigrep through our mail log and return any line containing an @ (meaning pretty much everything) cut out the dovecot_login or courier_login (whichever one you use) and then sort it and count how many instances there are. In this case you can see that the email account notaspamer@example.com is sending much, much more than the other two email accounts it found. This doesn’t immediately mean that its a spamming account, it could be legitimate of course but it gets you on the right path.


Now lets look at a few one liners for checking which user/account has been hacked:



$ exigrep yahoo /var/log/exim_mainlog | grep U= | sed -n 's/.*U=(.*)S=.*/1/p' | sort | uniq -c
3 user1 P=local
74 user2 P=local

So here the user “user2” is sending the most email on the system so we know that this user is likely responsible for the spam. Lets see if we can track down the script!



grep "cwd=" /var/log/exim_mainlog | awk '{for(i=1;i<=10;i++){print $i}}' | sort |uniq -c| grep cwd | sort -n | grep /home/

Running this will look at any lines in the exam log that contains the “cwd” string. This should help narrow it down the folder where the spam is happening. But we can get even more specific! Note that this command doesn’t have as high of a success rate as the previous ones but when it works it saves so much headache.



grep X-PHP-Script /var/spool/exim/input/*/*-H | awk '{print $3}' | sort | uniq -c | sort -nr

Now unlike the others this actually searches the active email queue. So if you have hundreds or thousands of email queued up (and you can check this by running exam -bpc) this should work. It looks for the X-PHP-Script field in the header of the emails. This should be enabled by default in cpanel, if not it can be enabled in the whm. But anyways this should again sort and count exactly which script sent the email! Pretty cool, right!

Friday, 20 December 2013

How to Install an SSL Certificate on a Shared IP

It was kind of annoying in the midst of cPanel 11?s release when cPanel discontinued the ability to install an SSL certificate on a shared IP.  Sure, you can do it as ‘nobody’, but this tends to mess up things when you have open_basedir set up or you’re using suPHP. Sometimes you might not have another IP to use, or you’re using an IP-based monitoring system and don’t want to complicate things. I, personally, prefer to install my shared SSL certificates on the shared IP. But that’s just me…and I’m right about everything.


There is a crafty way to get around this limitation, though it does require root access and it can be a little tedious when it’s something you have to do on a normal basis. Come on cPanel, just let us do this again so we don’t have to keep coming up with workarounds. I assume at this point that you already know how to use WHM to install a certificate, and you’re aware that the hostname for the certificate has to correspond with a user already on the server.


First, hop into WHM ~> Install a SSL Certificate and Setup the Domain and install the SSL normally, BUT, in the user field, put ‘nobody’ and finish the installation.


Now that the certificate is installed, you need to force cPanel to accept its fate to allow a shared SSL on the main IP. Go into /var/cpanel/userdata and run the following commands, noting that $user should be replaced with the username owning the SSL domain, $ssldomain.



mv nobody/$ssldomain_SSL* $user/

replace nobody $user — $user/*

Then edit ./nobody/main and remove the SSL domain from the sub_domains list, and run /scripts/rebuildhttpdconf to rebuild the Apache configuration, then restart Apache for the changes to take effect. Voila.


IF this is a shared SSL certificate (meaning, other users on the server can call it via https://$ssldomain/~$user):


You’ll want the SSL to show as the shared certificate on the server, which you can either select from the dropdown in WHM ~> SSL Hosts, or run:



echo $sslhostname > /var/cpanel/.sharedcrtname

If you use mod_php with open_basedir, and/or you have mod_userdir enabled, you need to exclude the user ‘owning’ the SSL certificate from both, to allow other sites on the server to use its certificate. These can both be set in WHM ~> Security Center

Thursday, 19 December 2013

ChkRootKit Installation

About Chkrootkit


chkrootkit is a collection of tools to detect the presence of rootkits, and is a gift to Linux systems administrators for two specific reasons:

  1. it is a free, open source utility, and available for multiple distros,

  2. it detects almost all the latest rootkits out there, since the open source community of contributors keeps it up to date.


Over time, the Chkrootkit scan engine has also improved, making it faster, which is especially useful in performing detailed kernel checks against a number of supported kit detections.

A few great features of chkrootkit are that it detects more than 60 old and new kits, is capable of detecting network interfaces in promiscuous mode, can efficiently detect altered lastlog and wtmp files (which in turn alerts admins about intrusions), has easy command-line access with straightforward options, and has a verbose output mode to help admins automate tasks.

chkrootkit uses C and shell scripts to perform a detailed process check, and scans systems binaries to detect kit signatures. Upon detection, in most cases, it can remove rootkits too. It also has a few algorithms that can report trends of a possible rootkit, even if it is not yet officially supported. The following lists the chkrootkit internal programs and what each of them do.





























chkrootkit ProgramPurpose
chkrootkitMain script to check for tampered system files
strings.cDetects and performs string replacement
ifpromisc.cChecks network interface for promiscuous mode
chklastlog.c, chkwtmp.cChecks if lastlog and wtmp entries are deleted
chkproc.c, chkdirs.cChecks for Linux kernel module-based Trojans

Installation

Follow these steps to install ChkRootKit

cd /usr/local/src/

– Down load the chkrootkit.
# wget http://www.spenneberg.org/chkrootkit-mirror/files/chkrootkit.tar.gz
# wget ftp://ftp.pangeia.com.br/pub/seg/pac/chkrootkit.tar.gz

- Unpack the chkrootkit you just downloaded.
# tar -xvzf chkrootkit.tar.gz

- Change to new directory
# cd chkrootkit-*
(select the version )

- Compile chkrootkit
# make sense

- Run chkrootkit
# ./chkrootkit

How to setup a daily scan report?

- Load crontab
# crontab -e

- Add this line to the top:
===============================================================>
0 1 * * * (cd /usr/local/src/chkrootkit*; ./chkrootkit 2>&1 | mail -s “chkrootkit output” email@domain.com)
===============================================================>

Monday, 16 December 2013

Fixing rndc error in WHM/cPanel (ndc: connection failed: connection refused)

It is quite a common occurrence in cPanel to receive such an error as rndc:
connect failed: connection refused

To get your name servers working, you will need to eliminate this error, it
is quite a simple fix and can be completed in a few minutes via the
standard cPanel /scripts
1. Login to your server as root via SSH
2. Run: /scripts/updatenow
3. Run: /scripts/fixndc

This will fix your problems some of the time, but if it does not, do the
following steps:
1. Login to your server as root via SSH
2. Run: vi /etc/rndc.conf (or vi /etc/namedb/rndc.conf on FreeBSD)
replace all instances of "rndc-key" with "rndckey"
3. Run: vi /etc/named.conf (or vi /etc/namedb/named.conf on FreeBSD)
replace all instances of "rndc-key" with "rndckey"
4. Run: /scripts/fixnamed
5. Run: /scripts/fixndc
6. If you received an error in the last step, run /scripts/fixndc another
time.
7. Restart named (on RH this is service named restart)

If you are still having issues, try checking out the cpanel forums, or
contact cpanel support for more help.

Saturday, 14 December 2013

Helpful Exim Commands

1. To check the number of emails present in the queue:# exim -bpc2. To check the emails present in the queue with the mail id and sender ID:# exim -bp
# exim -bp | less

3. To view the header of a particular email using mail ID:

# exim -MvH mail_id

4.  To view the body of a particular email using mail ID:

# exim -Mvb mail_id

5. To view a message's logs:

# exim -Mvl mail_id




  1. To trace path:



exim -d -bt user@domain.com




  1. To get sorted list of email sender in exim queue:



exim -bpr | grep "<" | awk {'print $4'} |cut -d "<" -f 2 | cut -d ">" -f 1 | sort -n | uniq -c| sort -n




  1. To check the script that will originate spam mails:



grep "cwd=" /var/log/exim_mainlog|awk '{for(i=1;i<=10;i++){print $i}}'|sort| uniq -c|grep cwd|sort -n




  1. If we need to find out exact spamming script. To do this, run following command:



ps auxwwwe | grep user | grep --color=always "/home/user/public_html/templates/" | head



10.  To delete the emails of a specific user:

grep -lr 'user@domain.com' /var/spool/exim/input/ | sed -e 's/^./([a-zA-Z0-9-])-[DH]$/1/g' | xargs exim -Mrm



exim -bp | grep "user_email-account" | awk '{print $3}' | xargs exim -Mrm




  1. To delete Frozen emails from the email queue:



grep -R -l '*** Frozen' /var/spool/exim/msglog/*|cut -b26-|xargs exim -Mrm



exim -bp| grep frozen | awk '{print $3}'| xargs exim -Mrm



exiqgrep -z -i | xargs exim -Mrm



12.  To delete Spam emails from the email queue:

#  grep -R -l [SPAM] /var/spool/exim/msglog/*|cut -b26-|xargs exim -Mrm


  1. To check the no. of frozen mails:



exiqgrep -z -c




  1. To check exim logs:



tail -f /var/log/exim_mainlog




  1. Force delivery of one message:



exim -M mail_id




  1. Force another queue run:



exim -qf




  1. Force another queue run and attempt to flush frozen messages:



exim -qff



Precautions:
1)Turn on the SMTP tweak. It will block the users to bypass the mail server for sending out spam.
2)Turn on blacklisting ability in whm.
3)Use spamassassin to stop receiving spam mails.

Friday, 13 December 2013

Backup a Database using Management Studio

If you are a VPS or Dedicated Server customer running SQL Server on your Server, this article will walk you through how to backup a database and make a copy of a database using Management Studio Express.



Step 1


You will first need to connect to your database server, using SQL Server Management Studio Express. Please see our article “How to connect to a database using SQL Server Management Studio Express” if you require assistance with this.

For this example, we will backup the database named “backmeup” to our “C:backup” folder.

Once connected to your database server, you will need to browse to your database in the left window pane of Management Studio.

Right Click on your database, and under “Tasks” choose “Back Up…“
le_sqlbackup_ms1










Step 2


A new window will open where we will configure where to save the database backup file.

You will want to ensure that the “Backup type” is set to “Full” to get a Full backup of your database.

Now, highlight the Destination file that is already in the list by clicking on it, and Click “Remove“.
le_sqlbackup_ms2











Step 3


Now, Click “Add…” to specify where to save the backup file.
le_sqlbackup_ms3







 

Step 4


A window will pop up, asking you where to save the backup file. Enter a location of your choice. For this example, we will save it to “C:Backupbackmeup-Full-041608.bak“.

Note that this is the entire actual filepath.
le_sqlbackup_ms4











Step 5


Now, we have to make sure that the backup you are creating overwrites all existing backup sets, as appending it to an existing set can cause conflicts when attempting to perform a restore. On the left-hand side of the window, click on ‘Options‘, and then click on ‘Overwrite all existing backup sets‘.
le_sqlbackup_ms5











Step 6


Once this option is in place, all that is left to do is to run the backup! Click “OK” to begin the database backup.
le_sqlbackup_ms6







Step 7


If the database backed up successfully, you should receive a message as pictured below.

le_sqlbackup_ms7

 

 

 

 

 

 

 

 

 

 

 

If at any point you require assistance, please do not hesitate to contact your Technical Support team!

Thursday, 12 December 2013

Installing CSF on Linux Server (ConfigServer Firewall)

This guide will walk you through installing CSF on a VPS/Dedicated server.

Preparing the server



After logging in via SSH (as root!) we will need to download the CSF firewall itself, let's make a temporary directory called "tmp" first and then move into it and download latest version of CSF:

mkdir tmp
cd tmp
wget http://configserver.com/free/csf.tgz


Once CSF is downloaded, we will need to extract the archive containing it and move into the directory it creates:

tar xf csf.tgz
cd csf


We are now ready to install CSF.

Installing CSF on a server with cPanel



CSF includes integration with control panels such as cPanel, in order to install it for a cPanel based server, we would execute the install script that is meant for cPanel.

./install.cpanel.sh


Installing CSF on a server without cPanel



If you do not have cPanel on your server and would still like to install CSF, you can certainly do so. Execute the following command:

./install.sh


Configuring CSF - allowing ports



Configuring your firewall is required for it to function correctly. Let's open the CSF configuration file:

nano /etc/csf/csf.conf


To turn the firewall on, inside the editor, navigate down until you see TESTING="1" and change it to TESTING="0".

All blocks are commented inside (a comment is a line that starts with a #) for you to understand what each section does, for example to add a incoming TCP port to the allow list, we would edit this variable:

TCP_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995,2077,2078,2082,2083,2086,2087,2095,2096"


To allow incoming MySQL connections we would add 3306 to the list:

TCP_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995,2077,2078,2082,2083,2086,2087,2095,2096,3306"


When you make the changes, hit CTRL+X to exit the editor and press Y when it asks you if you want to overwrite the file.

Don’t forget to:


  1. Configure the TCP_IN, TCP_OUT, UDP_IN and UDP_OUT options in the csf configuration to suite your server

  2. Restart csf and lfd


  3. Set TESTING to 0 once you’re happy with the firewall




CSF is preconfigured to work on a cPanel server with all the standard cPanel
ports open. It also auto-configures your SSH port if it’s non-standard on
installation.

You should ensure that kernel logging daemon (klogd) is enabled. Typically, VPS
servers have this disabled and you should check /etc/init.d/syslog and make
sure that any klogd lines are not commented out. If you change the file,
remember to restart syslog.

Now – login to your cPanel server’s WHM as root and go to the bottom  left menu. If already logged in then reload the page. In Plugins – you  will see: ConfigServer Security Firewall
The firewall is STOPPED by default – it is not running. We need to configured it, and then take it out of Test Mode.

Click on Firewall Configuration

ETH_DEVICE =: Set this to eth+

TCP_IN/TCP_OUT/UDP_IN/UDP_OUT  = : These are the ports you want to leave open for your server to  operate. If you change the default SSH port make sure to add it here.  Also add any other services you might have running such as Shoutcast or  game servers. By default most of the ports used should already be  configured.

MONOLITHIC_KERNEL = : 0 Only change this to 1 if your firewall will not start – otherwise leave it as it.

LF_DSHIELD  = 0: Change this option to 86400. This is an automatic updated list of  known attacking IPs. Enabling this will stop them from being able to  connect to your server.

Spam Protection Alerts
If you want to add some spam protection, CSF can help. Look in the configuraiton for the following:

LF_SCRIPT_ALERT  = 0 change this to 1. This will send an email alert to the system  administrator when the limit configured below is reached within an hour.

LF_SCRIPT_LIMIT = 100 change this to 250. This will alert you when any scripts sends out 250 email messages in an hour.

Configuration Complete – Almost
Scroll down to the bottom and click on Change to save the settings. Then click Restart csf+lfd

You should see a big page of ACCEPT and near the bottom you should see:

csf: TESTING mode is enabled – don’t forget to disable it in the configuration
Starting lfd:[  OK  ]

Click on Return

Now TEST all your services to make sure  everything is working – SSH, FTP, http. After you do a few quick tests  go back into the Firewall Configuration page.

TESTING = 1 change this to 0 and click Change at the bottom. Then Restart csf+lfd

That’s it, the firewall is successfully installed and running!!

Firewall Status: Running – you should see this on the main CSF page in WHM.

Uninstallation



Removing csf and lfd is even more simple:

cd /etc/csf
sh uninstall.sh