没有合适的资源?快使用搜索试试~ 我知道了~
首页android流量防火墙iptables原理详解
资源详情
资源评论
资源推荐
Android 网络防火墙的实现 Iptables 解决方案 收藏
通过对 Android SDK 帮助文档的阅读,我没有发现 Android 的高层提供的 API,于是通过
更底层考虑,我发现了可以采用 Iptables 实现防火墙的功能。而且 linux 下主流的防火墙也
是 Iptables。
Iptables 的介绍:
iptables 是与最新的 2.6.x 版本 Linux 内核集成的 IP 信息包过滤系统。如果 Linux 系统连接
到因特网或 LAN、服务器或连接 LAN 和因特网的代理服务器, 则该系统有利于在 Linux
系统上更好地控制 IP 信息包过滤和防火墙配置。
其工作原理:
netfilter/iptables IP 信息包过滤系统是一种功能强大的工具, 可用于添加、编辑和除去规则,
这些规则是在做信息包过滤决定时,防火墙所遵循和组成的规则。这些规则存储在专用的
信息包过滤表中, 而这些表集成在 Linux 内核中。 在信息包过滤表中,规则被分组放在我
们所谓的 链(chain)中。我马上会详细讨论这些规则以及如何建立这些规则并将它们分组
在链中。
虽然 netfilter/iptables IP 信息包过滤系统被称为单个实体,但它实际上由两个组件 netfilter
和 iptables 组成。
netfilter 组件也称为 内核空间(kernelspace),是内核的一部分,由一些信息包过滤表组成
这些表包含内核用来控制信息包过滤处理的规则集。
iptables 组件是一种工具,也称为 用户空间(userspace),它使插入、修改和除去信息包过
滤表中的规则变得容易。 除非您正在使用 Red Hat Linux 7.1 或更高版本,否则需要从
netfilter.org 下载该工具并安装使用它。
通过使用用户空间,可以构建自己的定制规则,这些规则存储在内核空间的信息包过滤表
中。 这些规则具有 目标,它们告诉内核对来自某些源、前往某些目的地或具有某些协议类
型的信息包做些什么。 如果某个信息包与规则匹配,那么使用目标 ACCEPT 允许该信息
包通过。还可以使用目标 DROP 或 REJECT 来阻塞并杀死信息包。对于可对信息包执行的
其它操作,还有许多其它目标。
根据规则所处理的信息包的类型,可以将规则分组在链中。处理入站信息包的规则被添加
到 INPUT 链中。处理出站信息包的规则被添加到 OUTPUT 链中。处理正在转发的信息包
的规则被添加到 FORWARD 链中。这三个链是基本信息包过滤表中内置的缺省主链。 另
外,还有其它许多可用的链的类型(如 PREROUTING 和 POSTROUTING ), 以及提供
用户定义的链。每个链都可以有一个 策略, 它定义“缺省目标”,也就是要执行的缺省操作,
当信息包与链中的任何规则都不匹配时,执行此操作。
建立规则并将链放在适当的位置之后,就可以开始进行真正的信息包过滤工作了。 这时内
核空间从用户空间接管工作。当信息包到达防火墙时,内核先 检查信息包的头信息,尤其
是信息包的目的地。 我们将这个过程称为 路由。
如果信息包源自外界并前往系统,而且防火墙是打开的,那么内核将它传递到内核空间信
息包过滤表的 INPUT 链。如果信息包源自系统内部或系统所连接的内部网上的其它源,并
且此信息包要前往另一个外部系统, 那么信息包被传递到 OUTPUT 链。类似的,源自外
部系统并前往外部系统的信息包被传递到 FORWARD 链。
接下来,将信息包的头信息与它所传递到的链中的每条规则进行比较,看它是否与某条规
则完全匹配。 如果信息包与某条规则匹配,那么内核就对该信息包执行由该规则的目标指
定的操作。 但是,如果信息包与这条规则不匹配,那么它将与链中的下一条规则进行比较。
最后,如果信息包与链中的任何规则都不匹配,那么内核将参考该链的策略来决定如何处
理该信息包。 理想的策略应该告诉内核 DROP 该信息包。下图用图形说明了这个信息包过
滤过程。
使用 Iptables 进行防火墙软件设计的解决方案。
由于 Iptables 已经有了完善的防火墙规则,我们只需要设计一个基于 Iptables 的 Android 前
台即可,通过运行脚本,调用 iptables 设置防火墙规则即可。
大家可以学习下 Droid wall 的源代码
view plaincopy to clipboardprint?
package com.googlecode.droidwall;
/**
* Contains shared programming interfaces.
* All iptables "communication" is handled by this class.
*
* Copyright (C) 2009 Rodrigo Zechin Rosauro
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Rodrigo Zechin Rosauro
* @version 1.0
*/
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;
/**
* Contains shared programming interfaces.
* All iptables "communication" is handled by this class.
*/
public final class Api {
public static final String VERSION = "1.3.6";
// Preferences
public static final String PREFS_NAME = "DroidWallPrefs";
public static final String PREF_ALLOWEDUIDS = "AllowedUids";
public static final String PREF_PASSWORD = "Password";
public static final String PREF_MODE = "BlockMode";
public static final String PREF_ITFS = "Interfaces";
// Modes
public static final String MODE_WHITELIST = "whitelist";
public static final String MODE_BLACKLIST = "blacklist";
// Interfaces
public static final String ITF_3G = "2G/3G";
public static final String ITF_WIFI = "Wi-fi";
// Cached applications
public static DroidApp applications[] = null;
// Do we have "Wireless Tether for Root Users" installed?
public static String hastether = null;
// Do we have root access?
private static boolean hasroot = false;
/**
* Display a simple alert box
* @param ctx context
* @param msg message
*/
public static void alert(Context ctx, CharSequence msg) {
if (ctx != null) {
new AlertDialog.Builder(ctx)
.setNeutralButton(android.R.string.ok, null)
.setMessage(msg)
.show();
}
}
/**
* Purge and re-add all rules (internal implementation).
* @param ctx application context (mandatory)
* @param uids list of selected uids to allow or disallow (depending on the working mode)
* @param showErrors indicates if errors should be alerted
*/
private static boolean applyIptablesRulesImpl(Context ctx, List<Integer> uids, boolean
showErrors) {
if (ctx == null) {
return false;
}
final SharedPreferences prefs = ctx.getSharedPreferences(PREFS_NAME, 0);
final boolean whitelist = prefs.getString(PREF_MODE,
MODE_WHITELIST).equals(MODE_WHITELIST);
boolean wifi = false; // Wi-fi selected ?
final String itfs = prefs.getString(PREF_ITFS, ITF_3G);
String itfFilter;
if (itfs.indexOf("|") != -1) {
itfFilter = ""; // Block all interfaces
wifi = true;
} else if (itfs.indexOf(ITF_3G) != -1) {
itfFilter = "-o rmnet+";; // Block all rmnet interfaces
} else {
itfFilter = "-o tiwlan+";; // Block all tiwlan interfaces
wifi = true;
}
final StringBuilder script = new StringBuilder();
try {
int code;
script.append("iptables -F || exit\n");
final String targetRule = (whitelist ? "ACCEPT" : "REJECT");
if (whitelist && wifi) {
// When "white listing" Wi-fi, we need ensure that the dhcp and wifi users are
allowed
int uid = android.os.Process.getUidForName("dhcp");
if (uid != -1) script.append("iptables -A OUTPUT " + itfFilter + " -m owner --
uid-owner " + uid + " -j ACCEPT || exit\n");
uid = android.os.Process.getUidForName("wifi");
if (uid != -1) script.append("iptables -A OUTPUT " + itfFilter + " -m owner --
uid-owner " + uid + " -j ACCEPT || exit\n");
}
for (Integer uid : uids) {
script.append("iptables -A OUTPUT " + itfFilter + " -m owner --uid-owner " +
uid + " -j " + targetRule + " || exit\n");
}
if (whitelist) {
script.append("iptables -A OUTPUT " + itfFilter + " -j REJECT || exit\n");
}
StringBuilder res = new StringBuilder();
code = runScriptAsRoot(script.toString(), res);
if (showErrors && code != 0) {
String msg = res.toString();
Log.e("DroidWall", msg);
// Search for common error messages
if (msg.indexOf("Couldn't find match `owner'") != -1 || msg.indexOf("no
chain/target match") != -1) {
alert(ctx, "Error applying iptables rules.\nExit code: " + code + "\n\n" +
"It seems your Linux kernel was not compiled with the
netfilter \"owner\" module enabled, which is required for Droid Wall to work properly.\n\n" +
"You should check if there is an updated version of your Android
ROM compiled with this kernel module.");
} else {
// Remove unnecessary help message from output
if (msg.indexOf("\nTry `iptables -h' or 'iptables --help' for more
information.") != -1) {
msg = msg.replace("\nTry `iptables -h' or 'iptables --help' for more
information.", "");
}
// Try `iptables -h' or 'iptables --help' for more information.
alert(ctx, "Error applying iptables rules. Exit code: " + code + "\n\n" +
msg.trim());
}
剩余23页未读,继续阅读
xuzhitaosunshine
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1