|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import stat |
| 6 | +import string |
| 7 | +import sys |
| 8 | + |
| 9 | +import bpftools |
| 10 | + |
| 11 | + |
| 12 | +def main(): |
| 13 | + parser = argparse.ArgumentParser( |
| 14 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 15 | + description=r''' |
| 16 | +
|
| 17 | +This tool creates a Berkeley Packet Filter (BPF) bytecode that will |
| 18 | +match packets based on given criteria. Right now we support the |
| 19 | +following generators: |
| 20 | +
|
| 21 | + dns - matches dns queries for given domains |
| 22 | + dns_validate - matches dns malformed requests |
| 23 | + suffix - matches packets with given suffix |
| 24 | +
|
| 25 | +Generators can take arbitrary parameters and command line options. To |
| 26 | +read more on their usage pass '--help' option to the genrator (not to |
| 27 | +this wrapper), for example: |
| 28 | +
|
| 29 | + %(prog)s dns -- --help |
| 30 | +
|
| 31 | +Example of use: |
| 32 | +
|
| 33 | + %(prog)s dns -- -i *.example.com |
| 34 | + %(prog)s dns -- -i example.com *.example.com *.*.example.com |
| 35 | + %(prog)s dns_validate |
| 36 | + %(prog)s dns_validate -- --strict |
| 37 | + %(prog)s suffix -- 010203 |
| 38 | +
|
| 39 | +Note that some common options are accepted by this wrapper, not by the |
| 40 | +BPF generators, for example: |
| 41 | +
|
| 42 | + %(prog)s -s suffix -- 010203 |
| 43 | + %(prog)s -s -n suffix -- 010203 |
| 44 | + %(prog)s -s -n -o 14 suffix -- 010203 |
| 45 | + %(prog)s -s -n -o 14 -6 suffix -- 010203 |
| 46 | + ''') |
| 47 | + |
| 48 | + parser.add_argument('-6', '--inet6', action='store_true', |
| 49 | + help='generate script for IPv6') |
| 50 | + parser.add_argument('-n', '--negate', action='store_true', |
| 51 | + help='negate the logic') |
| 52 | + parser.add_argument('-o', '--offset', type=int, default=0, |
| 53 | + help='offset of an L3 header') |
| 54 | + parser.add_argument('-s', '--assembly', action='store_true', |
| 55 | + help='print readable assembly, not numeric bytecode') |
| 56 | + parser.add_argument('type', nargs=1, choices=bpftools.generator_names, |
| 57 | + help='BPF generator type') |
| 58 | + parser.add_argument('parameters', nargs='*', |
| 59 | + help='parameters passed to the BPF generator') |
| 60 | + |
| 61 | + args = parser.parse_args() |
| 62 | + |
| 63 | + if len(args.type) != 1: |
| 64 | + parser.print_help() |
| 65 | + sys.exit(-1) |
| 66 | + |
| 67 | + name, ret = bpftools.gen(args.type[0], |
| 68 | + args.parameters, |
| 69 | + assembly=args.assembly, |
| 70 | + l3_off=args.offset, |
| 71 | + ipversion=4 if not args.inet6 else 6, |
| 72 | + negate=args.negate, |
| 73 | + ) |
| 74 | + print ret |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + main() |
0 commit comments