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









  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值