Skip to content

Commit 2865eda

Browse files
test(dgw): drive RDCleanPath injection with ironrdp-agent 0.1.0
Pin the public CLI release and complete NTLM and Kerberos target CredSSP over ws://127.0.0.1/jet/rdp. Issue: DGW-1900 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 476b8c3 commit 2865eda

1 file changed

Lines changed: 267 additions & 0 deletions

File tree

‎testsuite/tests/cli/dgw/cred_injection_kdc.rs‎

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
//! Proves the target-leg path: Gateway fetches tickets from a TCP KDC (`kdc` crate from
44
//! sspi-rs) and completes CredSSP with a fake RDP acceptor. The Gateway-facing client uses
55
//! NTLM so the test does not depend on the in-process synthetic KDC.
6+
//!
7+
//! RDCleanPath coverage drives the public `ironrdp-agent` 0.1.0 CLI (`cargo install
8+
//! ironrdp-agent --version 0.1.0`) over `ws://127.0.0.1/jet/rdp`.
69
10+
use std::path::{Path, PathBuf};
11+
use std::process::Stdio;
712
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
813
use std::sync::{Arc, Mutex};
914
use std::time::{Duration, Instant};
@@ -1334,6 +1339,268 @@ impl FakeClosedTarget {
13341339
}
13351340
}
13361341

