日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android底层网络防火墙,详解Android 利用Iptables实现网络黑白名单(防火墙)

發(fā)布時間:2024/10/8 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android底层网络防火墙,详解Android 利用Iptables实现网络黑白名单(防火墙) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、概述

為了使讀此簡筆的人對Iptables有一個簡單的了解,此處強行百度了一波概念,如果想深入的了解Iptables的各種配置規(guī)則和內核對其的管理運行機制請自行www.baidu.com,這些并不是本簡筆的目的所在。

閑言少敘,開始正文

---->以下概述來自baidu,讀者可酌情跳過

iptables的前身叫ipfirewall (內核1.x時代),是從freeBSD上移植過來的,能夠工作在內核當中的,對數(shù)據(jù)包進行檢測的一款簡易訪問控制工具。但是ipfirewall工作功能極其有限(它需要將所有的規(guī)則都放進內核當中,這樣規(guī)則才能夠運行起來,而放進內核,這個做法一般是極其困難的)。當內核發(fā)展到2.x系列的時候,軟件更名為ipchains,它可以定義多條規(guī)則,將他們串起來,共同發(fā)揮作用,而現(xiàn)在,它叫做iptables,可以將規(guī)則組成一個列表,實現(xiàn)絕對詳細的訪問控制功能。

他們都是工作在用戶空間中,定義規(guī)則的工具,本身并不算是防火墻。它們定義的規(guī)則,可以讓在內核空間當中的netfilter來讀取,并且實現(xiàn)讓防火墻工作。而放入內核的地方必須要是特定的位置,必須是tcp/ip的協(xié)議棧經過的地方。而這個tcp/ip協(xié)議棧必須經過的地方,可以實現(xiàn)讀取規(guī)則的地方就叫做 netfilter.(網(wǎng)絡過濾器)

---->以下是本文所關注的重點

二、Iptables網(wǎng)絡黑白名單(防火墻)實現(xiàn)細節(jié)

因為考慮到一些權限的問題所以在實現(xiàn)方法上采用的是創(chuàng)建一個systemserver來運行這些方法。并提供出manager到三方應用,這樣在調用時可以排除一些權限的限制。同時本文只是做一個簡單的參考概述,所以在后文中只提供了增加黑白名單的方法和iptables規(guī)則,并沒有提供相應的刪除規(guī)則等,原理類似大家可自行補充添加。

2.1、創(chuàng)建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中添加

也可以不添加這個,只不過為了后面調用方便所以添加了。如果跳過此步,那么后面出現(xiàn)Context.FXJNET_SERVICE的地方都用字串代替即可。

public static final String FXJNET_SERVICE="fxjnet";

2.2.4、在/frameworks/base/core/java/android/app/SystemServiceRegistry.java的靜態(tài)代碼塊中添加如下代碼注冊service。

registerService(Context.FXJNET_SERVICE, FXJNETManager.class,

new CachedServiceFetcher() {

@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 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 ipName,int type) {

try{

mService.addNetworkRestriction(ipName,type);

}catch (RemoteException e){

}

}//end addNetworkRestriction

}

2.2.8、系統(tǒng)服務即AIDL的實現(xiàn)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");

/**

* 增加{網(wǎng)絡IP訪問}黑白名單數(shù)據(jù)

*/

public void addNetworkRestriction(List ipName,int type) {

String str= getIPlist(type,ipName);

setiptablesRestriction();

}

//構建Iptables的規(guī)則,1-黑名單 ;2-白名單

private String getIPlist(int type,List 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();//給創(chuàng)建的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")執(zhí)行service,來運行腳本,

//fxjmotnitor為service名稱,可以根據(jù)自己的愛好隨便叫

SystemProperties.set("ctl.start", "fxjmotnitor");

} catch (Exception ex) {

if (res != null)

res.append("\n" + ex);

} finally {

//destroy();

}

}

}

三、fxjmotnitor service的創(chuàng)建步驟如下。

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/目錄下創(chuàng)建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規(guī)則對ip地址進行管控,從而限制手機那些ip可以訪問那些不可訪問的流程實現(xiàn)之細節(jié),當然iptables的作用不僅僅局限于此,有興趣的可自行了解學習。也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的Android底层网络防火墙,详解Android 利用Iptables实现网络黑白名单(防火墙)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。