本文轉自:https://blog.csdn.net/wind0419/article/details/78413978
目前的廣告路由器,或多或少都跟wifidog相關,而nodogsplash就是與wifidog一樣功能,除了沒有遠程服務器認證之外。
對於其內部分析,以nodogsplash開始較爲方便。
其本質爲:標記包,然後針對標記的包做防火牆規則更新。
主要用到iptables幾個方面:MARK,NAT 和 MANGLE
首先我們看看iptables的包處理流向:
想要在連接的客戶端訪問web時,彈出指定的廣告頁,我們需要以下過程:
1. 怎麼知道客戶在訪問web?
2. 怎麼修改客戶在訪問URL,進而返回給客戶?
3. 彈出廣告一次之後,怎麼做到正常訪問?
針對以上問題,nodogsplash的處理機制是這樣的:
1. 重定向80端口數據到內部的http服務器(2050端口)
2. 內部的http服務器,返回302,307等http相應給客戶端
3. 客戶端訪問http服務器返回的廣告頁,同時給包打上標記
4. 客戶端再次訪問,檢測到已打標記,放行通過
對應的iptables規則如下:
重定向80端口數據:
iptables -t nat -S
-A PREROUTING -i br-lan -j ndsOUT
-A ndsOUT -m mark --mark 0x200/0x700 -j ACCEPT
-A ndsOUT -m mark --mark 0x400/0x700 -j ACCEPT --標記爲200和400的包放行
-A ndsOUT -p tcp -m tcp --dport 53 -j ACCEPT
-A ndsOUT -p udp -m udp --dport 53 -j ACCEPT -- DNS放行
-A ndsOUT -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.8.1:2050 --重定向80端口
-A ndsOUT -j ACCEPT
返回302重定向
新的nodogsplash採用libmicrohttp作爲內部的http服務器,其具體使用各位自行查閱
microhttp的鏈接處理回調爲libmicrohttpd_cb
int
libmicrohttpd_cb(void *cls,
struct MHD_Connection *connection,
const char *url,
const char *method,
const char *version,
const char *upload_data, size_t *upload_data_size, void **ptr)
{
t_client *client;
char *ip_addr;
char *mac;
int ret;
s_config *config = config_get_config();
const char *redirect_url;
debug(LOG_DEBUG, "access: %s %s", method, url);
/* only allow get */
if (0 != strcmp(method, "GET")) {
debug(LOG_DEBUG, "Unsupported http method %s", method);
return send_error(connection, 503);
}
/* switch between preauth, authenticated */
/* - always - set caching headers
* a) possible implementation - redirect first and serve them using a tempo redirect
* b) serve direct
* should all requests redirected? even those to .css, .js, ... or respond with 404/503/...
*/
ip_addr = get_ip(connection);
mac = arp_get(ip_addr);
client = client_list_find(ip_addr, mac);
if (client) {
if (client->fw_connection_state == FW_MARK_AUTHENTICATED ||
client->fw_connection_state == FW_MARK_TRUSTED) {
/* client already authed - dangerous!!! This should never happen */
debug(LOG_WARNING, "client already authed - dangerous!!! This should never happen");
//ret = authenticated(connection, ip_addr, mac, url, client);
redirect_url = config->redirectURL;
ret = send_redirect_temp(connection, redirect_url); //我修改了此處,針對已經認證通過的客戶端再次訪問2050時,
直接返回重定向網頁,而不是再次做一次認證流程
free(mac);
free(ip_addr);
return ret;
}
}
ret = preauthenticated(connection, ip_addr, mac, url, client);
free(mac);
free(ip_addr);
return ret;
}
打標記,主要爲MARK使用
數據包進來和出去時,打MARK
iptables -t mangle -S
-P PREROUTING ACCEPT
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-P POSTROUTING ACCEPT
-N ndsBLK
-N ndsINC
-N ndsOUT
-N ndsTRU
-A PREROUTING -i br-lan -j ndsOUT
-A PREROUTING -i br-lan -j ndsBLK
-A PREROUTING -i br-lan -j ndsTRU
-A FORWARD -o eth0.2 -p tcp -m tcp --tcp-flags SYN,RST SYN -m comment --comment "!fw3: wan (mtu_fix)" -j TCPMSS --clamp-mss-to-pmtu
-A POSTROUTING -o br-lan -j ndsINC
-A ndsINC -d 192.168.8.100/32 -j MARK --set-xmark 0xa400/0xa400
-A ndsINC -d 192.168.8.100/32 -j ACCEPT
-A ndsOUT -s 192.168.8.100/32 -m mac --mac-source 00:0E:C6:FA:E9:1F -j MARK --set-xmark 0xa400/0xa400
MARK匹配,進入時
iptables -t nat -S
-N ndsOUT
-A PREROUTING -i br-lan -j ndsOUT
-A ndsOUT -m mark --mark 0x200/0x700 -j ACCEPT
-A ndsOUT -m mark --mark 0x400/0x700 -j ACCEPT
MARK匹配,轉發時
iptables -S
-N ndsAUT
-N ndsNET
-A FORWARD -i br-lan -j ndsNET
-A ndsNET -m mark --mark 0x100/0x700 -j DROP
-A ndsNET -m conntrack --ctstate INVALID -j DROP
-A ndsNET -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
-A ndsNET -m mark --mark 0x200/0x700 -j ACCEPT
-A ndsNET -m mark --mark 0x400/0x700 -j ndsAUT
-A ndsNET -p tcp -m tcp --dport 53 -j ACCEPT
-A ndsNET -p udp -m udp --dport 53 -j ACCEPT
-A ndsNET -j REJECT --reject-with icmp-port-unreachable
整個流程爲:
第一次進入時,沒有任何標記,80被重定向到2050,之後客戶端訪問了重定向網頁,被標記爲400;
之後的數據包,NAT表PREROUTING鏈,數據進入ndsOUT自定義鏈,--mark 0x400/0x700 -j ACCEPT,400包直接放行