Android 利用Iptables實現網路黑白名單(防火牆)

weixin_34321977發表於2017-10-23

一、概述
為了使讀此簡筆的人對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的作用不僅僅侷限於此,有興趣的可自行了解學習。
筆末,祝諸君百順!

相關文章