Skip to content

fix(932130): detect ANSI-C quoting hex-encoded commands#4598

Open
zoutjebot wants to merge 2 commits into
coreruleset:mainfrom
zoutjebot:fix/932130-ansi-c-hex-quoting
Open

fix(932130): detect ANSI-C quoting hex-encoded commands#4598
zoutjebot wants to merge 2 commits into
coreruleset:mainfrom
zoutjebot:fix/932130-ansi-c-hex-quoting

Conversation

@zoutjebot

Copy link
Copy Markdown
Contributor

What

Detects $'\x69\x64' style shell evasion where commands are hex-encoded in ANSI-C quoting. Requires 3+ hex pairs to reduce FPs.

Context

Part of CVE-derived payload research FN improvements. See tracking issue #4584 for full context.

Refs: #4584

Add detection for ANSI-C quoting syntax used in shell command evasion:
$'\x69\x64' (which evaluates to 'id' in bash).

This evasion technique encodes command characters as hex values within
$'...' quoting, bypassing string-based command detection. The pattern
requires at least 3 consecutive hex pairs to reduce false positives.

Refs: coreruleset#4584
@github-actions

github-actions Bot commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

📊 Quantitative test results for language: eng, year: 2023, size: 10K, paranoia level: 1:
🚀 Quantitative testing did not detect new false positives

@HackingRepo

Copy link
Copy Markdown
Contributor

That is a Good PR

@fzipi fzipi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests? Positive and negative, so we cover both cases. Thanks!

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@HackingRepo

HackingRepo commented Apr 19, 2026

Copy link
Copy Markdown
Contributor

also $'\u' can be used @zoutjebot
and $'\U' can be used also

Not just $'\x'

@HackingRepo

Copy link
Copy Markdown
Contributor

also can be used

relunsec@relunsec:~$ $'\103'
C: command not found

We need a dedicated rule for that one at PL3 for reducing FPs

@fzipi

fzipi commented Apr 20, 2026

Copy link
Copy Markdown
Member

Thanks for the fix and for adding tests. The approach is clever — leveraging t:cmdLine to strip backslashes and quotes so the regex only needs to match the normalized form. Low-complexity addition with no FP regressions per the quantitative test.

A few gaps worth addressing before merge:

1. Missing ANSI-C escape variants

Bash ANSI-C quoting supports more escape forms than just \x, and all of them are real evasion techniques:

Escape Example After t:cmdLine
\x $'\x63\x61\x74' $x63x61x74 ✅ covered
\u $'\u0063\u0061\u0074' $u0063u0061u0074
\U $'\U00000063' $u00000063
octal $'\143\141\164' $143141164

(@HackingRepo flagged \u, \U, and the octal form earlier in the thread.)

A broader pattern covers all of them:

$(?:x[0-9a-f]{2}|u[0-9a-f]{4}|[0-7]{3}){3,} (but this should be written in .ra format)

Note that after t:cmdLine lowercasing, \U collides with \u, so the 4-hex-digit branch handles both.

2. Threshold of {3,} misses common short commands

Classic RCE probes use 2-char commands (id, ls, cd, sh, ps):

$'\x69\x64' → $x69x64 → only 2 pairs → no match

id is arguably the most common first-probe in real exploits. I'd suggest {2,} — the $ anchor plus explicit escape syntax keeps FP risk low, and the quantitative test would confirm.

3. Test coverage

Only one positive (cat) and one near-miss negative. Worth adding:

  • positive for \u Unicode variant
  • positive for octal variant
  • positive for a 2-char command if the threshold drops to {2,}
  • negative for a legitimate value that happens to contain $abc123-style hex-looking content, to confirm the $ anchor requirement

4. Minor: .ra comment

The raw input $'\x69\x64' never actually contains the substring $x69x64 — the match only works after t:cmdLine strips the backslashes and quotes. Worth noting this in the .ra comment so future maintainers don't puzzle over the regex:

##! ANSI-C quoting hex-encoded commands (`$'\x69\x64'` -> $x69x64 after t:cmdLine)
$(?:x[0-9a-f]{2}){3,}

@HackingRepo

HackingRepo commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

But wait, @fzipi,

After deep fuzzing $'\U, you can also do that, so attackers can evade that

relunsec@relunsec:~$ echo -n $'\U00000043' 
Crelunsec@relunsec:~$ 
relunsec@relunsec:~$ echo -n $'\U00000043' 
Crelunsec@relunsec:~$ 

and

relunsec@relunsec:~$ echo -n $'\U000043' 
Crelunsec@relunsec:~$ 

also works, for $'\U', it can be 8 digits or just 4 or 6

and even 2 digits

relunsec@relunsec:~$ echo -n $'\U43' 
Crelunsec@relunsec:~$ 

@HackingRepo

Copy link
Copy Markdown
Contributor

ping @zoutjebot, can you address that evaison with $'\U', and add tests for that is detected

@fzipi

fzipi commented Jun 19, 2026

Copy link
Copy Markdown
Member

hey @zoutjebot, care to push this one to the finish line?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants