-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8459 from inverse-inc/feature/parse_fortigate_dhc…
…p_syslog FortiGate event handler DHCP parser
- Loading branch information
Showing
11 changed files
with
219 additions
and
7 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package detectparser | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/inverse-inc/go-utils/sharedutils" | ||
) | ||
|
||
var fortiGateDhcpRegexPattern1 = regexp.MustCompile(`(\w+)="([^"]*)"|(\w+)=([^\s]+)`) | ||
|
||
type FortiGateDhcpParser struct { | ||
Pattern1 *regexp.Regexp | ||
parser | ||
} | ||
|
||
func (s *FortiGateDhcpParser) Parse(line string) ([]ApiCall, error) { | ||
matches := s.Pattern1.FindAllStringSubmatch(line, -1) | ||
var mac, ip, lease, hostname, ack string | ||
var err error | ||
|
||
attributes := make(map[string]string) | ||
hostname = "N/A" | ||
for _, match := range matches { | ||
if match[1] != "" { | ||
attributes[match[1]] = match[2] | ||
} else { | ||
attributes[match[3]] = match[4] | ||
} | ||
} | ||
|
||
if value, ok := attributes["mac"]; ok { | ||
mac = strings.ToLower(value) | ||
} | ||
if value, ok := attributes["ip"]; ok { | ||
ip = value | ||
} | ||
if value, ok := attributes["lease"]; ok { | ||
lease = value | ||
} | ||
if value, ok := attributes["hostname"]; ok { | ||
hostname = value | ||
} | ||
if value, ok := attributes["dhcp_msg"]; ok { | ||
ack = value | ||
} | ||
|
||
if ack != "Ack" { | ||
// Silent error to avoid spamming logs | ||
return nil, nil // errors.New("Not an Ack") | ||
} | ||
|
||
if ip, err = sharedutils.CleanIP(ip); err != nil { | ||
return nil, errors.New("Invalid IP") | ||
} | ||
|
||
if err := s.NotRateLimited(mac + ":" + ip); err != nil { | ||
return nil, err | ||
} | ||
apiCall := []ApiCall{ | ||
&PfqueueApiCall{ | ||
Method: "update_ip4log", | ||
Params: []interface{}{ | ||
"mac", mac, | ||
"ip", ip, | ||
"lease_length", lease, | ||
}, | ||
}, | ||
} | ||
if hostname != "N/A" && hostname != "" { | ||
apiCall = append(apiCall, &PfqueueApiCall{ | ||
Method: "modify_node", | ||
Params: []interface{}{ | ||
"mac", mac, | ||
"computername", hostname, | ||
}, | ||
}) | ||
} | ||
return apiCall, nil | ||
} | ||
|
||
func NewFortiGateDhcpParser(config *PfdetectConfig) (Parser, error) { | ||
return &FortiGateDhcpParser{ | ||
Pattern1: fortiGateDhcpRegexPattern1, | ||
parser: setupParser(config), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package detectparser | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFortiGateDhcpParse(t *testing.T) { | ||
parser, _ := NewFortiGateDhcpParser(nil) | ||
var parseTests = []ParseTest{ | ||
{ | ||
Line: `date=2024-12-24 time=11:56:25 devname="FGT50E3U16014289" devid="FGT50E3U16014289" logid="0100026001" type="event" subtype="system" level="information" vd="root" eventtime=1735059387564643583 tz="-0500" logdesc="DHCP Ack log" interface="VLAN_41" dhcp_msg="Ack" mac="B0:2A:43:C1:97:DC" ip=192.168.41.249 lease=300 hostname="Laptop" msg="DHCP server sends a DHCPACK"`, | ||
Calls: []ApiCall{ | ||
&PfqueueApiCall{ | ||
Method: "update_ip4log", | ||
Params: []interface{}{ | ||
"mac", "b0:2a:43:c1:97:dc", | ||
"ip", "192.168.41.249", | ||
"lease_length", "300", | ||
}, | ||
}, | ||
&PfqueueApiCall{ | ||
Method: "modify_node", | ||
Params: []interface{}{ | ||
"mac", "b0:2a:43:c1:97:dc", | ||
"computername", "Laptop", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Line: `date=2024-12-24 time=11:56:25 devname="FGT50E3U16014289" devid="FGT50E3U16014289" logid="0100026001" type="event" subtype="system" level="information" vd="root" eventtime=1735059387564643583 tz="-0500" logdesc="DHCP Ack log" interface="VLAN_41" dhcp_msg="Nak" mac="B0:2A:43:C1:97:DC" ip=192.168.41.249 lease=300 hostname="Laptop" msg="DHCP server sends a DHCPACK"`, | ||
Calls: nil, | ||
}, | ||
} | ||
RunParseTests(parser, parseTests, t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
html/pfappserver/lib/pfappserver/Form/Config/Pfdetect/fortigate_dhcp.pm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package pfappserver::Form::Config::Pfdetect::fortigate_dhcp; | ||
|
||
=head1 NAME | ||
pfappserver::Form::Config::Pfdetect::fortigate_dhcp - Web form for a pfdetect detector | ||
=head1 DESCRIPTION | ||
Form definition to create or update a pfdetect detector. | ||
=cut | ||
|
||
use HTML::FormHandler::Moose; | ||
extends 'pfappserver::Form::Config::Pfdetect'; | ||
with qw(pfappserver::Base::Form::Role::PfdetectRateLimit); | ||
|
||
has_field '+type' => | ||
( | ||
default => 'fortigate_dhcp', | ||
); | ||
|
||
=over | ||
=back | ||
=head1 COPYRIGHT | ||
Copyright (C) 2005-2024 Inverse inc. | ||
=head1 LICENSE | ||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU General Public License | ||
as published by the Free Software Foundation; either version 2 | ||
of the License, or (at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, | ||
USA. | ||
=cut | ||
|
||
__PACKAGE__->meta->make_immutable unless $ENV{"PF_SKIP_MAKE_IMMUTABLE"}; | ||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ use warnings; | |
our @TYPES = qw( | ||
dhcp | ||
fortianalyser | ||
fortigate_dhcp | ||
nexpose | ||
regex | ||
security_onion | ||
|