連上Wi-Fi 熱點自動彈窗的實現方法

  當咱們連上某個熱點, 會自動彈出登陸窗口的專業名稱叫作: Captive portal php

  原理, 實現方式有三種

  1 : dns 跳轉, 在熱點上面實現配置, 把全部dns請求返回都配置爲:服務器地址 ;服務器有404跳轉或者DNS url跳轉 , 跳轉到的界面即爲自動彈出登陸界面;html

  2 :   http跳轉,對全部的http請求返回302或者301或者404跳轉, 跳轉到的界面即爲自動彈出登陸界面;node

  3 :   ip跳轉,既把全部的ip包裏的目標地址改成認證服務器,而後在認證服務器上作404跳轉linux

  第三種實現起來比較麻煩, 咱們要實現第一種和第二種 Captive portal;git

  配置本地服務器

  使用NodeJS搭建一個本地WEB服務器, 服務器端口爲默認的80, 不管訪問服務器的任意地址 都有正常200返回,app.use函數是精華.., 而後使用node app.js啓動服務器:github

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require("http");
var index = require('./routes/index');

var app = express();

app.set('views', path.join(__dirname, 'views'));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use('/', index);
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//該函數是關鍵 , 如論用戶訪問任何頁面 , 都會重定向到本地index.html文件
app.use(function(req, res, next) {
  res.status = 200;
  res.redirect('/index.html');
});

http.createServer(app).listen(80);

  

  使用HTTP跳轉

  本機環境是kali系統, 外加一個usb網卡, 使用如下代碼實現熱點:express

#!/bin/bash
## quick and dirty AP with hostapd and dnsmasq
## exit properly with ctrl-c

echo "Exit this script with Ctrl-C and it will attempt to clean up properly."

if [ -z $1 ]; then
   echo -n "SSID: "
   read ssid
else
   ssid=$1
fi

# get wep key
function get_wep_key() { 
    echo -n "WEP Key [must be exactly 5 or 13 ascii characters]: " 
    read wep_key
    if [[  $wep_key =~ ^[a-zA-Z0-9]{5}$ ]] ; then
       echo "Key accepted"
   elif [[  $wep_key =~ ^[a-zA-Z0-9]{13}$ ]] ; then
        echo "Key accepted"
    else
        echo "WEP key must be exactly 5 or 13 characters"
        get_wep_key
    fi
}

# get mac
function get_mac() {
    echo -n "Enter MAC address in the following format AB:CD:EF:12:34:56: "
    read new_mac
    if [[  $new_mac =~ ^[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}$ ]] ; then
        macchanger --mac=$new_mac wlan0
   else
        echo "MAC Address format not correct."
        get_mac
    fi
}

# ask for WEP
echo -n "Do you want WEP enabled? [y/n]: "
read wep
case $wep in
    y*)
        get_wep_key
    ;;
    *)
    ;;
esac

# ask for MAC change
echo -n "Do you want to change your MAC? [y/n]: "
read changemac
case $changemac in
    y*)
        echo -n "Custom MAC? [y/n]: "
      read random_mac
        case $random_mac in
            y*)
                get_mac
            ;;
            n*)
                macchanger -r wlan0
            ;;
            *)
                echo "Invalid choice, keeping current MAC address."
            ;;
        esac
    ;;
   n*)
    ;;
esac

# install packages if need be
if [ $(dpkg-query -W -f='${Status}' dnsmasq 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
  apt-get install dnsmasq
fi
if [ $(dpkg-query -W -f='${Status}' hostapd 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
  apt-get install hostapd
fi

# trap control c
trap ctrl_c INT

function ctrl_c() {
   echo "wlan0 managed mode"
   iwconfig wlan0 mode managed
   echo "downing wlan0"
   ifconfig wlan0 down
   echo "flushing firewall"
   iptables -F
   iptables -F -t nat
   echo "resetting wlan0 mac"
   macchanger -p wlan0
   kill -9 `cat /tmp/dnsmasq.run`
}


## script begins

# stop and disable services
service hostapd stop
service dnsmasq stop
pkill -9 dnsmasq
pkill -9 hostapd

# bring up wlan0
nmcli radio wifi off
rfkill unblock wlan
iwconfig wlan0 mode monitor
ifconfig wlan0 10.0.0.1/24 up

# forwarding and nat
sysctl -w net.ipv4.conf.all.route_localnet=1
iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to 10.0.0.1:80

# dns masq conf
cat > /tmp/dnsmasq.conf <<!
bind-interfaces
interface=wlan0
dhcp-range=10.0.0.2,10.0.0.254
!

# hostapd conf
cat > /tmp/hostapd.conf<<!
interface=wlan0
driver=nl80211
ssid=${ssid}
hw_mode=g
channel=6
!

# if WEP key, add to hostapd conf
if [[ -n $wep_key ]]; then echo -e "wep_default_key=0\nwep_key0=\"${wep_key}\"" >> /tmp/hostapd.conf; fi

# run dnsmasq and hostapd
dnsmasq --pid-file=/tmp/dnsmasq.run -C /tmp/dnsmasq.conf
hostapd /tmp/hostapd.conf
View Code

  以上代碼中:json

    sysctl -w net.ipv4.conf.all.route_localnet=1
    iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to 10.0.0.1:80小程序

  是本例子的精華所在, 第一句的意思是指全部的ipv4請求所有重定向到本地, 第二句的意思爲全部準備訪問本地80端口的機器,所有指向到10.0.0.1的80端口, 而咱們本地的80端口爲咱們部署的服務器, 這樣便可實現 Captive portal bash

 

  使用DNS跳轉

  在本地網關搭建DNS服務器, 局域網內部的全部DNS請求所有返回10.0.0.1 

  sh文件代碼以下:

#!/bin/bash
## quick and dirty AP with hostapd and dnsmasq
## exit properly with ctrl-c

echo "Exit this script with Ctrl-C and it will attempt to clean up properly."

if [ -z $1 ]; then
   echo -n "SSID: "
   read ssid
else
   ssid=$1
fi

# get wep key
function get_wep_key() { 
    echo -n "WEP Key [must be exactly 5 or 13 ascii characters]: " 
    read wep_key
    if [[  $wep_key =~ ^[a-zA-Z0-9]{5}$ ]] ; then
       echo "Key accepted"
   elif [[  $wep_key =~ ^[a-zA-Z0-9]{13}$ ]] ; then
        echo "Key accepted"
    else
        echo "WEP key must be exactly 5 or 13 characters"
        get_wep_key
    fi
}

# get mac
function get_mac() {
    echo -n "Enter MAC address in the following format AB:CD:EF:12:34:56: "
    read new_mac
    if [[  $new_mac =~ ^[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}$ ]] ; then
        macchanger --mac=$new_mac wlan0
   else
        echo "MAC Address format not correct."
        get_mac
    fi
}

# ask for WEP
echo -n "Do you want WEP enabled? [y/n]: "
read wep
case $wep in
    y*)
        get_wep_key
    ;;
    *)
    ;;
esac

# ask for MAC change
echo -n "Do you want to change your MAC? [y/n]: "
read changemac
case $changemac in
    y*)
        echo -n "Custom MAC? [y/n]: "
      read random_mac
        case $random_mac in
            y*)
                get_mac
            ;;
            n*)
                macchanger -r wlan0
            ;;
            *)
                echo "Invalid choice, keeping current MAC address."
            ;;
        esac
    ;;
   n*)
    ;;
esac

# install packages if need be
if [ $(dpkg-query -W -f='${Status}' dnsmasq 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
  apt-get install dnsmasq
fi
if [ $(dpkg-query -W -f='${Status}' hostapd 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
  apt-get install hostapd
fi

# trap control c
trap ctrl_c INT

function ctrl_c() {
   echo "wlan0 managed mode"
   iwconfig wlan0 mode managed
   echo "downing wlan0"
   ifconfig wlan0 down
   echo "flushing firewall"
   iptables -F
   iptables -F -t nat
   echo "resetting wlan0 mac"
   macchanger -p wlan0
   kill -9 `cat /tmp/dnsmasq.run`
}


## script begins

# stop and disable services
service hostapd stop
service dnsmasq stop
pkill -9 dnsmasq
pkill -9 hostapd

# bring up wlan0
nmcli radio wifi off
rfkill unblock wlan
iwconfig wlan0 mode monitor
ifconfig wlan0 10.0.0.1/24 up

#dhcp
iptables --policy INPUT ACCEPT
iptables --policy FORWARD ACCEPT
iptables --policy OUTPUT ACCEPT
iptables -F
iptables -t nat -F
iptables -t nat -A PREROUTING -i wlan0 -p udp --dport 53 -j DNAT --to 10.0.0.1

# dns masq conf
cat > /tmp/dnsmasq.conf <<!
bind-interfaces
interface=wlan0
address=/#/10.0.0.1
dhcp-range=10.0.0.2,10.0.0.254
dhcp-option=6,10.0.0.1 #DNS
dhcp-option=3,10.0.0.1 #Gateway
dhcp-option=252,"http://wpad.example.com/wpad.dat\n" #WPAD
dhcp-authoritative
!

# hostapd conf
cat > /tmp/hostapd.conf<<!
interface=wlan0
driver=nl80211
ssid=${ssid}
hw_mode=g
channel=6
!

# if WEP key, add to hostapd conf
if [[ -n $wep_key ]]; then echo -e "wep_default_key=0\nwep_key0=\"${wep_key}\"" >> /tmp/hostapd.conf; fi

# run dnsmasq and hostapd
dnsmasq --pid-file=/tmp/dnsmasq.run -C /tmp/dnsmasq.conf
hostapd /tmp/hostapd.conf
View Code

  其中如下代碼尤其重要:

  bind-interfaces
  interface=wlan0
  address=/#/10.0.0.1  《==這個配置說明爲全部dns請求都返回10.0.0.1 
  dhcp-range=10.0.0.2,10.0.0.254  《==客戶機器IP池
  dhcp-option=6,10.0.0.1 #DNS   《==本地DNS服務器
  dhcp-option=3,10.0.0.1 #Gateway 《==本地網關
  dhcp-option=252,"http://wpad.example.com/wpad.dat\n" #WPAD
  dhcp-authoritative

 

  參考

  dnsmasq參考API : https://wiki.archlinux.org/index.php/dnsmasq

  Captive portal是怎樣強制彈出窗口的呢? : https://www.zhihu.com/question/38843766

做者: NONO
出處:http://www.cnblogs.com/diligenceday/
企業網站:http://www.idrwl.com/
開源博客:http://www.github.com/sqqihao
QQ:287101329
微信:18101055830 

廈門點燃將來網絡科技有限公司, 是廈門最好的微信應用, 小程序, 微信網站, 公衆號開發公司

相關文章
相關標籤/搜索