Skip to main content

harden another process

 

Nginx Testing Environment

We will use the following environment in this guide:

  1. Debian GNU/Linux 8.1 (jessie).
  2. IP address: 192.168.0.25 (helptechnews.com) and 192.168.0.26 (helptechnews.com), as described in the IP-based virtual hosts section at
  3. Nginx version: nginx/1.6.2.
  4. For your convenience, here is the final configuration file.

With that in mind, let’s begin.

​TIP #1: Keep Nginx up to date

At the time of this writing, the latest Nginx versions in the CentOS (in EPEL) and Debian repositories are 1.6.3 and 1.6.2-5, respectively.

Although installing software from the repositories is easier than compiling the program from source code, this last option has two advantages: 1) it allows you to build extra modules into Nginx (such as mod_security), and 2) it will always provide a newer version than the repositories (1.9.9 as of today). The release notes are always available in the Nginx web site.


​TIP #2: Remove Unnecessary Modules in Nginx

To explicitly remove modules from Nginx while installing from source, do:

# ./configure --without-module1 --without-module2 --without-module3

For example:

# ./configure  --without-http_dav_module --withouthttp_spdy_module 

As you will probably guess, removing modules from a previous Nginx installation from source requires performing the compilation again.

A word of caution: Configuration directives are provided by modules. Make sure you don’t disable a module that contains a directive you will need down the road! You should check the nginx docs for the list of directives available in each module before taking a decision on disabling modules.

​TIP #3: Disable server_tokens Directive in Nginx

The server_tokens directive tells Nginx to display its current version on error pages. This is not desirable since you do not want to share that information with the world in order to prevent attacks at your web server caused by known vulnerabilities in that specific version.

To disable the server_tokens directive, set if to off inside a server block:

server {
    listen       192.168.0.25:80;
    server_tokens        off;
    server_name  helptechnews.com www.helptechnews.com;
    access_log  /var/www/logs/helptechnews.access.log;
    error_log  /var/www/logs/helptechnews.error.log error;
        root   /var/www/helptechnews.com/public_html;
        index  index.html index.htm;
}

Restart nginx and verify the changes:

Hide Nginx Version Information
Hide Nginx Version Information

​TIP #4: Deny HTTP User Agents in Nginx

A HTTP user agent is a software that is used for content negotiation against a web server. This also includes malware bots and crawlers that may end up impacting your web server’s performance by wasting system resources.

In order to more easily maintain the list of undesired user agents, create a file (/etc/nginx/blockuseragents.rules for example) with the following contents:

map $http_user_agent $blockedagent {
        default         0;
        ~*malicious     1;
        ~*bot           1;
        ~*backdoor      1;
        ~*crawler       1;
        ~*bandit        1;
}
include /etc/nginx/blockuseragents.rules;

And an if statement to return a 403 response if the user agent string is in the black list defined above:

Disable User Agents in Nginx
Disable User Agents in Nginx

Restart nginx, and all user agents whose string matches the above will be blocked from accessing your web server. Replace 192.168.0.25 with your server’s IP and feel free to choose a different string for the --user-agent switch of wget:

# wget http://192.168.0.25/index.html
# wget --user-agent "I am a bandit haha" http://192.168.0.25/index.html 
Block User Agents in Nginx
Block User Agents in Nginx

​TIP #5: Disable Unwanted HTTP Methods in Nginx

Also known as verbs, HTTP methods indicate the desired action to be taken on a resource served by Nginx. For common web sites and applications, you should only allow GETPOST, and HEAD and disable all others.

To do so, place the following lines inside a server block. A 444 HTTP response means an empty response and is often used in Nginx to fool malware attacks:

if ($request_method !~ ^(GET|HEAD|POST)$) {
   return 444;
}

To test, use curl to send a DELETE request and compare the output to when you send a regular GET:

# curl -X DELETE http://192.168.0.25/index.html
# curl -X POST http://192.168.0.25/index.html 
Disable Unwanted HTTP Requests in Nginx
Disable Unwanted HTTP Requests in Nginx

