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
//! NSE target library wrapper
//!
//! Utility functions for adding new discovered targets to Nmap scan queue.
//!
//! M007B corrective: `target.resolve` previously called
//! `std::net::ToSocketAddrs::to_socket_addrs` directly. That made the
//! library a direct host-network effect site (a DNS query leaving the
//! process) while the effect manifest classified it `Pure`, so the
//! name was exposed to `AgentSafe`/`CiSafe` with no capability check,
//! no cancellation, and no accounting.
//!
//! Resolution now goes through [`broker_dns_lookup`] (M005B), which
//! applies the `DnsResolution` capability gate, cancellation, and
//! resource accounting before the injected [`NseDnsProvider`] is
//! touched. The literal-IP fast path is unchanged and performs no
//! provider call.
use mlua::{Lua, Result as LuaResult};
use std::net::IpAddr;
use std::sync::{Arc, Mutex};
use crate::capabilities::NseCapabilityContext;
use crate::providers::{broker_dns_lookup, NseDnsRecordType, NseHostServices};
static TARGET_QUEUE: std::sync::LazyLock<Arc<Mutex<Vec<String>>>> =
std::sync::LazyLock::new(|| Arc::new(Mutex::new(Vec::new())));
/// Clear per-run library globals so back-to-back scans do not
/// leak state (handles, sessions, compiled patterns) between runs.
pub fn reset_for_run() {
if let Ok(mut s) = TARGET_QUEUE.lock() {
s.clear();
}
}
/// Compatibility registration with native services and a manual-permissive
/// capability context.
///
/// Retained so the pre-M007B public signature keeps working for embedders
/// that only have a [`Lua`] handle. `ExecutorCore` uses
/// [`register_target_library_with_services`] so the runtime profile,
/// capability context, and injected providers apply.
pub fn register_target_library(lua: &Lua) -> LuaResult<()> {
let profile = crate::profile::ResolvedNseExecutionProfile::manual_permissive(None);
let ctx = NseCapabilityContext::from_profile(
&profile,
Arc::new(crate::limits::NseResourceCounters::new()),
);
register_target_library_with_services(lua, &ctx, &NseHostServices::native())
}
/// Provider-backed registration.
///
/// `services.dns()` backs `target.resolve`; the DNS capability gate,
/// cancellation, and accounting are applied by the broker. Every other
/// `target` entry is in-memory bookkeeping with no host effect.
pub fn register_target_library_with_services(
lua: &Lua,
capability_ctx: &NseCapabilityContext,
services: &NseHostServices,
) -> LuaResult<()> {
let globals = lua.globals();
let target = lua.create_table()?;
let cap_ctx = capability_ctx.clone();
let svc = services.clone();
target.set(
"add",
lua.create_function(|lua, host: String| {
if let Ok(mut queue) = TARGET_QUEUE.lock() {
if !queue.contains(&host) {
queue.push(host.clone());
}
}
let result = lua.create_table()?;
result.set("host", host)?;
result.set("status", "added")?;
Ok(result)
})?,
)?;
target.set(
"add_with_port",
lua.create_function(|lua, (host, port): (String, u16)| {
let host_port = format!("{}:{}", host, port);
if let Ok(mut queue) = TARGET_QUEUE.lock() {
if !queue.contains(&host_port) {
queue.push(host_port);
}
}
let result = lua.create_table()?;
result.set("host", host)?;
result.set("port", port)?;
result.set("status", "added")?;
Ok(result)
})?,
)?;
target.set(
"exclude",
lua.create_function(|_lua, host: String| {
if let Ok(mut queue) = TARGET_QUEUE.lock() {
queue.retain(|h| h != &host);
}
Ok(true)
})?,
)?;
target.set(
"get",
lua.create_function(|lua, _: ()| {
let result = lua.create_table()?;
if let Ok(queue) = TARGET_QUEUE.lock() {
for (i, host) in queue.iter().enumerate() {
result.set(i + 1, host.clone())?;
}
}
Ok(result)
})?,
)?;
target.set(
"count",
lua.create_function(|_lua, _: ()| match TARGET_QUEUE.lock() {
Ok(queue) => Ok(queue.len() as i32),
_ => Ok(0),
})?,
)?;
target.set(
"clear",
lua.create_function(|_lua, _: ()| {
if let Ok(mut queue) = TARGET_QUEUE.lock() {
queue.clear();
}
Ok(true)
})?,
)?;
target.set(
"exists",
lua.create_function(|_lua, host: String| match TARGET_QUEUE.lock() {
Ok(queue) => Ok(queue.contains(&host)),
_ => Ok(false),
})?,
)?;
target.set(
"resolve",
lua.create_function(move |_lua, hostname: String| {
if hostname.parse::<IpAddr>().is_ok() {
return Ok(hostname);
}
for record in [NseDnsRecordType::A, NseDnsRecordType::Aaaa] {
if let Ok(answer) =
broker_dns_lookup(&cap_ctx, &svc, &hostname, record, "target.resolve")
{
if let Some(first) = answer.addresses().first() {
return Ok(first.to_string());
}
}
}
Ok(hostname)
})?,
)?;
target.set(
"reverse",
lua.create_function(|_lua, ip: String| {
if let Ok(addr) = ip.parse::<std::net::Ipv4Addr>() {
let octets = addr.octets();
return Ok(format!(
"{}.{}.{}.{}.in-addr.arpa",
octets[3], octets[2], octets[1], octets[0]
));
}
Ok(String::new())
})?,
)?;
globals.set("target", target)?;
Ok(())
}
pub fn get_target_queue() -> Vec<String> {
TARGET_QUEUE.lock().map(|q| q.clone()).unwrap_or_default()
}
pub fn clear_target_queue() {
if let Ok(mut queue) = TARGET_QUEUE.lock() {
queue.clear();
}
}