欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

nodogsplash的内部机制分析

发布时间:2024/10/12 编程问答 58 豆豆
生活随笔 收集整理的这篇文章主要介绍了 nodogsplash的内部机制分析 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

目前的广告路由器,或多或少都跟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 ACCEPTMARK匹配,转发时

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包直接放行









总结

以上是生活随笔为你收集整理的nodogsplash的内部机制分析的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。