iptables_parser

package module
v0.0.0-...-3d9e685 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 12 Imported by: 0

README

iptables-parser

Documentation

Parse lines generated by iptables-save. This parser is inspired by Ben Johnson's SQL Parser.

Description

This parser parses lines returned from iptables-save or iptables -S and returns a Line or an Error. A Line can be a Rule, Comment, Policy (default rule) or Header, all of them being structs.

Match Extensions

iptables has a lot of match extensions. Only a few are implemented. If one is not implemented, the parses returns an error for that line.

Target Extensions

Just like in Match Extensions, not all of the target extensions are implemented.

Example

package main

import (
	"fmt"
	"log"

	ipt "github.com/coreos/go-iptables/iptables"
	iptp "github.com/kilo-io/iptables_parser"
)

func main() {
	t, err := ipt.NewWithProtocol(ipt.ProtocolIPv4)
	if err != nil {
		log.Fatal(err.Error())
	}
	rs, err := t.List("filter", "DOCKER")
	if err != nil {
		log.Fatal(err.Error())
	}
	for _, r := range rs {
		fmt.Println(r)
		tr, err := iptp.NewFromString(r)
		if err != nil {
			fmt.Printf("Error: %v", err)
			continue
		}
		switch r := tr.(type) {
		case iptp.Rule:
			fmt.Printf("rule parsed: %v\n", r)
		case iptp.Policy:
			fmt.Printf("policy parsed: %v\n", r)
		default:
			fmt.Printf("something else happend: %v\n", r)
		}

	}
}

Documentation

Index

Constants

View Source
const BUFSIZE = 16

BUFSIZE is the max buffer size of the ring buffer in the parser.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comment

type Comment struct {
	Content string
}

Comment represents a comment in an iptables dump. Comments start with #.

func (Comment) String

func (c Comment) String() string

type Commit

type Commit struct{}

func (Commit) String

func (c Commit) String() string

type Counter

type Counter struct {
	// contains filtered or unexported fields
}

Counter represents the package and byte counters.

func (Counter) String

func (c Counter) String() string

type DNSOrIP

type DNSOrIP struct {
	// contains filtered or unexported fields
}

DNSOrIP represents either a DNS name or an IP address. IPs, as they are more specific, are preferred.

func NewDNSOrIP

func NewDNSOrIP(s string) (*DNSOrIP, error)

NewDNSOrIP takes a string and return a DNSOrIP, or an error. It tries to parse it as an IP, if this fails it will check, whether the input is a valid DNS name.

func (*DNSOrIP) Set

func (d *DNSOrIP) Set(s string) error

Set IP if string is a valid IP address, or DNS if string is a valid DNS name, else return error.

func (*DNSOrIP) String

func (d *DNSOrIP) String() string

type DNSOrIPPair

type DNSOrIPPair struct {
	Value DNSOrIP
	Not   bool
}

DNSOrIPPair either holds an IP or DNS and a flag. The boolean not-flag is used when an address or DNS name is reverted with a "!" character.

func (DNSOrIPPair) Spec

func (d DNSOrIPPair) Spec(f string) []string

Spec returns a DNSOrIPPair how coreos' iptables package would expect it.

func (DNSOrIPPair) String

func (d DNSOrIPPair) String(f string) string

String returns the part of the iptables rule. It requires its flag as string to generate the correct string, e.g. "! -s 10.0.0.1/32".

type Flag

type Flag struct {
	Not    bool
	Values []string
}

Flag is flag, e.g. --dport 8080. It can be negated with a leading !. Sometimes a flag is followed by several arguments.

func (Flag) Spec

func (fl Flag) Spec(f string) []string

Spec returns a Flag how coreos' iptables package would expect it.

func (Flag) String

func (fl Flag) String(f string) string
type Header struct {
	Content string
}

Header represents a header in an iptables dump and introduce a new table. They start with *.

func (Header) String

func (h Header) String() string

type Line

type Line interface {
	String() string
}

Line represents a line in a iptables dump, e.g. generated with iptables-save. It is either Comment, Header, Default or Rule.

func NewFromString

func NewFromString(s string) (Line, error)

NewFromString takes a string a parses it until the EOF or NEWLINE to return a Header, Policy or Rule. It will return an error otherwise.

type Match

type Match struct {
	Type  string
	Flags map[string]Flag
}

Match represents one match expression from the iptables-extension. See man iptables-extenstion for more info.

func (Match) Spec

func (m Match) Spec() []string

