Logo

dev-resources.site

for different kinds of informations.

Setting up WireGuard VPN with WAG for Enhanced Security and MFA

Published at
11/16/2024
Categories
vpn
wireguard
security
infrasecurity
Author
mayurbhatti
Author
11 person written this
mayurbhatti
open
Setting up WireGuard VPN with WAG for Enhanced Security and MFA

In today’s security-conscious environment, having a VPN setup that integrates multi-factor authentication (MFA) is essential. WAG, a tool that adds 2FA and device enrollment capabilities to WireGuard, enables secure VPN access with MFA for specific routes. This guide walks you through setting up a WireGuard VPN with WAG on an Ubuntu server.

Introduction to WAG with WireGuard

WAG enhances WireGuard by providing 2FA on selected routes while allowing other routes to remain accessible as long as the client has a valid public key. With WAG, you can ensure only authenticated users can access sensitive network resources, securing your VPN further.

Reference: WAG GitHub Repository

Prerequisites

  • System Requirements: This guide assumes you are working on Ubuntu 20.04.
  • Necessary Tools: Make sure iptables is installed and IP forwarding is enabled. Wag must be run as root, to manage iptables and the wireguard device.
sysctl -w net.ipv4.ip_forward=1
Enter fullscreen mode Exit fullscreen mode

Step 1: Install WAG

  • Create Directory: Install WAG in the /opt directory.
mkdir /opt/wag && cd /opt/wag
Enter fullscreen mode Exit fullscreen mode
  • Download WAG:
curl -L $(curl -s https://api.github.com/repos/NHAS/wag/releases/latest | jq -M -r '.assets[0].browser_download_url') -o wag
Enter fullscreen mode Exit fullscreen mode
  • Set Permissions:
chmod 700 wag
Enter fullscreen mode Exit fullscreen mode

Step 2: Generate WAG Configurations

  • Generate the initial configuration file:
sudo ./wag gen-config
Enter fullscreen mode Exit fullscreen mode
  • Rename the generated config file:

After providing valid information in the above command, it will generate one config file with the tag "config.json.*". Rename it to "config.json"

mv config.json.* config.json
Enter fullscreen mode Exit fullscreen mode

Step 3: Modify WireGuard Port and Add ACLs in the Config File

  • Set Port and Add Private Key: Configure WireGuard to listen on a chosen port (e.g., 51820).
"Wireguard": {
    "DevName": "wg0",
    "ListenPort": 51820,
    "PrivateKey": "your_private_key_here",
    "Address": "10.1.2.1/24",
    "MTU": 1420,
    "PersistentKeepAlive": 25,
    "DNS": ["8.8.8.8"]
}
Enter fullscreen mode Exit fullscreen mode
  • Define ACLs: Use ACL policies to enforce 2FA for specific networks while allowing general access to others. Here, a username "mayur" is allowed only for network "0.0.0.0/0, ::/0" but it must enter MFA for network "172.69.0.0/16 and following", mfa defines the network which will be accessible only if it is authorized.

Example ACL Configuration:

"Acls": {
    "Groups": {
        "group:admin": ["mayur"]
    },
    "Policies": {
        "group:admin": {
            "Mfa": [
               "172.69.0.0/16", "172.63.0.0/16"
            ],
            "Allow": [
               "0.0.0.0/0", "::/0"
            ]
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

After adding this, the config will look like this:

{
  "Proxied": false,
  "ExposePorts": null,
  "HelpMail": "<email address>",
  "Lockout": 5,
  "ExternalAddress": "<public ip>",
  "MaxSessionLifetimeMinutes": 1440,
  "SessionInactivityTimeoutMinutes ": 60,
  "ManagementUI": {
    "ListenAddress": "",
    "Enabled": false
  },
  "Webserver": {
    "Public": {
      "ListenAddress": ":8080"
    },
    "Tunnel": {
      "Port": "80"
    }
  },
  "Authenticators": {
    "Issuer": "Wireguard",
    "Methods": ["totp"],
    "DomainURL": "",
    "OIDC": {
      "IssuerURL": "",
      "ClientSecret": "",
      "ClientID": ""
    }
  },
  "Wireguard": {
    "DevName": "wg0",
    "ListenPort": 51820,
    "PrivateKey": "<private key>",
    "Address": "10.1.2.1/24",
    "MTU": 1420,
    "PersistentKeepAlive": 25,
    "DNS": ["8.8.8.8"]
  },
  "DatabaseLocation": "devices.db",
  "Acls": {
    "Groups": {
      "group:admin": ["mayur"]
    },
    "Policies": {
      "group:admin": {
        "Mfa": ["172.69.0.0/16", "172.63.0.0/16"],
        "Allow": ["0.0.0.0/0", "::/0"]
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Start the WAG Service

  • Run WAG with the following command:
./wag start -config config.json
Enter fullscreen mode Exit fullscreen mode

wag will create a database "devices.db" where it will store all users and their respective keys.

Step 5: Set Up WAG as a Service

  • Create a wag.service file for managing WAG via systemd:
sudo nano /etc/systemd/system/wag.service
Enter fullscreen mode Exit fullscreen mode
  • Add the following service configuration:
[Unit]
Description=WireguardManager

[Service]
User=root

WorkingDirectory=/opt/wag
ExecStart=/opt/wag/wag start

# If any of the ExecStarttasks fail, then ExecStopPostwill run
ExecStopPost=/opt/wag/wag cleanup

Restart=on-failure
RestartSec=10
RestartPreventExitStatus=3

KillSignal=SIGINT

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode
  • Enable and start the service:
sudo systemctl enable wag
sudo systemctl start wag
Enter fullscreen mode Exit fullscreen mode

Step 6: Register New Users with WAG

  • Register a user:
./wag registration -add -username your_username
Enter fullscreen mode Exit fullscreen mode

token

This command generates a token for device enrollment.

  • Restart the WAG service:

Now copy the token that was generated from the adding username, Now restart the wag service

sudo systemctl restart wag
Enter fullscreen mode Exit fullscreen mode

Step 7: Download User Configuration File

  • Users can retrieve their VPN configuration file using their generated token:
curl http://{public.server.address}:8080/register_device?key={token}
Enter fullscreen mode Exit fullscreen mode

This will download the config that will later need to be used in the WireGuard Client.


Additional Feature:

Registration - Deals with creating, deleting and listing the registration tokens

Usage of registration:

  • "-add" : Create a new enrolment token
  • "-del" : Delete existing enrolment token
  • "-group value" : Manually set user group (can supply multiple -group, or use -groups for delimited group list, useful for OIDC)
  • "-groups string" : Set user groups manually, ',' delimited list of groups, useful for OIDC
  • "-list" : List tokens
  • "-overwrite string" : Add registration token for an existing user device, will overwrite wireguard public key (but not 2FA)
  • "-socket string" : Wag socket to act on (default "/tmp/wag.sock")
  • "-token string" : Manually set registration token (Optional)
  • "-username string" : User to add device

(Optional) Configure WAG Management UI

  • To enable the WAG UI, make the following changes in config.json:
"ManagementUI": {
    "ListenAddress": ":9000",
    "Enabled": true
}
Enter fullscreen mode Exit fullscreen mode
  • Restart WAG to apply changes:
sudo systemctl restart wag
Enter fullscreen mode Exit fullscreen mode
  • Set up an admin account for the web console:
sudo ./wag webadmin -add -username admin -password admin_password
Enter fullscreen mode Exit fullscreen mode

Now, Just use http://{public_ip}:9000 and we will be able to access the WAG UI console where we will be able to manage the user and config.

wag ui

vpn Article's
30 articles in total
Favicon
Как создать свой VPN и получить доступ ко всему?
Favicon
Installing VPN on iPad: complete instructions
Favicon
How Private Internet Access (PIA) VPN Enhances Online Privacy, Security, and Performance
Favicon
Meilleurs proxy anonymes pour le torrent et la confidentialité
Favicon
Fixing OpenVPN Connection Issues in Ubuntu 24.04
Favicon
Pritunl client cli
Favicon
Your Data, Your Privacy: How VPNs Hide Your Identity
Favicon
What Is the Best Free VPN for Mac?
Favicon
How to Access Google Veo 2 AI Using a VPN
Favicon
Understanding the Concept of VPNs
Favicon
Возьму в оборот)
Favicon
Vpn কী এবং এটি কিভাবে কাজ করে: সহজ ভাষায় বিশ্লেষণ
Favicon
F-Secure Freedome VPN 2.69.35.0 Crack + Activation Code [Latest] Free 2025
Favicon
How to Choose the Best Free VPN Chrome Extension for Your Needs
Favicon
AWS Verified Access preview non-review!
Favicon
PIA VPN Costs and Value
Favicon
Private Internet Access for Android: Top-Tier Mobile Security
Favicon
NordVPN's Monthly Plans and Pricing Explained
Favicon
Anonymous Proxy vs VPN: Key Differences and Best Uses
Favicon
How to Install a Private OpenVPN Server on Ubuntu 22.04
Favicon
Setting up your own secure VPN with Amnezia VPN
Favicon
Setting up WireGuard VPN with WAG for Enhanced Security and MFA
Favicon
Setting up WireGuard VPN at AWS with Terraform
Favicon
Yarp.ReverseProxy
Favicon
Cheap VPN with MFA from Home to AWS
Favicon
Accessing GitHub Action runners using Netbird
Favicon
Best VPN Service: 5 VPN Providers That Actually Deliver Results
Favicon
What is the best way to prevent sites from tracking my multiple accounts?
Favicon
V2Ray VPN and JavaScript: A Powerful Combination for Secure Browsing
Favicon
PotatoVPN Review 2024: Is It Truly Safe?

Featured ones: