Restricting outgoing SMTP with Exim

Posted September 30, 2015 9.5k views

I am trying to secure my droplet to prevent too many emails being sent out possibly my malicious scripts that get injected to the server. I know one option would be to monitor the logs and when a certain limit is exceeded per hour I get notified by email but I would like to know if there is a way to set limits to how many outgoing emails can be sent by the server or for a way to restrict outgoing smtp to only specific users i.e. exim, sendmail and root.

I have a droplet with Ubuntu 14 and Exim4.

Any help would be greatly appreciated. Thanks

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hey there,

As mentioned here, you can restrict how many emails per hour a user can send out with the following in your exim configuration:

ratelimit = 50 / 1h / strict / $authenticated_sender

Personally I’ve used csf to alert on such an occasion. You can set the emails sent to root to forward to an email account that you can keep an eye on. You can do this by placing a file with an email address named /root/.forward. Some systems may prefer a different method or settings (SELINUX contexts for example).

CSF controls the firewall by writing it’s own iptables rules. You can use the /etc/csf/csf.conf file to set the default allowed ports in and out for both TCP and UDP. There are other configuration files like /etc/csf/csf.allow and /etc/csf/csf.ignore / /etc/csf/csf.pignore where you can whitelist IP addresses and processes.

LFD is another component that is installed with CSF. It stands for Login Failure Daemon. That process will watch for failed login attempts and automatically block IP addresses. It will also watch mail logs and alert when there are excessive emails sent per hour or suspicious processes running.

Here is an article that describes the install and setup of CSF on Ubuntu.

While it’s a little more complex than rate limiting exim, it really helps pinpoint the issue and the exact cause. More often than not you’ll be notified of the exact script generating the emails.

Happy coding,

Jon Schwenn
Platform Support Specialist

  • Thank you Jon,

    I installed CSF some time back so that has been of some help. I didn’t know about the rate limit option, thanks for that tip. It actually helps solve the problem I have.

    For anyone that may be interested an alternative to discovering spam script I found is this link. It was pretty useful in helping me find scripts that were sending spam emails.