详解Android 利用Iptables实现网络黑白名单(防火墙)
一、概述
为了使读此简笔的人对Iptables有一个简单的了解,此处强行百度了一波概念,如果想深入的了解Iptables的各种配置规则和内核对其的管理运行机制请自行www.baidu.com,这些并不是本简笔的目的所在。
闲言少叙,开始正文
---->以下概述来自baidu,读者可酌情跳过
iptables的前身叫ipfirewall(内核1.x时代),是从freeBSD上移植过来的,能够工作在内核当中的,对数据包进行检测的一款简易访问控制工具。但是ipfirewall工作功能极其有限(它需要将所有的规则都放进内核当中,这样规则才能够运行起来,而放进内核,这个做法一般是极其困难的)。当内核发展到2.x系列的时候,软件更名为ipchains,它可以定义多条规则,将他们串起来,共同发挥作用,而现在,它叫做iptables,可以将规则组成一个列表,实现绝对详细的访问控制功能。
他们都是工作在用户空间中,定义规则的工具,本身并不算是防火墙。它们定义的规则,可以让在内核空间当中的netfilter来读取,并且实现让防火墙工作。而放入内核的地方必须要是特定的位置,必须是tcp/ip的协议栈经过的地方。而这个tcp/ip协议栈必须经过的地方,可以实现读取规则的地方就叫做netfilter.(网络过滤器)
---->以下是本文所关注的重点
二、Iptables网络黑白名单(防火墙)实现细节
因为考虑到一些权限的问题所以在实现方法上采用的是创建一个systemserver来运行这些方法。并提供出manager到三方应用,这样在调用时可以排除一些权限的限制。同时本文只是做一个简单的参考概述,所以在后文中只提供了增加黑白名单的方法和iptables规则,并没有提供相应的删除规则等,原理类似大家可自行补充添加。
2.1、创建systemserver
2.1.1、在/system/sepolicy/service.te中添加
typefxjnet_service,system_api_service,system_server_service,service_manager_type;
2.2.2、在/system/sepolicy/service_contexts中添加如下,
fxjnetu:object_r:fxjnet_service:s0
2.2.3、在frameworks/base/core/java/android/content/Context.java中添加
也可以不添加这个,只不过为了后面调用方便所以添加了。如果跳过此步,那么后面出现Context.FXJNET_SERVICE的地方都用字串代替即可。
publicstaticfinalStringFXJNET_SERVICE="fxjnet";
2.2.4、在/frameworks/base/core/java/android/app/SystemServiceRegistry.java的静态代码块中添加如下代码注册service。
registerService(Context.FXJNET_SERVICE,FXJNETManager.class, newCachedServiceFetcher(){ @Override publicFXJNETManagercreateService(ContextImplctx){ IBinderb=ServiceManager.getService(Context.FXJNET_SERVICE); IFXJNETServiceservice=IFXJNETService.Stub.asInterface(b); returnnewFXJNETManager(ctx,service); }});
2.2.5、在frameworks/base/services/java/com/android/server/SystemServer.java中添加如下代码,将service加入systemserver中。
ServiceManager.addService(Context.FXJNET_SERVICE,newFXJNETService());
2.2.6、AIDL文件
packageandroid.os;
interfaceIFXJNETService{
voidaddNetworkRestriction(ListipName,inttype);
}
2.2.7、提供给外部的FXJNETManager
packageandroid.app;
importandroid.os.IFXJNETService;
importandroid.os.RemoteException;
importandroid.content.Context;
publicclassFXJNETManager{
IFXJNETServicemService;
publicFXJNETManager(Contextctx,IFXJNETServiceservice){
mService=service;
}
publicvoidaddNetworkRestriction(ListipName,inttype){
try{
mService.addNetworkRestriction(ipName,type);
}catch(RemoteExceptione){
}
}//endaddNetworkRestriction
}
2.2.8、系统服务即AIDL的实现server
packagecom.android.server;
importandroid.os.IFXJNETService;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
publicclassFXJNETServiceextendsIFXJNETService.Stub{
finalFilefile=newFile("/data/fxj/","firewall.sh");
/**
*增加{网络IP访问}黑白名单数据
*/
publicvoidaddNetworkRestriction(ListipName,inttype){
Stringstr=getIPlist(type,ipName);
setiptablesRestriction();
}
//构建Iptables的规则,1-黑名单;2-白名单
privateStringgetIPlist(inttype,Listiplist){
StringBuildersb=newStringBuilder();
sb.append("echorunscriptstart\n");
sb.append("iptables-FOUTPUT\n");
if(type==1){
if(iplist!=null&&iplist.size()>0){
for(inti=0;i0){
for(inti=0;i
2.2.9、运行IPTABLES脚本命令的工具类
packagecom.android.server;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStreamWriter;
importandroid.os.FileUtils;
importandroid.os.SystemProperties;
importandroid.util.Log;
publicclassFXJScriptRunnerextendsThread{
privatefinalFilefile;
privatefinalStringscript;
privatefinalStringBuilderres;
publicintexitcode=-1;
privatefinalStringTAG="ScriptRunner";
publicScriptRunner(Filefile,Stringscript,StringBuilderres,
booleanasroot){
this.file=file;
this.script=script;
this.res=res;
}
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
try{
file.delete();
file.createNewFile();
finalStringabspath=file.getAbsolutePath();
//makesurewehaveexecutionpermissiononthescriptfile
FileUtils.setPermissions(abspath,00700,-1,-1);
Runtime.getRuntime().exec("chmod777"+abspath).waitFor();//给创建的sh文件设置权限
//Writethescripttobeexecuted
finalOutputStreamWriterout=newOutputStreamWriter(
newFileOutputStream(file));
if(newFile("/system/bin/sh").exists()){
out.write("#!/system/bin/sh\n");
}
out.write(script);
if(!script.endsWith("\n"))
out.write("\n");
out.write("exit0\n");
out.flush();
out.close();
//通过SystemProperties.set("ctl.start","fxjmotnitor")执行service,来运行脚本,
//fxjmotnitor为service名称,可以根据自己的爱好随便叫
SystemProperties.set("ctl.start","fxjmotnitor");
}catch(Exceptionex){
if(res!=null)
res.append("\n"+ex);
}finally{
//destroy();
}
}
}
三、fxjmotnitorservice的创建步骤如下。
3.1、在/system/core/rootdir/init.rc中添加如下,使得service在开机时就运行起来
servicefxjmotnitor/system/bin/sh/data/fxj/firewall.sh
classmain
oneshot
seclabelu:r:fxjmotnitor:s0
3.2、在/sepolicy/目录下创建fxjmotnitor.te文件,内容如下
typefxjmotnitor,domain;
typefxjmotnitor_exec,exec_type,file_type;
init_daemon_domain(fxjmotnitor)
allowfxjmotnitorshell_exec:file{entrypointgetattrread};
3.3、在/sepolicy/file_contexts中添加
/data/fxj/firewall.shu:object_r:fxjmotnitor_exec:s0
3.4、在sepolicy/Android.mk中的
BOARD_SEPOLICY_UNION+=\
#追加如下
......\
fxjmotnitor.te\
......\
以上就是基于iptables规则对ip地址进行管控,从而限制手机那些ip可以访问那些不可访问的流程实现之细节,当然iptables的作用不仅仅局限于此,有兴趣的可自行了解学习。也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。