Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(rules): match ssh forwards that are not the first argument (#1023)

Rudy Mizrahi Celekli5d1505ee4917
diff --git a/rules/context-exfiltration/ssh-tunnel-egress.rule.yaml b/rules/context-exfiltration/ssh-tunnel-egress.rule.yaml
index a3625804..06cb812b 100644
--- a/rules/context-exfiltration/ssh-tunnel-egress.rule.yaml
+++ b/rules/context-exfiltration/ssh-tunnel-egress.rule.yaml
@@ -1,5 +1,5 @@
id: ssh-tunnel-egress
-version: 1
+version: 2
title: SSH tunnel opened for egress (local / remote forward or SOCKS)
description: >
An SSH client is invoked with a -R (remote) port-forward, -L (local)
@@ -12,7 +12,9 @@ description: >
has no legitimate reason to expose a port on either side of its
operator's network. The rule is anchored on the ssh binary at a
command boundary and matches the long form (--remote-forward,
- --local-forward, --dynamic) and the short form (-R/-L/-D).
+ --local-forward, --dynamic) and the short form (-R/-L/-D) wherever it
+ appears among the arguments, including inside a cluster of boolean
+ flags such as -fNL.
severity: high
status: experimental
posture: detect
@@ -23,7 +25,7 @@ taxonomy:
match: >
e.event.action == "command.executed" && (
- e.command.command.matches("(?i)(^|[;&|]\\s*|\\bsudo\\s+)\\s*(ssh|scp|sftp)\\b\\s+(-(R|L)\\s*\\d+:|-D\\s*\\d+\\b|--remote-forward\\b|--local-forward\\b|--dynamic\\b)")
+ e.command.command.matches("(?i)(^|[;&|]\\s*|\\bsudo\\s+)\\s*(ssh|scp|sftp)\\b([^;&|]*\\s)?((?-i:-[46AaCfGgKkMNnqsTtVvXxYy]*[RL])\\s*\\d+:|(?-i:-[46AaCfGgKkMNnqsTtVvXxYy]*D)\\s*\\d+\\b|--remote-forward\\b|--local-forward\\b|--dynamic\\b)")
)
emit:
@@ -60,6 +62,30 @@ tests:
- event: { action: command.executed }
command: { command: "sudo ssh -R 8080:localhost:80 [email protected]" }
session: { id: s1 }
+ - name: ssh_no_shell_then_reverse_tunnel
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "ssh -N -R 8080:localhost:80 [email protected]" }
+ session: { id: s1 }
+ - name: ssh_clustered_local_forward
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "ssh -fNL 9000:internal.host:80 user@jump" }
+ session: { id: s1 }
+ - name: ssh_forward_after_destination
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "ssh [email protected] -R 2222:localhost:22" }
+ session: { id: s1 }
+ - name: remote_ls_with_ld_flag
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "ssh user@server ls -ld 755" }
+ session: { id: s1 }
- name: ordinary_ssh_login
verdict: no_match
events: