GithubHelp home page GithubHelp logo

iptables_parser's Introduction

iptables parser

用途

用于解析iptables规则,将每一个iptables文件转化成一个Iptables对象实例。而每一个Iptables对象实例的rules属性是一个Rule对象实例的列表。

有了Rule实例,我们就可以对每一条iptables规则进行判断分析。

Rule规则说明

RULE_OPTIONS = {
    "-A": "method",
    "-I": "method",
    "-D": "method",
    "-p": "protocol",
    "--protocol": "protocol",
    "-s": "source",
    "--source": "source",
    "-d": "destination",
    "--destination": "destination",
    "--dport": "ports",
    "-i": "in-interface",
    "--in-interface": "in-interface",
    "-o": "out-interface",
    "--out-interface": "out-interface",
    "-j": "target",
    "--jump": "target",
    "--reject-with": "reject-with",
}

这里将iptables规则中的具体的"参数"和其"含义"进行了一一映射,而后面的"含义"就是Rule对象的属性名称。

除了此处映射的属性名称外,-m--match拥有特殊的对待,它们使用其match的名称来当做其属性名称,例如:-m set --match-set es-node src的属性名称为set

特殊modules,MODULE_NO_EXT = ["tcp"],因为-m tcp并不一定强制需要。以下规则都是正确的,所以需要跳过这个module的判断。

  • -p tcp -m tcp --dport 80
  • -p tcp --dport 80

使用范例

例如,我们可以通过以下逻辑来实现判断

if __name__ == '__main__':
    iptables = Iptables.parse_iptables("/vagrant/iptables-parser/iptables-template", file=True)
    for i in iptables.rules:
        if (not hasattr(i, "set")
            and i.type is "rule"
            and not hasattr(i, "reject-with")):
            print i.raw

这里我们需要筛选出满足以下条件的规则

  • 使用了ipset
  • 规则类型为rule
  • 非reject-with类型的rule

同理可以根据Rule中的属性来灵活组合出不同的逻辑,方便后面整理或者二次处理。

iptables_parser's People

Contributors

myself01 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.