openmoat 0.28.0

Policy checks for AI coding agents: decides allow, defer, review or deny for each tool call the hook of Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot CLI and seven more agents reports, configures Claude Code's, Codex's and Cursor's own OS sandboxes from the same policy, and keeps a local audit log.
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
//! OpenMoat's agent account on Windows (ADR-129): what `openmoat
//! agent-account setup` makes, records and checks, and what `openmoat run`
//! changes per run. Pure: this file decides and compares; the Win32 calls are
//! in `commands/run/windows/account/`, so these tests run on every system.
//!
//! The account is a local user `openmoat-<person>` in no local group, so it
//! holds only what `Authenticated Users`, `Everyone` and entries naming it
//! grant. Setup grants it read and run on the person's profile (one inherited
//! entry), and `openmoat run` grants it write on the run's write roots and
//! denies it the places the policy's read denies name. The state directory
//! needs no entry: its list is protected and names the person and `SYSTEM`
//! only (ADR-083), so the profile's entry never reaches it.
//!
//! The record, `agent-account.json` in the state directory, names everything
//! setup and the runs changed, so `remove` undoes exactly that and `status`
//! can compare it with the system.

use std::path::Path;

use anyhow::{Context as _, Result, bail};
use serde::{Deserialize, Serialize};
use zeroize::Zeroizing;

/// The record's file in the state directory.
pub const RECORD_FILE: &str = "agent-account.json";

/// The folder of the state directory that holds the account's password,
/// encrypted for the person with DPAPI.
pub const DIR: &str = "agent-account";

/// The encrypted password's file in [`DIR`].
pub const PASSWORD_FILE: &str = "password.dpapi";

/// The record's format.
pub const VERSION: u32 = 1;

/// Every agent account's name starts with this.
pub const PREFIX: &str = "openmoat-";

/// Windows' limit on a local account's name: "User account names are limited
/// to 20 characters" (`USER_INFO_1`, `NetUserAdd`).
const MAX_NAME: usize = 20;

/// Characters a local account's name may not hold (Microsoft's "User name
/// restrictions"), and the space, which no tool quotes reliably.
const FORBIDDEN: &[char] = &[
    '"', '/', '\\', '[', ']', ':', ';', '|', '=', ',', '+', '*', '?', '<', '>', '@', ' ', '%',
];

/// The one logon right the account holds: the Secondary Logon service starts
/// `CreateProcessWithLogonW`'s process with an interactive logon.
pub const INTERACTIVE_RIGHT: &str = "SeInteractiveLogonRight";

/// The logon rights the account is denied: from the network, over remote
/// desktop, as a service and as a batch job.
pub const DENIED_RIGHTS: [&str; 4] = [
    "SeDenyNetworkLogonRight",
    "SeDenyRemoteInteractiveLogonRight",
    "SeDenyServiceLogonRight",
    "SeDenyBatchLogonRight",
];

/// What the account may do on the person's profile: `FILE_GENERIC_READ |
/// FILE_GENERIC_EXECUTE`, read and run, nothing else.
pub const PROFILE_MASK: u32 = 0x0012_00A9;

/// What a read deny takes from the account on a place: `FILE_ALL_ACCESS`.
pub const READ_DENY_MASK: u32 = 0x001F_01FF;

/// Where Windows reads which accounts the sign-in screen leaves out: a
/// `DWORD` value 0 named after the account (HKLM).
pub const USER_LIST_KEY: &str =
    r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList";

/// Where setup registers the account's SID for the hook relay (HKLM): only an
/// administrator can write there, so a hook that finds its own SID there is
/// one an administrator set up as an agent account.
pub const RELAY_KEY: &str = r"SOFTWARE\OpenMoat\AgentAccounts";

/// `CreateProcessWithLogonW`'s limit on `lpCommandLine`: "The maximum length
/// of this string is 1024 characters."
pub const COMMAND_LINE_MAX: usize = 1024;

/// How the refusals of an unelevated setup or remove end.
pub const ELEVATE: &str = "run it from an administrator terminal (Terminal or PowerShell, \"Run \
                           as administrator\")";

/// One access-list entry that names the account.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AclEntry {
    /// The file or folder, slash-separated.
    pub path: String,
    /// An allow entry; otherwise a deny entry.
    pub allows: bool,
    pub mask: u32,
    /// Handed to everything below.
    pub inherit: bool,
}

impl AclEntry {
    fn same(&self, other: &Self) -> bool {
        self.path.eq_ignore_ascii_case(&other.path)
            && self.allows == other.allows
            && self.mask == other.mask
            && self.inherit == other.inherit
    }
}

pub mod wfp;

pub use wfp::Wfp;

/// Whom a run starts the agent as, as far as the agent account goes.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum RunAs {
    /// The person's own user: every run off Windows, and ADR-115's
    /// write-restricted token on Windows.
    #[default]
    Person,
    /// The agent account, without network filters confirmed: a setup of
    /// ADR-129's first step, or filters this terminal cannot read back.
    Account,
    /// The agent account with its network filters read back as recorded.
    Filtered,
}

impl RunAs {
    /// Whether the agent starts as the agent account.
    pub fn account(self) -> bool {
        self != Self::Person
    }
}

/// `agent-account.json`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Record {
    pub version: u32,
    /// The account's name.
    pub account: String,
    /// The account's SID.
    pub sid: String,
    /// The SID of the person who set it up.
    pub person_sid: String,
    /// The person's profile folder, slash-separated.
    pub profile: String,
    /// The logon rights it holds.
    pub rights: Vec<String>,
    /// The logon rights it is denied.
    pub denied_rights: Vec<String>,
    /// Its value in [`USER_LIST_KEY`] was set.
    pub hidden: bool,
    /// Its SID was registered in [`RELAY_KEY`].
    pub relay_registered: bool,
    /// The read entry on the profile.
    pub profile_entry: AclEntry,
    /// The network filters; `None` in a record of ADR-129's first step,
    /// which running setup again completes.
    pub wfp: Option<Wfp>,
    /// The write entries runs set (kept between runs, like ADR-115's).
    #[serde(default)]
    pub write_entries: Vec<AclEntry>,
    /// The read denies the last run set (reconciled by each run).
    #[serde(default)]
    pub read_entries: Vec<AclEntry>,
}

impl Record {
    /// A record read from `text`, refused when its version, name or SID are
    /// not the ones setup writes.
    pub fn parse(text: &str) -> Result<Self> {
        let record: Self =
            serde_json::from_str(text).context("reading the agent account's record")?;
        if record.version != VERSION {
            bail!(
                "the agent account's record has version {}, this OpenMoat reads {VERSION}",
                record.version
            );
        }
        if !record.account.starts_with(PREFIX) || record.account.len() > MAX_NAME {
            bail!(
                "the agent account's record names `{}`, not an account setup makes",
                record.account
            );
        }
        if !record.sid.starts_with("S-1-5-21-") {
            bail!(
                "the agent account's record names the SID {}, not a local account's",
                record.sid
            );
        }
        if let Some(wfp) = &record.wfp {
            wfp.check()
                .context("in the agent account's record of its network filters")?;
        }
        Ok(record)
    }

    /// The record in `dir` (the state directory); `None` when there is none.
    pub fn load(dir: &Path) -> Result<Option<Self>> {
        let path = dir.join(RECORD_FILE);
        match std::fs::read_to_string(&path) {
            Ok(text) => Self::parse(&text)
                .with_context(|| format!("in {}", path.display()))
                .map(Some),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
            Err(e) => Err(e).with_context(|| format!("reading {}", path.display())),
        }
    }

    /// The record as setup writes it.
    pub fn to_json(&self) -> Result<String> {
        Ok(serde_json::to_string_pretty(self)? + "\n")
    }

    /// Every entry naming the account that `remove` takes out: the profile's,
    /// then the runs'.
    pub fn entries(&self) -> impl Iterator<Item = &AclEntry> {
        std::iter::once(&self.profile_entry)
            .chain(&self.write_entries)
            .chain(&self.read_entries)
    }

    /// Add `entries` to the write entries the record names, once each.
    pub fn add_writes(&mut self, entries: &[AclEntry]) {
        for entry in entries {
            if !self.write_entries.iter().any(|e| e.same(entry)) {
                self.write_entries.push(entry.clone());
            }
        }
    }
}

/// The read denies of an earlier run that this one does not set: what a run
/// takes out first, so a run for another agent does not inherit the last
/// one's (ADR-129 §2, "Reads").
pub fn stale(previous: &[AclEntry], wanted: &[AclEntry]) -> Vec<AclEntry> {
    previous
        .iter()
        .filter(|old| !wanted.iter().any(|new| new.same(old)))
        .cloned()
        .collect()
}

/// The account's name for the person `person` (their Windows user name):
/// [`PREFIX`] and the name in lower case, without the characters Windows
/// refuses, cut to Windows' 20 characters.
pub fn account_name(person: &str) -> String {
    let mut name = PREFIX.to_owned();
    for c in person.chars().flat_map(char::to_lowercase) {
        if c.is_control() || FORBIDDEN.contains(&c) {
            continue;
        }
        if name.len() + c.len_utf8() > MAX_NAME {
            break;
        }
        name.push(c);
    }
    if name.len() == PREFIX.len() {
        name.push_str("agent");
    }
    name
}

/// The account's password from 32 random bytes as hex (`random_hex`): the
/// hex, then one upper-case letter, digit and symbol, so a machine whose
/// policy asks for three kinds of character takes it too. No one sees it.
pub fn password(hex: &str) -> Zeroizing<String> {
    let mut password = Zeroizing::new(String::with_capacity(hex.len() + 3));
    password.push_str(hex);
    password.push_str("A7!");
    password
}

/// Refuse a command line `CreateProcessWithLogonW` would cut: `units` UTF-16
/// units, its NUL not counted.
pub fn command_line_fits(units: usize) -> Result<()> {
    if units > COMMAND_LINE_MAX {
        bail!(
            "the agent's command line is {units} characters long; Windows starts a program as \
             another account with at most {COMMAND_LINE_MAX} (`CreateProcessWithLogonW`), so \
             it does not start: shorten its arguments"
        );
    }
    Ok(())
}

/// What `status` reads back from the system for the record's account.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Observed {
    /// The SID the account's name has now; `None` when no such account.
    pub sid: Option<String>,
    /// The local groups it is a member of; `None` when this process may not
    /// read them.
    pub groups: Option<Vec<String>>,
    /// The logon rights it holds or is denied; `None` when this process may
    /// not read them (the local security policy may refuse a standard user).
    pub rights: Option<Vec<String>>,
    /// Its value in [`USER_LIST_KEY`] is 0.
    pub hidden: bool,
    /// What [`RELAY_KEY`] holds for its SID.
    pub relay: Option<String>,
    /// The profile's read entry is there.
    pub profile_entry: bool,
    /// The encrypted password's file is there.
    pub password: bool,
    /// What the Windows Filtering Platform holds of the record's network
    /// filters.
    pub wfp: wfp::Read,
}

/// What `seen` could not read, for `status` to name: an unelevated process
/// may not read the account's groups, rights or network filters, which only
/// an administrator can change; [`drift`] judges the rest. A run does not
/// hold them against the account, but counts no filter it could not read.
pub fn unread(seen: &Observed) -> Vec<&'static str> {
    let mut out = Vec::new();
    if seen.groups.is_none() {
        out.push("its local groups");
    }
    if seen.rights.is_none() {
        out.push("its logon rights");
    }
    if seen.wfp == wfp::Read::Unread {
        out.push("its network filters");
    }
    out
}

/// How the system differs from `record`: one line each; empty when it does
/// not. What could not be read ([`unread`]) is not judged.
pub fn drift(record: &Record, seen: &Observed) -> Vec<String> {
    let mut out = Vec::new();
    let name = &record.account;
    match &seen.sid {
        None => out.push(format!("the account {name} is gone")),
        Some(sid) if !sid.eq_ignore_ascii_case(&record.sid) => out.push(format!(
            "the account {name} has the SID {sid}, not the recorded {}",
            record.sid
        )),
        Some(_) => {}
    }
    if let Some(groups) = seen.groups.as_ref().filter(|g| !g.is_empty()) {
        out.push(format!(
            "the account {name} is in local groups ({}): it must be in none",
            groups.join(", ")
        ));
    }
    if let Some(rights) = &seen.rights {
        out.extend(rights_drift(rights));
    }
    if record.hidden && !seen.hidden {
        out.push(format!("{name} is shown on the sign-in screen again"));
    }
    let relay_ok = seen
        .relay
        .as_deref()
        .is_some_and(|p| p.eq_ignore_ascii_case(&record.person_sid));
    if record.relay_registered && !relay_ok {
        out.push(
            "the hook relay's registration of the account (HKLM\\SOFTWARE\\OpenMoat) is gone or \
             changed"
                .to_owned(),
        );
    }
    if !seen.profile_entry {
        out.push(format!(
            "the account's read entry on {} is gone",
            record.profile
        ));
    }
    if !seen.password {
        out.push("the account's encrypted password is gone".to_owned());
    }
    match (&record.wfp, &seen.wfp) {
        (Some(recorded), wfp::Read::Seen(filters)) => out.extend(wfp::drift(recorded, filters)),
        (Some(_), wfp::Read::NotAsked) => {
            out.push("the network filters were not read back".to_owned());
        }
        _ => {}
    }
    out
}

/// How `rights` differ from the ones setup gives: those missing, then those
/// setup did not give.
fn rights_drift(rights: &[String]) -> Vec<String> {
    let wanted: Vec<&str> = std::iter::once(INTERACTIVE_RIGHT)
        .chain(DENIED_RIGHTS)
        .collect();
    let missing: Vec<&str> = wanted
        .iter()
        .filter(|r| !rights.iter().any(|s| s.eq_ignore_ascii_case(r)))
        .copied()
        .collect();
    let extra: Vec<&str> = rights
        .iter()
        .filter(|s| !wanted.iter().any(|r| s.eq_ignore_ascii_case(r)))
        .map(String::as_str)
        .collect();
    let mut out = Vec::new();
    if !missing.is_empty() {
        out.push(format!("logon rights missing: {}", missing.join(", ")));
    }
    if !extra.is_empty() {
        out.push(format!(
            "logon rights setup did not give: {}",
            extra.join(", ")
        ));
    }
    out
}

#[cfg(test)]
mod tests;