7. Zeek Integration
While Spicy itself remains application independent, transparent integration into Zeek has been a primary goal for its development. To facilitate adding new protocol and file analyzers to Zeek, there is a Zeek plugin that makes Spicy parsers accessible to Zeek’s processing pipeline. In the following, we dig deeper into how to use all of this.
Note
There are a number of pieces involved in creating a full Zeek analyzer, in particular if you want to distribute it as a Zeek package. To help you get started with that, Zeek’s package manager can create a skeleton Spicy package by running:
# zkg create --features=spicy-protocol-analyzer --packagedir <packagedir>
The generated files mark places that will need manual editing with
TODO
. See the tutorial for more on
this.
7.1. Terminology
In Zeek, the term “analyzer” refers generally to a component that processes a particular protocol (“protocol analyzer”), file format (“file analyzer”), or low-level packet structure (“packet analyzer”). “Processing” here means more than just parsing content: An analyzer controls when it wants to be used (e.g., with connections on specific ports, or with files of a specific MIME type); what events to generate for Zeek’s scripting layer; and how to handle any errors occurring during parsing. While Spicy itself focuses just on the parsing part, the Spicy plugin makes it possible to provide the remaining pieces to Zeek, turning a Spicy parser into a full Zeek analyzer. That’s what we refer to as a “Spicy (protocol/file/packet) analyzer” for Zeek.
7.2. Installation
Since Zeek version 5.0, Spicy and Spicy plugin are by default bundled with Zeek. To confirm whether Spicy plugin is available you can inspect the list of installed plugin:
# zeek -N Zeek::Spicy
Zeek::Spicy - Support for Spicy parsers (*.hlto) (built-in)
If you see Zeek::Spicy
listed, spicy-plugin is available, otherwise it
needs to be installed manually. The recommended way to do so is through Zeek’s
package manager zkg.
If you have not yet installed zkg, follow its instructions.
You will need to have Spicy and Zeek installed as well of course.
Before proceeding, make sure spicy-config
and zeek-config
are
in your PATH
:
# which spicy-config
/opt/spicy/bin/spicy-config
# which zeek-config
/usr/local/zeek/bin/zeek-config
7.2.1. Package Installation
The easiest way to install the plugin is through Zeek’s package manager:
# zkg install zeek/spicy-plugin
This will pull down the plugin’s package, compile and test the plugin,
and then install and activate it. That process may take a bit to
complete. To check afterwards that the plugin has
become available, run zeek -N Zeek::Spicy
, it should show output
like this:
# zeek -N Zeek::Spicy
Zeek::Spicy - Support for Spicy parsers (*.hlto) (dynamic, version x.y.z)
By default, zkg will install the most recent release version of the
plugin. If you want to install the current development version, use
zkg install --version main zeek/spicy-plugin
instead.
If you want to develop your own Spicy analyzers for Zeek, you will
need a tool that comes with the plugin’s installation: spicyz
. If
you are using a recent version of zkg (>= 2.8.0), it’s easy to make
the tool show up in your PATH
: Either run zkg env
(see this configuration advice)
or update your PATH
manually:
# export PATH=$(zkg config bin_dir):$PATH
If you are using an older version of zkg (including the version
coming with Zeek 4.0), it’s a bit more difficult to find spicyz
:
it will be inside your zkg state directory at
<state_dir>/clones/package/spicy-plugin/build/bin/spicyz
.
We recommend adding that directory to your PATH
. (The state
directory is usually either <zeek-prefix>/var/lib/zkg
or
~/.zkg
, depending on how you have set up zkg.)
7.2.2. Manual Installation
If you prefer, you can also compile the Zeek plugin yourself, outside
of the package manager by cloning the plugin’s GitHub repository and
building it through CMake. See the instructions in its README. This will install spicyz
into <prefix>/bin
.
Note
Developer’s note: It works to point ZEEK_PLUGIN_PATH
directly
to the plugin’s build directory, without installing it first. If
you are building the plugin as part of the Spicy distribution, it
will land in <build-directory>/zeek/spicy-plugin
.
7.3. Interface Definitions (“evt files”)
Per above, a Spicy analyzer for Zeek does more than just parsing data. Accordingly, we need to tell the Zeek plugin a couple of additional pieces about analyzers we want it to provide to Zeek:
- Analyzer setup
The plugin needs to know what type of analyzers we are creating, when we want Zeek to activate them, and what Spicy unit types to use as their parsing entry point.
- Event definitions
We need to tell the Spicy plugin what Zeek events to provide and when to trigger them.
We define all of these through custom interface definition files that
Spicy’s compiler for Zeek reads in. These files use an *.evt
extension, and the following subsections discuss their content in more
detail.
Generally, empty lines and comments starting with #
are ignored in
an *.evt
.
Note
The syntax for *.evt
files comes with some legacy pieces that
aren’t particularly pretty. We may clean that up at some point.
7.3.1. Analyzer Setup
You can define protocol analyzers, packet analyzers and file analyzers in an
*.evt
file, per the following.
Protocol Analyzer
To define a protocol analyzer, add a new section to an *.evt
file that looks like this:
protocol analyzer ANALYZER_NAME over TRANSPORT_PROTOCOL:
PROPERTY_1,
PROPERTY_2,
...
PROPERTY_N;
Here, ANALYZER_NAME
is a name to identify your analyzer inside
Zeek. You can choose names arbitrarily as long as they are unique. As
a convention, however, we recommend name with a spicy::*
prefix
(e.g., spicy::BitTorrent
).
On the Zeek-side, through some normalization, these names
automatically turn into tags added to Zeek’s Analyzer::Tag
enum.
For example, spicy::BitTorrent
turns into
Analyzer::ANALYZER_SPICY_BITTORRENT
.
The analyzer’s name is also what goes into Zeek signatures to activate
an analyzer DPD-style. If the name is spicy::BitTorrent
, you’d
write enable "spicy::BitTorrent"
into the signature.
Note
Once you have made your analyzers available to Zeek (which we will
discuss below), running zeek -NN Zeek::Spicy
will show you a
summary of what’s now available, including their Zeek-side names
and tags.
TRANSPORT_PROTOCOL
can be either tcp
or udp
, depending on
the transport-layer protocol that your new analyzer wants to sit on
top of.
Following that initial protocol analyzer ...
line, a set of
properties defines further specifics of your analyzer. The following
properties are supported:
parse [originator|responder] with SPICY_UNIT
Specifies the top-level Spicy unit(s) the analyzer uses for parsing payload, with
SPICY_UNIT
being a fully-qualified Spicy-side type name (e.g.HTTP::Request
). The unit type must have been declared aspublic
in Spicy.If
originator
is given, the unit is used only for parsing the connection’s originator-side payload; and ifresponder
is given, only for responder-side payload. If neither is given, it’s used for both sides. In other words, you can use different units per side by specifying two propertiesparse originator with ...
andparse responder with ...
.port PORT
orports { PORT_1, ..., PORT_M }
Specifies one or more well-known ports for which you want Zeek to automatically activate your analyzer with corresponding connections. Each port must be specified in Spicy’s syntax for port constants (e.g.,
80/tcp
), or as a port rangePORT_START-PORT_END
where start and end port are port constants forming a closed interval. The ports’ transport protocol must match that of the analyzer.Note
The plugin will also honor any
%port
meta data property that the responder-sideSPICY_UNIT
may define (as long as the attribute’s direction is notoriginator
).replaces ANALYZER_NAME
Disables an existing analyzer that Zeek already provides internally, allowing you to replace a built-in analyzer with a new Spicy version.
ANALYZER_NAME
is the Zeek-side name of the analyzer. To find that name, inspect the output ofzeek -NN
for available analyzers:# zeek -NN | grep '\[Analyzer\]' ... [Analyzer] SMTP (ANALYZER_SMTP, enabled) ...Here,
SMTP
is the name you would write intoreplaces
to disable the built-in SMTP analyzer.
As a full example, here’s what a new HTTP analyzer could look like:
protocol analyzer spicy::HTTP over TCP:
parse originator with HTTP::Requests,
parse responder with HTTP::Replies,
port 80/tcp,
replaces HTTP;
Packet Analyzer
Defining packet analyzers works quite similar to protocol analyzers through
*.evt
sections like this:
packet analyzer ANALYZER_NAME:
PROPERTY_1,
PROPERTY_2,
...
PROPERTY_N;
Here, ANALYZER_NAME
is again a name to identify your analyzer
inside Zeek. On the Zeek-side, the name will be added to Zeek’s
PacketAnalyzer::Tag
enum.
Packet analyzers support the following properties:
parse with SPICY_UNIT
Specifies the top-level Spicy unit the analyzer uses for parsing each packet, with
SPICY_UNIT
being a fully-qualified Spicy-side type name. The unit type must have been declared aspublic
in Spicy.replaces ANALYZER_NAME
Disables an existing packet analyzer that Zeek already provides internally, allowing you to replace a built-in analyzer with a new Spicy version.
ANALYZER_NAME
is the Zeek-side name of the existing analyzer. To find that name, inspect the output ofzeek -NN
for available analyzers:# zeek -NN | grep '\[Packet Analyzer\]' ... [Packet Analyzer] Ethernet (ANALYZER_ETHERNET) ...Here,
Ethernet
is the name you would give toreplaces
to disable the built-in Ethernet analyzer.When replacing an existing packet analyzer, you still need to configure your new analyzer on the Zeek side through a zeek_init() event handler. See below for more.
Note
This feature requires Zeek >= 5.2.
As a full example, here’s what a new packet analyzer could look like:
packet analyzer spicy::RawLayer:
parse with RawLayer::Packet;
In addition to the Spicy-side configuration, packet analyzers also
need to be registered with Zeek inside a zeek_init
event handler;
see the Zeek documentation
for more. The -NN
output shows your new analyzer’s ANALYZER_*
tag to use.
Note
Depending on relative loading order between your analyzer and
Zeek’s scripts, in some situations you may not have access to the
ANALYZER_*
tag referring to your new analyzer. If you run into
this, use PacketAnalyzer::try_register_packet_analyzer_by_name
to register your Spicy analyzer by name (as shown by -NN
).
Example:
event zeek_init()
{
if ( ! PacketAnalyzer::try_register_packet_analyzer_by_name("Ethernet", 0x88b5, "spicy::RawLayer") )
Reporter::error("unknown analyzer name used");
}
File Analyzer
Defining file analyzers works quite similar to protocol analyzers,
through *.evt
sections like this:
file analyzer ANALYZER_NAME:
PROPERTY_1,
PROPERTY_2,
...
PROPERTY_N;
Here, ANALYZER_NAME
is again a name to identify your analyzer
inside Zeek. On the Zeek-side, the name will be added to Zeek’s
Files::Tag
enum.
File analyzers support the following properties:
parse with SPICY_UNIT
Specifies the top-level Spicy unit the analyzer uses for parsing file content, with
SPICY_UNIT
being a fully-qualified Spicy-side type name. The unit type must have been declared aspublic
in Spicy.mime-type MIME-TYPE
Specifies a MIME type for which you want Zeek to automatically activate your analyzer when it sees a corresponding file on the network. The type is specified in standard
type/subtype
notion, without quotes (e.g.,image/gif
).Note
The plugin will also honor any
%mime-type
meta data property that theSPICY_UNIT
may define.Note
Keep in mind that Zeek identifies MIME types through “content sniffing” (i.e., similar to libmagic), and usually not by protocol-level headers (e.g., not through HTTP’s
Content-Type
header). If in doubt, examinefiles.log
for what it records as a file’s type.replaces ANALYZER_NAME
Disables an existing file analyzer that Zeek already provides internally, allowing you to replace a built-in analyzer with a new Spicy version.
ANALYZER_NAME
is the Zeek-side name of the analyzer. To find that name, inspect the output ofzeek -NN
for available analyzers:# zeek -NN | grep '\[File Analyzer\]' ... [File Analyzer] PE (ANALYZER_PE, enabled) ...Here,
PE
is the name you would write intoreplaces
to disable the built-in PE analyzer.Note
This feature requires Zeek >= 4.1.
As a full example, here’s what a new GIF analyzer could look like:
file analyzer spicy::GIF:
parse with GIF::Image,
mime-type image/gif;
7.3.2. Event Definitions
To define a Zeek event that you want the Spicy plugin to trigger, you add lines of the form:
on HOOK_ID -> event EVENT_NAME(ARG_1, ..., ARG_N);
on HOOK_ID if COND -> event EVENT_NAME(ARG_1, ..., ARG_N);
The Zeek plugin automatically derives from this everything it needs to register new events with Zeek, including a mapping of the arguments’ Spicy types to corresponding Zeek types. More specifically, these are the pieces going into such an event definition:
on HOOK_ID
A Spicy-side ID that defines when you want to trigger the event. This works just like an
on ...
unit hook, and you can indeed use anything here that Spicy supports for those as well (except container hooks). So, e.g.,on HTTP::Request::%done
triggers an event whenever aHTTP::Request
unit has been fully parsed, andon HTTP::Request::uri
leads to an event each time theuri
field has been parsed. (In the former example you may skip the%done
, actually:on HTTP::Request
implicitly adds it.)EVENT_NAME
The Zeek-side name of the event you want to generate, preferably including a namespace (e.g.,
http::request
).ARG_1, ..., ARG_N
Arguments to pass to the event, given as arbitrary Spicy expressions. Each expression will be evaluated within the context of the unit that the
on ...
triggers on, similar to code running inside the body of a corresponding unit hook. That means the expression has access toself
for accessing the unit instance that’s currently being parsed.The Spicy type of the expression determines the Zeek-side type of the corresponding event argument. Most Spicy types translate over pretty naturally, the following summarizes the translation:
Type Conversion from Spicy to Zeek Spicy Type
Zeek Type
Notes
addr
addr
bool
bool
enum { ... }
enum { ... }
[1]
int(8|16|32|64)
int
interval
interval
list<T>
vector of T
map<V,K>
table[V] of K
optional<T>
T
[2]
port
port
real
double
set<T>
set[T]
string
string
time
time
tuple<T_1, ... ,T_N>
record { T1, ..., T_N }
[3]
uint(8|16|32|64)
count
vector<T>
vector of T
Note
- [1]
A corresponding Zeek-side
enum
type is automatically created. See below for more.- [2]
The optional value must have a value, otherwise a runtime exception will be thrown.
- [3]
Must be mapped to a Zeek-side record type with matching fields.
If a tuple element is mapped to a record field with a
&default
or&optional
attribute, a couple special cases are supported:If the expression evaluates to
Null
, the record field is left unset.If the element’s expression uses the
.?
operator and that fails to produce a value, the record field is likewise left unset.
In addition to full Spicy expressions, there are three reserved IDs with specific meanings when used as arguments:
$conn
Refers to the connection that’s currently being processed by Zeek. On the Zeek-side this will turn into a parameter of Zeek type
connection
. This ID can be used only with protocol analyzers.$file
Refers to the file that’s currently being processed by Zeek. On the Zeek-side this will turn into a parameter of Zeek type
fa_file
. This ID can be used with file or protocol analyzers. For protocol analyzers it refers to the most recently opened, but not yet closed file, see zeek::file_begin and zeek::file_end.$packet
Refers to the packet that’s currently being processed by Zeek. On the Zeek-side this will turn into a parameter of Zeek type
raw_pkt_hdr
, with any fields filled in that have already been parsed by Zeek’s built-in analyzers. This ID can be used only with packet analyzers. (Note that instantiation ofraw_pkt_hdr
values can be relatively expensive on the Zeek side, so best to limit usage of this ID to a small part of the overall traffic.)$is_orig
A boolean indicating if the data currently being processed is coming from the originator (
True
) or responder (False
) of the underlying connection. This turns into a corresponding boolean value on the Zeek side. This ID can be used only with protocol analyzers.
Note
Some tips:
If you want to force a specific type on the Zeek-side, you have a couple of options:
Spicy may provide a
cast
operator from the actual type into the desired type (e.g.,cast<uint64>(..)
).Argument expressions have access to global functions defined in the Spicy source files, so you can write a conversion function taking an argument with its original type and returning it with the desired type.
List comprehension can be convenient to fill Zeek vectors:
[some_func(i) for i in self.my_list]
.
if COND
If given, events are only generated if the expression
COND
evaluates to true. Just like event arguments, the expression is evaluated in the context of the current unit instance and has access toself
.
7.3.3. Exporting Types
As we discuss above, the type of each event
argument maps over to a corresponding Zeek type. On the Zeek side,
that corresponding type needs to be known by Zeek. That is always the
case for built-in atomic types (per the conversion table), but it can turn out more challenging
to achieve for custom types, such as enum
and record
, for
which you would normally need to create matching type declarations in
your Zeek scripts. While that’s not necessarily hard, but it can
become quite cumbersome.
Fortunately, the Spicy plugin can help: for most types, it can instantiate corresponding Zeek types automatically as it loads the corresponding analyzer. While you will never actually see the Zeek-side type declarations, they will be available inside your Zeek scripts as if you had typed them out yourself–similar to other types that are built into Zeek itself.
To have the plugin create a Zeek type for your analyzer automatically,
you need to export
the Spicy type in your EVT file. The syntax for
that is:
export SPICY_ID [as ZEEK_ID];
Here, SPICY_ID
is the fully-scoped type ID on the Spicy side, and
ZEEK_ID
is the fully-scoped type ID you want in Zeek. If you leave
out the as ...
part, the Zeek name will be the same as the Spicy
name, including its namespace. For example, say you have a Spicy unit
TFTP::Request
. Adding export TFTP::Request;
to your EVT file
will make a record
type of the same name, and with the same
fields, available in Zeek. If you instead use export TFTP::Request
as TheOtherTFTP::Request
, it will be placed into a different
namespace instead.
Exporting types generally works for most Spicy types as long as
there’s an ID associated with them in your Spicy code. However,
exporting is most helpful with user-defined types, such as enum
and unit
, because it can save you quite a bit of typing there. We
discuss the more common type conversions in more detail below.
To confirm the types made available to Zeek, you can see all exports
in the output of zeek -NN
. With our 2nd TFTP::Request
example, that looks like this:
[...]
# zeek -NN Zeek::Spicy
Zeek::Spicy - Support for Spicy parsers (*.hlto)
[Type] TheOtherTFTP::Request
[...]
Note
Most, but not all, types can be exported automatically. For
example, self-recursive types are currently not supported.
Generally, if you run into trouble exporting a type, you can always
fall back to declaring a corresponding Zeek version yourself in
your Zeek script. Consider the export
mechanism as a
convenience feature that helps avoid writing a lot of boiler plate
code in common cases.
7.3.3.1. Enum Types
When you export a Spicy enum
type, the Spicy plugin creates a
corresponding Zeek enum
type. For example, assume the following
Spicy declaration:
module Test;
type MyEnum = enum {
A = 83,
B = 84,
C = 85
};
Using export Test::MyEnum;
, the plugin will create the equivalent
of the following Zeek type for use in your scripts:
module Test;
export {
type MyEnum: enum {
MyEnum_A = 83,
MyEnum_B = 84,
MyEnum_A = 85,
MyEnum_Undef = -1
};
}
(The odd naming is due to ID limitations on the Zeek side.)
Note
For backward compatibility, the enum
type comes with an
additional property: all public enum types are automatically
exported, even without adding any export
to your EVT file.
This feature may go away at some point, and we suggest to not rely
on it on new code; always use export
for enum types as well.
7.3.3.2. Unit Types
When you export a Spicy unit
type, the Spicy plugin creates a
corresponding Zeek record
type. For example, assume the following
Spicy declaration:
module Test;
type MyRecord = unit {
a: bytes &size=4;
b: uint16;
var c: bool;
};
Using export Test::MyRecord;
, the plugin will then create the
equivalent of the following Zeek type for use in your scripts:
module Test;
export {
type MyRecord: record {
a: string &optional;
b: count &optional;
c: bool;
};
}
The individual fields map over just like event arguments do, following
the the table above. For aggregate
types, this works recursively: if, e.g., a field is itself of unit
type and that type has been exported as well, the plugin will map it
over accordingly to the corresponding record
type. Note that such
dependent types must be exported first in the EVT file for this to
work. As a result, you cannot export self-recursive unit types.
As you can see in the example, unit fields are always declared as
optional on the Zeek-side, as they may not have been set during
parsing. Unit variables are non-optional by default, unless declared
as &optional
in Spicy.
7.3.3.3. Struct Types
A Spicy struct
type maps over to Zeek in the same way as unit
types do, treating each field like a unit variable. See there for more information.
7.3.3.4. Tuple Types
A Spicy tuple
type maps over to a Zeek record
similar to how
unit
types do, treating each tuple element like a unit variable.
See there for more information.
Exporting works only for tuple
types that declare names for all
their elements.
7.3.4. Importing Spicy Modules
Code in an *.evt
file may need access to additional Spicy modules,
such as when expressions for event parameters call Spicy
functions defined elsewhere. To make a Spicy module available, you can
insert import
statements into the *.evt
file that work
just like in Spicy code:
import NAME
Imports Spicy module
NAME
.import NAME from X.Y.Z;
Searches for the module
NAME
(i.e., for the filenameNAME.spicy
) inside a sub-directoryX/Y/Z
along the search path, and then imports it.
7.3.5. Conditional Compilation
*.evt
files offer the same basic form of conditional
compilation through
@if
/@else
/@endif
blocks as Spicy scripts. The Zeek plugin
makes two additional identifiers available for testing to both
*.evt
and *.spicy
code:
HAVE_ZEEK
Always set to 1 by the plugin. This can be used for feature testing from Spicy code to check if it’s being compiled for Zeek.
ZEEK_VERSION
The numerical Zeek version that’s being compiled for (see
zeek -e 'print Version::number'
).
This is an example bracketing code by Zeek version in an EVT file:
@if ZEEK_VERSION < 30200
<EVT code for Zeek versions older than 3.2>
@else
<EVT code for Zeek version 3.2 or newer>
@endif
7.4. Compiling Analyzers
Once you have the *.spicy
and *.evt
source files for your new
analyzer, you need to precompile them into an *.hlto
object file
containing their final executable code, which Zeek will then use. To
do that, pass the relevant *.spicy
and *.evt
files to
spicyz
, then have Zeek load the output. To repeat the
example from the Getting Started
guide:
# spicyz -o my-http-analyzer.hlto my-http.spicy my-http.evt
# zeek -Cr request-line.pcap my-http-analyzer.hlto my-http.zeek
Zeek saw from 127.0.0.1: GET /index.html 1.0
Instead of providing the precompiled analyzer on the Zeek command
line, you can also copy them into
${prefix}/lib/spicy/Zeek_Spicy/modules
. The Spicy plugin will
automatically load any *.hlto
object files it finds there. In
addition, the plugin also scans Zeek’s plugin directory for *.hlto
files. Alternatively, you can override both of those locations by
setting the environment variable ZEEK_SPICY_MODULE_PATH
to a set of
colon-separated directories to search instead. The plugin will then
only look there. In all cases, the plugin searches any directories
recursively, so it will find *.hlto
also if they are nested in
subfolders.
Run spicyz -h
to see some additional options it provides, which
are similar to spicy-driver.
7.5. Controlling Zeek from Spicy
Spicy grammars can import a provided library module zeek
to gain
access to Zeek-specific functions that call back into Zeek’s
processing:
function zeek::confirm_protocol()
[Deprecated] Triggers a DPD protocol confirmation for the current connection.
This function has been deprecated and will be removed. Use spicy::accept_input
instead, which will have the same effect with Zeek.
function zeek::reject_protocol(reason: string)
[Deprecated] Triggers a DPD protocol violation for the current connection.
This function has been deprecated and will be removed. Use spicy::decline_input
instead, which will have the same effect with Zeek.
function zeek::weird(id: string, addl: string = "") : &cxxname="spicy::zeek::rt::weird";
Reports a “weird” to Zeek. This should be used with similar semantics as in Zeek: something quite unexpected happening at the protocol level, which however does not prevent us from continuing to process the connection.
id: the name of the weird, which (just like in Zeek) should be a static
string identifying the situation reported (e.g., unexpected_command
).
addl: additional information to record along with the weird
function zeek::is_orig() : bool
Returns true if we’re currently parsing the originator side of a connection.
function zeek::uid() : string
Returns the current connection’s UID.
function zeek::conn_id() : tuple<orig_h: addr, orig_p: port, resp_h: addr, resp_p: port>
Returns the current connection’s 4-tuple ID.
function zeek::flip_roles()
Instructs Zeek to flip the directionality of the current connection.
function zeek::number_packets() : uint64
Returns the number of packets seen so far on the current side of the current connection.
function zeek::protocol_begin(analyzer: optional<string> = Null)
Adds a Zeek-side child protocol analyzer to the current connection.
If the same analyzer was added previously with protocol_handle_get_or_create or protocol_begin with same argument, and not closed with protocol_handle_close or protocol_end, no new analyzer will be added.
See protocol_handle_get_or_create for the error semantics of this function.
analyzer: type of analyzer to instantiate, specified through its Zeek-side name (similar to what Zeek’s signature action enable takes); if not specified, Zeek will perform its usual dynamic protocol detection to figure out how to parse the data (the latter will work only for TCP protocols, though.)
function zeek::protocol_handle_get_or_create(analyzer: string) : ProtocolHandle
Gets a handle to a Zeek-side child protocol analyzer for the current connection.
If no such child exists it will be added; otherwise a handle to the existing child protocol analyzer will be returned.
This function will return an error
if not called from a protocol analyzer, or
the requested child protocol analyzer is unknown, or
creation of a child analyzer of the requested type was prevented by a previous call of disable_analyzer with prevent=T
analyzer: type of analyzer to instantiate, specified through its Zeek-side name (similar to what Zeek’s signature action enable takes).
function zeek::protocol_data_in(is_orig: bool, data: bytes, h: optional<ProtocolHandle> = Null)
Forwards protocol data to all previously instantiated Zeek-side child protocol analyzers.
is_orig: true to feed the data to the child’s originator side, false for the responder data: chunk of data to forward to child analyzer h: optional handle to the child analyzer to forward data into, else forward to all child analyzers
function zeek::protocol_gap(is_orig: bool, offset: uint64, len: uint64, h: optional<ProtocolHandle> = Null)
Signals a gap in input data to all previously instantiated Zeek-side child protocol analyzers.
is_orig: true to signal gap to the child’s originator side, false for the responder offset: start offset of gap in input stream len: size of gap h: optional handle to the child analyzer signal a gap to, else signal to all child analyzers
function zeek::protocol_end()
Signals end-of-data to all previously instantiated Zeek-side child protocol analyzers and removes them.
function zeek::protocol_handle_close(handle: ProtocolHandle)
Signals end-of-data to the given child analyzer and removes it.
The given handle must be live, i.e., it must not have been used in a previous protocol_handle_close call, and must not have been live when protocol_end was called. If the handle is not live a runtime error will be triggered.
handle: handle to the child analyzer to remove
function zeek::file_begin(mime_type: optional<string> = Null) : string
Signals the beginning of a file to Zeek’s file analysis, associating it with the current connection. Optionally, a mime type can be provided. It will be passed on to Zeek’s file analysis framework. Returns the Zeek-side file ID of the new file.
function zeek::fuid() : string
Returns the current file’s FUID.
function zeek::terminate_session()
Terminates the currently active Zeek-side session, flushing all state. Any subsequent activity will start a new session from scratch. This can only be called from inside a protocol analyzer.
function zeek::file_set_size(size: uint64, fid: optional<string> = Null)
Signals the expected size of a file to Zeek’s file analysis.
size: expected size of file fid: Zeek-side ID of the file to operate on; if not given, the file started by the most recent file_begin() will be used
function zeek::file_data_in(data: bytes, fid: optional<string> = Null)
Passes file content on to Zeek’s file analysis.
data: chunk of raw data to pass into analysis fid: Zeek-side ID of the file to operate on; if not given, the file started by the most recent file_begin() will be used
function zeek::file_data_in_at_offset(data: bytes, offset: uint64, fid: optional<string> = Null)
Passes file content at a specific offset on to Zeek’s file analysis.
data: chunk of raw data to pass into analysis offset: position in file where data starts fid: Zeek-side ID of the file to operate on; if not given, the file started by the most recent file_begin() will be used
function zeek::file_gap(offset: uint64, len: uint64, fid: optional<string> = Null)
Signals a gap in a file to Zeek’s file analysis.
offset: position in file where gap starts len: size of gap fid: Zeek-side ID of the file to operate on; if not given, the file started by the most recent file_begin() will be used
function zeek::file_end(fid: optional<string> = Null)
Signals the end of a file to Zeek’s file analysis.
fid: Zeek-side ID of the file to operate on; if not given, the file started by the most recent file_begin() will be used
function zeek::forward_packet(identifier: uint32)
Inside a packet analyzer, forwards what data remains after parsing the top-level unit on to another analyzer. The index specifies the target, per the current dispatcher table.
function zeek::network_time() : time
Gets the network time from Zeek.
7.6. Dynamic Protocol Detection (DPD)
Spicy protocol analyzers support Zeek’s Dynamic Protocol Detection (DPD), i.e., analysis independent of any well-known ports. To use that with your analyzer, add two pieces:
A Zeek signature to activate your analyzer based on payload patterns. Just like with any of Zeek’s standard analyzers, a signature can activate a Spicy analyzer through the
enable "<name>"
keyword. The name of the analyzer comes out of the EVT file: it is theANALYZER_NAME
with the double colons replaced with an underscore (e.g.,spicy::HTTP
turns intoenable "spicy_HTTP"
.You should call spicy::accept_input() from a hook inside your grammar at a point when the parser can be reasonably certain that it is processing the expected protocol. Optionally, you may also call spicy::decline_input() when you’re sure the parser is not parsing the right protocol. However, the Zeek plugin will also trigger this automatically whenever your parser aborts with an error.
7.7. Configuration
7.7.1. Options
The Spicy plugin provides a set of script-level options to tune its
behavior. These all live in the Spicy::
namespace:
## Constant for testing if Spicy is available.
const available = T;
## Show output of Spicy print statements.
const enable_print = F &redef;
# Record and display profiling information, if compiled into analyzer.
const enable_profiling = F &redef;
## abort() instead of throwing HILTI exceptions.
const abort_on_exceptions = F &redef;
## Include backtraces when reporting unhandled exceptions.
const show_backtraces = F &redef;
## Maximum depth of recursive file analysis (Spicy analyzers only)
const max_file_depth: count = 5 &redef;
7.7.2. Functions
The Spicy plugin also adds the following new built-in functions to
Zeek, which likewise live in the Spicy::
namespace:
## Enable a specific Spicy protocol analyzer if not already active. If this
## analyzer replaces an standard analyzer, that one will automatically be
## disabled.
##
## tag: analyzer to toggle
##
## Returns: true if the operation succeeded
global enable_protocol_analyzer: function(tag: Analyzer::Tag) : bool;
## Disable a specific Spicy protocol analyzer if not already inactive. If
## this analyzer replaces an standard analyzer, that one will automatically
## be re-enabled.
##
## tag: analyzer to toggle
##
## Returns: true if the operation succeeded
global disable_protocol_analyzer: function(tag: Analyzer::Tag) : bool;
## Enable a specific Spicy file analyzer if not already active. If this
## analyzer replaces an standard analyzer, that one will automatically be
## disabled.
##
## tag: analyzer to toggle
##
## Returns: true if the operation succeeded
global enable_file_analyzer: function(tag: Files::Tag) : bool;
## Disable a specific Spicy file analyzer if not already inactive. If
## this analyzer replaces an standard analyzer, that one will automatically
## be re-enabled.
##
## tag: analyzer to toggle
##
## Returns: true if the operation succeeded
global disable_file_analyzer: function(tag: Files::Tag) : bool;
## Returns current resource usage as reported by the Spicy runtime system.
global resource_usage: function() : ResourceUsage;
7.8. Debugging
If Zeek doesn’t seem to be doing the right thing with your Spicy
analyzer, there are several ways to debug what’s going on. To
facilitate that, compile your analyzer with spicyz -d
and, if
possible, use a debug version of Zeek (i.e., build Zeek with
./configure --enable-debug
).
If your analyzer doesn’t seem to be active at all, first make sure
Zeek actually knows about it: It should show up in the output of
zeek -NN Zeek::Spicy
. If it doesn’t, you might not have been using
the right *.spicy
or *.evt
files when precompiling, or Zeek is
not loading the *.hlto
file. Also check your *.evt
if it
defines your analyzer correctly.
If Zeek knows about your analyzer and just doesn’t seem to activate
it, double-check that ports or MIME types are correct in the *.evt
file. If you’re using a signature instead, try a port/MIME type first,
just to make sure it’s not a matter of signature mismatches.
If there’s nothing obviously wrong with your source files, you can
trace what the plugin is compiling by running spicyz
with -D
zeek
. For example, reusing the HTTP example from the Getting Started guide:
# spicyz -D zeek my-http.spicy my-http.evt -o my-http.hlt
[debug/zeek] Loading Spicy file "/Users/robin/work/spicy/main/tests/spicy/doc/my-http.spicy"
[debug/zeek] Loading EVT file "/Users/robin/work/spicy/main/doc/examples/my-http.evt"
[debug/zeek] Loading events from /Users/robin/work/spicy/main/doc/examples/my-http.evt
[debug/zeek] Got protocol analyzer definition for spicy_MyHTTP
[debug/zeek] Got event definition for MyHTTP::request_line
[debug/zeek] Running Spicy driver
[debug/zeek] Got unit type 'MyHTTP::Version'
[debug/zeek] Got unit type 'MyHTTP::RequestLine'
[debug/zeek] Adding protocol analyzer 'spicy_MyHTTP'
[debug/zeek] Adding Spicy hook 'MyHTTP::RequestLine::0x25_done' for event MyHTTP::request_line
[debug/zeek] Done with Spicy driver
You can see the main pieces in there: The files being loaded, unit types provided by them, analyzers and events being created.
If that all looks as expected, it’s time to turn to the Zeek side and
see what it’s doing at runtime. You’ll need a debug version of Zeek
for that, as well as a small trace with traffic that you expect your
analyzer to process. Run Zeek with -B dpd
(or -B file_analysis
if you’re debugging a file analyzer) on your trace to record the
analyzer activity into debug.log
. For example, with the same HTTP
example, we get:
1# zeek -B dpd -Cr request-line.pcap my-http.hlto
2# cat debug.log
3[dpd] Registering analyzer SPICY_MYHTTP for port 12345/1
4[...[
5[dpd] Available analyzers after zeek_init():
6[...]
7[dpd] spicy_MyHTTP (enabled)
8[...]
9[dpd] Analyzers by port:
10[dpd] 12345/tcp: SPICY_MYHTTP
11[...]
12[dpd] TCP[5] added child SPICY_MYHTTP[7]
13[dpd] 127.0.0.1:59619 > 127.0.0.1:12345 activated SPICY_MYHTTP analyzer due to port 12345
14[...]
15[dpd] SPICY_MYHTTP[7] DeliverStream(25, T) [GET /index.html HTTP/1.0\x0a]
16[dpd] SPICY_MYHTTP[7] EndOfData(T)
17[dpd] SPICY_MYHTTP[7] EndOfData(F)
The first few lines show that Zeek’s analyzer system registers the analyzer as expected. The subsequent lines show that the analyzer gets activated for processing the connection in the trace, and that it then receives the data that we know indeed constitutes its payload, before it eventually gets shutdown.
To see this from the plugin’s side, set the zeek
debug stream
through the HILTI_DEBUG
environment variable:
# HILTI_DEBUG=zeek zeek -Cr request-line.pcap my-http.hlto
[zeek] Have Spicy protocol analyzer spicy_MyHTTP
[zeek] Registering Protocol::TCP protocol analyzer spicy_MyHTTP with Zeek
[zeek] Scheduling analyzer for port 12345/tcp
[zeek] Done with post-script initialization
[zeek] [SPICY_MYHTTP/7/orig] initial chunk: |GET /index.html HTTP/1.0\\x0a| (eod=false)
[zeek] [SPICY_MYHTTP/7/orig] -> event MyHTTP::request_line($conn, GET, /index.html, 1.0)
[zeek] [SPICY_MYHTTP/7/orig] done with parsing
[zeek] [SPICY_MYHTTP/7/orig] parsing finished, skipping further originator payload
[zeek] [SPICY_MYHTTP/7/resp] no unit specified for parsing
[zeek] [SPICY_MYHTTP/7/orig] skipping end-of-data delivery
[zeek] [SPICY_MYHTTP/7/resp] no unit specified for parsing
[zeek] [SPICY_MYHTTP/7/orig] skipping end-of-data delivery
[zeek] [SPICY_MYHTTP/7/resp] no unit specified for parsing
After the initial initialization, you see the data arriving and the event being generated for Zeek. The plugin also reports that we didn’t define a unit for the responder side—which we know in this case, but if that appears unexpectedly you probably found a problem.
So we know now that our analyzer is receiving the anticipated data to
parse. At this point, we can switch to debugging the Spicy side
through the usual mechanisms. In particular,
setting HILTI_DEBUG=spicy
tends to be helpful:
# HILTI_DEBUG=spicy zeek -Cr request-line.pcap my-http.hlto
[spicy] MyHTTP::RequestLine
[spicy] method = GET
[spicy] anon_2 =
[spicy] uri = /index.html
[spicy] anon_3 =
[spicy] MyHTTP::Version
[spicy] anon = HTTP/
[spicy] number = 1.0
[spicy] version = [$number=b"1.0"]
[spicy] anon_4 = \n
If everything looks right with the parsing, and the right events are
generated too, then the final part is to check out the events that
arrive on the Zeek side. To get Zeek to see an event that the plugin
raises, you need to have at least one handler implemented for it in
one of your Zeek scripts. You can then load Zeek’s
misc/dump-events
to see them as they are being received, including
their full Zeek-side values:
# zeek -Cr request-line.pcap my-http.hlto misc/dump-events
[...]
1580991211.780489 MyHTTP::request_line
[0] c: connection = [id=[orig_h=127.0.0.1, orig_p=59619/tcp, ...] ...]
[1] method: string = GET
[2] uri: string = /index.html
[3] version: string = 1.0
[...]