当前位置: 首页 > news >正文

android ndc firewall 命令type 黑名单 白名单差异

可以看到以白名单方式使能防火墙,fw_FORWARD fw_INPUT fw_OUTPUT 的操作是DROP或REJEDCT。即默认所有应用不允许上网,需要

XXX:/ # ndc firewall enable whitelist
200 0 Firewall command succeeded
XXX:/ # iptables -t filter -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destinationChain FORWARD (policy ACCEPT)
target     prot opt source               destinationChain OUTPUT (policy ACCEPT)
target     prot opt source               destinationChain fw_FORWARD (0 references)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachableChain fw_INPUT (0 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhereChain fw_OUTPUT (0 references)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable
static const std::vector<const char*> FILTER_INPUT = {// Bandwidth should always be early in input chain, to make sure we// correctly count incoming traffic against data plan.BandwidthController::LOCAL_INPUT,FirewallController::LOCAL_INPUT,
};
const char BandwidthController::LOCAL_INPUT[] = "bw_INPUT";
const char BandwidthController::LOCAL_FORWARD[] = "bw_FORWARD";
const char BandwidthController::LOCAL_OUTPUT[] = "bw_OUTPUT";
const char BandwidthController::LOCAL_RAW_PREROUTING[] = "bw_raw_PREROUTING";
const char BandwidthController::LOCAL_MANGLE_POSTROUTING[] = "bw_mangle_POSTROUTING";
const char BandwidthController::LOCAL_GLOBAL_ALERT[] = "bw_global_alert";
const char* FirewallController::TABLE = "filter";const char* FirewallController::LOCAL_INPUT = "fw_INPUT";
const char* FirewallController::LOCAL_OUTPUT = "fw_OUTPUT";
const char* FirewallController::LOCAL_FORWARD = "fw_FORWARD";const char* FirewallController::LOCAL_DOZABLE = "fw_dozable";
const char* FirewallController::LOCAL_STANDBY = "fw_standby";
const char* FirewallController::LOCAL_POWERSAVE = "fw_powersave";
void Controllers::initChildChains() {/** This is the only time we touch top-level chains in iptables; controllers* should only mutate rules inside of their children chains, as created by* the constants above.** Modules should never ACCEPT packets (except in well-justified cases);* they should instead defer to any remaining modules using RETURN, or* otherwise DROP/REJECT.*/// Create chains for child modules.//往filter表的INPUT链添加子链fw_INPUTcreateChildChains(V4V6, "filter", "INPUT", FILTER_INPUT, true);//往filter表的FORWARD链添加子链fw_FORWARDcreateChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD, true);createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING, true);createChildChains(V4V6, "mangle", "FORWARD", MANGLE_FORWARD, true);createChildChains(V4V6, "mangle", "INPUT", MANGLE_INPUT, true);createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING, true);createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING, true);//往filter表的OUTPUT链添加子链fw_OUTPUTcreateChildChains(V4, "filter", "OUTPUT", FILTER_OUTPUT, false);createChildChains(V6, "filter", "OUTPUT", FILTER_OUTPUT, false);createChildChains(V4, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false);createChildChains(V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false);
}
/* static */
//以 createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT, true);为例
void Controllers::createChildChains(IptablesTarget target, const char* table,const char* parentChain,const std::vector<const char*>& childChains,bool exclusive) {std::string command = StringPrintf("*%s\n", table);//*后指跟的table表,这里是filter//*filter// We cannot just clear all the chains we create because vendor code modifies filter OUTPUT and// mangle POSTROUTING directly. So://// - If we're the exclusive owner of this chain, simply clear it entirely.// - If not, then list the chain's current contents to ensure that if we restart after a crash,//   we leave the existing rules alone in the positions they currently occupy. This is faster//   than blindly deleting our rules and recreating them, because deleting a rule that doesn't//   exists causes iptables-restore to quit, which takes ~30ms per delete. It's also more//   correct, because if we delete rules and re-add them, they'll be in the wrong position with//   regards to the vendor rules.//// TODO: Make all chains exclusive once vendor code uses the oem_* rules.std::set<std::string> existingChildChains;if (exclusive) {// Just running ":chain -" flushes user-defined chains, but not built-in chains like INPUT.// Since at this point we don't know if parentChain is a built-in chain, do both.StringAppendF(&command, ":%s -\n", parentChain);// 链名默认策略表示相应的链及默认策略,具体的规则部分省略了命令名iptables//:INPUT -StringAppendF(&command, "-F %s\n", parentChain);//-F指代清空防火墙规则,默认规则除外//-F INPUT} else {existingChildChains = findExistingChildChains(target, table, parentChain);}for (const auto& childChain : childChains) {// Always clear the child chain.StringAppendF(&command, ":%s -\n", childChain);// But only add it to the parent chain if it's not already there.if (existingChildChains.find(childChain) == existingChildChains.end()) {//static const char* CHILD_CHAIN_TEMPLATE = "-A %s -j %s\n";StringAppendF(&command, CHILD_CHAIN_TEMPLATE, parentChain, childChain);}}command += "COMMIT\n";execIptablesRestore(target, command);
}
//以 createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT, true);为例,相当于执行了
iptable-restore <
*filter \n
:INPUT -  \n
-F INPUT \n
:fw_INPUT - \n
-A INPUT -j  fw_INPUT 
//即在filter表的INPUT链处理时调用 fw_INPUT 链,所以fw_INPUT 时INPUT链的子链

int FirewallController::resetFirewall(void) {mFirewallType = WHITELIST;mIfaceRules.clear();// flush any existing rulesstd::string command ="*filter\n"":fw_INPUT -\n"":fw_OUTPUT -\n"":fw_FORWARD -\n""COMMIT\n";return (execIptablesRestore(V4V6, command.c_str()) == 0) ? 0 : -EREMOTEIO;
}int FirewallController::setFirewallType(FirewallType ftype) {int res = 0;if (mFirewallType != ftype) {// flush any existing rulesresetFirewall();if (ftype == WHITELIST) {// create default rule to drop all trafficstd::string command ="*filter\n""-A fw_INPUT -j DROP\n""-A fw_OUTPUT -j REJECT\n""-A fw_FORWARD -j REJECT\n""COMMIT\n";res = execIptablesRestore(V4V6, command.c_str());}// Set this after calling disableFirewall(), since it defaults to WHITELIST theremFirewallType = ftype;}return res ? -EREMOTEIO : 0;
}

所以调用ndc firewall enable whitelist相当于:

无论防火墙是黑白名单哪种类型,都先清空规则,此时所有应用可以上网
iptable-restore < "*filter\n"":fw_INPUT -\n"":fw_OUTPUT -\n"":fw_FORWARD -\n""COMMIT\n";//白名单类型再调用如下规则,再将所有链的数据都DROp或REJECT,相当与所有应用默认无法上网。
iptable-restore < "*filter\n""-A fw_INPUT -j DROP\n""-A fw_OUTPUT -j REJECT\n""-A fw_FORWARD -j REJECT\n""COMMIT\n";

即,黑名单默认上网,白名单默认不上网

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • make是什么
  • VBA即用型代码手册:删除Excel中空白行Delete Blank Rows in Excel
  • Android Studio 问题集锦
  • Java JUnit单元测试
  • Spring MVC/Web
  • 人才测评的应用:人才选拔,岗位晋升,面试招聘测评
  • 开源网页视频会议,WebRTC音视频功能比较
  • kafka 消费模式基础架构
  • Flutter 中的 ExpansionTile 小部件:全面指南
  • BWVS 靶场测试
  • CSS布局和定位应用方案
  • 网络编程-TCP并发服务器-多点通信-域套接字
  • 重学java 39.多线程 — 线程安全
  • 一篇文章讲透排序算法之希尔排序
  • 大摩:AI PC渗透率到2028年将达65%,联想和戴尔是最大受益者
  • JavaScript 如何正确处理 Unicode 编码问题!
  • [iOS]Core Data浅析一 -- 启用Core Data
  • 《网管员必读——网络组建》(第2版)电子课件下载
  • 【刷算法】从上往下打印二叉树
  • C++11: atomic 头文件
  • CentOS7简单部署NFS
  • Hibernate【inverse和cascade属性】知识要点
  • Java 最常见的 200+ 面试题:面试必备
  • JS基础之数据类型、对象、原型、原型链、继承
  • LintCode 31. partitionArray 数组划分
  • Markdown 语法简单说明
  • MYSQL如何对数据进行自动化升级--以如果某数据表存在并且某字段不存在时则执行更新操作为例...
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • Python进阶细节
  • SpiderData 2019年2月13日 DApp数据排行榜
  • Spring-boot 启动时碰到的错误
  • swift基础之_对象 实例方法 对象方法。
  • Theano - 导数
  • Webpack 4 学习01(基础配置)
  • windows下如何用phpstorm同步测试服务器
  • 分布式任务队列Celery
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 猴子数据域名防封接口降低小说被封的风险
  • 如何抓住下一波零售风口?看RPA玩转零售自动化
  • 探索 JS 中的模块化
  •  一套莫尔斯电报听写、翻译系统
  • 原生 js 实现移动端 Touch 滑动反弹
  • 阿里云API、SDK和CLI应用实践方案
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • #数学建模# 线性规划问题的Matlab求解
  • $.ajax()参数及用法
  • (STM32笔记)九、RCC时钟树与时钟 第二部分
  • (vue)el-tabs选中最后一项后更新数据后无法展开
  • (搬运以学习)flask 上下文的实现
  • (草履虫都可以看懂的)PyQt子窗口向主窗口传递参数,主窗口接收子窗口信号、参数。
  • (二)c52学习之旅-简单了解单片机
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (企业 / 公司项目)前端使用pingyin-pro将汉字转成拼音
  • (四)opengl函数加载和错误处理
  • (一)pytest自动化测试框架之生成测试报告(mac系统)