netget-tool 0.3.4

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//! Kerberos client actions, events and metadata.
//!
//! The model decides which tickets to get — a TGT, then service tickets for the services it
//! chooses — and with what lifetime and flags. The password is a startup parameter the
//! transport uses to derive keys; it is in no action, no event and no log line, and the model
//! never sees a key.

use std::sync::LazyLock;

use anyhow::{anyhow, Context, Result};
use serde_json::{json, Value};

use crate::llm::actions::{
    client_trait::{Client, ClientActionResult},
    protocol_trait::Protocol,
    ActionDefinition, Parameter, ParameterDefinition,
};
use crate::protocol::log_template::LogTemplate;
use crate::protocol::EventType;
use crate::server::kerberos::codec::Principal;
use crate::state::app_state::AppState;

use super::wire::Request;

/// The `ClientActionResult::Custom` name every exchange travels as.
pub const EXCHANGE_RESULT: &str = "kerberos_exchange";

/// Milliseconds to wait for a reply before retransmitting (UDP) or giving up (TCP).
pub const DEFAULT_TIMEOUT_MS: u64 = 3000;
/// Retransmissions of an unanswered UDP request.
pub const DEFAULT_RETRIES: u64 = 2;
/// Transport used unless the operator names another; MIT's and Heimdal's default.
pub const DEFAULT_TRANSPORT: &str = "udp";
/// Lifetime requested when the model names none: ten hours, MIT's default.
pub const DEFAULT_LIFETIME_SECS: u64 = 10 * 3600;
/// Longest lifetime the model may request (the KDC clamps further).
pub const MAX_REQUEST_LIFETIME_SECS: u64 = 365 * 24 * 3600;

fn param(name: &str, type_hint: &str, description: &str, required: bool) -> Parameter {
    Parameter {
        name: name.to_string(),
        type_hint: type_hint.to_string(),
        description: description.to_string(),
        required,
    }
}

fn credential_params() -> Vec<Parameter> {
    vec![
        param("client", "string", "Client principal with realm", true),
        param("service", "string", "Service principal with realm", true),
        param(
            "authtime",
            "string",
            "When the client authenticated (RFC 3339)",
            true,
        ),
        param("starttime", "string", "When the ticket became valid", true),
        param("endtime", "string", "When the ticket expires", true),
        param(
            "lifetime_secs",
            "number",
            "endtime - starttime, as the KDC granted it",
            true,
        ),
        param(
            "expires_in_secs",
            "number",
            "Seconds from now until it expires",
            true,
        ),
        param("renew_till", "string", "Renewable until, or null", false),
        param(
            "flags",
            "array",
            "Ticket flags the KDC set: forwardable, proxiable, renewable, initial, pre_authent, …",
            true,
        ),
        param("session_etype", "string", "Session key enctype", true),
        param(
            "ticket_etype",
            "string",
            "Enctype the service's ticket is encrypted with",
            true,
        ),
    ]
}

pub static KERBEROS_CLIENT_READY_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "kerberos_client_ready",
        "Ready to talk to the KDC (nothing has been sent yet). Usually: request a TGT",
        json!({"type": "kerberos_request_tgt", "lifetime_secs": 36000}),
    )
    .with_parameters(vec![
        param("kdc", "string", "The KDC's address", true),
        param(
            "principal",
            "string",
            "The client principal, with realm",
            true,
        ),
        param(
            "transport",
            "string",
            "How requests reach the KDC: udp, or tcp with a 4-byte length prefix",
            true,
        ),
    ])
    .with_actions(all_actions())
});

pub static KERBEROS_TGT_OBTAINED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "kerberos_tgt_obtained",
        "The KDC issued a TGT, verified: its reply decrypted with the key derived from the \
         password and echoed this client's nonce. Next, usually: request service tickets",
        json!({"type": "kerberos_request_service_ticket", "service": "host/web.netget.test"}),
    )
    .with_parameters(credential_params())
    .with_actions(all_actions())
});

pub static KERBEROS_SERVICE_TICKET_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "kerberos_service_ticket_obtained",
        "The KDC issued a service ticket, verified with the TGT's session key and this \
         request's subkey",
        json!({"type": "disconnect"}),
    )
    .with_parameters(credential_params())
    .with_actions(all_actions())
});