Spec returns a Match how coreos' iptables package would expect it.

func (Match) String

func (m Match) String() string

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser represents a parser.

func NewParser

func NewParser(r io.Reader) *Parser

NewParser returns a new instance of Parser.

func (*Parser) Parse

func (p *Parser) Parse() (l Line, err error)

Parse parses one line and returns a Rule, Comment, Header or DEFAULT.

func (*Parser) ParseRule

func (p *Parser) ParseRule() (*Rule, error)

type Policy

type Policy struct {
	Chain       string
	Action      string
	UserDefined *bool // nil if unknown
	Counter     *Counter
}

Policy represents a build-in policy. They can be parsed from iprables-save looking like ":FORWARD DROP [0:100]" They start with :. They can also be parsed from "iptables -S" looking like "-N|-P chain [target]". In the latter case, UserDefined will be set. For user defined policies, Action should be an empty string "" or "-".

func (Policy) String

func (d Policy) String() string

type Rule

type Rule struct {
	Chain       string       // Name of the chain
	Source      *DNSOrIPPair // Will be nil, if -s flag was not set.
	Destination *DNSOrIPPair // Will be nil, if -s flag was not set.
	InInterf    *StringPair  // Will be nil, if -i flag was not set.
	OutInterf   *StringPair  // Will be nil, if -o flag was not set.
	Protocol    *StringPair  // Be aware that the protocol names can be different depending on your system.
	Fragment    *bool        // Will be nil, if flag was not set.
	IPv4        bool         // False, if flag was not set.
	IPv6        bool         // False, if flag was not set.
	Jump        *Target      // Will be nil, if -j flag was not set.
	Goto        *Target      // Will be nil, if -g flag was not set.
	Counter     *Counter     // Will be nil, if no counter was parsed.
	Matches     []Match      // Matches need to be a slice because order can matter. See man iptables-extension.
}

Rule represents a rule in an iptables dump. Normally the start with -A. The parser treats the -A flag like any other flag, thus does not require the -A flag as the leading flag.

func NewRuleFromSpec

func NewRuleFromSpec(chain string, rulespec ...string) (*Rule, error)

NewRuleFromSpec returns a rule from a given rulespec and chain name. It will return nil and an error, if the rulespec does not resemble a valid rule, or contains unknown, or not implemented extensions.

func NewRuleFromString

func NewRuleFromString(s string) (*Rule, error)

NewRuleFromString returns a rule for the given string. It can only handle appended rules with the "-A <chain name>" flag. It will return nil and an error, if the given string does not resemble a valid rule, or contains unknown, or not implemented extensions.

func (Rule) EqualTo

func (r Rule) EqualTo(r2 Rule) bool

EqualTo returns true, if the rules are equal to each other.

func (Rule) Spec

func (r Rule) Spec() (ret []string)

Spec returns the rule specifications of the rule. The rulespec does not contain the chain name. Different rule specs can describe the same rule, so don't use the rulespec to compare rules. The rule spec can be used to append, insert or delete rules with coreos' go-iptables module.

func (Rule) String

func (r Rule) String() (s string)

String returns the rule as a String, similar to how iptables-save prints the rules Note: Don't use this functions to compare rules because the order of flags can be different and different flags can have equal meanings.

type StringPair

type StringPair struct {
	Not   bool
	Value string
}

StringPair is a string with a flag. It is used to represent flags that specify a string value and can be negated with a "!".

func (StringPair) Spec

func (sp StringPair) Spec(f string) []string

Spec returns a StringPair how coreos' iptables package would expect it.

func (StringPair) String

func (sp StringPair) String(f string) string

type Target

type Target struct {
	Name  string
	Flags map[string]Flag
}

Target represents a Target Extension. See iptables-extensions(8).

func (Target) Spec

func (t Target) Spec(f string) []string

Spec returns a Target how coreos' iptables package would expect it.

func (Target) String

func (t Target) String(name string) string

type Token

type Token int
const (
	// Special tokens
	ILLEGAL Token = iota
	EOF
	WS

	// Literals
	IDENT // main
	COMMIT

	// Misc characters
	COLON     // :
	HASHTAG   // #
	QUOTATION // "
	BACKSLASH // \
	NOT       // !
	COMMA
	NEWLINE

	// Keywords
	SRC
	DEST
	COUNTER
	HEADER
	COMMENT
	COMMENTLINE
	APPEND
	FLAG
	DEFAULT
	LINE
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL