美文网首页Android开发经验谈Android开发程序员
Android 利用Iptables实现网络黑白名单(防火墙)

Android 利用Iptables实现网络黑白名单(防火墙)

作者: 代码视觉 | 来源:发表于2017-10-23 14:30 被阅读169次

一、概述
为了使读此简笔的人对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中添加

   type fxjnet_service, system_api_service, system_server_service, service_manager_type; 

2.2.2、在/system/sepolicy/service_contexts中添加如下,

            fxjnet                                    u:object_r:fxjnet_service:s0

2.2.3、在frameworks/base/core/java/android/content/Context.java中添加
也可以不添加这个,只不过为了后面调用方便所以添加了。如果跳过此步,那么后面出现Context.FXJNET_SERVICE的地方都用字串代替即可。

 public static final String FXJNET_SERVICE="fxjnet";

2.2.4、在/frameworks/base/core/java/android/app/SystemServiceRegistry.java的静态代码块中添加如下代码注册service。

registerService(Context.FXJNET_SERVICE, FXJNETManager.class,
             new CachedServiceFetcher<FXJNETManager>() {
             @Override
             public FXJNETManager createService(ContextImpl ctx) {
             IBinder b = ServiceManager.getService(Context.FXJNET_SERVICE);
             IFXJNETService service = IFXJNETService.Stub.asInterface(b);
             return new FXJNETManager(ctx, service);
  }});

2.2.5、在frameworks/base/services/java/com/android/server/SystemServer.java中添加如下代码,将service加入systemserver中。

 ServiceManager.addService(Context.FXJNET_SERVICE, new FXJNETService());

2.2.6 、AIDL文件

package android.os;
interface IFXJNETService{
    void addNetworkRestriction(List<String> ipName,int type);
}

2.2.7、提供给外部的FXJNETManager

package android.app;
import android.os.IFXJNETService;
import android.os.RemoteException;
import android.content.Context;
public class FXJNETManager{

 IFXJNETService mService;
 public FXJNETManager(Context ctx,IFXJNETService service){
        mService=service;
 }
 public void addNetworkRestriction(List<String> ipName,int type) {
       try{
            mService.addNetworkRestriction(ipName,type);
      }catch (RemoteException e){
      }
  }//end addNetworkRestriction
} 

2.2.8、系统服务即AIDL的实现server

package com.android.server;

import android.os.IFXJNETService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FXJNETService extends IFXJNETService.Stub {
final File file = new File("/data/fxj/", "firewall.sh");
    /**
     * 增加{网络IP访问}黑白名单数据
     */
    public void addNetworkRestriction(List<String> ipName,int type) {
           String str= getIPlist(type,ipName);
           setiptablesRestriction();
    }
//构建Iptables的规则,1-黑名单 ;2-白名单
 private String getIPlist(int type,List<String> iplist){
        StringBuilder sb = new StringBuilder();
        sb.append("echo runscript start\n");
        sb.append("iptables -F OUTPUT\n");
        if (type == 1){
            if (iplist != null && iplist.size() > 0){
                for (int i = 0 ; i < iplist.size() ;i++){
                    String ipname = iplist.get(i);
                    sb.append("echo blacklist mode\n");
                    sb.append("iptables -I OUTPUT -d ");
                    sb.append(ipname);
                    sb.append(" -j DROP\n");
                }
            }
        }else if (type == 2){
            if (iplist != null && iplist.size() > 0){
                for (int i = 0 ; i < iplist.size() ; i++){
                    String ipname =iplist.get(i);
                    sb.append("echo whitelist mode\n");
                    sb.append("iptabless -P OUTPUT DROP\n");
                    sb.append("iptables -I OUTPUT -d ");
                    sb.append(ipname);
                    sb.append(" -j ACCEPT\n");
                }
            }
        }
        sb.append("run script end\n");
        return sb.toString();
    }
 private void setiptablesRestriction(String ipName){
        final FXJScriptRunner runner = new FXJScriptRunner(file,ipName,new StringBuilder());
        new Thread(new Runnable() {
            @Override
            public void run() {
                runner.run();
            }
        }).start();
    }
}

2.2.9、运行IPTABLES脚本命令的工具类

package com.android.server;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;

import android.os.FileUtils;
import android.os.SystemProperties;
import android.util.Log;

public class FXJScriptRunner extends Thread{
    private final File file;
    private final String script;
    private final StringBuilder res;
    public int exitcode = -1;
    private final String TAG = "ScriptRunner" ;
    
    public ScriptRunner(File file, String script, StringBuilder res,
            boolean asroot) {
        this.file = file;
        this.script = script;
        this.res = res;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            file.delete();
            file.createNewFile();
            final String abspath = file.getAbsolutePath();
            // make sure we have execution permission on the script file
            FileUtils.setPermissions(abspath, 00700, -1, -1);
            Runtime.getRuntime().exec("chmod 777 " + abspath).waitFor();//给创建的sh文件设置权限
            // Write the script to be executed
            final OutputStreamWriter out = new OutputStreamWriter(
                    new FileOutputStream(file));
            if (new File("/system/bin/sh").exists()) {
                out.write("#!/system/bin/sh\n");
            }
            out.write(script);
            if (!script.endsWith("\n"))
                out.write("\n");
            out.write("exit 0\n");
            out.flush();
            out.close();
//通过 SystemProperties.set("ctl.start", "fxjmotnitor")执行service,来运行脚本,
//fxjmotnitor为service名称,可以根据自己的爱好随便叫
            SystemProperties.set("ctl.start", "fxjmotnitor");
        } catch (Exception ex) {
            if (res != null)
                res.append("\n" + ex);
        } finally {
            //destroy();
        }
    }
}

三、fxjmotnitor service的创建步骤如下。

3.1、在/system/core/rootdir/init.rc中添加如下,使得service在开机时就运行起来

service fxjmotnitor /system/bin/sh /data/fxj/firewall.sh
 class main
 oneshot
 seclabel u:r:fxjmotnitor:s0 

3.2、在/sepolicy/目录下创建fxjmotnitor.te文件,内容如下

type fxjmotnitor, domain;
type fxjmotnitor_exec, exec_type, file_type;
init_daemon_domain(fxjmotnitor)
allow fxjmotnitor shell_exec:file { entrypoint getattr read }; 

3.3、在/sepolicy/file_contexts中添加

    /data/fxj/firewall.sh    u:object_r:fxjmotnitor_exec:s0

3.4、在sepolicy/Android.mk中的

BOARD_SEPOLICY_UNION += \
#追加如下
......\
fxjmotnitor.te \
......\

以上就是基于iptables规则对ip地址进行管控,从而限制手机那些ip可以访问那些不可访问的流程实现之细节,当然iptables的作用不仅仅局限于此,有兴趣的可自行了解学习。
笔末,祝诸君百顺!

相关文章

网友评论

    本文标题:Android 利用Iptables实现网络黑白名单(防火墙)

    本文链接:https://www.haomeiwen.com/subject/zlwouxtx.html