1342+
const IRONRDP_AGENT_VERSION: &str = "0.1.0";
1343+
const RDCLEANPATH_INJECT_LOG: &str = "Switching to RdpProxy for credential injection (WebSocket)";
1344+
const RDCLEANPATH_FORWARD_LOG: &str = "RDP-TLS forwarding (RDCleanPath)";
1345+
1346+
fn ironrdp_agent_bin() -> Option<PathBuf> {
1347+
if let Ok(path) = std::env::var("IRONRDP_AGENT") {
1348+
return Some(PathBuf::from(path));
1349+
}
1350+
let name = if cfg!(windows) {
1351+
"ironrdp-agent.exe"
1352+
} else {
1353+
"ironrdp-agent"
1354+
};
1355+
if let Ok(home) = std::env::var("CARGO_HOME") {
1356+
let path = PathBuf::from(home).join("bin").join(name);
1357+
if path.is_file() {
1358+
return Some(path);
1359+
}
1360+
}
1361+
let cargo_home = std::env::var_os("USERPROFILE")
1362+
.or_else(|| std::env::var_os("HOME"))
1363+
.map(PathBuf::from)
1364+
.map(|home| home.join(".cargo").join("bin").join(name));
1365+
if let Some(path) = cargo_home
1366+
&& path.is_file()
1367+
{
1368+
return Some(path);
1369+
}
1370+
if let Ok(path) = std::env::var("PATH") {
1371+
for dir in std::env::split_paths(&path) {
1372+
let candidate = dir.join(name);
1373+
if candidate.is_file() {
1374+
return Some(candidate);
1375+
}
1376+
}
1377+
}
1378+
None
1379+
}
1380+
1381+
fn require_ironrdp_agent() -> anyhow::Result<Option<PathBuf>> {
1382+
let Some(bin) = ironrdp_agent_bin() else {
1383+
eprintln!(
1384+
"skipping RDCleanPath ironrdp-agent test: cargo install ironrdp-agent --version {IRONRDP_AGENT_VERSION}"
1385+
);
1386+
return Ok(None);
1387+
};
1388+
let output = std::process::Command::new(&bin)
1389+
.arg("--version")
1390+
.output()
1391+
.with_context(|| format!("run {} --version", bin.display()))?;
1392+
let version = String::from_utf8_lossy(&output.stdout);
1393+
anyhow::ensure!(
1394+
version.contains(IRONRDP_AGENT_VERSION),
1395+
"expected ironrdp-agent {IRONRDP_AGENT_VERSION}, got {version:?} from {}",
1396+
bin.display()
1397+
);
1398+
Ok(Some(bin))
1399+
}
1400+
1401+
fn ironrdp_agent_endpoint() -> String {
1402+
let name = format!("ironrdp-e2e-{}", next_id().replace('-', ""));
1403+
if cfg!(windows) {
1404+
format!(r"\\.\pipe\{name}")
1405+
} else {
1406+
std::env::temp_dir().join(format!("{name}.sock")).display().to_string()
1407+
}
1408+
}
1409+
1410+
async fn start_ironrdp_daemon(bin: &Path, endpoint: &str) -> anyhow::Result<tokio::process::Child> {
1411+
let child = tokio::process::Command::new(bin)
1412+
.args(["--endpoint", endpoint, "daemon-start"])
1413+
.kill_on_drop(true)
1414+
.stdout(Stdio::null())
1415+
.stderr(Stdio::null())
1416+
.spawn()
1417+
.context("start ironrdp-agent daemon")?;
1418+
let deadline = Instant::now() + Duration::from_secs(10);
1419+
loop {
1420+
let status = tokio::process::Command::new(bin)
1421+
.args(["--endpoint", endpoint, "status"])
1422+
.output()
1423+
.await
1424+
.context("ironrdp-agent status")?;
1425+
if status.status.success() {
1426+
return Ok(child);
1427+
}
1428+
if Instant::now() >= deadline {
1429+
anyhow::bail!(
1430+
"ironrdp-agent daemon not ready at {endpoint}: {}",
1431+
String::from_utf8_lossy(&status.stderr)
1432+
);
1433+
}
1434+
tokio::time::sleep(Duration::from_millis(50)).await;
1435+
}
1436+
}
1437+
1438+
async fn connect_ironrdp_rdcleanpath(
1439+
bin: &Path,
1440+
endpoint: &str,
1441+
server: &str,
1442+
token: &str,
1443+
http_port: u16,
1444+
) -> anyhow::Result<tokio::process::Child> {
1445+
let url = format!("ws://127.0.0.1:{http_port}/jet/rdp");
1446+
tokio::process::Command::new(bin)
1447+
.args([
1448+
"--endpoint",
1449+
endpoint,
1450+
"connect",
1451+
"--server",
1452+
server,
1453+
"--username",
1454+
PROXY_USER,
1455+
"--password",
1456+
PROXY_PASSWORD,
1457+
"--prop",
1458+
&format!("ironrdp_rdcleanpathurl:s:{url}"),
1459+
"--prop",
1460+
&format!("ironrdp_rdcleanpathtoken:s:{token}"),
1461+
])
1462+
.kill_on_drop(true)
1463+
.stdout(Stdio::piped())
1464+
.stderr(Stdio::piped())
1465+
.spawn()
1466+
.context("start ironrdp-agent connect")
1467+
}
1468+
1469+
async fn agent_query_logs(bin: &Path, endpoint: &str) -> String {
1470+
tokio::process::Command::new(bin)
1471+
.args(["--endpoint", endpoint, "query-logs"])
1472+
.output()
1473+
.await
1474+
.ok()
1475+
.map(|output| String::from_utf8_lossy(&output.stdout).into_owned())
1476+
.unwrap_or_default()
1477+
}
1478+
1479+
#[tokio::test]
1480+
async fn ironrdp_agent_rdcleanpath_ntlm_injection() -> anyhow::Result<()> {
1481+
let Some(bin) = require_ironrdp_agent()? else {
1482+
return Ok(());
1483+
};
1484+
install_crypto_provider();
1485+
let rdp = MockRdp::start_ntlm().await?;
1486+
let mut gateway = GatewayProc::start(false).await?;
1487+
let endpoint = ironrdp_agent_endpoint();
1488+
let mut daemon = start_ironrdp_daemon(&bin, &endpoint).await?;
1489+
1490+
let jti = next_id();
1491+
let jet_aid = next_id();
1492+
let token = association_token_for_host(&jti, &jet_aid, rdp.port, 60)?;
1493+
provision_credentials(gateway.config.http_port(), &token, TARGET_USER, 300, None).await?;
1494+
1495+
let mut connect = connect_ironrdp_rdcleanpath(
1496+
&bin,
1497+
&endpoint,
1498+
&format!("{SERVICE_HOST}:{}", rdp.port),
1499+
&token,
1500+
gateway.config.http_port(),
1501+
)
1502+
.await?;
1503+
let wait = rdp.wait_credssp().await;
1504+
let agent_logs = agent_query_logs(&bin, &endpoint).await;
1505+
wait.with_context(|| {
1506+
format!(
1507+
"RDCleanPath NTLM target CredSSP; gateway logs:\n{}\nagent logs:\n{agent_logs}",
1508+
gateway.logs.snapshot()
1509+
)
1510+
})?;
1511+
let logs = gateway.logs.snapshot();
1512+
anyhow::ensure!(
1513+
logs.contains(RDCLEANPATH_INJECT_LOG),
1514+
"RDCleanPath must take the injection path; logs:\n{logs}"
1515+
);
1516+
anyhow::ensure!(
1517+
!logs.contains(RDCLEANPATH_FORWARD_LOG),
1518+
"RDCleanPath injection must not ordinary-forward; logs:\n{logs}"
1519+
);
1520+
anyhow::ensure!(
1521+
rdp.finished_account().as_deref() == Some(TARGET_USER),
1522+
"RDP NTLM CredSSP Finished account must be {TARGET_USER}; got={:?}",
1523+
rdp.finished_account()
1524+
);
1525+
anyhow::ensure!(
1526+
rdp.cookies().iter().any(|cookie| cookie == PROXY_USER),
1527+
"RDCleanPath forwards the client X.224 cookie; cookies={:?}",
1528+
rdp.cookies()
1529+
);
1530+
1531+
let _ = connect.start_kill();
1532+
let _ = daemon.start_kill();
1533+
let _ = gateway.process.start_kill();
1534+
Ok(())
1535+
}
1536+
1537+
#[tokio::test]
1538+
async fn ironrdp_agent_rdcleanpath_kerberos_injection() -> anyhow::Result<()> {
1539+
let Some(bin) = require_ironrdp_agent()? else {
1540+
return Ok(());
1541+
};
1542+
install_crypto_provider();
1543+
let kdc = MockKdc::start().await?;
1544+
let rdp = MockRdp::start_kerberos(kdc.url()).await?;
1545+
let mut gateway = GatewayProc::start(true).await?;
1546+
let endpoint = ironrdp_agent_endpoint();
1547+
let mut daemon = start_ironrdp_daemon(&bin, &endpoint).await?;
1548+
1549+
let jti = next_id();
1550+
let jet_aid = next_id();
1551+
let token = association_token_for_host(&jti, &jet_aid, rdp.port, 60)?;
1552+
provision_credentials(
1553+
gateway.config.http_port(),
1554+
&token,
1555+
KERBEROS_TARGET_USER,
1556+
300,
1557+
Some(&kdc.url()),
1558+
)
1559+
.await?;
1560+
1561+
let mut connect = connect_ironrdp_rdcleanpath(
1562+
&bin,
1563+
&endpoint,
1564+
&format!("{SERVICE_HOST}:{}", rdp.port),
1565+
&token,
1566+
gateway.config.http_port(),
1567+
)
1568+
.await?;
1569+
let wait = rdp.wait_credssp().await;
1570+
let agent_logs = agent_query_logs(&bin, &endpoint).await;
1571+
wait.with_context(|| {
1572+
format!(
1573+
"RDCleanPath Kerberos target CredSSP; gateway logs:\n{}\nagent logs:\n{agent_logs}",
1574+
gateway.logs.snapshot()
1575+
)
1576+
})?;
1577+
assert_target_kdc_as_and_tgs(&kdc)?;
1578+
let logs = gateway.logs.snapshot();
1579+
anyhow::ensure!(
1580+
logs.contains(RDCLEANPATH_INJECT_LOG),
1581+
"RDCleanPath must take the injection path; logs:\n{logs}"
1582+
);
1583+
anyhow::ensure!(
1584+
!logs.contains(RDCLEANPATH_FORWARD_LOG),
1585+
"RDCleanPath injection must not ordinary-forward; logs:\n{logs}"
1586+
);
1587+
anyhow::ensure!(
1588+
rdp.finished_account().as_deref() == Some("administrator"),
1589+
"RDP CredSSP Finished account must be administrator; got={:?}",
1590+
rdp.finished_account()
1591+
);
1592+
anyhow::ensure!(
1593+
rdp.cookies().iter().any(|cookie| cookie == PROXY_USER),
1594+
"RDCleanPath forwards the client X.224 cookie; cookies={:?}",
1595+
rdp.cookies()
1596+
);
1597+
1598+
let _ = connect.start_kill();
1599+
let _ = daemon.start_kill();
1600+
let _ = gateway.process.start_kill();
1601+
Ok(())
1602+
}
1603+
13371604
const CERT_PEM: &str = r#"-----BEGIN CERTIFICATE-----
13381605
MIIDCzCCAfOgAwIBAgIUPRJa8i280unV3/kW6TE2fSUw8PwwDQYJKoZIhvcNAQEL
13391606
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MCAXDTI1MTEyNTA5NDAzMFoYDzIxMjUx

0 commit comments

Comments
 (0)