Commit graph

220 commits

Author SHA1 Message Date
Arne Welzel
b57a854633 MIME: Cap nested MIME analysis depth to 100
OSS-Fuzz managed to produce a MIME multipart message construction with
thousands of nested entities (or that's what Zeek makes out of it anyhow).
Prevent such deep analysis by capping at a nesting depth of 100,
preventing unnecessary resource usage. A new weird named exceeded_mime_max_depth
is reported when this limit is reached.

This change reduces the runtime of the OSS-Fuzz reproducer from ~45 seconds
to ~2.5 seconds.

The test PCAP was produced from a Python script using the email package
and sending the rendered version via POST to a HTTP server.

(cherry picked from commit 997c017df937ea47d999d9724e247c3d0e38e509)
2024-01-19 08:22:50 -07:00
Arne Welzel
39a7375f0c ssl: Cap number of alerts parsed from SSL record
Limit the number of events raised from an SSL record with content_type
alert (21) to a configurable maximum number (default 10). For TLS 1.3,
the limit is set to 1 as specified in the RFC. Add a new weird cases
where the limit is exceeded.

OSS-Fuzz managed to generate a reproducer that raised ~660k ssl_plaintext
and ssl_alert events given ~810kb of input data. This change prevents this
with hopefully no negative side-effect in the real-world.
2023-10-27 11:24:08 -07:00
Arne Welzel
ffc281703f Merge remote-tracking branch 'origin/topic/awelzel/mmdb-fix-and-tests'
* origin/topic/awelzel/mmdb-fix-and-tests:
  base/frameworks/spicy: Do not load base/misc/version
  btest/core/mmdb: Basic lookup_autonomous_system / lookup_location tests
  btest/files: Add mmdb testing databases and generator code
  zeek.bif: Switch mmdb stale check to network_time
  Fix mmdb pointer destruction

(cherry picked from commit 44b67894ac)
2023-10-24 13:13:16 -07:00
Arne Welzel
7fac5837c3 iosource/pcap: Support configurable buffer size
On Linux with a default ext4 or tmpfs filesystem, the default buffer size for
reading a pcap is chosen as 4k (strace/gdb validated). When reading large pcaps
containing raw data transfers, the syscall overhead for read becomes visible
in profiles. Support configurability of the buffer size and default to 128kb.

When processing a ~830M PCAP (16 UDP connections, each transferring ~50MB) in
bare mode, this change improves runtime from 1.39 sec to 1.29 sec. Increasing
the buffer further didn't provide a noticeable boost.
2023-10-10 15:08:51 +02:00
Tim Wojtulewicz
1dc9235cee Pass parsed file record information with ReadFile/WriteFile events 2023-08-07 13:44:38 -07:00
Tim Wojtulewicz
18fd384469 Add length field from header to ModbusHeaders record type 2023-08-07 13:44:37 -07:00
Tim Wojtulewicz
f9904511ab Merge remote-tracking branch 'origin/topic/awelzel/3145-dcerpc-state-clean'
* origin/topic/awelzel/3145-dcerpc-state-clean:
  dce-rpc: Test cases for unbounded state growth
  dce-rpc: Handle smb2_close_request() in scripts
  smb/dce-rpc: Cleanup DCE-RPC analyzers when fid is closed and limit them
  dce-rpc: Do not repeatedly register removal hooks
2023-07-11 16:17:12 -07:00
Arne Welzel
0d6174a5d6 Remove icmp_conn leftovers
Roughly 2.5 years ago all events taking the ``icmp_conn`` parameter were
removed with 44ad614094 and the NetVar.cc
type not populated anymore.

Remove the left-overs in script land, too.
2023-07-04 17:57:20 +02:00
Arne Welzel
6517ed94f2 smb/dce-rpc: Cleanup DCE-RPC analyzers when fid is closed and limit them
This patch does two things:

1) For SMB close requests, tear down any associated DCE-RPC
   analyzer if one exists.

2) Protect from fid_to_analyzer_map growing unbounded by introducing a
   new SMB::max_dce_rpc_analyzers limit and forcefully wipe the
   analyzers if exceeded. Propagate this to script land as event
   smb_discarded_dce_rpc_analyzers() for additional cleanup.

This is mostly to fix how the binpac SMB analyzer tracks individual
DCE-RPC analyzers per open fid. Connections that re-open the same or
different pipe may currently allocate unbounded number of analyzers.

Closes #3145.
2023-06-30 15:14:32 +02:00
Arne Welzel
480d52ca1f from_json: Support function to normalize key names
When a JSON document contains key names containing colons or other
special characters that are not valid in Zeek identifiers, from_json()
cannot be used to parse such input.

This change allows a customizable normalization function.

Closes #3142.
2023-06-29 15:57:49 +02:00
Tim Wojtulewicz
9a79b98a1e Remove analyzer_confirmation/analyzer_violation events (6.1 deprecation) 2023-06-14 10:07:22 -07:00
Tim Wojtulewicz
4229af6820 Remove deprecations tagged for v6.1 2023-06-14 10:07:22 -07:00
Arne Welzel
0b0f6e509f Stmt: Rework assertion hooks break semantics
Using break in either of the hooks allows to suppress the default reporter
error message rather than suppressing solely based on the existence of an
assertion_failure() handler.
2023-06-13 16:18:01 +02:00
Arne Welzel
25ea678626 Stmt: Introduce assert statement and related hooks
including two hooks called assertion_failure() and assertion_result() for
customization and tracking of assertion results.
2023-06-12 18:16:02 +02:00
Robin Sommer
a62e153dd3
Do not load Spicy scripts if Spicy is not available. 2023-05-16 10:21:21 +02:00
Robin Sommer
0040111955
Integrate the Spicy plugin into Zeek proper.
This reflects the `spicy-plugin` code as of `d8c296b81cc2a11`.

In addition to moving the code into Zeek's source tree, this comes
with a couple small functional changes:

- `spicyz` no longer tries to infer if it's running from the build
  directory. Instead `ZEEK_SPICY_LIBRARY` can be set to a custom
  location. `zeek-set-path.sh` does that now.

- ZEEK_CONFIG can be set to change what `spicyz -z` print out. This is
  primarily for backwards compatibility.

Some further notes on specifics:

- We raise the minimum Spicy version to 1.8 (i.e., current `main`
  branch).

- Renamed the `compiler/` subdirectory to `spicyz` to avoid
  include-path conflicts with the Spicy headers.

- In `cmake/`, the corresponding PR brings a new/extended version of
  `FindZeek`, which Spicy analyzer packages need. We also now install
  some of the files that the Spicy plugin used to bring for testing,
  so that existing packages keep working.

- For now, this all remains backwards compatible with the current
  `zkg` analyzer templates so that they work with both external and
  integrated Spicy support. Later, once we don't need to support any
  external Spicy plugin versions anymore, we can clean up the
  templates as well.

- All the plugin's tests have moved into the standard test suite. They
  are skipped if configure with `--disable-spicy`.

This holds off on adapting the new code further to Zeek's coding
conventions, so that it remains easier to maintain it in parallel to
the (now legacy) external plugin. We'll make a pass over the
formatting for (presumable) Zeek 6.1.
2023-05-16 10:17:45 +02:00
Arne Welzel
264284150b Merge remote-tracking branch 'amazing-pp/topic/fupeng/from_json_bif'
* amazing-pp/topic/fupeng/from_json_bif:
  Implement from_json bif

