Commit graph

1122 commits

Author SHA1 Message Date
Ivan Nardi
1c1535738f ndpiReader: ranking unit tests: disable logging 2025-09-23 14:38:25 +02:00
Luca
52ce501355 Improved ndpi_ranling calculation for
- keeping track of the number of updates without rank changes
- not creating new slots (but overwriting the last one) when a new update with no rank changes is computed. This way in the ranking atastructure there are only entries that caused ranking chnages
2025-09-17 19:45:04 +02:00
Ivan Nardi
8c81859467 Update "only_classification" configuration 2025-09-09 16:08:20 +02:00
Ivan Nardi
6a3228388b ndpiReader: improve debug option '-x' to test category matches 2025-09-05 19:58:25 +02:00
Luca Deri
52d4607bbd Extended ndpi_ranking_add_epoch() API 2025-09-05 07:33:08 +02:00
Ivan Nardi
efccc7d5e4
Rework flow breed (#2926)
Right now, there is, in essence, a static mapping between flow protocols
and flow breeds.
Make it dynamic: allow to have different flows, with the same
classification but differents breeds. This is the same logic that we
already have for categories....

Preliminary work to support breed in category lists.

API change from the app POV: to get the flow breed don't use anymore
`ndpi_get_proto_breed()`, but access directly `struct ndpi_proto->breed`

The functions `ndpi_domain_classify_*()` and
`ndpi_get_host_domain_suffix()` now have a `u_int32_t` parameter as
`class_id` (instead of `u_int_16_t`), with the following logic:
```
class_id = (breed << 16) | category
```
instead of the old:
```
class_id = category
```
Please note that this change is back-compatible: if you are not
interested into breeds, you don't need to update the application code.
2025-09-02 16:54:34 +02:00
Ivan Nardi
c25c1be778 tests: add an example of custom rule with nDPI fingerprint 2025-08-31 19:10:05 +02:00
Ivan Nardi
1da8b85ee7
Fix compilation and unit tests (#2948)
```
ndpi_analyze.c: In function ‘ndpi_deserialize_ranking’:
ndpi_analyze.c:2244:3: warning: ignoring return value of ‘fread’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
 2244 |   fread(&rank->header, sizeof(ndpi_ranking_header), 1, fd);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ndpi_main.c: In function ‘ndpi_match_host_subprotocol’:
ndpi_main.c:11798:9: warning: ‘__builtin_strncpy’ output may be truncated copying between 0 and 63 bytes from a string of length 255 [-Wstringop-truncation]
11798 |         strncpy(str, string_to_match, ndpi_min(string_to_match_len, sizeof(str)-1));
      |         ^
ndpi_main.c:11811:7: warning: ‘__builtin_strncpy’ output may be truncated copying between 0 and 63 bytes from a string of length 255 [-Wstringop-truncation]
11811 |       strncpy(str, string_to_match, ndpi_min(string_to_match_len, sizeof(str)-1));

```
2025-08-30 21:05:40 +02:00
Luca Deri
a6e2b4e252 Initial (WiP/basic) implementation of the ranking detection API used to
determine rank changes

  void ndpi_init_ranking(ndpi_ranking *rank, u_int16_t max_num_items, u_int16_t num_epochs);
  void ndpi_term_ranking(ndpi_ranking *rank);
  bool ndpi_serialize_ranking(ndpi_ranking *rank, const char *path);
  bool ndpi_deserialize_ranking(ndpi_ranking *rank, const char *path);
  void ndpi_print_ranking(ndpi_ranking *rank);
  u_int16_t ndpi_ranking_add_epoch(ndpi_ranking *rank, u_int32_t epoch,
                                  ndpi_ranking_epoch_entry *entries,
                                  u_int16_t num_epoch_entries,
                                  ndpi_ranking_change *changes /* Out */);
2025-08-28 16:26:44 +02:00
Luca Deri
7c53fcde85 Code cleanup
Added check in fingeprinting code
2025-08-21 12:30:40 +02:00
Luca Deri
11d74ea286 Implemented nDPI fingerprint that is computed using
- TCP fingerprint
- JA4 fingepriint
- TLS SHA1 certificate (if present), or JA3S fingerprint (is SHA1 is missing)

By default the fingerprint uses the client and server fingerprints (format 0)
and combines them. However you can chnge it format (eg. use only the client info,
format 1) with

--cfg NULL,metadata.ndpi_fingerprint_format,X

where X is the fingerprint format.

By default nDPI fingerprint is enabled but you can enable/disble it as follows

--cfg NULL,metadata.ndpi_fingerprint,0
2025-08-21 10:34:49 +02:00
Luca Deri
087726d12d Added support for JA4 in protos.txt
Format: ja4:XXXXX@CustomProtoJA4
2025-08-20 21:31:10 +02:00
fanxb
7a2ca82c9d
ndpiReader: Fix the crash issue during protocol guessing in multi-core scenarios. (#2939) 2025-08-08 11:58:17 +02:00
Ivan Nardi
eb5f8a037c
fuzz: improve coverage (#2931)
Sync `pl7m` code with upstream.
Add a new fuzzer to test the same flows with different L4 ports
2025-08-04 12:52:51 +02:00
Ivan Nardi
8dd2220116
Add the concept of protocols stack: more than 2 protocols per flow (#2913)
The idea is to remove the limitation of only two protocols ("master" and
"app") in the flow classifcation.
This is quite handy expecially for STUN flows and, in general, for any
flows where there is some kind of transitionf from a cleartext protocol
to TLS: HTTP_PROXY -> TLS/Youtube; SMTP -> SMTPS (via STARTTLS msg).

In the vast majority of the cases, the protocol stack is simply
Master/Application.

Examples of real stacks (from the unit tests)  different from the standard
"master/app":
* "STUN.WhatsAppCall.SRTP": a WA call
* "STUN.DTLS.GoogleCall": a Meet call
* "Telegram.STUN.DTLS.TelegramVoip": a Telegram call
* "SMTP.SMTPS.Google": a SMTP connection to Google server started in
  cleartext and updated to TLS
* "HTTP.Google.ntop": a HTTP connection to a Google domain (match via
  "Host" header) and to a ntop server (match via "Server" header)

The logic to create the stack is still a bit coarse: we have a decade of
code try to push everything in only ywo protocols... Therefore, the
content of the stack is still **highly experimental** and might change
in the next future; do you have any suggestions?

It is quite likely that the legacy fields "master_protocol" and
"app_protocol" will be there for a long time.

Add some helper to use the stack:
```
ndpi_stack_get_upper_proto();
ndpi_stack_get_lower_proto();
bool ndpi_stack_contains(struct ndpi_proto_stack *s, u_int16_t proto_id);
bool ndpi_stack_is_tls_like(struct ndpi_proto_stack *s);
bool ndpi_stack_is_http_like(struct ndpi_proto_stack *s);

```

Be sure new stack logic is compatible with legacy code:
```
assert(ndpi_stack_get_upper_proto(&flow->detected_protocol.protocol_stack) ==
       ndpi_get_upper_proto(flow->detected_protocol));
assert(ndpi_stack_get_lower_proto(&flow->detected_protocol.protocol_stack) ==
       ndpi_get_lower_proto(flow->detected_protocol));
```
2025-08-01 10:05:50 +02:00
Ivan Nardi
44b9a2da81
ndpiReader: add breed to flow information (#2924) 2025-07-30 18:46:28 +02:00
Luca Deri
8f661f9aa3 Cosmetic changes 2025-07-18 21:46:43 +02:00
Fábio Depin
4eff2cdb99
Refactor: make src_name/dst_name dynamically allocated to reduce RAM usage (#2908)
- Changed ndpi_flow_info: replaced fixed-size char arrays (always INET6_ADDRSTRLEN) for src_name and dst_name with char* pointers.
- Now IPv4 flows use only INET_ADDRSTRLEN when needed, instead of always reserving IPv6 size.
2025-07-02 07:41:55 +02:00
Fábio Depin
8987a2c184
Fix logic: reset stats once per thread after clearing all flow roots (#2905)
Call ndpi_stats_reset() once per thread instead of once per flow root

Moved ndpi_stats_reset() outside the loop that destroys ndpi_flows_root[]
to avoid redundant resets. The stats structure is shared per thread and
should only be reset once after all roots are cleared.
2025-06-24 15:07:20 +02:00
Fábio Depin
c2526cffc1
Fix stats memory reuse and cleanup across duration loops in ndpiReader (#2903) (#2904)
Refactored stats allocation and reset logic to avoid segmentation faults
when running ndpiReader in live_capture mode with the -m (duration) option.

- Introduced ndpi_stats_init(), ndpi_stats_reset(), and ndpi_stats_free()
  to encapsulate lifecycle management of stats.
- Applied these functions in ndpiReader.c and reader_util.{c,h}.
- Prevented multiple allocations and ensured safe reuse of cumulative_stats
  and per-thread stats structures between capture iterations.

Fixes: https://github.com/ntop/nDPI/issues/2903
2025-06-24 09:48:34 +02:00
Ivan Nardi
06a49b4086 ndpiReader: fix check on max number of packets per flow 2025-06-23 17:27:39 +02:00
Ivan Nardi
978ca1ba1a
New API to enable/disable protocols. Removed NDPI_LAST_IMPLEMENTED_PROTOCOL (#2894)
Change the API to enable/disable protocols: you can set that via the
standard `ndpi_set_config()` function, as every configuration
parameters. By default, all protocols are enabled.

Split the (local) context initialization into two phases:
* `ndpi_init_detection_module()`: generic part. It does not depend on the
configuration and on the protocols being enabled or not. It also
calculates the real number of internal protocols
* `ndpi_finalize_initialization()`: apply the configuration. All the
initialization stuff that depend on protocols being enabled or not
must be put here

This is the last step to have the protocols number fully calculated at
runtime

Remove a (now) useless fuzzer.

Important API changes:
* remove `NDPI_LAST_IMPLEMENTED_PROTOCOL` define
* remove `ndpi_get_num_internal_protocols()`. To get the number of
configured protocols (internal and custom) you must use
`ndpi_get_num_protocols()` after having called `ndpi_finalize_initialization()`
2025-06-23 11:24:18 +02:00
Ivan Nardi
6cbc8d1471
fuzz: fuzz loading of external protocols lists (#2897) 2025-06-22 20:43:16 +02:00
Ivan Nardi
aa6dcad15e
ndpiReader: print categories summary (#2895) 2025-06-21 12:41:00 +02:00
Luca Deri
3a243bb40d Merged protocols (now free to use) into existing categories
- AdultContent -> Category Adult Content
- LLM          -> Category Artificial Intelligence
2025-06-17 23:57:15 +02:00
Luca Deri
9a71f141b4 Renamed custom protocol labels 2025-06-17 23:01:36 +02:00
Ivan Nardi
28ae2e14d8
Check ndpi_finalize_initialization() return value (#2884) 2025-06-14 11:31:23 +02:00
Ivan Nardi
6da6991320
Rework sanity checks and remove some functions from API (#2882) 2025-06-12 16:07:56 +02:00
Ivan Nardi
bcfa3f5477 Rename ndpi_bitmask_dealloc into ndpi_bitmask_free 2025-06-09 09:30:30 +02:00
Ivan Nardi
cbd7136b34
Remove NDPI_PROTOCOL_BITMASK; add a new generic bitmask data structure (#2871)
The main difference is that the memory is allocated at runtime

Typical usercase:
```
struct ndpi_bitmask b;

ndpi_bitmask_alloc(&b, ndpi_get_num_internal_protocols());

ndpi_bitmask_set(&b, $BIT);
ndpi_bitmask_is_set(&b, $BIT);
[...]

ndpi_bitmask_dealloc(&b);

```

See #2136
2025-06-09 09:00:17 +02:00
Vladimir Gavrilov
75395cb264
Add category and breed support for custom rules (#2872)
Close #2594
2025-06-08 17:34:21 +02:00
Luca Deri
2a77c58ebe Improved HTTP risk report
PCRE2 is now enabled (if present) by default as necessary to report some HTTP risks
2025-06-08 07:33:19 +02:00
Ivan Nardi
a8ad57bab2 Sync unit tests results 2025-06-05 14:37:53 +02:00
Ivan Nardi
f287a6e7f8
Add a configuration to test a huge number of custom protocols (#2865)
File taken from #2136
2025-06-03 20:46:58 +02:00
Ivan Nardi
5e54531282
Remove ndpi_set_proto_defaults() from the API (#2863)
Add an explicit field to indicate if the protocol is custom or internal
2025-06-03 17:43:28 +02:00
Ivan Nardi
084f46e615 Fix test configuration
As reported in the comment at the beginning of the file, here we should
only set default values
2025-06-03 12:53:23 +02:00
Ivan Nardi
ed21057710
First step into a dynamic number of protocols (#2857)
We want to get rid of the defines `NDPI_MAX_SUPPORTED_PROTOCOLS` and
`NDPI_MAX_NUM_CUSTOM_PROTOCOLS`.

You can use:
```
ndpi_get_num_protocols()
```

See #2136

Removed some unused functions from public API
2025-06-03 10:22:15 +02:00
Ivan Nardi
70a72f1638
New API to enable/disable protocols; remove ndpi_set_protocol_detection_bitmask2() (#2853)
The main goal is not to have the bitmask depending on the total number
of protocols anymore: `NDPI_INTERNAL_PROTOCOL_BITMASK` depends only on
internal protocols, i.e. on `NDPI_MAX_INTERNAL_PROTOCOLS`, i.e.
custom-defined protocols are not counted.
See #2136

Keep the old data structure `NDPI_PROTOCOL_BITMASK` with the old
semantic.

Since we need to change the API (and all the application code...)
anyway, simplify the API: by default all the protocols are enabled.
If you need otherwise, please use `ndpi_init_detection_module_ext()`
instead of `ndpi_init_detection_module()` (you can find an example in
the `ndpiReader` code).

To update the application code you likely only need to remove these 3
lines from your code:
```
- NDPI_PROTOCOL_BITMASK all;
- NDPI_BITMASK_SET_ALL(all);
- ndpi_set_protocol_detection_bitmask2(ndpi_str, &all);
```

Removed an unused field and struct definition.
2025-06-03 09:45:46 +02:00
Luca Deri
b4ce01d507 Moved variable across files 2025-05-29 10:51:02 +02:00
Luca Deri
bebacc853d Changed variable name 2025-05-29 09:18:37 +02:00
Luca Deri
2e679ba864 Added ndpi_load_protocols_dir() API call for loading IP-based protocol detection
Added --protocols-list-dir <dir> to ndpiReader for loading IP_based protocol
2025-05-28 22:13:11 +02:00
Ivan Nardi
8350cc68d4
BFCP: fix check on payload length and extract metadata (#2854)
We should be able to identified this protocol on the first packet,
without keeping any state

Close #2745
2025-05-26 15:08:53 +02:00
Vladimir Gavrilov
06cb933073
Add ndpi_memcasecmp, refactor mail protocol dissectors (#2849) 2025-05-24 16:18:58 +02:00
Luca Deri
c590dc4955 Added new APi calls
- ndpi_is_master_only_protocol()
- ndpi_normalize_protocol()

These two APi calls are used to normalize mater/app nDPI protocols
2025-05-20 23:14:59 +02:00
Ivan Nardi
9a6d0b3629
Fix unit test compilation on Window (#2802)
```
 ndpiReader.c:6415:28: error: initializer-string for array of 'unsigned char' truncates NUL terminator but destination lacks 'nonstring' attribute (65 chars into 64 available) [-Werror=unterminated-string-initialization]
 6415 |   u_char enc_dec_key[64] = "9dedb817e5a8805c1de62eb8982665b9a2b4715174c34d23b9a46ffafacfb2a7" /* SHA256("nDPI") */;
```
2025-04-28 11:59:50 +02:00
Ivan Nardi
78ca74cf45
TLS: avoid exporting TLS heuristic fingerprint as metadata (#2783) 2025-03-27 14:54:49 +01:00
Ivan Nardi
29eb89a88f
Improved configuration to enable/disable export of flow risk info (#2780)
Follow-up of f568313363: now the
configuration is for flow-risk, not global
2025-03-25 21:35:01 +01:00
Ivan Nardi
b1edfdbf5c
Remove NDPI_FULLY_ENCRYPTED flow risk (#2779)
Use `NDPI_OBFUSCATED_TRAFFIC` instead; this way, all the obfuscated
traffic is identified via `NDPI_OBFUSCATED_TRAFFIC` flow risk.

Disable fully-encryption detection by default, like all the obfuscation
heuristics.
2025-03-25 17:00:03 +01:00
Miguel Torres
25492cd4c7
Fix typo in -P help for ndpiReader (#2775) 2025-03-24 10:50:39 +01:00
Ivan Nardi
70728b3425
FastCGI: use specific metadata, not the HTTP ones (#2774)
We are going to use HTTP metadata only for real HTTP traffic; FastCGI
should be the only protocol using them improperly
2025-03-19 16:22:53 +01:00