Coovachilli. RaspberryPI. Centos 7. Access Point + Hotspot + RADIUS server + Captive Portal

Материал из Wiki
Перейти к навигации Перейти к поиску

Description

Coovachilli at Raspberry PI (with centos 7) as: AP, hotspot, radius server, captive portal

Installation

Download raspberry pi centos 7 img

http://isoredirect.centos.org/altarch/7/isos/armhfp

http://mirrors.powernet.com.ru/centos-altarch/7.9.2009/isos/armhfp/CentOS-Userland-7-armv7hl-generic-Minimal-2009-sda.raw.xz

Utils

yum install -y libnl3-devel libtalloc-devel iptables

yum install -y openssl-devel rfkill psmisc.armv7hl

yum install -y libtool m4 automake gcc gcc-c++ gengetopt patch git net-tools

yum install -y freeradius freeradius-mysql freeradius-utils freeradius-sqlite

Epel-release

yum install -y epel-release

cat > /etc/yum.repos.d/epel.repo << EOF

[epel]

name=Epel rebuild for armhfp

baseurl=https://armv7.dev.centos.org/repodir/epel-pass-1/

enabled=1

gpgcheck=0

EOF

MariaDB

yum install mariadb mariadb-server -y

systemctl enable mariadb.service

systemctl start mariadb.service

mysql_secure_installation

mysqladmin -u root -pPASSWORD#1 create radius

sudo cat /etc/raddb/mods-config/sql/main/mysql/schema.sql | mysql -u root -pPASSWORD#1 radius

mysql -u root -pPASSWORD#1 radius

   GRANT ALL PRIVILEGES ON radius.* to radius@localhost IDENTIFIED by 'PASSWORD#1';

   flush privileges;

   \q

Hostapd

cd /usr/src

sudo git clone git://w1.fi/hostap.git

cd hostap/hostapd

Hostapd does not have a 'configure' command, so before we compile hostapd, we need to change the install prefix. A a quick and simple way to change the default install directory is by using sed:

sed -i "s:export BINDIR ?= /usr/local/bin/:export BINDIR ?= /usr/sbin:g" Makefile

Next, copy default config file:

cp -v defconfig .config

nano .config

# Uncomment the following options:
CONFIG_LIBNL32=y # Use libnl 3.2 libraries
CONFIG_IEEE80211N=y # Enables IEEE 802.11n support
CONFIG_WNM=y # Enables Network Management support
CONFIG_IEEE80211AC=y # Enables IEEE 802.11ac support
CONFIG_DEBUG_FILE=y # Support for writing debug log to file

make

sudo make install

hostapd -v

Create hostapd config file using sample file:

mkdir /etc/hostapd

cp -v /usr/src/hostap/hostapd/hostapd.conf /etc/hostapd/

vim /etc/hostapd/hostapd.conf

# Change the following parameters in hostapd.conf file:
driver=nl80211
interface=wlan0 # Change this to your wireless device
ssid=RPI # Change this to your SSID
hw_mode=g
channel=6 # Enter your desired channel
ieee80211n=1 # Enable IEEE 802.11n
#wpa=1
#wpa_passphrase=myverysecretpassword
wpa_pairwise=TKIP CCMP
rsn_pairwise=CCMP

Create directory for hostapd sockets:

mkdir /var/run/hostapd

Set status of WiFi interface to 'UP' and unblock Wifi if soft switch is on:

rfkill unblock wifi

ip link set dev wlan0 up

Test and start hostapd

sudo hostapd -d /etc/hostapd/hostapd.conf

If all goes well, the hostapd daemon should start and not quit.

Next, create a systemd service file. By now, most Linux distributions use systemd to control services:

nano /etc/systemd/system/hostapd.service

[Unit]
Description=Hostapd IEEE 802.11 AP, IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator
After=mariadb.service network.target

[Service]
Type=forking
PIDFile=/var/run/hostapd.pid
ExecStartPre=/bin/mkdir -p /var/run/hostapd
ExecStart=/usr/sbin/hostapd /etc/hostapd/hostapd.conf -P /var/run/hostapd.pid -B

[Install]
WantedBy=multi-user.target

Enable the service to start automatically at boot:

systemctl enable hostapd.service

systemctl start hostapd.service

FreeRADIUS

Configure the SQL radius module:

nano /etc/raddb/mods-available/sql

    # Uncomment and or change the following parameters:

    driver = "rlm_sql_mysql"
    dialect = "mysql"
    server = "localhost"
    port = 3306
    login = "radius"
    password = "PASSWORD#1"
    read_clients = yes

