wifidog 源码初分析(一)

wifidog 的核心还是依赖于 iptables 防火墙过滤规则来实现的,所以建议对 iptables 有了了解后再去阅读 wifidog 的源码。

在路由器上启动 wifidog 之后,wifidog 在启动时会初始化一堆的防火墙规则,如下:

/** Initialize the firewall rules

*/

int iptables_fw_init(void)

{

const s_config *config;

char * ext_interface = NULL;

int gw_port = 0;

t_trusted_mac *p;

fw_quiet = 0;

LOCK_CONFIG();

config = config_get_config();

gw_port = config->gw_port;

if (config->external_interface) {

ext_interface = safe_strdup(config->external_interface);

} else {

ext_interface = get_ext_iface();

}

if (ext_interface == NULL) {

UNLOCK_CONFIG();

debug(LOG_ERR, "FATAL: no external interface");

return 0;

}

/*

*

* Everything in the MANGLE table

*

*/

/* Create new chains */

iptables_do_command("-t mangle -N " TABLE_WIFIDOG_TRUSTED);

iptables_do_command("-t mangle -N " TABLE_WIFIDOG_OUTGOING);

iptables_do_command("-t mangle -N " TABLE_WIFIDOG_INCOMING);

/* Assign links and rules to these new chains */

iptables_do_command("-t mangle -I PREROUTING 1 -i %s -j " TABLE_WIFIDOG_OUTGOING, config->gw_interface);

iptables_do_command("-t mangle -I PREROUTING 1 -i %s -j " TABLE_WIFIDOG_TRUSTED, config->gw_interface);//this rule will be inserted before the prior one

iptables_do_command("-t mangle -I POSTROUTING 1 -o %s -j " TABLE_WIFIDOG_INCOMING, config->gw_interface);

for (p = config->trustedmaclist; p != NULL; p = p->next)

iptables_do_command("-t mangle -A " TABLE_WIFIDOG_TRUSTED " -m mac --mac-source %s -j MARK --set-mark %d", p->mac, FW_MARK_KNOWN);

/*

*

* Everything in the NAT table

*

*/

/* Create new chains */

iptables_do_command("-t nat -N " TABLE_WIFIDOG_OUTGOING);

iptables_do_command("-t nat -N " TABLE_WIFIDOG_WIFI_TO_ROUTER);

iptables_do_command("-t nat -N " TABLE_WIFIDOG_WIFI_TO_INTERNET);

iptables_do_command("-t nat -N " TABLE_WIFIDOG_GLOBAL);

iptables_do_command("-t nat -N " TABLE_WIFIDOG_UNKNOWN);

iptables_do_command("-t nat -N " TABLE_WIFIDOG_AUTHSERVERS);

/* Assign links and rules to these new chains */

iptables_do_command("-t nat -A PREROUTING -i %s -j " TABLE_WIFIDOG_OUTGOING, config->gw_interface);

iptables_do_command("-t nat -A " TABLE_WIFIDOG_OUTGOING " -d %s -j " TABLE_WIFIDOG_WIFI_TO_ROUTER, config->gw_address);

iptables_do_command("-t nat -A " TABLE_WIFIDOG_WIFI_TO_ROUTER " -j ACCEPT");

iptables_do_command("-t nat -A " TABLE_WIFIDOG_OUTGOING " -j " TABLE_WIFIDOG_WIFI_TO_INTERNET);

iptables_do_command("-t nat -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j ACCEPT", FW_MARK_KNOWN);

iptables_do_command("-t nat -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j ACCEPT", FW_MARK_PROBATION);

iptables_do_command("-t nat -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -j " TABLE_WIFIDOG_UNKNOWN);

iptables_do_command("-t nat -A " TABLE_WIFIDOG_UNKNOWN " -j " TABLE_WIFIDOG_AUTHSERVERS);

iptables_do_command("-t nat -A " TABLE_WIFIDOG_UNKNOWN " -j " TABLE_WIFIDOG_GLOBAL);

iptables_do_command("-t nat -A " TABLE_WIFIDOG_UNKNOWN " -p tcp --dport 80 -j REDIRECT --to-ports %d", gw_port);

/*

*

* Everything in the FILTER table

*

*/

/* Create new chains */

iptables_do_command("-t filter -N " TABLE_WIFIDOG_WIFI_TO_INTERNET);

iptables_do_command("-t filter -N " TABLE_WIFIDOG_AUTHSERVERS);

iptables_do_command("-t filter -N " TABLE_WIFIDOG_LOCKED);

iptables_do_command("-t filter -N " TABLE_WIFIDOG_GLOBAL);

iptables_do_command("-t filter -N " TABLE_WIFIDOG_VALIDATE);

iptables_do_command("-t filter -N " TABLE_WIFIDOG_KNOWN);

iptables_do_command("-t filter -N " TABLE_WIFIDOG_UNKNOWN);

/* Assign links and rules to these new chains */

/* Insert at the beginning */

iptables_do_command("-t filter -I FORWARD -i %s -j " TABLE_WIFIDOG_WIFI_TO_INTERNET, config->gw_interface);

iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m state --state INVALID -j DROP");

/* XXX: Why this? it means that connections setup after authentication

stay open even after the connection is done...

iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m state --state RELATED,ESTABLISHED -j ACCEPT");*/

//Won't this rule NEVER match anyway?!?!? benoitg, 2007-06-23

//iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -i %s -m state --state NEW -j DROP", ext_interface);

/* TCPMSS rule for PPPoE */

iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -o %s -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu", ext_interface);

iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -j " TABLE_WIFIDOG_AUTHSERVERS);

iptables_fw_set_authservers();

iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j " TABLE_WIFIDOG_LOCKED, FW_MARK_LOCKED);

iptables_load_ruleset("filter", "locked-users", TABLE_WIFIDOG_LOCKED);

iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -j " TABLE_WIFIDOG_GLOBAL);

iptables_load_ruleset("filter", "global", TABLE_WIFIDOG_GLOBAL);

iptables_load_ruleset("nat", "global", TABLE_WIFIDOG_GLOBAL);

iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j " TABLE_WIFIDOG_VALIDATE, FW_MARK_PROBATION);

iptables_load_ruleset("filter", "validating-users", TABLE_WIFIDOG_VALIDATE);

iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j " TABLE_WIFIDOG_KNOWN, FW_MARK_KNOWN);

iptables_load_ruleset("filter", "known-users", TABLE_WIFIDOG_KNOWN);

iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -j " TABLE_WIFIDOG_UNKNOWN);

iptables_load_ruleset("filter", "unknown-users", TABLE_WIFIDOG_UNKNOWN);

iptables_do_command("-t filter -A " TABLE_WIFIDOG_UNKNOWN " -j REJECT --reject-with icmp-port-unreachable");

UNLOCK_CONFIG();

return 1;

}

