Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
952e010b
Commit
952e010b
authored
Jun 08, 2015
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(ids) add some backend scripting, work in progress
parent
b56e6824
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
114 additions
and
0 deletions
+114
-0
getRuleJSON.py
src/opnsense/scripts/suricata/getRuleJSON.py
+107
-0
actions_ids.conf
src/opnsense/service/conf/actions.d/actions_ids.conf
+7
-0
No files found.
src/opnsense/scripts/suricata/getRuleJSON.py
0 → 100755
View file @
952e010b
#!/usr/bin/env python2.7
"""
Copyright (c) 2015 Ad Schellevis
part of OPNsense (https://www.opnsense.org/)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------------
script to fetch all suricata rule information into a single json object with the following contents:
timestamp : (last timestamp of all files)
files : (number of files)
rules : all relevant metadata from the rules including the default enabled or disabled state
"""
import
os
import
os.path
import
glob
import
json
import
sys
# suricata rule settings, source directory and cache json file to use
rule_source_dir
=
'/usr/local/etc/suricata/rules/'
rule_cache_json
=
'
%
srules.json'
%
rule_source_dir
# all relevant metadata tags to fetch
all_metadata_tags
=
[
'sid'
,
'msg'
,
'classtype'
,
'rev'
,
'gid'
]
# Because rule parsing isn't very useful when the rule definitions didn't change we create a single json file
# to hold the last results (combined with creation date and number of files).
if
__name__
==
'__main__'
:
# collect file metadata
result_structure
=
{
'timestamp'
:
0
,
'files'
:
0
,
'rules'
:
[]}
all_rule_files
=
[]
last_mtime
=
0
for
filename
in
glob
.
glob
(
'
%
s*.rules'
%
(
rule_source_dir
)):
file_mtime
=
os
.
stat
(
filename
)
.
st_mtime
if
file_mtime
>
last_mtime
:
last_mtime
=
file_mtime
all_rule_files
.
append
(
filename
)
result_structure
[
'files'
]
=
len
(
all_rule_files
)
result_structure
[
'timestamp'
]
=
last_mtime
# return last known info if nothing has changed
if
os
.
path
.
isfile
(
rule_cache_json
):
try
:
prev_rules_data
=
open
(
rule_cache_json
,
'rb'
)
.
read
()
prev_rules
=
json
.
loads
(
prev_rules_data
)
if
'timestamp'
in
prev_rules
and
'files'
in
prev_rules
:
if
prev_rules
[
'timestamp'
]
==
result_structure
[
'timestamp'
]
\
and
prev_rules
[
'files'
]
==
result_structure
[
'files'
]:
print
(
prev_rules_data
)
sys
.
exit
(
0
)
except
ValueError
:
pass
# parse all rule files and create json cache file for all data
for
filename
in
all_rule_files
:
rules
=
[]
data
=
open
(
filename
)
for
rule
in
data
.
read
()
.
split
(
'
\n
'
):
if
rule
.
find
(
'msg:'
)
!=
-
1
:
record
=
{
'enabled'
:
True
,
'source'
:
filename
.
split
(
'/'
)[
-
1
]}
if
rule
.
strip
()[
0
]
==
'#'
:
record
[
'enabled'
]
=
False
rule_metadata
=
rule
[
rule
.
find
(
'msg:'
):
-
1
]
for
field
in
rule_metadata
.
split
(
';'
):
fieldName
=
field
[
0
:
field
.
find
(
':'
)]
.
strip
()
fieldContent
=
field
[
field
.
find
(
':'
)
+
1
:]
.
strip
()
if
fieldName
in
all_metadata_tags
:
if
fieldContent
[
0
]
==
'"'
:
record
[
fieldName
]
=
fieldContent
[
1
:
-
1
]
else
:
record
[
fieldName
]
=
fieldContent
result_structure
[
'rules'
]
.
append
(
record
)
open
(
rule_cache_json
,
'wb'
)
.
write
(
json
.
dumps
(
result_structure
))
# print json data
print
(
result_structure
)
src/opnsense/service/conf/actions.d/actions_ids.conf
0 → 100644
View file @
952e010b
[
installedrules
]
command
:/
usr
/
local
/
opnsense
/
scripts
/
suricata
/
getRuleJSON
.
py
parameters
:
type
:
script_output
message
:
request
installed
suricata
rules
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment