Android底层网络防火墙,详解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中添加
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() {
@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、系統服務即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 ipName,int type) {
String str= getIPlist(type,ipName);
setiptablesRestriction();
}
//構建Iptables的規則,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();//給創建的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底层网络防火墙,详解Android 利用Iptables实现网络黑白名单(防火墙)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谁能来给讲解下,柏尔定制地板好不好?值得
- 下一篇: 国6的车到底还能开几年,??