在该 防火墙规则的初始化过程中,会首先清除掉已有的防火墙规则,重新创建新的过滤链,另外,除了通过iptables_do_command("-t nat -A "TABLE_WIFIDOG_UNKNOWN " -p tcp --dport 80 -j REDIRECT --to-ports %d",gw_port); 这个命令将 接入设备的 80 端口(HTTP)的访问重定向至网关自身的 HTTP 的端口之外,还通过iptables_fw_set_authservers(); 函数设置了 鉴权服务器(auth-server) 的防火墙规则:

void iptables_fw_set_authservers(void)

{

const s_config *config;

t_auth_serv *auth_server;

config = config_get_config();

for (auth_server = config->auth_servers; auth_server != NULL; auth_server = auth_server->next) {

if (auth_server->last_ip && strcmp(auth_server->last_ip, "0.0.0.0") != 0) {

iptables_do_command("-t filter -A " TABLE_WIFIDOG_AUTHSERVERS " -d %s -j ACCEPT", auth_server->last_ip);

iptables_do_command("-t nat -A " TABLE_WIFIDOG_AUTHSERVERS " -d %s -j ACCEPT", auth_server->last_ip);

}

}

}

首先从上面的代码可以看出 wifidog 支持多个鉴权服务器,并且针对每一个鉴权服务器设置了如下两条规则:

1)在filter表中追加一条[任何访问鉴权服务器都被接受]的WiFiDog_$ID$_AuthServers过滤链:iptables -t filter -A  WiFiDog_$ID$_AuthServers -d auth-server地址 -j ACCEPT

2)在nat表中追加一条[任何访问鉴权服务器都被接受]的WiFiDog_$ID$_AuthServers过滤链:iptables -t nat -A WiFiDog_$ID$_AuthServers  -d auth-server地址 -j ACCEPT

这样确保可以访问鉴权服务器,而不是拒绝所有的出口访问。

本文由http://www.wifidog.pro/2014/12/08/wifidog%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90.html整理编辑,转载请注明出处

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,192评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,186评论 1 303
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,844评论 0 252
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,471评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,876评论 3 294
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,891评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,068评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,791评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,539评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,772评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,250评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,577评论 3 260
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,244评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,146评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,949评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,995评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,812评论 2 276

推荐阅读更多精彩内容

  • 1.安全技术 (1)入侵检测与管理系统(Intrusion Detection Systems): 特点是不阻断任...
    尛尛大尹阅读 2,441评论 0 2
  • 上一篇分析了 接入设备 在接入路由器,并发起首次 HTTP/80 请求到路由器上时,wifidog 是如何将此 H...
    3c937c88e6c0阅读 1,333评论 0 2
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 8,604评论 0 23
  • 2012年研二暑假,我鼓足勇气一个人只身北上,让朋友帮忙联系了住处,去北大修习课程,圆了自己一个北大梦。这是我送给...
    酸爽的橙子阅读 256评论 0 0
  • 我是奶茶,我有一个百万粉丝的梦想。没有受过专业的播音主持训练,也没有任何在电台的从业经验,只是怀着一颗对于播音和电...
    暖心奶茶阅读 380评论 1 1