Minor updates during merge: Moved ValFromJSON into zeek::detail for the
time being, removed gotos, normalized some error messages to lower case,
minimal test extension and added a raw reader input framework test reading
"json lines" as a demo, adding notes about the implicit type
conversions.
2023-05-09 10:36:58 +02:00
Fupeng Zhao
584e68434d Implement from_json bif 2023-05-06 00:42:46 +00:00
Arne Welzel
5caab1a667 smb2: Limit per-connection read/ioctl/tree state
Users on Slack observed memory growth in an environment with a lot of
SMB traffic. jeprof memory profiling pointed at the offset and fid maps
kept per-connection for smb2 read requests.

These maps can grow unbounded if responses are seen before requests, there's
packet drops, just one side of the connection is visible, or we fail to parse
responses properly.

Forcefully wipe out these maps when they grow too large and raise
smb2_discarded_messages_state() to notify script land about this.
2023-05-03 10:58:59 +02:00
Arne Welzel
df4f90b65c ip4_hdr: Add DF, MF, offset and sum fields
For low-level packet analysis use-cases, these fields are currently
not script-land accessible via raw_packet() or so. They are accessible
on the icmp_context record, but not on the actual ip4_hdr record, so
add them.
2023-04-29 17:09:43 +02:00
Arne Welzel
f4bb8fae33 record_fields: Include information about optionality of fields
This was reported as a wish for log schema generation, so add it...
2023-04-27 21:18:35 +02:00
Christian Kreibich
99de7b7526 Add community_id_v1() based on corelight/zeek-community-id
"Community ID" has become an established flow hash for connection correlation
across different monitoring and storage systems. Other NSMs have had native
and built-in support for Community ID since late 2018. And even though the
roots of "Community ID" are very close to Zeek, Zeek itself has never provided
out-of-the-box support and instead required users to install an external plugin.

While we try to make that installation as easy as possible, an external plugin
always sets the bar higher for an initial setup and can be intimidating.
It also requires a rebuild operation of the plugin during upgrades. Nothing
overly complicated, but somewhat unnecessary for such popular functionality.

This isn't a 1:1 import. The options are parameters and the "verbose"
functionality  has been removed. Further, instead of a `connection`
record, the new bif works with `conn_id`, allowing computation of the
hash with little effort on the command line:

    $ zeek -e 'print community_id_v1([$orig_h=1.2.3.4, $orig_p=1024/tcp, $resp_h=5.6.7.8, $resp_p=80/tcp])'
    1:RcCrCS5fwYUeIzgDDx64EN3+okU

Reference: https://github.com/corelight/zeek-community-id/
2023-04-21 20:44:09 +02:00
Tim Wojtulewicz
67802e711a Report packet statistics via the telemetry framework 2023-04-06 13:41:09 -07:00
Tim Wojtulewicz
ae3d6a4df0 Add optional packet filtered statistics for packet sources 2023-04-06 09:47:04 -07:00
Arne Welzel
f00d6198af PktSrc: Introduce Pcap::non_fd_timeout
Increasing this value 10x has lowered CPU usage on a Myricom based
deployment significantly with reportedly no adverse side-effects.

After reviewing the Zeek 3 IO loop, my hunch is that previously when
no packets were available, we'd sleep 20usec every loop iteration after
calling ->Process() on the packet source. With current master ->Process()
is called 10 times on a packet source before going to sleep just once
for 20 usec. Likely this explains the increased CPU usage reported.

It's probably too risky to increase the current value, so introduce
a const &redef value for advanced users to tweak it. A middle ground
might be to lower ``io_poll_interval_live`` to 5 and increase the new
``Pcap::non_fd_timeout`` setting to 100usec.

While this doesn't really fix #2296, we now have enough knobs for tweaking.

Closes #2296.
2023-03-31 18:48:08 +02:00
Arne Welzel
d4e31e7d2b RunState: Implement forward_network_time_if_applicable()
Add a central place where the decision when it's okay to update network time
to the current time (wallclock) is. It checks for pseudo_realtime and packet
source existence as well as packet source idleness.

A new const &redef allows to completely disable forwarding of network time.
2023-03-23 12:40:39 +01:00
Arne Welzel
46c432dc8b iosource: Make poll intervals configurable
This probably should not be changed by users, but it's useful for
testing and experimentation rather than needing to recompile.

Processing 100 packets without checking an FD based IO source can
actually mean that FD based sources are never checked during a read
of a very small pcap...
2023-03-21 09:15:33 +01:00
Michael R. Torres
fe8390c646 Parse DNSSEC AD and CD bits
Parse authentic data (AD) and checking disabled (CD) bits according to
RFC 2535. Leaves the Z field as-is, in case users are already handling
this elsewhere and depend on the value being the integer for all 3 bits.

https://www.rfc-editor.org/rfc/rfc2535#section-6.1

Fixes #2672
2023-03-13 14:35:06 -07:00
Arne Welzel
2251c67e56 get_dns_stats: Expose total cache size and cached text entries
It wasn't possible from script land to determine the total size
of the cache table held by the DNS_Mgr. Add the total and also
also the TEXT entries count.
2023-03-10 09:22:45 +01:00
Johanna Amann
989e9c29d2 X.509: expose the signature type inside the tbs certificate
This change exposes the signature tyope inside the signed portion of an
X.509 certificate. In the past, we only exposed the signature type that
is contained inside the signature, which is outside the signed portion
of the X.509 certificate.

In theory, both signature fields should have the same value; it is,
however, possible to encode differing values in both fields. The new
field is not logged by default.
2023-02-28 19:24:16 +00:00
Tim Wojtulewicz
8cf1e51623 Add max_size argument for find_all/find_all_ordered BIFs 2023-02-21 12:27:54 -07:00
Tim Wojtulewicz
0fd335f7f0 Merge remote-tracking branch 'security/topic/timw/131-smb-fscontrol-overflow'
* security/topic/timw/131-smb-fscontrol-overflow:
  Restore/rename field in SMB2::Fscontrol record type
2023-02-01 10:48:16 -07:00
Tim Wojtulewicz
84ac362c67 Restore/rename field in SMB2::Fscontrol record type
b41a4bf06d removed a field from this record
because it had a duplicate name as another field. The field does need to
exist, but it needs the correct name.
2023-01-27 17:39:10 -07:00
Arne Welzel
26b1558cd1 analyzer: Move disabling_analyzer() hook into Analyzer module
When disabling_analyzer() was introduced, it was added to the GLOBAL
module. The awkward side-effect is that implementing a hook handler
in another module requires to prefix it with GLOBAL. Alternatively, one
can re-open the GLOBAL module and implement the handler in that scope.

