Recently got hacked. How should I prevent it from happening again? (Django / Ubuntu / Postgres / Gunicorn / Nginx)

May 27, 2015 2k views
Django Security Ubuntu

So our website (clothing e-commerce startup) was recently attacked by an SYN flood initiated by a Chinese IP. It took almost a full day getting everything back up, reloading the database, copying images etc. - Needless to say, not a very pleasant experience and lost a lot of precious time.

Tech stack: Django 1.6.7/ Postgres / Nginx / Gunicorn / Elasticsearch

I want to know what all I can do to prevent something like this from happening again. Have already done the following steps, following the DO tutorials (they are great btw) at

  1. Updated all packages and downloaded security updates
  2. Set up SSH keys
  3. Disabled root login
  4. Changed SSH port to a random large number
  5. Setup UFW with disabling all incoming and outgoing connections except for the ports on which services are running (For Nginx, Gunicorn, Elasticsearch, Postgres and Redis)

Anything else I can do to prevent such a hack from happening again? Also do I need to install fail2ban given that root login is disabled and UFW rules are setup?

1 comment
  • I'm also looking at a way to block all IP addresses from China - can anyone suggest an easy way to do that?

3 Answers

I am not tech expert here but no matter how strong steps we take, if some one wants to hack, he/she or they will hack.. Heck even country specific security and intelligence agencies with highest level protection possible gets hacked..

Anyway, try CDN service as a layer of extra protection and services like cloudflare allows you to block specific region, like China for ex.

Manual via server rules, and firewall, you can block, but professional service like CDN services has much better database of such blocked IP or faster update as IP gets handed over from one ISP to another !!

Regarding security, best is a good backup cycle, with professional website like yours, top up with modular backup also..

So when things goes wrong, restore ASAP..

With CDN, even if server IP gets change, impact is minimal since Domain was and will continue to resolve to that CDN provider and its CDN provider's end your IP details may get modified..

I agree with hunky, but there are many tools that may help you out a bit. I would highly recommend OSSEC Great write up on DO here It is an intrusion detection system, but has a couple of ips features like iptables dropping. This should stop brute force attacks. Also is you limit your root login to Digital Ocean to ssh keys that would almost certainly reduce the risk of hacking. That said make sure file and folder permissions are all good and search the web for other things to do. Web applications are usually the weak point in a hacking attempt as their surface area is much greater. Hope this helps! And good luck!

by finid
This tutorial will show you how to install and configure OSSEC to monitor one DigitalOcean server running Ubuntu 14.04 LTS. We'll configure OSSEC so that if a file is modified, deleted, or added to the server, OSSEC will notify you by email - in real-time. That's in addition to other integrity-checking features that OSSEC offers.

MicroPyramid sponsored Django-mfa is a simple package to add extra layer of security to your django web application. It gives web app a randomly changing password as an extra protection. Follow documentation and raise issues if you want further support, features.

Read more about multifactor authentication:
Get the code in our git repository,
For detail documentation visit

Have another answer? Share your knowledge.