| 1 | package eris::log::context::iptables; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Parses iptables messages into structured data. | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Const::Fast; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | with qw( | |||||||||||||||||||||||||||
| 7 | eris::role::context | ||||||||||||||||||||||||||||
| 8 | ); | ||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our $VERSION = '0.008'; # VERSION | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub _build_field { 'message' } | ||||||||||||||||||||||||||
|
Subroutine
_build_field: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub _build_matcher { qr/^iptables\b/ } | ||||||||||||||||||||||||||
|
Subroutine
_build_matcher: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub sample_messages { | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my @msgs = split /\r?\n/, <<'EOF'; | ||||||||||||||||||||||||||
|
Subroutine
sample_messages: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | Dec 4 00:41:19 janus kernel: iptables - ACTION=DROP IN=eth0 OUT= MAC=d4:3d:7e:f8:f4:57:cc:e1:7f:ac:7a:18:08:00 SRC=99.46.177.250 DST=148.251.14.68 LEN=40 TOS=0x02 PREC=0x00 TTL=50 ID=0 DF PROTO=TCP SPT=19219 DPT=993 WINDOW=0 RES=0x00 RST URGP=0 | ||||||||||||||||||||||||||||
| 25 | Dec 4 00:41:22 janus kernel: iptables - ACTION=outbound IN= OUT=eth0 SRC=148.251.14.68 DST=194.146.106.106 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=59545 DF PROTO=TCP SPT=43210 DPT=53 WINDOW=14600 RES=0x00 SYN URGP=0 | ||||||||||||||||||||||||||||
| 26 | EOF | ||||||||||||||||||||||||||||
| 27 | 0 | return @msgs; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | const my %map => qw( | |||||||||||||||||||||||||||
| 32 | mac src_mac | ||||||||||||||||||||||||||||
| 33 | proto proto_app | ||||||||||||||||||||||||||||
| 34 | src src_ip | ||||||||||||||||||||||||||||
| 35 | dst dst_ip | ||||||||||||||||||||||||||||
| 36 | spt src_port | ||||||||||||||||||||||||||||
| 37 | dpt dst_port | ||||||||||||||||||||||||||||
| 38 | mac src_mac | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub contextualize_message { | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | my ($self,$log) = @_; | ||||||||||||||||||||||||||
|
Subroutine
contextualize_message: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | my $content = $log->context->{message}; | |||||||||||||||||||||||||||
| 45 | 0 | my %data = (); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | foreach my $token (split /\s+/, $content) { | |||||||||||||||||||||||||||
| 48 | 0 | my ($k,$v) = split /=/, $token, 2; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | next unless defined $v && length $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $v and length $v
MC/DC0%0/2
defined $v and length $v
defined $v
length $v
Truth table0%0/3
A and B
A= defined $vB= length $v
| |||||||||||||||||||||||||||||
| 50 | 0 | my $key = lc $k; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | $key = $map{$key} if exists $map{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | $data{$key} = $v; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if( keys %data ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | my $dir = exists $data{in} ? 'in' | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | : exists $data{out} ? 'out' | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 57 | : undef; | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if( $dir ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | $data{dev} = delete $data{$dir}; | |||||||||||||||||||||||||||
| 60 | 0 | $data{"${dir}_bytes"} = delete $data{len}; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | # Override the perceived program | ||||||||||||||||||||||||||||
| 63 | 0 | $data{program} = 'iptables'; | |||||||||||||||||||||||||||
| 64 | # Add our contextual data | ||||||||||||||||||||||||||||
| 65 | 0 | $log->add_context($self->name,\%data); | |||||||||||||||||||||||||||
| 66 | 0 | $log->add_tags(qw(security firewall kernel)); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | 1; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | __END__ | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =pod | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head1 NAME | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | eris::log::context::iptables - Parses iptables messages into structured data. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head1 VERSION | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | version 0.008 | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | Parses iptables messages into structured data. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head2 field | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Our regex will match the message string | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head2 matcher | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | A regex starting with the word 'iptables' | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 METHODS | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head2 contextualize_message | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Parses the iptables log into structured data containing the keys: | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | dev => Physical interface | ||||||||||||||||||||||||||||
| 108 | src_mac => Source MAC Address | ||||||||||||||||||||||||||||
| 109 | src_ip => Source IP Address | ||||||||||||||||||||||||||||
| 110 | src_port => Source Port | ||||||||||||||||||||||||||||
| 111 | dst_ip => Destination IP Address | ||||||||||||||||||||||||||||
| 112 | dst_port => Destination Port | ||||||||||||||||||||||||||||
| 113 | proto_app => Protocol | ||||||||||||||||||||||||||||
| 114 | in_bytes => Bytes In | ||||||||||||||||||||||||||||
| 115 | out_bytes => Bytes Out | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Tags messages with 'security' and 'firewall' | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =for Pod::Coverage sample_messages | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | L<eris::log::contextualizer>, L<eris::role::context> | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | Brad Lhotsky <brad@divisionbyzero.net> | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | This software is Copyright (c) 2015 by Brad Lhotsky. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | The (three-clause) BSD License | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =cut | ||||||||||||||||||||||||||||