Both are not great, and prefixing with GLOBAL is ugly, so move the
identifier to the Analyzer module and ask users to prefix with Analyzer.
2023-01-23 12:22:05 +01:00
Arne Welzel
2becb1337f TimerMgr: Add back max_timer_expires=0 special case
Commit 58fae22708 removed the max_expire==0
handling from DoAdvance() due to not being obvious what use it is. Jan
later reported that it broke the `redef max_timer_expires=0` (#2514).

This commit adds back the special case re-introducing the `max_timer_expires=0` ,
trying to make it fairly explicit that it exists.

This is an adaption of #2516 not adding a new option and trying a bit
to avoid global variable accesses down in DoAdvance(), though that
just moved to InitPostScript().

Fixes #2514.
2022-11-27 15:02:14 +01:00
Tim Wojtulewicz
9e8833e2d5 Merge remote-tracking branch 'security/topic/awelzel/121-ftp-timeout-again'
* security/topic/awelzel/121-ftp-timeout-again:
  ftp: Introduce FTP::max_command_length
2022-11-22 12:27:37 -07:00
Tim Wojtulewicz
26030f4a57 Merge remote-tracking branch 'nadav/topic/nadavk/ntlm'
* nadav/topic/nadavk/ntlm:
  Added NTLM challenge and response
2022-11-21 09:09:18 -07:00
Arne Welzel
3f5cb75a2a ftp: Introduce FTP::max_command_length
oss-fuzz produced FTP traffic with a ~550KB long FTP command. Cap FTP command
length at 100 bytes, log a weird if a command is larger than that and move
on to the next. Likely it's not actual FTP traffic, but raising an
analyzer violation would allow clients an easy way to disable the analyzer
by sending an overly long command.

The added test PCAP was generated using a fake Python socket server/client.
2022-11-21 09:36:29 +01:00
Josh Soref
186fe346ad spelling: repetitions
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2022-11-18 10:26:33 -05:00
nadavkluger
dd849bc339 Added NTLM challenge and response 2022-11-17 18:38:49 +02:00
Arne Welzel
c9b2b15eb9 init-bare: Fix zeek:see after bif renaming
The docs build is currently broken due to this :-/
2022-11-15 16:04:37 +01:00
Tim Wojtulewicz
bfd5b06943 Minor renaming changes to event handler stats bif, plus a test 2022-11-14 09:13:31 -07:00
Tim Wojtulewicz
5d5f5de1d1 Remove statistics plugin in favor of stats bif 2022-11-10 13:37:31 -07:00
Elad Solomon
c3b9756576 Zeek Statistics
- Added statistics hook
- Fixed rlimit usage
- Removed POpen for windows implementation layer
- Completed statistics plugin
2022-11-09 18:15:33 +02:00
Arne Welzel
c58cdf407a analyzer: Cap analyzer violations at 1000 per analyzer instance 2022-11-08 16:44:30 -07:00
Josh Soref
21e0d777b3 Spelling fixes: scripts
* accessing
* across
* adding
* additional
* addresses
* afterwards
* analyzer
* ancillary
* answer
* associated
* attempts
* because
* belonging
* buffer
* cleanup
* committed
* connects
* database
* destination
* destroy
* distinguished
* encoded
* entries
* entry
* hopefully
* image
* include
* incorrect
* information
* initial
* initiate
* interval
* into
* java
* negotiation
* nodes
* nonexistent
* ntlm
* occasional
* omitted
* otherwise
* ourselves
* paragraphs
* particular
* perform
* received
* receiver
* referring
* release
* repetitions
* request
* responded
* retrieval
* running
* search
* separate
* separator
* should
* synchronization
* target
* that
* the
* threshold
* timeout
* transaction
* transferred
* transmission
* triggered
* vetoes
* virtual

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2022-11-02 17:36:39 -04:00
Tim Wojtulewicz
81357853ed Restore reporting messages for pcap filter issues 2022-10-21 10:50:00 -07:00
Arne Welzel
3e0374f564 const: Deprecate tunnel-specific enable_* flags
With packet analyzers being toggle-able at runtime these can go.
They hadn't been consistently implemented either (VXLAN, Geneve).
2022-09-30 09:31:02 +02:00
Arne Welzel
bc8fd5a4c6 Introduce generic analyzer_confirmation_info and analyzer_violation_info
Introduce two new events for analyzer confirmation and analyzer violation
reporting. The current analyzer_confirmation and analyzer_violation
events assume connection objects and analyzer ids are available which
is not always the case. We're already passing aid=0 for packet analyzers
and there's not currently a way to report violations from file analyzers
using analyzer_violation, for example.

These new events use an extensible Info record approach so that additional
(optional) information can be added later without changing the signature.
It would allow for per analyzer extensions to the info records to pass
analyzer specific info to script land. It's not clear that this would be
a good idea, however.

The previous analyzer_confirmation and analyzer_violation events
continue to exist, but are deprecated and will be removed with Zeek 6.1.
2022-09-27 17:49:51 +02:00
Arne Welzel
8d19fa23ef Remove unified2 file analyzer 2022-08-19 14:05:00 +02:00