pub static KERBEROS_REQUEST_FAILED_EVENT: LazyLock<EventType> =
    LazyLock::new(|| {
        EventType::new(
        "kerberos_request_failed",
        "A ticket request failed: the KDC refused it with a Kerberos error, or its reply did not \
         verify, or it did not answer",
        json!({"type": "disconnect"}),
    )
    .with_parameters(vec![
        param("stage", "string", "as (TGT) or tgs (service ticket)", true),
        param("service", "string", "The service requested", true),
        param(
            "kind",
            "string",
            "kdc_error (see error), integrity (the reply did not decrypt or verify: wrong \
             password or forged reply), timeout, transport, malformed, oversize or no_tgt",
            true,
        ),
        param("error", "string", "Kerberos error name, for kind=kdc_error", false),
        param("code", "number", "Kerberos error code, for kind=kdc_error", false),
        param("text", "string", "The KDC's e-text, if any", false),
        param("message", "string", "What went wrong, for the other kinds", false),
    ])
    .with_actions(all_actions())
    });

fn lifetime_params() -> Vec<Parameter> {
    vec![
        param(
            "lifetime_secs",
            "number",
            "Lifetime to request (default 36000); the KDC may grant less",
            false,
        ),
        param(
            "renewable_lifetime_secs",
            "number",
            "Ask for a renewable ticket with this renewable lifetime",
            false,
        ),
        param(
            "forwardable",
            "boolean",
            "Ask for a forwardable ticket",
            false,
        ),
        param("proxiable", "boolean", "Ask for a proxiable ticket", false),
    ]
}

pub fn all_actions() -> Vec<ActionDefinition> {
    let mut service_params = vec![param(
        "service",
        "string",
        "Service principal, e.g. host/web.netget.test or HTTP/www.netget.test (same realm)",
        true,
    )];
    service_params.extend(lifetime_params());
    vec![
        ActionDefinition {
            name: "kerberos_request_tgt".to_string(),
            description: "Get a TGT for the configured principal (AS exchange): pre-authenticates \
                          with the configured password when the KDC asks"
                .to_string(),
            parameters: lifetime_params(),
            example: json!({"type": "kerberos_request_tgt", "lifetime_secs": 36000, "forwardable": true}),
            log_template: Some(LogTemplate::new().with_info("-> Kerberos AS-REQ")),
        },
        ActionDefinition {
            name: "kerberos_request_service_ticket".to_string(),
            description: "Get a service ticket with the TGT (TGS exchange). Needs a TGT first"
                .to_string(),
            parameters: service_params,
            example: json!({"type": "kerberos_request_service_ticket", "service": "host/web.netget.test"}),
            log_template: Some(LogTemplate::new().with_info("-> Kerberos TGS-REQ")),
        },
        ActionDefinition {
            name: "disconnect".to_string(),
            description: "Stop the client".to_string(),
            parameters: vec![],
            example: json!({"type": "disconnect"}),
            log_template: Some(LogTemplate::new().with_info("-> Kerberos client stopped")),
        },
    ]
}

/// What one action asks the transport to do.
pub enum Exchange {
    Tgt(Request),
    Service(Principal, Request),
}

fn seconds(action: &Value, key: &str) -> Result<Option<i64>> {
    match action.get(key) {
        None | Some(Value::Null) => Ok(None),
        Some(v) => {
            let n = v
                .as_u64()
                .with_context(|| format!("'{key}' must be a positive number of seconds"))?;
            if n == 0 || n > MAX_REQUEST_LIFETIME_SECS {
                return Err(anyhow!(
                    "'{key}' must be between 1 and {MAX_REQUEST_LIFETIME_SECS}"
                ));
            }
            Ok(Some(n as i64))
        }
    }
}

fn boolean(action: &Value, key: &str) -> Result<bool> {
    match action.get(key) {
        None | Some(Value::Null) => Ok(false),
        Some(Value::Bool(b)) => Ok(*b),
        Some(_) => Err(anyhow!("'{key}' must be true or false")),
    }
}

fn request_of(action: &Value) -> Result<Request> {
    Ok(Request {
        lifetime_secs: seconds(action, "lifetime_secs")?.unwrap_or(DEFAULT_LIFETIME_SECS as i64),
        renewable_lifetime_secs: seconds(action, "renewable_lifetime_secs")?,
        forwardable: boolean(action, "forwardable")?,
        proxiable: boolean(action, "proxiable")?,
        addresses: Vec::new(),
    })
}

/// Validate an action into the exchange it asks for; `None` for `disconnect`. Every refusal
/// happens here, before anything is sent.
pub fn exchange_from_action(action: &Value) -> Result<Option<Exchange>> {
    let kind = action
        .get("type")
        .and_then(Value::as_str)
        .context("action has no 'type'")?;
    match kind {
        "kerberos_request_tgt" => Ok(Some(Exchange::Tgt(request_of(action)?))),
        "kerberos_request_service_ticket" => {
            let text = action
                .get("service")
                .and_then(Value::as_str)
                .context("kerberos_request_service_ticket requires 'service'")?;
            let (service, realm) = Principal::parse(text)?;
            if realm.is_some() {
                return Err(anyhow!(
                    "name the service without a realm: this client asks its own realm's KDC \
                     only (no cross-realm)"
                ));
            }
            Ok(Some(Exchange::Service(service, request_of(action)?)))
        }
        "disconnect" => Ok(None),
        other => Err(anyhow!("Unknown Kerberos client action: {other}")),
    }
}

/// Kerberos client protocol handler.
#[derive(Default)]
pub struct KerberosClientProtocol;

impl KerberosClientProtocol {
    pub fn new() -> Self {
        Self
    }
}

impl Protocol for KerberosClientProtocol {
    fn get_startup_parameters(&self) -> Vec<ParameterDefinition> {
        vec![
            ParameterDefinition {
                name: "principal".to_string(),
                type_hint: "string".to_string(),
                description: "Client principal, e.g. alice or alice@NETGET.TEST".to_string(),
                required: true,
                example: json!("alice@NETGET.TEST"),
                default: None,
            },
            ParameterDefinition {
                name: "password".to_string(),
                type_hint: "string".to_string(),
                description: "The principal's password. Used only to derive keys; never shown \
                              in events or logs"
                    .to_string(),
                required: true,
                example: json!("wonderland"),
                default: None,
            },
            ParameterDefinition {
                name: "realm".to_string(),
                type_hint: "string".to_string(),
                description: "Realm, when the principal does not name one".to_string(),
                required: false,
                example: json!("NETGET.TEST"),
                default: None,
            },
            ParameterDefinition {
                name: "transport".to_string(),
                type_hint: "string".to_string(),
                description: "udp or tcp (a UDP reply too big for a datagram is retried over \
                              TCP automatically)"
                    .to_string(),
                required: false,
                example: json!("tcp"),
                default: Some(json!(DEFAULT_TRANSPORT)),
            },
            ParameterDefinition {
                name: "timeout_ms".to_string(),
                type_hint: "integer".to_string(),
                description: "Milliseconds to wait for each reply (100-60000)".to_string(),
                required: false,
                example: json!(3000),
                default: Some(json!(DEFAULT_TIMEOUT_MS)),
            },
            ParameterDefinition {
                name: "retries".to_string(),
                type_hint: "integer".to_string(),
                description: "UDP retransmissions before a request times out (0-5)".to_string(),
                required: false,
                example: json!(2),
                default: Some(json!(DEFAULT_RETRIES)),
            },
            ParameterDefinition {
                name: "ccache_path".to_string(),
                type_hint: "string".to_string(),
                description: "Write every ticket obtained to this MIT FILE credential cache \
                              (mode 0600), which klist and kvno can use. It holds the session \
                              keys, as any ccache does"
                    .to_string(),
                required: false,
                example: json!("/tmp/krb5cc_netget"),
                default: None,
            },
        ]
    }

    fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
        all_actions()
    }

    fn get_sync_actions(&self) -> Vec<ActionDefinition> {
        all_actions()
    }

    fn protocol_name(&self) -> &'static str {
        "Kerberos"
    }

    fn get_event_types(&self) -> Vec<EventType> {
        vec![
            KERBEROS_CLIENT_READY_EVENT.clone(),
            KERBEROS_TGT_OBTAINED_EVENT.clone(),
            KERBEROS_SERVICE_TICKET_EVENT.clone(),
            KERBEROS_REQUEST_FAILED_EVENT.clone(),
        ]
    }

    fn stack_name(&self) -> &'static str {
        "ETH>IP>UDP>KERBEROS"
    }

    fn keywords(&self) -> Vec<&'static str> {
        vec!["kerberos", "kinit", "kerberos client", "krb5", "tgt"]
    }

    fn metadata(&self) -> crate::protocol::metadata::ProtocolMetadataV2 {
        use crate::protocol::metadata::{
            DevelopmentState, PrivilegeRequirement, ProtocolMetadataV2,
        };
        ProtocolMetadataV2::builder()
            .state(DevelopmentState::Experimental)
            .privilege_requirement(PrivilegeRequirement::None)
            .implementation(
                "Kerberos V5 client (RFC 4120) over UDP or TCP. AS exchange as MIT kinit does it: \
                 an unauthenticated AS-REQ, then on KDC_ERR_PREAUTH_REQUIRED the key derived \
                 with the ETYPE-INFO2 salt (PBKDF2, RFC 3962) and PA-ENC-TIMESTAMP. TGS \
                 exchange with an AP-REQ whose authenticator carries a keyed checksum over the \
                 request body and a fresh subkey. Every reply is bounded (64 KiB, DER depth 24) \
                 and verified before it is believed: the encrypted part must decrypt (constant \
                 time HMAC check), echo the nonce and name the client and service asked for. \
                 aes256/aes128-cts-hmac-sha1-96 only. Optionally writes an MIT FILE ccache (v4).",
            )
            .llm_control(
                "Which tickets to get (a TGT, then service tickets for the services it names) \
                 and with what lifetime, renewable lifetime and flags; it is told what the KDC \
                 granted or why it refused. It never sees the password or a key.",
            )
            .e2e_testing(
                "tests/client/kerberos/real_server_test.rs against two independent KDCs, each \
                 in a temporary realm built with its own admin tool: MIT krb5kdc (kdb5_util, \
                 kadmin.local) and Heimdal's kdc (kadmin -l). With a python chain as the model: \
                 a TGT with the model's lifetime and flags, a service ticket, and a refused \
                 unknown service. MIT klist and Heimdal klist read the ccache NetGet wrote; MIT \
                 kvno -k decrypts the service ticket with the keytab each KDC's database \
                 exported; MIT kvno and Heimdal kgetcred use NetGet's TGT and session key for a \
                 ticket NetGet never asked for; krb5kdc's log shows the AS_REQ and TGS_REQ it \
                 issued. A wrong password is refused by the KDC (pre-authentication) or by \
                 NetGet (an AS-REP it cannot decrypt). Traffic to MIT is dissected by tshark. \
                 Fails, not skips, without the KDCs. bounds_test.rs: oversize, deeply nested \
                 and wrong-nonce replies, a service ticket before a TGT, and the follow-up depth.",
            )
            .notes(
                "Experimental: two independent KDCs complete real sessions with it, but nobody \
                 has reviewed it beyond its tests and the pair job has not run in CI. No FAST, \
                 PKINIT, renewal, cross-realm, AP exchange or RFC 8009 enctypes.",
            )
            .build()
    }

    fn description(&self) -> &'static str {
        "Kerberos V5 client that gets TGTs and service tickets from a KDC"
    }

    fn example_prompt(&self) -> &'static str {
        "Get a TGT for alice@NETGET.TEST (password wonderland) from the KDC at localhost:88, \
         then a ticket for host/web.netget.test"
    }

    fn group_name(&self) -> &'static str {
        "Authentication"
    }

    fn get_startup_examples(&self) -> crate::llm::actions::StartupExamples {
        use crate::llm::actions::StartupExamples;
        let params = json!({"principal": "alice@NETGET.TEST", "password": "wonderland"});
        StartupExamples::new(
            json!({
                "type": "open_client",
                "remote_addr": "localhost:88",
                "base_stack": "kerberos",
                "startup_params": params,
                "instruction": "Get a TGT, then a service ticket for host/web.netget.test"
            }),
            json!({
                "type": "open_client",
                "remote_addr": "localhost:88",
                "base_stack": "kerberos",
                "startup_params": params,
                "event_handlers": [{
                    "event_pattern": "kerberos_tgt_obtained",
                    "handler": {
                        "type": "script",
                        "language": "python",
                        "code": "respond([{'type': 'kerberos_request_service_ticket', 'service': 'host/web.netget.test'}])"
                    }
                }]
            }),
            json!({
                "type": "open_client",
                "remote_addr": "localhost:88",
                "base_stack": "kerberos",
                "startup_params": params,
                "event_handlers": [
                    {
                        "event_pattern": "kerberos_client_ready",
                        "handler": {"type": "static", "actions": [{"type": "kerberos_request_tgt"}]}
                    },
                    {
                        "event_pattern": "kerberos_tgt_obtained",
                        "handler": {"type": "static", "actions": [{"type": "disconnect"}]}
                    }
                ]
            }),
        )
    }
}

impl Client for KerberosClientProtocol {
    fn connect(
        &self,
        ctx: crate::protocol::ConnectContext,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = anyhow::Result<std::net::SocketAddr>> + Send>,
    > {
        Box::pin(super::connect(ctx))
    }

    fn execute_action(&self, action: Value) -> Result<ClientActionResult> {
        match exchange_from_action(&action)? {
            None => Ok(ClientActionResult::Disconnect),
            Some(_) => Ok(ClientActionResult::Custom {
                name: EXCHANGE_RESULT.to_string(),
                data: action,
            }),
        }
    }
}