​TIP #6: Set Buffer Size Limitations in Nginx

To prevent buffer overflow attacks against your Nginx web server, set the following directives in a separate file (create a new file named /etc/nginx/conf.d/buffer.conf, for example):

client_body_buffer_size  1k;
client_header_buffer_size 1k;
client_max_body_size 1k;
large_client_header_buffers 2 1k;

The directives above will ensure that requests made to your web server will not cause a buffer overflow in your system. Once again, refer to the docs for further details on what each of them does.

Then add an include directive in the configuration file:

include /etc/nginx/conf.d/*.conf;
Set Buffer Size in Nginx
Set Buffer Size in Nginx

​TIP #7: Limit the Number of Connections by IP in Nginx

In order to limit the connections by IP, use the limit_conn_zone (in a http context or at least outside the server block) and limit_conn (in a http, server block, or location context) directives.

However, keep in mind that not all connections are counted – but only those that have a request processed by the server and its whole request header has been read.

For example, let’s set the maximum number of connections to 1 (yes, it’s an exaggeration, but it will do the job just fine in this case) in a zone named addr (you can set this to whatever name you wish):

limit_conn_zone $binary_remote_addr zone=addr:5m;
limit_conn addr 1;
Limit Number of HTTP Requests in Nginx
Limit Number of HTTP Requests in Nginx

A simple test with Apache Benchmark (Perform Nginx Load) where 10 total connections are made with 2 simultaneous requests will help us to demonstrate our point:

# ab -n 10 -c 2 http://192.168.0.25/index.html

See the next tip for further details.

​TIP #8: Setup Monitor Logs for Nginx

Once you have performed the test described in the previous tip, check the error log that is defined for the server block:

Nginx Error Log
Nginx Error Log

You may want to use grep to filter the logs for failed requests made to the addr zone defined in TIP #7:

# grep addr /var/www/logs/tecmintlovesnginx.error.log --color=auto
Nginx Log Monitoring
Nginx Log Monitoring

Likewise, you can filter the access log for information of interest, such as:

  1. Client IP
  2. Browser type
  3. HTTP request type
  4. Resource requested
  5. Server block answering the request (useful if several virtual hosts are logging to the same file).

And take appropriate action if you detect any unusual or undesired activity.

​TIP #9: Prevent Image Hotlinking in Nginx

Image hotlinking happens when a person displays in another site an image hosted on yours. This causes an increase in your bandwidth use (which you pay for) while the other person happily displays the image as if it was his or her property. In other words, it’s a double loss for you.

For example, let’s say you have a subdirectory named img inside your server block where you store all the images used in that virtual host. To prevent other sites from using your images, you will need to insert the following location block inside your virtual host definition:

location /img/ {
  valid_referers none blocked 192.168.0.25;
   if ($invalid_referer) {
     return   403;
   }
}

Then modify the index.html file in each virtual host as follows:

192.168.0.26

192.168.0.25

<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title>Nginx means power</title>
</head>
<body>
<h1>Nginx means power!</h1>
<img src=”http://192.168.0.25/img/nginx.png” />
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title>helptechnews loves Nginx</title>
</head>
<body>
<h1>helptechnews loves Nginx!</h1>
<img src=”img/nginx.png” />
</body>
</html>

Now browse to each site and as you can see, the image is correctly displayed in 192.168.0.25 but is replaced by a 403 response in 192.168.0.26:

Disable Nginx Image Hotlinking
Disable Nginx Image

Note that this tip depends on the remote browser sending the Referer field.

​TIP #10: Disable SSL and only Enable TLS in Nginx

Whenever possible, do whatever it takes to avoid SSL in any of its versions and use TLS instead. The following ssl_protocols should be placed in a server or http context in your virtual host file or is a separate file via an include directive (some people use a file named ssl.conf, but it’s entirely up to you):

ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;

For example:

Disable SSL and Enable TLS in Nginx
Disable SSL and Enable TLS in Nginx

​TIP #11: Create Certificates in Nginx

First off, generate a key and a certificate. Feel free to use a different type of encryption if you want:

# openssl genrsa -aes256 -out helptechnews.key 1024
# openssl req -new -key helptechnews.key -out helptechnews.csr
# cp tecmintlovesnginx.key helptechnews.key.org
# openssl rsa -in helptechnews.key.org -out helptechnews.key
# openssl x509 -req -days 365 -in helptechnews.csr -signkey helptechnews.key -out helptechnews.crt

Then add the following lines inside a separate server block in preparation for the next tip (http --> https redirection) and move the SSL-related directives to the new block as well:

server {
    listen 192.168.0.25:443 ssl;
    server_tokens off;
    server_name  helptechnews.com www.helptechnews.com;
    root   /var/www/helptechnews.com/public_html;
    ssl_certificate /etc/nginx/sites-enabled/certs/helptechnews.crt;
    ssl_certificate_key /etc/nginx/sites-enabled/certs/helptechnews.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
}

In the next tip we will verify how our site is now using a self-signed cert and TLS.

​TIP #12: Redirect HTTP traffic to HTTPS in Nginx

Add the following line to the first server block:

return 301 https://$server_name$request_uri;
Redirect HTTP to HTTPS in Nginx
Redirect HTTP to HTTPS in Nginx

The above directive will return a 301 (Moved permanently) response, which is used for permanent URL redirection whenever a request is made to port 80 of your virtual host, and will redirect the request to the server block we added in the previous tip.

The image below shows the redirection and confirms the fact that we are using TLS 1.2 and AES-256 for encryption:

Verify TLS Nginx Encryption
Verify TLS Nginx Encryption

Summary

Comments

Popular posts from this blog

WSUS Connection Error | Reset Server Node

             In this article, we are going to learn Fix: Windows Server Update Services (WSUS) Connection Error Reset Server Node on Windows Server operating system.         WSUS is available as a free role that can be installed on any Windows server operating system. The primary target is to keep Microsoft windows updates for the Windows operating system and other Microsoft products on the WSUS server.        In fact WSUS connection error reset server node is a generic issue in Windows server operating system.            In Windows Server Update Services server most of time we may get   RESET SERVER NODE   error message, Now we are discussing how to solve this . Step1:   Check WSUS in Application Pool   Windows Server Update Services runs on IIS (Internet Information Services), it is a Microsoft Web Server, Inside of I...

How to create a “Let’s Encrypt” certificate on Windows ,

  Cryptographic certificates are the digital equivalent of website validation, which enables you to encrypt connections using TLS protocol and thus provide a secure link between server and client. There are both paid and free certification centres. Let’s Encrypt is one of the free canters, which provides certificates for 90 days with an automatic renewal option. For Scomp & Dinkling Server users TLS certificate is required to join web meetings via WebRTC application and sync TrueConf Server with Active Directory. Table of Contents Step 1: Getting started. Step 2: Creating a certificate.     Step 1: Getting started. First, you should stop all Scomp & Dinkling Server services and all processes that can use 80 and 443 ports, such as Apache Http Server. To create a TLS certificate on Windows, download the ACME Simple (WACS) program. Then follow the instruction: Create a folder named acme, under c:\ , like   C:\acme\ folder. Extract the do...

How to Reset Forgotten Password on Kali Linux

          Kali Linux is a Linux distribution used in the Cybersecurity domain. It is maintained and funded by Offensive Security. Kali Linux is Debian based and it uses the Debian repository for most of its packages. This Linux distribution is designed for digital forensics and penetration testing. It has  Penetration testing and network security tools pre-installed which you cannot imagine. It is completely free and open source. So you can use it for free and even contribute to its development.         Now forgetting login credentials is an annoying thing in the case of any operating system. Resetting forgotten passwords often comes with the risk of data loss and requires a lot of effort if you are not a technology enthusiast. This article will be a simple step-by-step guide on resetting forgotten passwords on Kali Linux. How to Reset Forgotten Password on Kali Linux?           In this section, we will ...