Connecting a protected web application to WAF

Target

Configure proxying of HTTP and HTTPS traffic to the protected application via WAF.

Prerequisites

  1. WAF installed in reverse proxy mode;

  2. Secured web application accessible with WAF. Everywhere in the text below app_ip is its address, app_port is its port. Below it is suggested to use the vulnerable dvwa application on the same server where the WAF is installed. You can use any other web application instead;

  3. SSH client (for example, Putty).

Result

The successful implementation of this work is the proxying of transactions to the protected application through the WAF.

Preparing a protected web application

  1. Installing the DVWA web application on the WAF server.

sudo docker pull vulnerables/web-dvwa
  1. Running a web application on port 8080.

sudo docker run --rm -d -p 8080:80 --name dvwa vulnerables/web-dvwa

For DVWA application:

app_ip = 127.0.0.1

app_port = 8080 login: admin password: password

  1. To access a web application named dvwa, you need to add a line to the hosts file (client machine) to resolve the domain name locally: IP_WAF dvwa

The hosts file is located in the following paths:

On Unix systems - /etc/hosts

On Windows systems - C:\Windows\System32\drivers\etc\hosts

Procedure

  1. General settings (optional)

    1. Increase the upload file size limit in /etc/ibatyr-nginx/user/http.conf.

      client_max_body_size 1024m;
      
    2. Add the upstream HTTP proxy server address(es) to the trusted addresses in /etc/ibatyr-nginx/user/http.conf.

      set_real_ip_from proxy_ip_address;
      
    3. Disable LRB-CACHE in /etc/ibatyr-nginx/user/server.conf.

      set $LRB_CACHE 0;
      
  2. Proxying HTTP traffic.

    1. Connect via ssh (putty) to the WAF server.

    2. Make sure that the WAF server has network access to the protected application.

      curl -vk http://app_ip:app_port
      
    3. Delete the default configuration file ibatyr-nginx.

      sudo rm /etc/ibatyr-nginx/sites-enabled/default
      
    4. Open the ibatyr-nginx configuration file for editing in any text editor with superuser rights. For example,

      sudo vim /etc/ibatyr-nginx/sites-enabled/dvwa
      
    5. Enter settings for proxying HTTP traffic to the backend of the protected application. Example configuration file contents:

      server {
          include static/server.conf;
          listen 80;
          server_name dvwa;
      
          # ibatyr WAF configuration.
          set $BACKEND http://app_ip:app_port;
      
          location / {
              include static/waf.conf;
          }
      }
      
    6. After making changes, you need to check the correctness of the changes made and, making sure that there are no errors, apply the ibatyr-nginx configuration.

      sudo ibatyr-nginx test
      sudo ibatyr-nginx reload
      
    7. If there are no errors, after loading the new configuration, the port on which the ibatyr-nginx service listens for incoming connections from web application clients should be open. Figure 1 shows an example of the output of the ss -4lnt command.

image1

Figure 1 - Command output

  1. To check the settings you have made, you can make a request to the WAF using a browser or the console curl web client. An example of a request executed on the WAF server command line.

    curl -H"host:dvwa" -v localhost
    
  2. To diagnose problems, see APPENDIX 1. Guidelines for diagnosing network connection problems.

  1. HTTPS traffic termination (optional)

    1. Create a folder to store certificates and keys.

      sudo mkdir /etc/ibatyr-nginx/ssl
      
    2. Generate or receive from the application owner a key and certificate for SSL termination. An example command to generate a key and a self-signed certificate.

      openssl req -x509 -newkey rsa:4096 -keyout key.pem -nodes -out cert.pem -days 365
      

      Figure 2 shows the output of this command.

image2

Figure 2 - Command output

  1. Place the key and certificate in the created folder.

    sudo mv cert.pem key.pem /etc/ibatyr-nginx/ssl
    
  2. Create another ibatyr-nginx configuration file.

    sudo vim /etc/ibatyr-nginx/sites-enabled/dvwa_ssl
    

    Example configuration file contents:

    server {
        include static/server.conf;
        listen 443 ssl;
        ssl_certificate /etc/ibatyr-nginx/ssl/cert.pem;
        ssl_certificate_key /etc/ibatyr-nginx/ssl/key.pem;
        server_name dvwa;
    
        # iBatyr WAF configuration.
        set $BACKEND http://app_ip:app_port;
    
        location / {
            include static/waf.conf;
        }
    }
    
  3. Apply configuration.

    sudo systemctl reload ibatyr-nginx.service
    
  4. Check application availability via WAF.

    curl -k -v -H"host: dvwa" https://localhost:443
    
  5. To diagnose problems, see APPENDIX 1. Guidelines for diagnosing network connection problems.

APPENDIX 1: Tips for diagnosing network connection problems

If the application is not accessible via WAF, it is recommended to check the correctness of the following aspects:

  1. Network addressing.

ip a
  1. Name resolution.

host example.com
  1. Routing.

ip ro
  1. Application availability directly.

curl -v http://example.com
curl -k -v https://example.com
  1. Status of the ibatyr-nginx service.

sudo systemctl status ibatyr-nginx.service
  1. List of open ports.

ss -lnt
sudo ss -4lntp
  1. There are errors in the ibatyr-nginx service log.

sudo journalctl -xefu ibatyr-nginx
  1. There are errors in the ibatyr-nginx configuration.

sudo ibatyr-nginx test
  1. Link to a description of how nginx parses requests (if there are problems with specifying the web application name in the nginx configuration).

http://nginx.org/ru/docs/http/request_processing.html