zeek/testing/btest/spicy/file-analysis-data-in-concurrent.zeek
Robin Sommer 56b9a79a65
Spicy: Query Zeek scriptland for file handles.
Like traditional file analyzers, we now query Zeek's
`get_file_handle()` event for handles when a connection begins
analyzing an embedded file. That means that Spicy-side protocol
analyzers that are forwarding data into file analysis now need to call
Zeek's `Files::register_protocol()` and provide a callback for
computing file handles. If that's missing, Zeek will now issue a
warning. This aligns with the requirements Zeek's traditional protocol
analyzers. (If the EVT file defines a protocol analyzer to `replace`
an existing one, that one's `register_protocol()` will be consulted.)

Because Zeek's `get_file_handle()` event requires a current
connection, if a Spicy file analyzer isn't directly part of a
connection context (e.g., with nested files), we continue to use
hardcoded, built-in file handle. Scriptland won't be consulted in
that case, just like before.

Closes #3440.
2024-05-06 09:20:38 +02:00

74 lines
1.7 KiB
Text

# @TEST-REQUIRES: have-spicy
#
# @TEST-EXEC: spicyz -d -o test.hlto ssh.spicy ./ssh-cond.evt
# @TEST-EXEC: zeek -r ${TRACES}/ssh/single-conn.trace test.hlto %INPUT Spicy::enable_print=T >output 2>&1
# @TEST-EXEC: btest-diff output
module SSH;
global i: count = 0;
function get_file_handle(c: connection, is_orig: bool): string
{
return cat(c$uid, ++i);
}
event zeek_init()
{
Analyzer::register_for_port(Analyzer::ANALYZER_SPICY_SSH, 22/tcp);
Files::register_protocol(Analyzer::ANALYZER_SSH, [$get_file_handle=SSH::get_file_handle]); # use tag of replaced analyzer
}
# @TEST-START-FILE ssh.spicy
module SSH;
import spicy;
import zeek;
type Context = tuple<data_chunks: uint64>;
public type Banner = unit {
%context = Context;
magic : /SSH-/;
version : /[^-]*/;
dash : /-/;
software: /[^\r\n]*/;
};
public type Data = unit {
data: bytes &eod;
on %done { print self.data; }
};
on Banner::%done {
local fid1 = zeek::file_begin("foo/bar");
local fid2 = zeek::file_begin("foo/bar");
local fid3 = zeek::file_begin("foo/bar");
zeek::file_data_in(b"12", fid1);
zeek::file_data_in(b"!", fid3);
zeek::file_data_in(b"AAA", fid2);
zeek::file_data_in(b"@", fid3);
zeek::file_data_in(b"34", fid1);
zeek::file_data_in(b"#", fid3);
zeek::file_data_in(b"56", fid1);
zeek::file_data_in(b"BBB", fid2);
zeek::file_data_in(b"$"); # -> fid3
zeek::file_end(fid1);
zeek::file_data_in(b"CCC", fid2);
zeek::file_end(fid2);
zeek::file_end(fid3);
}
# @TEST-END-FILE
# @TEST-START-FILE ssh-cond.evt
import zeek;
protocol analyzer spicy::SSH over TCP:
parse originator with SSH::Banner,
replaces SSH;
file analyzer spicy::Text:
parse with SSH::Data,
mime-type foo/bar;
# @TEST-END-FILE