# Add chillispot SQL counters:

nano /etc/raddb/mods-available/sqlcounter

    # Add this line at the end of the above file:

    $INCLUDE ${modconfdir}/sql/counter/${modules.sql.dialect}/chillispot.conf

Next link sql, sqlcounter to modules available:

ln -s /etc/raddb/mods-available/sql /etc/raddb/mods-enabled/sql

ln -s /etc/raddb/mods-available/sqlcounter /etc/raddb/mods-enabled/sqlcounter

Configure radius clients

nano /etc/raddb/clients.conf

    # Change the password to the password used above for FreeRadius MySQL database:
    secret = PASSWORD#1

Configure radius server:

nano /etc/raddb/radiusd.conf

    # Under "security" section, change the user and group to name created during install:

    user = radiusd
    group = radiusd
    allow_vulnerable_openssl = yes

    # IMPORTANT: Don't do this. You really should update to recent versions of OpenSSL

    # Under the "instantiate" section (Near line 728), add the following counter modules:

    chillispot_max_bytes
    noresetcounter

Configure the default virtual server under sites-available:

nano /etc/raddb/sites-available/default

    # Under authorize section:

    Comment the following:

    #filter_username

    # Uncomment the following:

    auth_log
    unix

    # Change the following:

    '-sql' to sql

    # Add the following at the end of authorize section:

    chillispot_max_bytes
    noresetcounter

    # Next under accounting section, Uncomment the following:

    radutmp

    # Changethe following:

    '-sql' to sql

    # Next under session section, Uncomment the following:

    radutmp
    sql

    $ Next under post-auth section, Uncomment the following:

    reply_log

    # Change the following:

    '-sql' to sql

Configure the inner tunnel requests virtual server under sites-available:

nano /etc/raddb/sites-available/inner-tunnel

    # Under authorize section, change the following:

    '-sql' to sql

    # Add the following at the end of authorize section:

    chillispot_max_bytes
    noresetcounter

    # Next under the session section, Uncomment the following:

    sql

    # Next under post-auth section, Uncomment the following:

    reply_log

    # Change the following:

    '-sql' to sql

Add the following MySQL counters for Chillispot:

nano /etc/raddb/mods-config/sql/counter/mysql/chillispot.conf

    sqlcounter chillispot_max_bytes {
    counter_name = Max-Total-Octets
    check_name = ChilliSpot-Max-Total-Octets
    reply_name = ChilliSpot-Max-Total-Octets
    reply_message = "You have reached your bandwidth limit"
    sql_module_instance = sql
    key = User-Name
    reset = never
    query = "SELECT IFNULL((SUM(AcctInputOctets + AcctOutputOctets)),0) FROM radacct WHERE username = '%{${key}}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%%b'"
    }

Change ownership of config and log directories:

touch /var/log/radius/radutmp

chown -R radiusd:radiusd /etc/raddb

chown -R radiusd:radiusd /var/log/radius

Create Admin User in radius MySQL database:

echo "INSERT INTO radcheck (UserName, Attribute, Value, Op) VALUES ('admin', 'Cleartext-Password', 'PASSWORD#1', ':=');" | mysql -u radius -pPASSWORD#1 radius

Start radius for initialization and testing purposes

/usr/sbin/radiusd -X

Open a new terminal window to test connections:

radtest [ADMIN_USER] [ADMIN_PASSWORD] 127.0.0.1 0 [FREERADIUS_DB_PASS]

radtest admin PASSWORD#1 127.0.0.1 0 PASSWORD#1

If you get a message like this one, then you are done with the minimal and required radius setup for the next steps:

Received Access-Accept Id 174 from 127.0.0.1:1812 to 0.0.0.0:0 length 20

But before leaving radius aside, create a systemd service file for your radius server:

nano /etc/systemd/system/radiusd.service

    [Unit]
    Description=FreeRADIUS high performance RADIUS server.
    After=mariadb.service network.target

    [Service]
    Type=forking
    ExecStartPre=-/bin/mkdir /var/log/radius
    ExecStartPre=-/bin/mkdir /var/run/radiusd
    ExecStartPre=-/bin/chown -R radiusd.radiusd /var/log/radius
    ExecStartPre=-/bin/chown -R radiusd.radiusd /var/run/radiusd
    ExecStartPre=/usr/sbin/radiusd -C
    ExecStart=/usr/sbin/radiusd -d /etc/raddb
    ExecReload=/usr/sbin/radiusd -C
    ExecReload=/bin/kill -HUP $MAINPID

    [Install]
    WantedBy=multi-user.target

Enable the service to start automatically at boot:

systemctl enable radiusd.service

systemctl start radiusd.service

Haserl

Haserl is needed for the embedded miniportal included in CoovaChilli.

Download file haserl-0.9.35.tar.gz.zip, unzip it and copy to RPI /root/

Haserl-0.9.35.tar.gz.zip

cd /root/

tar zxvf haserl-0.9.35.tar.gz -C /usr/src/

cd /usr/src/haserl-0.9.35/

Compile and install

Make sure you change to correct library or desired prefix


./configure --prefix=/usr –libdir=/usr/lib

make

make install

Coovachilli

CoovaChilli is an open source based captive portal software. It started out from the deprecated chilli project. After installation and configuration of coovachilli is done, you will be able to redirect your WiFi hotspot clients to a login page, i.e. captive portal where they can log in and access the Internet.

Download the latest sources for coovachilli:

cd /usr/src

sudo git clone https://github.com/coova/coova-chilli.git

Configure and Compile coova:

Make sure you change to correct library or desired prefix

cd /usr/src/coova-chilli

sh bootstrap

./configure --prefix=/usr --libdir=/usr/lib --localstatedir=/var --sysconfdir=/etc --enable-miniportal --with-openssl --enable-libjson --enable-useragent --enable-sessionstate --enable-sessionid --enable-chilliredir --enable-binstatusfile --enable-statusfile --disable-static --enable-shared --enable-largelimits --enable-proxyvsa --enable-chilliproxy --enable-chilliradsec --with-poll

make

make install

Configure CoovaChilli

All configuration files are located under: /etc/chilli. You will need to create a config file with your sites modifications as follows:

cp -v /etc/chilli/defaults /etc/chilli/config

nano /etc/chilli/config

   #Change the following parameters to match your environment:
   HS_WANIF=eth0 # WAN Interface toward the Internet
   HS_LANIF=wlan0 # Subscriber Interface for client devices
   HS_NETWORK=10.1.0.0 # HotSpot Network (must include HS_UAMLISTEN)
   HS_NETMASK=255.255.255.0 # HotSpot Network Netmask
   HS_UAMLISTEN=10.1.0.1 # HotSpot IP Address (on subscriber network)
   HS_RADSECRET=PASSWORD#1 # Set to be your RADIUS shared secret
   HS_UAMSECRET=PASSWORD#1 # Set to be your UAM secret
   HS_ADMUSR=admin
   HS_ADMPWD=PASSWORD#1

Add the chilli ipup.sh script. The purpose of these script is to prepare the system to act as a router.  You may also wish to add other commands e.g. Setting the gateway.

nano /etc/chilli/ipup.sh

IMPORTANT: Change the Internet device to the correct one.

   #!/bin/sh
   #
   # Allow IP masquerading through this box
   /usr/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Make script executable

chmod 755 /etc/chilli/ipup.sh

Edit service file

nano /etc/rc.d/init.d/chilli

   ...
   #daemon -- $exec -c $CONFIG
   $exec -c $CONFIG
   ...

Enable coovachilli so it starts up at boot time:

systemctl enable chilli

Start Coovachilli

systemctl start chilli

Stop chilli

systemctl stop chilli && killall chilli

Users

Create user

echo "INSERT INTO radcheck (UserName, Attribute, Value, Op) VALUES ('[LOGIN_NAME]', 'Cleartext-Password', '[LOGIN_PASSWORD]', ':=');" | mysql -u radius -p[FREERADIUS_DB_PASS] radius

echo "INSERT INTO radcheck (UserName, Attribute, Value, Op) VALUES ('user', 'Cleartext-Password', '1234', ':=');" | mysql -u radius -pPASSWORD#1 radius

Session-timeout limit user

echo "INSERT INTO radreply (UserName, Attribute, Value, Op) VALUES ('user', 'Session-Timeout', '60', ':=');" | mysql -u radius -p[FREERADIUS_DB_PASS] radius

Test

NOW using a wireless client like smartphone or laptop, open your favourite web browser. Go to any URL/ website of your choice.

If your followed the above steps, then you will be redirected to the captive portal page

Sessions

Show sessions

dnat - user connected not authed, pass - user connected and authed

chilli_query list

   ...
   30-45-96-EE-32-6B 10.1.0.2 dnat 162965300100000001 0 admin 0/0 0/0 96917/0 65283/0 0 0 0/0 0/0 http://10.1.0.1:4990/favicon.ico
   ...

Manual auth connected user

chilli_query authorize ip 10.1.0.2

Manual logout connected and authed user

chilli_query logout ip 10.1.0.2

Radius sessions show

radwho -irR